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