BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-ui-kit / src / channel_tabs.rs

channel_tabs.rs

channel_tabs.rs 9.1 KiB Download Raw
1use std::rc::Rc;
2
3use gpui_kit::component::{
4 button::{Button, ButtonVariants as _},
5 h_flex,
6 menu::{DropdownMenu as _, PopupMenuItem},
7};
8use gpui_kit::prelude::FluentBuilder as _;
9use gpui_kit::*;
10use store::ChannelTabRecord;
11use teams_core::channel_tab_link;
12
13use app_base::theme;
14
15const TAB_GAP: f32 = 4.;
16const TAB_PADDING: f32 = 10.;
17const TAB_TEXT_SIZE: f32 = 14.;
18const INDICATOR_HEIGHT: f32 = 3.;
19const CHAR_WIDTH: f32 = 7.5;
20const ARROW_WIDTH: f32 = 14.;
21const OVERFLOW_WIDTH: f32 = 44.;
22const EXTERNAL_ARROW: &str = "\u{2197}";
23const TITLE_CHAR_WIDTH: f32 = 8.;
24pub const HEADER_TITLE_MAX_WIDTH: f32 = 240.;
25
26pub const POSTS_LABEL: &str = "Posts";
27pub const SHARED_LABEL: &str = "Shared";
28pub const CHAT_LABEL: &str = "Chat";
29pub const RECAP_LABEL: &str = "Recap";
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum ChannelPane {
33 Posts,
34 Shared,
35 Recap,
36}
37
38pub type SelectPane = Rc<dyn Fn(ChannelPane, &mut App)>;
39pub type OpenTab = Rc<dyn Fn(usize, &mut Window, &mut App)>;
40
41#[derive(Clone)]
42pub struct TabBarActions {
43 pub select: SelectPane,
44 pub open: OpenTab,
45}
46
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub enum TabAction {
49 Browser(String),
50 ResolveLink,
51}
52
53pub fn tab_action(tab: &ChannelTabRecord, resolved_link: Option<&str>) -> TabAction {
54 match (&tab.open_url, resolved_link) {
55 (Some(url), _) => TabAction::Browser(url.clone()),
56 (None, Some(link)) => TabAction::Browser(link.to_owned()),
57 (None, None) => TabAction::ResolveLink,
58 }
59}
60
61pub fn fallback_tab_link(tab: &ChannelTabRecord, channel_id: &str) -> String {
62 channel_tab_link(channel_id, &tab.tab_id, &tab.name)
63}
64
65fn label_width(label: &str, external: bool) -> f32 {
66 label.chars().count() as f32 * CHAR_WIDTH
67 + 2. * TAB_PADDING
68 + if external { ARROW_WIDTH } else { 0. }
69}
70
71pub fn header_title_width(title: &str) -> f32 {
72 (title.chars().count() as f32 * TITLE_CHAR_WIDTH).min(HEADER_TITLE_MAX_WIDTH)
73}
74
75pub fn visible_tab_count(tabs: &[ChannelTabRecord], available_width: f32) -> usize {
76 let fixed = label_width(POSTS_LABEL, false) + TAB_GAP + label_width(SHARED_LABEL, false);
77 let widths: Vec<f32> = tabs
78 .iter()
79 .map(|tab| TAB_GAP + label_width(&tab.name, true))
80 .collect();
81 if fixed + widths.iter().sum::<f32>() <= available_width {
82 return tabs.len();
83 }
84 let room = available_width - fixed - TAB_GAP - OVERFLOW_WIDTH;
85 let mut used = 0.;
86 widths
87 .iter()
88 .take_while(|width| {
89 used += **width;
90 used <= room
91 })
92 .count()
93}
94
95fn tab_label(
96 id: ElementId,
97 text: &str,
98 active: bool,
99 external: bool,
100 on_click: impl Fn(&mut Window, &mut App) + 'static,
101) -> Stateful<Div> {
102 let color = if active {
103 theme::text()
104 } else {
105 theme::text_muted()
106 };
107 h_flex()
108 .id(id)
109 .relative()
110 .h_full()
111 .flex_none()
112 .items_center()
113 .gap(px(4.))
114 .px(px(TAB_PADDING))
115 .cursor_pointer()
116 .text_size(px(TAB_TEXT_SIZE))
117 .font_weight(if active {
118 FontWeight::SEMIBOLD
119 } else {
120 FontWeight::NORMAL
121 })
122 .text_color(color)
123 .hover(|tab| tab.text_color(theme::text()))
124 .on_click(move |_, window, cx| on_click(window, cx))
125 .child(text.to_owned())
126 .when(external, |tab| {
127 tab.child(div().text_color(theme::text_faint()).child(EXTERNAL_ARROW))
128 })
129 .when(active, |tab| tab.child(indicator()))
130}
131
132fn indicator() -> Div {
133 div()
134 .absolute()
135 .bottom_0()
136 .left(px(TAB_PADDING))
137 .right(px(TAB_PADDING))
138 .h(px(INDICATOR_HEIGHT))
139 .rounded_full()
140 .bg(theme::accent())
141}
142
143fn header_tab_row() -> Div {
144 h_flex().h_full().gap(px(TAB_GAP)).items_center()
145}
146
147fn overflow_button(hidden: &[(usize, String)], actions: &TabBarActions) -> impl IntoElement {
148 let entries = hidden.to_vec();
149 let open = actions.open.clone();
150 Button::new("channel-tab-overflow")
151 .ghost()
152 .compact()
153 .label(format!("+{}", hidden.len()))
154 .dropdown_menu(move |menu, _, _| {
155 entries.iter().fold(menu, |menu, (index, name)| {
156 let (open, index) = (open.clone(), *index);
157 menu.item(
158 PopupMenuItem::new(format!("{name} {EXTERNAL_ARROW}"))
159 .on_click(move |_, window, cx| open(index, window, cx)),
160 )
161 })
162 })
163}
164
165pub fn render_tab_bar(
166 tabs: &[ChannelTabRecord],
167 pane: ChannelPane,
168 available_width: f32,
169 actions: &TabBarActions,
170) -> AnyElement {
171 let visible = visible_tab_count(tabs, available_width);
172 let select = actions.select.clone();
173 let posts = tab_label(
174 "channel-tab-posts".into(),
175 POSTS_LABEL,
176 pane == ChannelPane::Posts,
177 false,
178 {
179 let select = select.clone();
180 move |_, cx| select(ChannelPane::Posts, cx)
181 },
182 );
183 let shared = tab_label(
184 "channel-tab-shared".into(),
185 SHARED_LABEL,
186 pane == ChannelPane::Shared,
187 false,
188 move |_, cx| select(ChannelPane::Shared, cx),
189 );
190 let configured = tabs.iter().enumerate().take(visible).map(|(index, tab)| {
191 let open = actions.open.clone();
192 tab_label(
193 ElementId::NamedInteger("channel-tab".into(), index as u64),
194 &tab.name,
195 false,
196 true,
197 move |window, cx| open(index, window, cx),
198 )
199 });
200 let hidden: Vec<(usize, String)> = tabs
201 .iter()
202 .enumerate()
203 .skip(visible)
204 .map(|(index, tab)| (index, tab.name.clone()))
205 .collect();
206 header_tab_row()
207 .child(posts)
208 .child(shared)
209 .children(configured)
210 .when(!hidden.is_empty(), |bar| {
211 bar.child(overflow_button(&hidden, actions))
212 })
213 .into_any_element()
214}
215
216pub fn render_meeting_bar(pane: ChannelPane, select: SelectPane) -> AnyElement {
217 let chat = tab_label(
218 "meeting-tab-chat".into(),
219 CHAT_LABEL,
220 pane == ChannelPane::Posts,
221 false,
222 {
223 let select = select.clone();
224 move |_, cx| select(ChannelPane::Posts, cx)
225 },
226 );
227 let recap = tab_label(
228 "meeting-tab-recap".into(),
229 RECAP_LABEL,
230 pane == ChannelPane::Recap,
231 false,
232 move |_, cx| select(ChannelPane::Recap, cx),
233 )
234 .debug_selector(|| "shell/islands/main/recap-tab".into());
235 header_tab_row().child(chat).child(recap).into_any_element()
236}
237
238#[cfg(test)]
239mod tests {
240 use store::ChannelTabRecord;
241
242 use super::{
243 HEADER_TITLE_MAX_WIDTH, TabAction, fallback_tab_link, header_title_width, tab_action,
244 visible_tab_count,
245 };
246
247 fn tab(name: &str, open_url: Option<&str>) -> ChannelTabRecord {
248 ChannelTabRecord {
249 tab_id: "tab-1".to_owned(),
250 name: name.to_owned(),
251 definition_id: "app".to_owned(),
252 open_url: open_url.map(str::to_owned),
253 }
254 }
255
256 fn website(url: &str) -> ChannelTabRecord {
257 ChannelTabRecord {
258 definition_id: "com.microsoft.teamspace.tab.web".to_owned(),
259 ..tab("Docs", Some(url))
260 }
261 }
262
263 #[test]
264 fn website_tabs_open_in_the_browser() {
265 assert_eq!(
266 tab_action(&website("https://docs.example"), None),
267 TabAction::Browser("https://docs.example".to_owned())
268 );
269 }
270
271 #[test]
272 fn other_tabs_with_a_page_open_it_in_the_browser() {
273 let notes = tab("Notes", Some("https://onenote.example/web"));
274 assert_eq!(
275 tab_action(&notes, None),
276 TabAction::Browser("https://onenote.example/web".to_owned())
277 );
278 }
279
280 #[test]
281 fn app_tabs_resolve_their_link_once_and_then_reuse_it() {
282 let board = tab("Board", None);
283 assert_eq!(tab_action(&board, None), TabAction::ResolveLink);
284 assert_eq!(
285 tab_action(&board, Some("https://teams.example/l/tab")),
286 TabAction::Browser("https://teams.example/l/tab".to_owned())
287 );
288 }
289
290 #[test]
291 fn the_fallback_link_is_the_hand_built_deep_link() {
292 assert_eq!(
293 fallback_tab_link(&tab("Board", None), "19:c@thread.tacv2"),
294 "https://teams.microsoft.com/l/channel/19:c@thread.tacv2/tab%3A%3Atab-1?label=Board"
295 );
296 }
297
298 #[test]
299 fn long_header_titles_are_capped() {
300 assert_eq!(header_title_width("General"), 56.);
301 assert_eq!(header_title_width(&"x".repeat(80)), HEADER_TITLE_MAX_WIDTH);
302 }
303
304 #[test]
305 fn every_tab_stays_visible_while_it_fits() {
306 let tabs = [tab("Docs", None), tab("Planner", None)];
307 assert_eq!(visible_tab_count(&tabs, 600.), 2);
308 }
309
310 #[test]
311 fn tabs_that_do_not_fit_move_into_the_overflow() {
312 let tabs: Vec<ChannelTabRecord> = (0..8)
313 .map(|n| tab(&format!("Tab number {n}"), None))
314 .collect();
315 let visible = visible_tab_count(&tabs, 420.);
316 assert!(visible > 0 && visible < tabs.len());
317 assert_eq!(visible_tab_count(&tabs, 40.), 0);
318 }
319}