BlueGit

ict rusty-teams

Files

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

rail.rs

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