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