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 TAB_HEIGHT: f32 = 34.;
16const TAB_GAP: f32 = 14.;
17const TAB_TEXT_SIZE: f32 = 12.5;
18const BAR_PADDING: f32 = 20.;
19const CHAR_WIDTH: f32 = 6.5;
20const ARROW_WIDTH: f32 = 14.;
21const OVERFLOW_WIDTH: f32 = 44.;
22const EXTERNAL_ARROW: &str = "\u{2197}";
23
24pub const POSTS_LABEL: &str = "Posts";
25pub const SHARED_LABEL: &str = "Shared";
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum ChannelPane {
29 Posts,
30 Shared,
31 Web,
32}
33
34pub type SelectPane = Rc<dyn Fn(ChannelPane, &mut App)>;
35pub type OpenTab = Rc<dyn Fn(usize, &mut Window, &mut App)>;
36
37#[derive(Clone)]
38pub struct TabBarActions {
39 pub select: SelectPane,
40 pub open: OpenTab,
41}
42
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub enum TabAction {
45 Embed(String),
46 Browser(String),
47 ResolveLink,
48}
49
50pub fn tab_action(
51 tab: &ChannelTabRecord,
52 embedding: bool,
53 resolved_link: Option<&str>,
54) -> TabAction {
55 match (&tab.open_url, resolved_link) {
56 (Some(url), _) if embedding && tab.is_website() => TabAction::Embed(url.clone()),
57 (Some(url), _) => TabAction::Browser(url.clone()),
58 (None, Some(link)) => TabAction::Browser(link.to_owned()),
59 (None, None) => TabAction::ResolveLink,
60 }
61}
62
63pub fn fallback_tab_link(tab: &ChannelTabRecord, channel_id: &str) -> String {
64 channel_tab_link(channel_id, &tab.tab_id, &tab.name)
65}
66
67fn label_width(label: &str, external: bool) -> f32 {
68 label.chars().count() as f32 * CHAR_WIDTH + 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(3.))
109 .cursor_pointer()
110 .border_b_2()
111 .border_color(if active {
112 theme::accent()
113 } else {
114 transparent_black()
115 })
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}
130
131fn overflow_button(
132 hidden: &[(usize, String, bool)],
133 embedding: bool,
134 actions: &TabBarActions,
135) -> impl IntoElement {
136 let entries = hidden.to_vec();
137 let open = actions.open.clone();
138 Button::new("channel-tab-overflow")
139 .ghost()
140 .compact()
141 .label(format!("+{}", hidden.len()))
142 .dropdown_menu(move |menu, _, _| {
143 entries.iter().fold(menu, |menu, (index, name, website)| {
144 let (open, index) = (open.clone(), *index);
145 let label = if embedding && *website {
146 name.clone()
147 } else {
148 format!("{name} {EXTERNAL_ARROW}")
149 };
150 menu.item(
151 PopupMenuItem::new(label)
152 .on_click(move |_, window, cx| open(index, window, cx)),
153 )
154 })
155 })
156}
157
158pub fn render_tab_bar(
159 tabs: &[ChannelTabRecord],
160 pane: ChannelPane,
161 active_tab_id: Option<&str>,
162 embedding: bool,
163 available_width: f32,
164 actions: &TabBarActions,
165) -> AnyElement {
166 let visible = visible_tab_count(tabs, available_width - 2. * BAR_PADDING);
167 let select = actions.select.clone();
168 let posts = tab_label(
169 "channel-tab-posts".into(),
170 POSTS_LABEL,
171 pane == ChannelPane::Posts,
172 false,
173 {
174 let select = select.clone();
175 move |_, cx| select(ChannelPane::Posts, cx)
176 },
177 );
178 let shared = tab_label(
179 "channel-tab-shared".into(),
180 SHARED_LABEL,
181 pane == ChannelPane::Shared,
182 false,
183 move |_, cx| select(ChannelPane::Shared, cx),
184 );
185 let configured = tabs.iter().enumerate().take(visible).map(|(index, tab)| {
186 let open = actions.open.clone();
187 tab_label(
188 ElementId::NamedInteger("channel-tab".into(), index as u64),
189 &tab.name,
190 active_tab_id == Some(tab.tab_id.as_str()),
191 !(embedding && tab.is_website()),
192 move |window, cx| open(index, window, cx),
193 )
194 });
195 let hidden: Vec<(usize, String, bool)> = tabs
196 .iter()
197 .enumerate()
198 .skip(visible)
199 .map(|(index, tab)| (index, tab.name.clone(), tab.is_website()))
200 .collect();
201 h_flex()
202 .w_full()
203 .flex_none()
204 .px(px(BAR_PADDING))
205 .gap(px(TAB_GAP))
206 .items_center()
207 .border_b_1()
208 .border_color(theme::border())
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}