BlueGit

ict rusty-teams

Files

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

rail.rs

76d44f37 History
rail.rs 10.1 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 v_flex()
249 .debug_selector(|| "shell/islands/rail".into())
250 .w(px(RAIL_WIDTH))
251 .h_full()
252 .flex_none()
253 .items_center()
254 .gap(px(2.))
255 .pt(px(0.))
256 .children(items.iter().map(|item| self.item(item, item.tab == active)))
257 }
258}
259
260#[cfg(test)]
261mod tests {
262 use app_base::ui_locale::apply_locale;
263
264 use super::{RailBadge, count_badge_for, rail_items, tooltip_text};
265 use crate::i18n::tr;
266 use crate::views::sidebar::SidebarTab;
267
268 #[test]
269 fn items_follow_the_spec_order() {
270 apply_locale("en");
271 let tabs: Vec<SidebarTab> = rail_items(0, 0, false, true, None)
272 .iter()
273 .map(|item| item.tab)
274 .collect();
275 assert_eq!(
276 tabs,
277 [
278 SidebarTab::Activity,
279 SidebarTab::Chats,
280 SidebarTab::Channels,
281 SidebarTab::Calendar,
282 SidebarTab::Calls
283 ]
284 );
285 }
286
287 #[test]
288 fn calendar_is_left_out_when_unavailable() {
289 apply_locale("en");
290 let tabs: Vec<SidebarTab> = rail_items(0, 0, false, false, None)
291 .iter()
292 .map(|item| item.tab)
293 .collect();
294 assert!(!tabs.contains(&SidebarTab::Calendar));
295 assert_eq!(tabs.len(), 4);
296 }
297
298 #[test]
299 fn agents_comes_last_and_only_when_actions_exist() {
300 apply_locale("en");
301 assert_eq!(rail_items(0, 0, false, true, None).len(), 5);
302 let with_agents = rail_items(0, 0, false, true, Some(2));
303 assert_eq!(with_agents.len(), 6);
304 assert_eq!(with_agents[5].tab, SidebarTab::Agents);
305 assert_eq!(with_agents[5].label, tr("rail.agents"));
306 assert_eq!(with_agents[5].badge, RailBadge::Count(2));
307 assert_eq!(
308 rail_items(0, 0, false, true, Some(0))[5].badge,
309 RailBadge::None
310 );
311 }
312
313 #[test]
314 fn badges_follow_the_unread_inputs() {
315 apply_locale("en");
316 let items = rail_items(3, 5, true, true, None);
317 assert_eq!(items[0].badge, RailBadge::Count(3));
318 assert_eq!(items[1].badge, RailBadge::Count(5));
319 assert_eq!(items[2].badge, RailBadge::Dot);
320 assert_eq!(items[3].badge, RailBadge::None);
321 assert_eq!(items[4].badge, RailBadge::None);
322 let quiet = rail_items(0, 0, false, true, None);
323 assert!(quiet.iter().all(|item| item.badge == RailBadge::None));
324 assert_eq!(count_badge_for(usize::MAX), RailBadge::Count(u32::MAX));
325 }
326
327 #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")]
328 #[test]
329 fn every_item_shows_its_shortcut_in_the_tooltip() {
330 apply_locale("en");
331 let items = rail_items(0, 0, false, true, None);
332 assert_eq!(tooltip_text(&items[0]), "Activity (Ctrl+1)");
333 assert!(items.iter().all(|item| tooltip_text(item).ends_with(')')));
334 }
335}