BlueGit

ict rusty-teams

Files

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

channel_tabs.rs

channel_tabs.rs 7.8 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}
34
35pub type SelectPane = Rc<dyn Fn(ChannelPane, &mut App)>;
36pub type OpenTab = Rc<dyn Fn(usize, &mut Window, &mut App)>;
37
38#[derive(Clone)]
39pub struct TabBarActions {
40 pub select: SelectPane,
41 pub open: OpenTab,
42}
43
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub enum TabAction {
46 Browser(String),
47 ResolveLink,
48}
49
50pub fn tab_action(tab: &ChannelTabRecord, resolved_link: Option<&str>) -> TabAction {
51 match (&tab.open_url, resolved_link) {
52 (Some(url), _) => TabAction::Browser(url.clone()),
53 (None, Some(link)) => TabAction::Browser(link.to_owned()),
54 (None, None) => TabAction::ResolveLink,
55 }
56}
57
58pub fn fallback_tab_link(tab: &ChannelTabRecord, channel_id: &str) -> String {
59 channel_tab_link(channel_id, &tab.tab_id, &tab.name)
60}
61
62fn label_width(label: &str, external: bool) -> f32 {
63 label.chars().count() as f32 * CHAR_WIDTH
64 + 2. * TAB_PADDING
65 + if external { ARROW_WIDTH } else { 0. }
66}
67
68pub fn visible_tab_count(tabs: &[ChannelTabRecord], available_width: f32) -> usize {
69 let fixed = label_width(POSTS_LABEL, false) + TAB_GAP + label_width(SHARED_LABEL, false);
70 let widths: Vec<f32> = tabs
71 .iter()
72 .map(|tab| TAB_GAP + label_width(&tab.name, true))
73 .collect();
74 if fixed + widths.iter().sum::<f32>() <= available_width {
75 return tabs.len();
76 }
77 let room = available_width - fixed - TAB_GAP - OVERFLOW_WIDTH;
78 let mut used = 0.;
79 widths
80 .iter()
81 .take_while(|width| {
82 used += **width;
83 used <= room
84 })
85 .count()
86}
87
88fn tab_label(
89 id: ElementId,
90 text: &str,
91 active: bool,
92 external: bool,
93 on_click: impl Fn(&mut Window, &mut App) + 'static,
94) -> Stateful<Div> {
95 let color = if active {
96 theme::text()
97 } else {
98 theme::text_muted()
99 };
100 h_flex()
101 .id(id)
102 .h(px(TAB_HEIGHT))
103 .flex_none()
104 .items_center()
105 .gap(px(4.))
106 .px(px(TAB_PADDING))
107 .rounded(px(7.))
108 .cursor_pointer()
109 .when(active, |tab| tab.bg(theme::surface_raised()))
110 .text_size(px(TAB_TEXT_SIZE))
111 .font_weight(if active {
112 FontWeight::SEMIBOLD
113 } else {
114 FontWeight::NORMAL
115 })
116 .text_color(color)
117 .hover(|tab| tab.text_color(theme::text()))
118 .on_click(move |_, window, cx| on_click(window, cx))
119 .child(text.to_owned())
120 .when(external, |tab| {
121 tab.child(div().text_color(theme::text_faint()).child(EXTERNAL_ARROW))
122 })
123}
124
125fn overflow_button(hidden: &[(usize, String)], actions: &TabBarActions) -> impl IntoElement {
126 let entries = hidden.to_vec();
127 let open = actions.open.clone();
128 Button::new("channel-tab-overflow")
129 .ghost()
130 .compact()
131 .label(format!("+{}", hidden.len()))
132 .dropdown_menu(move |menu, _, _| {
133 entries.iter().fold(menu, |menu, (index, name)| {
134 let (open, index) = (open.clone(), *index);
135 menu.item(
136 PopupMenuItem::new(format!("{name} {EXTERNAL_ARROW}"))
137 .on_click(move |_, window, cx| open(index, window, cx)),
138 )
139 })
140 })
141}
142
143pub fn render_tab_bar(
144 tabs: &[ChannelTabRecord],
145 pane: ChannelPane,
146 available_width: f32,
147 actions: &TabBarActions,
148) -> AnyElement {
149 let visible = visible_tab_count(tabs, available_width - 2. * BAR_PADDING);
150 let select = actions.select.clone();
151 let posts = tab_label(
152 "channel-tab-posts".into(),
153 POSTS_LABEL,
154 pane == ChannelPane::Posts,
155 false,
156 {
157 let select = select.clone();
158 move |_, cx| select(ChannelPane::Posts, cx)
159 },
160 );
161 let shared = tab_label(
162 "channel-tab-shared".into(),
163 SHARED_LABEL,
164 pane == ChannelPane::Shared,
165 false,
166 move |_, cx| select(ChannelPane::Shared, cx),
167 );
168 let configured = tabs.iter().enumerate().take(visible).map(|(index, tab)| {
169 let open = actions.open.clone();
170 tab_label(
171 ElementId::NamedInteger("channel-tab".into(), index as u64),
172 &tab.name,
173 false,
174 true,
175 move |window, cx| open(index, window, cx),
176 )
177 });
178 let hidden: Vec<(usize, String)> = tabs
179 .iter()
180 .enumerate()
181 .skip(visible)
182 .map(|(index, tab)| (index, tab.name.clone()))
183 .collect();
184 h_flex()
185 .w_full()
186 .h(px(BAR_HEIGHT))
187 .flex_none()
188 .px(px(BAR_PADDING))
189 .gap(px(TAB_GAP))
190 .items_center()
191 .child(posts)
192 .child(shared)
193 .children(configured)
194 .when(!hidden.is_empty(), |bar| {
195 bar.child(overflow_button(&hidden, actions))
196 })
197 .into_any_element()
198}
199
200#[cfg(test)]
201mod tests {
202 use store::ChannelTabRecord;
203
204 use super::{TabAction, fallback_tab_link, tab_action, visible_tab_count};
205
206 fn tab(name: &str, open_url: Option<&str>) -> ChannelTabRecord {
207 ChannelTabRecord {
208 tab_id: "tab-1".to_owned(),
209 name: name.to_owned(),
210 definition_id: "app".to_owned(),
211 open_url: open_url.map(str::to_owned),
212 }
213 }
214
215 fn website(url: &str) -> ChannelTabRecord {
216 ChannelTabRecord {
217 definition_id: "com.microsoft.teamspace.tab.web".to_owned(),
218 ..tab("Docs", Some(url))
219 }
220 }
221
222 #[test]
223 fn website_tabs_open_in_the_browser() {
224 assert_eq!(
225 tab_action(&website("https://docs.example"), None),
226 TabAction::Browser("https://docs.example".to_owned())
227 );
228 }
229
230 #[test]
231 fn other_tabs_with_a_page_open_it_in_the_browser() {
232 let notes = tab("Notes", Some("https://onenote.example/web"));
233 assert_eq!(
234 tab_action(&notes, None),
235 TabAction::Browser("https://onenote.example/web".to_owned())
236 );
237 }
238
239 #[test]
240 fn app_tabs_resolve_their_link_once_and_then_reuse_it() {
241 let board = tab("Board", None);
242 assert_eq!(tab_action(&board, None), TabAction::ResolveLink);
243 assert_eq!(
244 tab_action(&board, Some("https://teams.example/l/tab")),
245 TabAction::Browser("https://teams.example/l/tab".to_owned())
246 );
247 }
248
249 #[test]
250 fn the_fallback_link_is_the_hand_built_deep_link() {
251 assert_eq!(
252 fallback_tab_link(&tab("Board", None), "19:c@thread.tacv2"),
253 "https://teams.microsoft.com/l/channel/19:c@thread.tacv2/tab%3A%3Atab-1?label=Board"
254 );
255 }
256
257 #[test]
258 fn every_tab_stays_visible_while_it_fits() {
259 let tabs = [tab("Docs", None), tab("Planner", None)];
260 assert_eq!(visible_tab_count(&tabs, 600.), 2);
261 }
262
263 #[test]
264 fn tabs_that_do_not_fit_move_into_the_overflow() {
265 let tabs: Vec<ChannelTabRecord> = (0..8)
266 .map(|n| tab(&format!("Tab number {n}"), None))
267 .collect();
268 let visible = visible_tab_count(&tabs, 420.);
269 assert!(visible > 0 && visible < tabs.len());
270 assert_eq!(visible_tab_count(&tabs, 40.), 0);
271 }
272}