use std::rc::Rc; use gpui_kit::component::{ button::{Button, ButtonVariants as _}, h_flex, menu::{DropdownMenu as _, PopupMenuItem}, }; use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::*; use store::ChannelTabRecord; use teams_core::channel_tab_link; use app_base::theme; const TAB_GAP: f32 = 4.; const TAB_PADDING: f32 = 10.; const TAB_TEXT_SIZE: f32 = 14.; const INDICATOR_HEIGHT: f32 = 3.; const CHAR_WIDTH: f32 = 7.5; const ARROW_WIDTH: f32 = 14.; const OVERFLOW_WIDTH: f32 = 44.; const EXTERNAL_ARROW: &str = "\u{2197}"; const TITLE_CHAR_WIDTH: f32 = 8.; pub const HEADER_TITLE_MAX_WIDTH: f32 = 240.; pub const POSTS_LABEL: &str = "Posts"; pub const SHARED_LABEL: &str = "Shared"; pub const CHAT_LABEL: &str = "Chat"; pub const RECAP_LABEL: &str = "Recap"; pub const CHAT_SHARED_TAB_TAG: &str = "shell/islands/main/chat-shared-tab"; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ChannelPane { Posts, Shared, Recap, } pub type SelectPane = Rc; pub type OpenTab = Rc; #[derive(Clone)] pub struct TabBarActions { pub select: SelectPane, pub open: OpenTab, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum TabAction { Browser(String), ResolveLink, } pub fn tab_action(tab: &ChannelTabRecord, resolved_link: Option<&str>) -> TabAction { match (&tab.open_url, resolved_link) { (Some(url), _) => TabAction::Browser(url.clone()), (None, Some(link)) => TabAction::Browser(link.to_owned()), (None, None) => TabAction::ResolveLink, } } pub fn fallback_tab_link(tab: &ChannelTabRecord, channel_id: &str) -> String { channel_tab_link(channel_id, &tab.tab_id, &tab.name) } fn label_width(label: &str, external: bool) -> f32 { label.chars().count() as f32 * CHAR_WIDTH + 2. * TAB_PADDING + if external { ARROW_WIDTH } else { 0. } } pub fn header_title_width(title: &str) -> f32 { (title.chars().count() as f32 * TITLE_CHAR_WIDTH).min(HEADER_TITLE_MAX_WIDTH) } pub fn visible_tab_count(tabs: &[ChannelTabRecord], available_width: f32) -> usize { let fixed = label_width(POSTS_LABEL, false) + TAB_GAP + label_width(SHARED_LABEL, false); let widths: Vec = tabs .iter() .map(|tab| TAB_GAP + label_width(&tab.name, true)) .collect(); if fixed + widths.iter().sum::() <= available_width { return tabs.len(); } let room = available_width - fixed - TAB_GAP - OVERFLOW_WIDTH; let mut used = 0.; widths .iter() .take_while(|width| { used += **width; used <= room }) .count() } fn tab_label( id: ElementId, text: &str, active: bool, external: bool, on_click: impl Fn(&mut Window, &mut App) + 'static, ) -> Stateful
{ let color = if active { theme::text() } else { theme::text_muted() }; h_flex() .id(id) .relative() .h_full() .flex_none() .items_center() .gap(px(4.)) .px(px(TAB_PADDING)) .cursor_pointer() .text_size(px(TAB_TEXT_SIZE)) .font_weight(if active { FontWeight::SEMIBOLD } else { FontWeight::NORMAL }) .text_color(color) .hover(|tab| tab.text_color(theme::text())) .on_click(move |_, window, cx| on_click(window, cx)) .child(text.to_owned()) .when(external, |tab| { tab.child(div().text_color(theme::text_faint()).child(EXTERNAL_ARROW)) }) .when(active, |tab| tab.child(indicator())) } fn indicator() -> Div { div() .absolute() .bottom_0() .left(px(TAB_PADDING)) .right(px(TAB_PADDING)) .h(px(INDICATOR_HEIGHT)) .rounded_full() .bg(theme::accent()) } fn header_tab_row() -> Div { h_flex().h_full().gap(px(TAB_GAP)).items_center() } fn overflow_button(hidden: &[(usize, String)], actions: &TabBarActions) -> impl IntoElement { let entries = hidden.to_vec(); let open = actions.open.clone(); Button::new("channel-tab-overflow") .ghost() .compact() .label(format!("+{}", hidden.len())) .dropdown_menu(move |menu, _, _| { entries.iter().fold(menu, |menu, (index, name)| { let (open, index) = (open.clone(), *index); menu.item( PopupMenuItem::new(format!("{name} {EXTERNAL_ARROW}")) .on_click(move |_, window, cx| open(index, window, cx)), ) }) }) } pub fn render_tab_bar( tabs: &[ChannelTabRecord], pane: ChannelPane, available_width: f32, actions: &TabBarActions, ) -> AnyElement { let visible = visible_tab_count(tabs, available_width); let select = actions.select.clone(); let posts = tab_label( "channel-tab-posts".into(), POSTS_LABEL, pane == ChannelPane::Posts, false, { let select = select.clone(); move |_, cx| select(ChannelPane::Posts, cx) }, ); let shared = tab_label( "channel-tab-shared".into(), SHARED_LABEL, pane == ChannelPane::Shared, false, move |_, cx| select(ChannelPane::Shared, cx), ); let configured = tabs.iter().enumerate().take(visible).map(|(index, tab)| { let open = actions.open.clone(); tab_label( ElementId::NamedInteger("channel-tab".into(), index as u64), &tab.name, false, true, move |window, cx| open(index, window, cx), ) }); let hidden: Vec<(usize, String)> = tabs .iter() .enumerate() .skip(visible) .map(|(index, tab)| (index, tab.name.clone())) .collect(); header_tab_row() .child(posts) .child(shared) .children(configured) .when(!hidden.is_empty(), |bar| { bar.child(overflow_button(&hidden, actions)) }) .into_any_element() } /// Chat, Shared and, for meeting chats with a recording, Recap. pub fn render_chat_bar(pane: ChannelPane, with_recap: bool, select: SelectPane) -> AnyElement { let chat = tab_label( "chat-tab-chat".into(), CHAT_LABEL, pane == ChannelPane::Posts, false, { let select = select.clone(); move |_, cx| select(ChannelPane::Posts, cx) }, ); let shared = tab_label( "chat-tab-shared".into(), SHARED_LABEL, pane == ChannelPane::Shared, false, { let select = select.clone(); move |_, cx| select(ChannelPane::Shared, cx) }, ) .debug_selector(|| CHAT_SHARED_TAB_TAG.into()); let recap = with_recap.then(|| { tab_label( "chat-tab-recap".into(), RECAP_LABEL, pane == ChannelPane::Recap, false, move |_, cx| select(ChannelPane::Recap, cx), ) .debug_selector(|| "shell/islands/main/recap-tab".into()) }); header_tab_row() .child(chat) .child(shared) .children(recap) .into_any_element() } #[cfg(test)] mod tests { use store::ChannelTabRecord; use super::{ HEADER_TITLE_MAX_WIDTH, TabAction, fallback_tab_link, header_title_width, tab_action, visible_tab_count, }; fn tab(name: &str, open_url: Option<&str>) -> ChannelTabRecord { ChannelTabRecord { tab_id: "tab-1".to_owned(), name: name.to_owned(), definition_id: "app".to_owned(), open_url: open_url.map(str::to_owned), } } fn website(url: &str) -> ChannelTabRecord { ChannelTabRecord { definition_id: "com.microsoft.teamspace.tab.web".to_owned(), ..tab("Docs", Some(url)) } } #[test] fn website_tabs_open_in_the_browser() { assert_eq!( tab_action(&website("https://docs.example"), None), TabAction::Browser("https://docs.example".to_owned()) ); } #[test] fn other_tabs_with_a_page_open_it_in_the_browser() { let notes = tab("Notes", Some("https://onenote.example/web")); assert_eq!( tab_action(¬es, None), TabAction::Browser("https://onenote.example/web".to_owned()) ); } #[test] fn app_tabs_resolve_their_link_once_and_then_reuse_it() { let board = tab("Board", None); assert_eq!(tab_action(&board, None), TabAction::ResolveLink); assert_eq!( tab_action(&board, Some("https://teams.example/l/tab")), TabAction::Browser("https://teams.example/l/tab".to_owned()) ); } #[test] fn the_fallback_link_is_the_hand_built_deep_link() { assert_eq!( fallback_tab_link(&tab("Board", None), "19:c@thread.tacv2"), "https://teams.microsoft.com/l/channel/19:c@thread.tacv2/tab%3A%3Atab-1?label=Board" ); } #[test] fn long_header_titles_are_capped() { assert_eq!(header_title_width("General"), 56.); assert_eq!(header_title_width(&"x".repeat(80)), HEADER_TITLE_MAX_WIDTH); } #[test] fn every_tab_stays_visible_while_it_fits() { let tabs = [tab("Docs", None), tab("Planner", None)]; assert_eq!(visible_tab_count(&tabs, 600.), 2); } #[test] fn tabs_that_do_not_fit_move_into_the_overflow() { let tabs: Vec = (0..8) .map(|n| tab(&format!("Tab number {n}"), None)) .collect(); let visible = visible_tab_count(&tabs, 420.); assert!(visible > 0 && visible < tabs.len()); assert_eq!(visible_tab_count(&tabs, 40.), 0); } }