BlueGit

ict rusty-teams

Files

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

channel_tabs.rs

channel_tabs.rs 8.5 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 crate::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";
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum ChannelPane {
31 Posts,
32 Shared,
33 Web,
34}
35
36pub type SelectPane = Rc<dyn Fn(ChannelPane, &mut App)>;
37pub type OpenTab = Rc<dyn Fn(usize, &mut Window, &mut App)>;
38
39#[derive(Clone)]
40pub struct TabBarActions {
41 pub select: SelectPane,
42 pub open: OpenTab,
43}
44
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub enum TabAction {
47 Embed(String),
48 Browser(String),
49 ResolveLink,
50}
51
52pub fn tab_action(
53 tab: &ChannelTabRecord,
54 embedding: bool,
55 resolved_link: Option<&str>,
56) -> TabAction {
57 match (&tab.open_url, resolved_link) {
58 (Some(url), _) if embedding && tab.is_website() => TabAction::Embed(url.clone()),
59 (Some(url), _) => TabAction::Browser(url.clone()),
60 (None, Some(link)) => TabAction::Browser(link.to_owned()),
61 (None, None) => TabAction::ResolveLink,
62 }
63}
64
65pub fn fallback_tab_link(tab: &ChannelTabRecord, channel_id: &str) -> String {
66 channel_tab_link(channel_id, &tab.tab_id, &tab.name)
67}
68
69fn label_width(label: &str, external: bool) -> f32 {
70 label.chars().count() as f32 * CHAR_WIDTH
71 + 2. * TAB_PADDING
72 + if external { ARROW_WIDTH } else { 0. }
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 .h(px(TAB_HEIGHT))
110 .flex_none()
111 .items_center()
112 .gap(px(4.))
113 .px(px(TAB_PADDING))
114 .rounded(px(7.))
115 .cursor_pointer()
116 .when(active, |tab| tab.bg(theme::surface_raised()))
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}
131
132fn overflow_button(
133 hidden: &[(usize, String, bool)],
134 embedding: bool,
135 actions: &TabBarActions,
136) -> impl IntoElement {
137 let entries = hidden.to_vec();
138 let open = actions.open.clone();
139 Button::new("channel-tab-overflow")
140 .ghost()
141 .compact()
142 .label(format!("+{}", hidden.len()))
143 .dropdown_menu(move |menu, _, _| {
144 entries.iter().fold(menu, |menu, (index, name, website)| {
145 let (open, index) = (open.clone(), *index);
146 let label = if embedding && *website {
147 name.clone()
148 } else {
149 format!("{name} {EXTERNAL_ARROW}")
150 };
151 menu.item(
152 PopupMenuItem::new(label)
153 .on_click(move |_, window, cx| open(index, window, cx)),
154 )
155 })
156 })
157}
158
159pub fn render_tab_bar(
160 tabs: &[ChannelTabRecord],
161 pane: ChannelPane,
162 active_tab_id: Option<&str>,
163 embedding: bool,
164 available_width: f32,
165 actions: &TabBarActions,
166) -> AnyElement {
167 let visible = visible_tab_count(tabs, available_width - 2. * BAR_PADDING);
168 let select = actions.select.clone();
169 let posts = tab_label(
170 "channel-tab-posts".into(),
171 POSTS_LABEL,
172 pane == ChannelPane::Posts,
173 false,
174 {
175 let select = select.clone();
176 move |_, cx| select(ChannelPane::Posts, cx)
177 },
178 );
179 let shared = tab_label(
180 "channel-tab-shared".into(),
181 SHARED_LABEL,
182 pane == ChannelPane::Shared,
183 false,
184 move |_, cx| select(ChannelPane::Shared, cx),
185 );
186 let configured = tabs.iter().enumerate().take(visible).map(|(index, tab)| {
187 let open = actions.open.clone();
188 tab_label(
189 ElementId::NamedInteger("channel-tab".into(), index as u64),
190 &tab.name,
191 active_tab_id == Some(tab.tab_id.as_str()),
192 !(embedding && tab.is_website()),
193 move |window, cx| open(index, window, cx),
194 )
195 });
196 let hidden: Vec<(usize, String, bool)> = tabs
197 .iter()
198 .enumerate()
199 .skip(visible)
200 .map(|(index, tab)| (index, tab.name.clone(), tab.is_website()))
201 .collect();
202 h_flex()
203 .w_full()
204 .h(px(BAR_HEIGHT))
205 .flex_none()
206 .px(px(BAR_PADDING))
207 .gap(px(TAB_GAP))
208 .items_center()
209 .child(posts)
210 .child(shared)
211 .children(configured)
212 .when(!hidden.is_empty(), |bar| {
213 bar.child(overflow_button(&hidden, embedding, actions))
214 })
215 .into_any_element()
216}
217
218#[cfg(test)]
219mod tests {
220 use store::ChannelTabRecord;
221
222 use super::{TabAction, fallback_tab_link, tab_action, visible_tab_count};
223
224 fn tab(name: &str, open_url: Option<&str>) -> ChannelTabRecord {
225 ChannelTabRecord {
226 tab_id: "tab-1".to_owned(),
227 name: name.to_owned(),
228 definition_id: "app".to_owned(),
229 open_url: open_url.map(str::to_owned),
230 }
231 }
232
233 fn website(url: &str) -> ChannelTabRecord {
234 ChannelTabRecord {
235 definition_id: "com.microsoft.teamspace.tab.web".to_owned(),
236 ..tab("Docs", Some(url))
237 }
238 }
239
240 #[test]
241 fn website_tabs_embed_only_where_the_host_can() {
242 let docs = website("https://docs.example");
243 assert_eq!(
244 tab_action(&docs, true, None),
245 TabAction::Embed("https://docs.example".to_owned())
246 );
247 assert_eq!(
248 tab_action(&docs, false, None),
249 TabAction::Browser("https://docs.example".to_owned())
250 );
251 }
252
253 #[test]
254 fn other_tabs_with_a_page_open_it_in_the_browser() {
255 let notes = tab("Notes", Some("https://onenote.example/web"));
256 assert_eq!(
257 tab_action(&notes, true, None),
258 TabAction::Browser("https://onenote.example/web".to_owned())
259 );
260 }
261
262 #[test]
263 fn app_tabs_resolve_their_link_once_and_then_reuse_it() {
264 let board = tab("Board", None);
265 assert_eq!(tab_action(&board, true, None), TabAction::ResolveLink);
266 assert_eq!(
267 tab_action(&board, true, Some("https://teams.example/l/tab")),
268 TabAction::Browser("https://teams.example/l/tab".to_owned())
269 );
270 }
271
272 #[test]
273 fn the_fallback_link_is_the_hand_built_deep_link() {
274 assert_eq!(
275 fallback_tab_link(&tab("Board", None), "19:c@thread.tacv2"),
276 "https://teams.microsoft.com/l/channel/19:c@thread.tacv2/tab%3A%3Atab-1?label=Board"
277 );
278 }
279
280 #[test]
281 fn every_tab_stays_visible_while_it_fits() {
282 let tabs = [tab("Docs", None), tab("Planner", None)];
283 assert_eq!(visible_tab_count(&tabs, 600.), 2);
284 }
285
286 #[test]
287 fn tabs_that_do_not_fit_move_into_the_overflow() {
288 let tabs: Vec<ChannelTabRecord> = (0..8)
289 .map(|n| tab(&format!("Tab number {n}"), None))
290 .collect();
291 let visible = visible_tab_count(&tabs, 420.);
292 assert!(visible > 0 && visible < tabs.len());
293 assert_eq!(visible_tab_count(&tabs, 40.), 0);
294 }
295}