BlueGit

ict rusty-teams

Files

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

rail.rs

rail.rs 7.6 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::shell::ISLAND_GAP;
7use super::shortcuts::keys_for_label;
8use super::sidebar::{SidebarTab, SidebarView};
9use super::widgets::icon;
10use crate::activity::ActivityCenter;
11use crate::app_state::AppState;
12use crate::format;
13use crate::sidebar_model::{any_unread_channel, unread_chat_count};
14use crate::theme;
15
16pub const RAIL_WIDTH: f32 = ITEM_WIDTH + 2. * ISLAND_GAP;
17const ITEM_WIDTH: f32 = 48.;
18const ITEM_HEIGHT: f32 = 52.;
19const ICON_SIZE: f32 = 20.;
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(activity_unread: usize, unread_chats: usize, channel_dot: bool) -> [RailItem; 5] {
45 [
46 RailItem {
47 tab: SidebarTab::Activity,
48 label: "Activity",
49 shortcut_label: "Open Activity",
50 icon: IconName::Bell,
51 badge: count_badge_for(activity_unread),
52 },
53 RailItem {
54 tab: SidebarTab::Chats,
55 label: "Chats",
56 shortcut_label: "Show Chats",
57 icon: IconName::MessageSquare,
58 badge: count_badge_for(unread_chats),
59 },
60 RailItem {
61 tab: SidebarTab::Channels,
62 label: "Channels",
63 shortcut_label: "Show Channels",
64 icon: IconName::Users,
65 badge: if channel_dot {
66 RailBadge::Dot
67 } else {
68 RailBadge::None
69 },
70 },
71 RailItem {
72 tab: SidebarTab::Calls,
73 label: "Calls",
74 shortcut_label: "Show Calls",
75 icon: IconName::Phone,
76 badge: RailBadge::None,
77 },
78 RailItem {
79 tab: SidebarTab::Saved,
80 label: "Saved",
81 shortcut_label: "Open Saved",
82 icon: IconName::Bookmark,
83 badge: RailBadge::None,
84 },
85 ]
86}
87
88fn tooltip_text(item: &RailItem) -> SharedString {
89 match keys_for_label(item.shortcut_label) {
90 Some(keys) => format!("{} ({keys})", item.label).into(),
91 None => item.label.into(),
92 }
93}
94
95pub struct RailView {
96 state: Entity<AppState>,
97 activity: Entity<ActivityCenter>,
98 sidebar: Entity<SidebarView>,
99 _observations: [Subscription; 3],
100}
101
102impl RailView {
103 pub fn new(
104 state: Entity<AppState>,
105 activity: Entity<ActivityCenter>,
106 sidebar: Entity<SidebarView>,
107 cx: &mut Context<Self>,
108 ) -> Self {
109 let observations = [
110 cx.observe(&state, |_, _, cx| cx.notify()),
111 cx.observe(&activity, |_, _, cx| cx.notify()),
112 cx.observe(&sidebar, |_, _, cx| cx.notify()),
113 ];
114 RailView {
115 state,
116 activity,
117 sidebar,
118 _observations: observations,
119 }
120 }
121
122 fn item(&self, item: &RailItem, active: bool) -> impl IntoElement {
123 let tab = item.tab;
124 let sidebar = self.sidebar.clone();
125 let tooltip = tooltip_text(item);
126 let color = if active {
127 theme::accent_text()
128 } else {
129 theme::text_muted()
130 };
131 div()
132 .id(SharedString::from(format!("rail-{}", item.label)))
133 .relative()
134 .w(px(ITEM_WIDTH))
135 .h(px(ITEM_HEIGHT))
136 .flex()
137 .flex_col()
138 .flex_none()
139 .items_center()
140 .justify_center()
141 .gap(px(4.))
142 .rounded(px(10.))
143 .cursor_pointer()
144 .when(active, |button| button.bg(theme::surface_raised()))
145 .when(!active, |button| button.hover(|button| button.bg(theme::row_hover())))
146 .child(icon(item.icon, ICON_SIZE, color))
147 .child(
148 div()
149 .whitespace_nowrap()
150 .text_size(px(10.))
151 .font_weight(FontWeight::SEMIBOLD)
152 .text_color(color)
153 .child(item.label),
154 )
155 .child(badge(item.badge))
156 .tooltip(move |window, cx| Tooltip::new(tooltip.clone()).build(window, cx))
157 .on_click(move |_, _, cx| {
158 sidebar.update(cx, |sidebar, cx| sidebar.open_tab(tab, cx));
159 })
160 }
161}
162
163fn badge(badge: RailBadge) -> Div {
164 match badge {
165 RailBadge::None => div(),
166 RailBadge::Dot => div()
167 .absolute()
168 .top(px(8.))
169 .left(px(31.))
170 .size(px(7.))
171 .rounded_full()
172 .bg(theme::accent()),
173 RailBadge::Count(count) => div()
174 .absolute()
175 .top(px(5.))
176 .left(px(28.))
177 .min_w(px(16.))
178 .h(px(16.))
179 .px(px(4.))
180 .flex()
181 .items_center()
182 .justify_center()
183 .rounded(px(8.))
184 .bg(theme::accent())
185 .text_color(theme::on_accent())
186 .text_size(px(10.))
187 .line_height(px(16.))
188 .font_weight(FontWeight::BOLD)
189 .child(format::badge_text(count)),
190 }
191}
192
193impl Render for RailView {
194 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
195 let active = self.sidebar.read(cx).tab();
196 let activity_unread = self.activity.read(cx).feed().unread_count();
197 let (unread_chats, channel_dot) = {
198 let state = self.state.read(cx);
199 (
200 unread_chat_count(&state.sidebar.chats),
201 any_unread_channel(&state.sidebar),
202 )
203 };
204 v_flex()
205 .w(px(RAIL_WIDTH))
206 .h_full()
207 .flex_none()
208 .items_center()
209 .gap(px(2.))
210 .pt(px(0.))
211 .children(
212 rail_items(activity_unread, unread_chats as usize, channel_dot)
213 .iter()
214 .map(|item| self.item(item, item.tab == active)),
215 )
216 }
217}
218
219#[cfg(test)]
220mod tests {
221 use super::{RailBadge, count_badge_for, rail_items, tooltip_text};
222 use crate::views::sidebar::SidebarTab;
223
224 #[test]
225 fn items_follow_the_spec_order() {
226 let tabs: Vec<SidebarTab> = rail_items(0, 0, false).iter().map(|item| item.tab).collect();
227 assert_eq!(
228 tabs,
229 [
230 SidebarTab::Activity,
231 SidebarTab::Chats,
232 SidebarTab::Channels,
233 SidebarTab::Calls,
234 SidebarTab::Saved
235 ]
236 );
237 }
238
239 #[test]
240 fn badges_follow_the_unread_inputs() {
241 let items = rail_items(3, 5, true);
242 assert_eq!(items[0].badge, RailBadge::Count(3));
243 assert_eq!(items[1].badge, RailBadge::Count(5));
244 assert_eq!(items[2].badge, RailBadge::Dot);
245 assert_eq!(items[3].badge, RailBadge::None);
246 assert_eq!(items[4].badge, RailBadge::None);
247 let quiet = rail_items(0, 0, false);
248 assert!(quiet.iter().all(|item| item.badge == RailBadge::None));
249 assert_eq!(count_badge_for(usize::MAX), RailBadge::Count(u32::MAX));
250 }
251
252 #[test]
253 fn every_item_shows_its_shortcut_in_the_tooltip() {
254 let items = rail_items(0, 0, false);
255 assert_eq!(tooltip_text(&items[0]), "Activity (Ctrl+1)");
256 assert!(items.iter().all(|item| tooltip_text(item).ends_with(')')));
257 }
258}