BlueGit

ict rusty-teams

Files

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

rail.rs

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