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, filled_icon, icon};
9use crate::activity::ActivityCenter;
10use crate::agents::{AgentRuns, runs_handle};
11use crate::app_state::AppState;
12use crate::i18n::tr;
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, PartialEq, Eq)]
30struct RailItem {
31 tab: SidebarTab,
32 label: String,
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: tr("rail.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: tr("rail.chat"),
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: tr("rail.teams"),
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::Calendar,
79 label: tr("rail.calendar"),
80 shortcut_label: "Open Calendar",
81 icon: IconName::Calendar,
82 badge: RailBadge::None,
83 },
84 RailItem {
85 tab: SidebarTab::Calls,
86 label: tr("rail.calls"),
87 shortcut_label: "Show Calls",
88 icon: IconName::Phone,
89 badge: RailBadge::None,
90 },
91 ];
92 if let Some(running) = agents_running {
93 items.push(RailItem {
94 tab: SidebarTab::Agents,
95 label: tr("rail.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.clone().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(if active {
145 filled_icon(item.icon, ICON_SIZE, color)
146 } else {
147 icon(item.icon, ICON_SIZE, color)
148 })
149 .child(
150 div()
151 .max_w(px(RAIL_ITEM_WIDTH - 2. * LABEL_INSET))
152 .truncate()
153 .h(px(LABEL_HEIGHT))
154 .text_size(px(9.))
155 .line_height(px(LABEL_HEIGHT))
156 .font_weight(FontWeight::SEMIBOLD)
157 .text_color(color)
158 .child(item.label.clone()),
159 )
160 .children(badge(item.badge))
161 .tooltip(move |window, cx| Tooltip::new(tooltip.clone()).build(window, cx))
162}
163
164impl RailView {
165 pub fn new(
166 state: Entity<AppState>,
167 activity: Entity<ActivityCenter>,
168 sidebar: Entity<SidebarView>,
169 cx: &mut Context<Self>,
170 ) -> Self {
171 let mut observations = vec![
172 cx.observe(&state, |_, _, cx| cx.notify()),
173 cx.observe(&activity, |_, _, cx| cx.notify()),
174 cx.observe(&sidebar, |_, _, cx| cx.notify()),
175 ];
176 let runs = runs_handle(cx);
177 if let Some(runs) = &runs {
178 observations.push(cx.observe(runs, |_, _, cx| cx.notify()));
179 }
180 RailView {
181 state,
182 activity,
183 sidebar,
184 runs,
185 _observations: observations,
186 }
187 }
188
189 fn item(&self, item: &RailItem, active: bool) -> AnyElement {
190 let tab = item.tab;
191 let sidebar = self.sidebar.clone();
192 item_element(item, active)
193 .on_click(move |_, _, cx| {
194 sidebar.update(cx, |sidebar, cx| sidebar.open_tab(tab, cx));
195 })
196 .into_any_element()
197 }
198
199 fn agents_running(&self, cx: &App) -> Option<usize> {
200 let runs = self.runs.as_ref()?;
201 crate::agents::agents_enabled(cx).then(|| runs.read(cx).running_count())
202 }
203}
204
205fn badge(badge: RailBadge) -> Option<Div> {
206 Some(match badge {
207 RailBadge::None => return None,
208 RailBadge::Dot => div()
209 .absolute()
210 .top(px(8.))
211 .left(px(31.))
212 .size(px(7.))
213 .rounded_full()
214 .bg(theme::accent()),
215 RailBadge::Count(count) => {
216 badge_pill(count, RAIL_BADGE, theme::accent(), theme::on_accent())
217 .absolute()
218 .top(px(5.))
219 .left(px(28.))
220 }
221 })
222}
223
224impl Render for RailView {
225 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
226 let active = self.sidebar.read(cx).tab();
227 let activity_unread = self.activity.read(cx).feed().bell_count()
228 + crate::agent_api::unseen_proposal_count(cx);
229 let (unread_chats, channel_dot) = {
230 let state = self.state.read(cx);
231 (
232 unread_chat_count(&state.sidebar.chats),
233 any_unread_channel(&state.sidebar),
234 )
235 };
236 let items = rail_items(
237 activity_unread,
238 unread_chats as usize,
239 channel_dot,
240 self.agents_running(cx),
241 );
242 v_flex()
243 .debug_selector(|| "shell/islands/rail".into())
244 .w(px(RAIL_WIDTH))
245 .h_full()
246 .flex_none()
247 .items_center()
248 .gap(px(2.))
249 .pt(px(0.))
250 .children(items.iter().map(|item| self.item(item, item.tab == active)))
251 }
252}
253
254#[cfg(test)]
255mod tests {
256 use app_base::ui_locale::apply_locale;
257
258 use super::{RailBadge, count_badge_for, rail_items, tooltip_text};
259 use crate::i18n::tr;
260 use crate::views::sidebar::SidebarTab;
261
262 #[test]
263 fn items_follow_the_spec_order() {
264 apply_locale("en");
265 let tabs: Vec<SidebarTab> = rail_items(0, 0, false, None)
266 .iter()
267 .map(|item| item.tab)
268 .collect();
269 assert_eq!(
270 tabs,
271 [
272 SidebarTab::Activity,
273 SidebarTab::Chats,
274 SidebarTab::Channels,
275 SidebarTab::Calendar,
276 SidebarTab::Calls
277 ]
278 );
279 }
280
281 #[test]
282 fn agents_comes_last_and_only_when_actions_exist() {
283 apply_locale("en");
284 assert_eq!(rail_items(0, 0, false, None).len(), 5);
285 let with_agents = rail_items(0, 0, false, Some(2));
286 assert_eq!(with_agents.len(), 6);
287 assert_eq!(with_agents[5].tab, SidebarTab::Agents);
288 assert_eq!(with_agents[5].label, tr("rail.agents"));
289 assert_eq!(with_agents[5].badge, RailBadge::Count(2));
290 assert_eq!(rail_items(0, 0, false, Some(0))[5].badge, RailBadge::None);
291 }
292
293 #[test]
294 fn badges_follow_the_unread_inputs() {
295 apply_locale("en");
296 let items = rail_items(3, 5, true, None);
297 assert_eq!(items[0].badge, RailBadge::Count(3));
298 assert_eq!(items[1].badge, RailBadge::Count(5));
299 assert_eq!(items[2].badge, RailBadge::Dot);
300 assert_eq!(items[3].badge, RailBadge::None);
301 assert_eq!(items[4].badge, RailBadge::None);
302 let quiet = rail_items(0, 0, false, None);
303 assert!(quiet.iter().all(|item| item.badge == RailBadge::None));
304 assert_eq!(count_badge_for(usize::MAX), RailBadge::Count(u32::MAX));
305 }
306
307 #[cfg_attr(target_os = "macos", ignore = "needs Ctrl bindings")]
308 #[test]
309 fn every_item_shows_its_shortcut_in_the_tooltip() {
310 apply_locale("en");
311 let items = rail_items(0, 0, false, None);
312 assert_eq!(tooltip_text(&items[0]), "Activity (Ctrl+1)");
313 assert!(items.iter().all(|item| tooltip_text(item).ends_with(')')));
314 }
315}