BlueGit

ict rusty-teams

Files

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

rail.rs

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