BlueGit

ict rusty-teams

Files

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

channel_tabs.rs

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