BlueGit

ict rusty-teams

Files

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

rail.rs

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