BlueGit

ict rusty-teams

Files

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

rail.rs

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