BlueGit

ict rusty-teams

Files

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

channel_tabs.rs

3e25d31a History
channel_tabs.rs 8.7 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 BAR_HEIGHT: f32 = 36.;
16const TAB_HEIGHT: f32 = 28.;
17const TAB_GAP: f32 = 4.;
18const TAB_PADDING: f32 = 12.;
19const TAB_TEXT_SIZE: f32 = 12.5;
20const BAR_PADDING: f32 = 20.;
21const CHAR_WIDTH: f32 = 6.5;
22const ARROW_WIDTH: f32 = 14.;
23const OVERFLOW_WIDTH: f32 = 44.;
24const EXTERNAL_ARROW: &str = "\u{2197}";
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 visible_tab_count(tabs: &[ChannelTabRecord], available_width: f32) -> usize {
72 let fixed = label_width(POSTS_LABEL, false) + TAB_GAP + label_width(SHARED_LABEL, false);
73 let widths: Vec<f32> = tabs
74 .iter()
75 .map(|tab| TAB_GAP + label_width(&tab.name, true))
76 .collect();
77 if fixed + widths.iter().sum::<f32>() <= available_width {
78 return tabs.len();
79 }
80 let room = available_width - fixed - TAB_GAP - OVERFLOW_WIDTH;
81 let mut used = 0.;
82 widths
83 .iter()
84 .take_while(|width| {
85 used += **width;
86 used <= room
87 })
88 .count()
89}
90
91fn tab_label(
92 id: ElementId,
93 text: &str,
94 active: bool,
95 external: bool,
96 on_click: impl Fn(&mut Window, &mut App) + 'static,
97) -> Stateful<Div> {
98 let color = if active {
99 theme::text()
100 } else {
101 theme::text_muted()
102 };
103 h_flex()
104 .id(id)
105 .h(px(TAB_HEIGHT))
106 .flex_none()
107 .items_center()
108 .gap(px(4.))
109 .px(px(TAB_PADDING))
110 .rounded(px(7.))
111 .cursor_pointer()
112 .when(active, |tab| tab.bg(theme::surface_raised()))
113 .text_size(px(TAB_TEXT_SIZE))
114 .font_weight(if active {
115 FontWeight::SEMIBOLD
116 } else {
117 FontWeight::NORMAL
118 })
119 .text_color(color)
120 .hover(|tab| tab.text_color(theme::text()))
121 .on_click(move |_, window, cx| on_click(window, cx))
122 .child(text.to_owned())
123 .when(external, |tab| {
124 tab.child(div().text_color(theme::text_faint()).child(EXTERNAL_ARROW))
125 })
126}
127
128fn overflow_button(hidden: &[(usize, String)], actions: &TabBarActions) -> impl IntoElement {
129 let entries = hidden.to_vec();
130 let open = actions.open.clone();
131 Button::new("channel-tab-overflow")
132 .ghost()
133 .compact()
134 .label(format!("+{}", hidden.len()))
135 .dropdown_menu(move |menu, _, _| {
136 entries.iter().fold(menu, |menu, (index, name)| {
137 let (open, index) = (open.clone(), *index);
138 menu.item(
139 PopupMenuItem::new(format!("{name} {EXTERNAL_ARROW}"))
140 .on_click(move |_, window, cx| open(index, window, cx)),
141 )
142 })
143 })
144}
145
146pub fn render_tab_bar(
147 tabs: &[ChannelTabRecord],
148 pane: ChannelPane,
149 available_width: f32,
150 actions: &TabBarActions,
151) -> AnyElement {
152 let visible = visible_tab_count(tabs, available_width - 2. * BAR_PADDING);
153 let select = actions.select.clone();
154 let posts = tab_label(
155 "channel-tab-posts".into(),
156 POSTS_LABEL,
157 pane == ChannelPane::Posts,
158 false,
159 {
160 let select = select.clone();
161 move |_, cx| select(ChannelPane::Posts, cx)
162 },
163 );
164 let shared = tab_label(
165 "channel-tab-shared".into(),
166 SHARED_LABEL,
167 pane == ChannelPane::Shared,
168 false,
169 move |_, cx| select(ChannelPane::Shared, cx),
170 );
171 let configured = tabs.iter().enumerate().take(visible).map(|(index, tab)| {
172 let open = actions.open.clone();
173 tab_label(
174 ElementId::NamedInteger("channel-tab".into(), index as u64),
175 &tab.name,
176 false,
177 true,
178 move |window, cx| open(index, window, cx),
179 )
180 });
181 let hidden: Vec<(usize, String)> = tabs
182 .iter()
183 .enumerate()
184 .skip(visible)
185 .map(|(index, tab)| (index, tab.name.clone()))
186 .collect();
187 h_flex()
188 .w_full()
189 .h(px(BAR_HEIGHT))
190 .flex_none()
191 .px(px(BAR_PADDING))
192 .gap(px(TAB_GAP))
193 .items_center()
194 .child(posts)
195 .child(shared)
196 .children(configured)
197 .when(!hidden.is_empty(), |bar| {
198 bar.child(overflow_button(&hidden, actions))
199 })
200 .into_any_element()
201}
202
203pub fn render_meeting_bar(pane: ChannelPane, select: SelectPane) -> AnyElement {
204 let chat = tab_label(
205 "meeting-tab-chat".into(),
206 CHAT_LABEL,
207 pane == ChannelPane::Posts,
208 false,
209 {
210 let select = select.clone();
211 move |_, cx| select(ChannelPane::Posts, cx)
212 },
213 );
214 let recap = tab_label(
215 "meeting-tab-recap".into(),
216 RECAP_LABEL,
217 pane == ChannelPane::Recap,
218 false,
219 move |_, cx| select(ChannelPane::Recap, cx),
220 )
221 .debug_selector(|| "shell/islands/main/recap-tab".into());
222 h_flex()
223 .w_full()
224 .h(px(BAR_HEIGHT))
225 .flex_none()
226 .px(px(BAR_PADDING))
227 .gap(px(TAB_GAP))
228 .items_center()
229 .child(chat)
230 .child(recap)
231 .into_any_element()
232}
233
234#[cfg(test)]
235mod tests {
236 use store::ChannelTabRecord;
237
238 use super::{TabAction, fallback_tab_link, tab_action, visible_tab_count};
239
240 fn tab(name: &str, open_url: Option<&str>) -> ChannelTabRecord {
241 ChannelTabRecord {
242 tab_id: "tab-1".to_owned(),
243 name: name.to_owned(),
244 definition_id: "app".to_owned(),
245 open_url: open_url.map(str::to_owned),
246 }
247 }
248
249 fn website(url: &str) -> ChannelTabRecord {
250 ChannelTabRecord {
251 definition_id: "com.microsoft.teamspace.tab.web".to_owned(),
252 ..tab("Docs", Some(url))
253 }
254 }
255
256 #[test]
257 fn website_tabs_open_in_the_browser() {
258 assert_eq!(
259 tab_action(&website("https://docs.example"), None),
260 TabAction::Browser("https://docs.example".to_owned())
261 );
262 }
263
264 #[test]
265 fn other_tabs_with_a_page_open_it_in_the_browser() {
266 let notes = tab("Notes", Some("https://onenote.example/web"));
267 assert_eq!(
268 tab_action(&notes, None),
269 TabAction::Browser("https://onenote.example/web".to_owned())
270 );
271 }
272
273 #[test]
274 fn app_tabs_resolve_their_link_once_and_then_reuse_it() {
275 let board = tab("Board", None);
276 assert_eq!(tab_action(&board, None), TabAction::ResolveLink);
277 assert_eq!(
278 tab_action(&board, Some("https://teams.example/l/tab")),
279 TabAction::Browser("https://teams.example/l/tab".to_owned())
280 );
281 }
282
283 #[test]
284 fn the_fallback_link_is_the_hand_built_deep_link() {
285 assert_eq!(
286 fallback_tab_link(&tab("Board", None), "19:c@thread.tacv2"),
287 "https://teams.microsoft.com/l/channel/19:c@thread.tacv2/tab%3A%3Atab-1?label=Board"
288 );
289 }
290
291 #[test]
292 fn every_tab_stays_visible_while_it_fits() {
293 let tabs = [tab("Docs", None), tab("Planner", None)];
294 assert_eq!(visible_tab_count(&tabs, 600.), 2);
295 }
296
297 #[test]
298 fn tabs_that_do_not_fit_move_into_the_overflow() {
299 let tabs: Vec<ChannelTabRecord> = (0..8)
300 .map(|n| tab(&format!("Tab number {n}"), None))
301 .collect();
302 let visible = visible_tab_count(&tabs, 420.);
303 assert!(visible > 0 && visible < tabs.len());
304 assert_eq!(visible_tab_count(&tabs, 40.), 0);
305 }
306}