BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / rail.rs

rail.rs

rail.rs 10.8 KiB Download Raw
1use gpui_kit::assets::IconName;
2use gpui_kit::component::{tooltip::Tooltip, v_flex};
3use gpui_kit::prelude::FluentBuilder as _;
4use gpui_kit::*;
5
6use super::shortcuts::keys_for_label;
7use super::sidebar::{SidebarTab, SidebarView};
8use super::widgets::{RAIL_BADGE, badge_pill, filled_icon, icon};
9use crate::activity::ActivityCenter;
10use crate::agents::{AgentRuns, runs_handle};
11use crate::app_state::AppState;
12use crate::i18n::tr;
13use crate::sidebar_model::{any_unread_channel, unread_chat_count};
14use crate::theme;
15use app_ui_kit::layout::{RAIL_ITEM_WIDTH, RAIL_WIDTH};
16
17const ITEM_HEIGHT: f32 = 52.;
18const ICON_SIZE: f32 = 20.;
19const LABEL_INSET: f32 = 2.;
20const LABEL_HEIGHT: f32 = 12.;
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23enum RailBadge {
24 None,
25 Count(u32),
26 Dot,
27}
28
29#[derive(Debug, Clone, PartialEq, Eq)]
30struct RailItem {
31 tab: SidebarTab,
32 label: String,
33 shortcut_label: &'static str,
34 icon: IconName,
35 badge: RailBadge,
36}
37
38fn count_badge_for(count: usize) -> RailBadge {
39 match count {
40 0 => RailBadge::None,
41 count => RailBadge::Count(count.min(u32::MAX as usize) as u32),
42 }
43}
44
45fn rail_items(
46 activity_unread: usize,
47 unread_chats: usize,
48 channel_dot: bool,
49 calendar: bool,
50 agents_running: Option<usize>,
51) -> Vec<RailItem> {
52 let mut items = vec![
53 RailItem {
54 tab: SidebarTab::Activity,
55 label: tr("rail.activity"),
56 shortcut_label: "Open Activity",
57 icon: IconName::Bell,
58 badge: count_badge_for(activity_unread),
59 },
60 RailItem {
61 tab: SidebarTab::Chats,
62 label: tr("rail.chat"),
63 shortcut_label: "Show Chats",
64 icon: IconName::MessageSquare,
65 badge: count_badge_for(unread_chats),
66 },
67 RailItem {
68 tab: SidebarTab::Channels,
69 label: tr("rail.teams"),
70 shortcut_label: "Show Channels",
71 icon: IconName::Users,
72 badge: if channel_dot {
73 RailBadge::Dot
74 } else {
75 RailBadge::None
76 },
77 },
78 RailItem {
79 tab: SidebarTab::Calendar,
80 label: tr("rail.calendar"),
81 shortcut_label: "Open Calendar",
82 icon: IconName::Calendar,
83 badge: RailBadge::None,
84 },
85 RailItem {
86 tab: SidebarTab::Calls,
87 label: tr("rail.calls"),
88 shortcut_label: "Show Calls",
89 icon: IconName::Phone,
90 badge: RailBadge::None,
91 },
92 ];
93 if !calendar {
94 items.retain(|item| item.tab != SidebarTab::Calendar);
95 }
96 if let Some(running) = agents_running {
97 items.push(RailItem {
98 tab: SidebarTab::Agents,
99 label: tr("rail.agents"),
100 shortcut_label: "Open Agents",
101 icon: IconName::Bot,
102 badge: count_badge_for(running),
103 });
104 }
105 items
106}
107
108fn tooltip_text(item: &RailItem) -> SharedString {
109 match keys_for_label(item.shortcut_label) {
110 Some(keys) => format!("{} ({keys})", item.label).into(),
111 None => item.label.clone().into(),
112 }
113}
114
115pub struct RailView {
116 state: Entity<AppState>,
117 activity: Entity<ActivityCenter>,
118 sidebar: Entity<SidebarView>,
119 runs: Option<Entity<AgentRuns>>,
120 _observations: Vec<Subscription>,
121}
122
123fn item_element(item: &RailItem, active: bool) -> Stateful<Div> {
124 let tooltip = tooltip_text(item);
125 let color = if active {
126 theme::accent_text()
127 } else {
128 theme::text_muted()
129 };
130 div()
131 .id(SharedString::from(format!("rail-{}", item.label)))
132 .relative()
133 .w(px(RAIL_ITEM_WIDTH))
134 .h(px(ITEM_HEIGHT))
135 .flex()
136 .flex_col()
137 .flex_none()
138 .items_center()
139 .justify_center()
140 .pt(px(2.))
141 .gap(px(4.))
142 .rounded(px(10.))
143 .cursor_pointer()
144 .when(active, |button| button.bg(theme::surface_raised()))
145 .when(!active, |button| {
146 button.hover(|button| button.bg(theme::row_hover()))
147 })
148 .child(if active {
149 filled_icon(item.icon, ICON_SIZE, color)
150 } else {
151 icon(item.icon, ICON_SIZE, color)
152 })
153 .child(
154 div()
155 .max_w(px(RAIL_ITEM_WIDTH - 2. * LABEL_INSET))
156 .truncate()
157 .h(px(LABEL_HEIGHT))
158 .text_size(px(9.))
159 .line_height(px(LABEL_HEIGHT))
160 .font_weight(FontWeight::SEMIBOLD)
161 .text_color(color)
162 .child(item.label.clone()),
163 )
164 .children(badge(item.badge))
165 .tooltip(move |window, cx| Tooltip::new(tooltip.clone()).build(window, cx))
166}
167
168impl RailView {
169 pub fn new(
170 state: Entity<AppState>,
171 activity: Entity<ActivityCenter>,
172 sidebar: Entity<SidebarView>,
173 cx: &mut Context<Self>,
174 ) -> Self {
175 let mut observations = vec![
176 cx.observe(&state, |_, _, cx| cx.notify()),
177 cx.observe(&activity, |_, _, cx| cx.notify()),
178 cx.observe(&sidebar, |_, _, cx| cx.notify()),
179 ];
180 let runs = runs_handle(cx);
181 if let Some(runs) = &runs {
182 observations.push(cx.observe(runs, |_, _, cx| cx.notify()));
183 }
184 RailView {
185 state,
186 activity,
187 sidebar,
188 runs,
189 _observations: observations,
190 }
191 }
192
193 fn item(&self, item: &RailItem, active: bool) -> AnyElement {
194 let tab = item.tab;
195 let sidebar = self.sidebar.clone();
196 item_element(item, active)
197 .on_click(move |_, _, cx| {
198 sidebar.update(cx, |sidebar, cx| sidebar.open_tab(tab, cx));
199 })
200 .into_any_element()
201 }
202
203 fn agents_running(&self, cx: &App) -> Option<usize> {
204 let runs = self.runs.as_ref()?;
205 crate::agents::agents_enabled(cx).then(|| runs.read(cx).running_count())
206 }
207}
208
209fn badge(badge: RailBadge) -> Option<Div> {
210 Some(match badge {
211 RailBadge::None => return None,
212 RailBadge::Dot => div()
213 .absolute()
214 .top(px(8.))
215 .left(px(31.))
216 .size(px(7.))
217 .rounded_full()
218 .bg(theme::accent()),
219 RailBadge::Count(count) => {
220 badge_pill(count, RAIL_BADGE, theme::accent(), theme::on_accent())
221 .absolute()
222 .top(px(5.))
223 .left(px(28.))
224 }
225 })
226}
227
228impl Render for RailView {
229 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
230 let active = self.sidebar.read(cx).tab();
231 let activity_unread = self.activity.read(cx).feed().bell_count()
232 + crate::agent_api::unseen_proposal_count(cx);
233 let (unread_chats, channel_dot, calendar) = {
234 let state = self.state.read(cx);
235 (
236 unread_chat_count(&state.sidebar.chats),
237 any_unread_channel(&state.sidebar),
238 state.calendar_available(),
239 )
240 };
241 let items = rail_items(
242 activity_unread,
243 unread_chats as usize,
244 channel_dot,
245 calendar,
246 self.agents_running(cx),
247 );
248 // KVT-fork: tegels Ponos en Asclepius direct onder Agents.
249 let tickets_unread = crate::kvt::tickets_unread(cx);
250 let items = crate::kvt::with_rail_tiles(items, |tile, label| match tile {
251 crate::kvt::RailTile::Ponos => RailItem {
252 tab: SidebarTab::Ponos,
253 label,
254 shortcut_label: "Open Ponos",
255 icon: IconName::ClipboardCheck,
256 badge: RailBadge::None,
257 },
258 crate::kvt::RailTile::Asclepius => RailItem {
259 tab: SidebarTab::Asclepius,
260 label,
261 shortcut_label: "Open Asclepius",
262 icon: IconName::Ticket,
263 badge: count_badge_for(tickets_unread),
264 },
265 });
266 v_flex()
267 .debug_selector(|| "shell/islands/rail".into())
268 .w(px(RAIL_WIDTH))
269 .h_full()
270 .flex_none()
271 .items_center()
272 .gap(px(2.))
273 .pt(px(0.))
274 .children(items.iter().map(|item| self.item(item, item.tab == active)))
275 }
276}
277
278#[cfg(test)]
279mod tests {
280 use app_base::ui_locale::apply_locale;
281
282 use super::{RailBadge, count_badge_for, rail_items, tooltip_text};
283 use crate::i18n::tr;
284 use crate::views::sidebar::SidebarTab;
285
286 #[test]
287 fn items_follow_the_spec_order() {
288 apply_locale("en");
289 let tabs: Vec<SidebarTab> = rail_items(0, 0, false, true, None)
290 .iter()
291 .map(|item| item.tab)
292 .collect();
293 assert_eq!(
294 tabs,
295 [
296 SidebarTab::Activity,
297 SidebarTab::Chats,
298 SidebarTab::Channels,
299 SidebarTab::Calendar,
300 SidebarTab::Calls
301 ]
302 );
303 }
304
305 #[test]
306 fn calendar_is_left_out_when_unavailable() {
307 apply_locale("en");
308 let tabs: Vec<SidebarTab> = rail_items(0, 0, false, false, None)
309 .iter()
310 .map(|item| item.tab)
311 .collect();
312 assert!(!tabs.contains(&SidebarTab::Calendar));
313 assert_eq!(tabs.len(), 4);
314 }
315
316 #[test]
317 fn agents_comes_last_and_only_when_actions_exist() {
318 apply_locale("en");
319 assert_eq!(rail_items(0, 0, false, true, None).len(), 5);
320 let with_agents = rail_items(0, 0, false, true, Some(2));
321 assert_eq!(with_agents.len(), 6);
322 assert_eq!(with_agents[5].tab, SidebarTab::Agents);
323 assert_eq!(with_agents[5].label, tr("rail.agents"));
324 assert_eq!(with_agents[5].badge, RailBadge::Count(2));
325 assert_eq!(
326 rail_items(0, 0, false, true, Some(0))[5].badge,
327 RailBadge::None
328 );
329 }
330
331 #[test]
332 fn badges_follow_the_unread_inputs() {
333 apply_locale("en");
334 let items = rail_items(3, 5, true, true, None);
335 assert_eq!(items[0].badge, RailBadge::Count(3));
336 assert_eq!(items[1].badge, RailBadge::Count(5));
337 assert_eq!(items[2].badge, RailBadge::Dot);
338 assert_eq!(items[3].badge, RailBadge::None);
339 assert_eq!(items[4].badge, RailBadge::None);
340 let quiet = rail_items(0, 0, false, true, None);
341 assert!(quiet.iter().all(|item| item.badge == RailBadge::None));
342 assert_eq!(count_badge_for(usize::MAX), RailBadge::Count(u32::MAX));
343 }
344
345 #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")]
346 #[test]
347 fn every_item_shows_its_shortcut_in_the_tooltip() {
348 apply_locale("en");
349 let items = rail_items(0, 0, false, true, None);
350 assert_eq!(tooltip_text(&items[0]), "Activity (Ctrl+1)");
351 assert!(items.iter().all(|item| tooltip_text(item).ends_with(')')));
352 }
353}