| 1 | use gpui_kit::assets::IconName; |
| 2 | use gpui_kit::component::{h_flex, v_flex}; |
| 3 | use gpui_kit::prelude::FluentBuilder as _; |
| 4 | use gpui_kit::*; |
| 5 | use teams_core::CardCarousel; |
| 6 | |
| 7 | use super::adaptive_card::{CardContext, ClickHandler, container_view, selectable, slot_handler}; |
| 8 | use super::widgets::icon; |
| 9 | use crate::theme; |
| 10 | |
| 11 | const ARROW_SIZE: f32 = 24.; |
| 12 | const ARROW_ICON_SIZE: f32 = 14.; |
| 13 | const DOT_SIZE: f32 = 7.; |
| 14 | const DOT_GAP: f32 = 6.; |
| 15 | const NAVIGATION_GAP: f32 = 8.; |
| 16 | const DISABLED_OPACITY: f32 = 0.35; |
| 17 | |
| 18 | pub(super) fn carousel_view( |
| 19 | carousel: &CardCarousel, |
| 20 | id: &str, |
| 21 | context: &CardContext, |
| 22 | ) -> AnyElement { |
| 23 | let page_key = format!("{id}-page"); |
| 24 | let last = carousel.pages.len() - 1; |
| 25 | let current = context |
| 26 | .state |
| 27 | .slot(&page_key, carousel.initial_page) |
| 28 | .min(last); |
| 29 | let page = &carousel.pages[current]; |
| 30 | let page_id = format!("{id}-{current}"); |
| 31 | let content = selectable( |
| 32 | container_view(&page.items, &page_id, context, &page.layout, None).into_any_element(), |
| 33 | page.select_action.as_ref(), |
| 34 | &page_id, |
| 35 | false, |
| 36 | context, |
| 37 | ); |
| 38 | v_flex() |
| 39 | .w_full() |
| 40 | .gap(px(NAVIGATION_GAP)) |
| 41 | .child(content) |
| 42 | .when(last > 0, |frame| { |
| 43 | frame.child( |
| 44 | h_flex() |
| 45 | .w_full() |
| 46 | .items_center() |
| 47 | .justify_center() |
| 48 | .gap(px(NAVIGATION_GAP)) |
| 49 | .child(arrow( |
| 50 | &format!("{id}-previous"), |
| 51 | IconName::ChevronLeft, |
| 52 | (current > 0).then(|| slot_handler(context, &page_key, current - 1)), |
| 53 | )) |
| 54 | .child(h_flex().gap(px(DOT_GAP)).children( |
| 55 | (0..=last).map(|index| dot(id, index, current, &page_key, context)), |
| 56 | )) |
| 57 | .child(arrow( |
| 58 | &format!("{id}-next"), |
| 59 | IconName::ChevronRight, |
| 60 | (current < last).then(|| slot_handler(context, &page_key, current + 1)), |
| 61 | )), |
| 62 | ) |
| 63 | }) |
| 64 | .into_any_element() |
| 65 | } |
| 66 | |
| 67 | fn arrow(arrow_id: &str, glyph: IconName, handler: Option<ClickHandler>) -> Stateful<Div> { |
| 68 | div() |
| 69 | .id(ElementId::Name(arrow_id.to_owned().into())) |
| 70 | .size(px(ARROW_SIZE)) |
| 71 | .flex_none() |
| 72 | .flex() |
| 73 | .items_center() |
| 74 | .justify_center() |
| 75 | .rounded_full() |
| 76 | .border_1() |
| 77 | .border_color(theme::border_strong()) |
| 78 | .child(icon(glyph, ARROW_ICON_SIZE, theme::text_strong())) |
| 79 | .map(|button| match handler { |
| 80 | Some(handler) => button |
| 81 | .cursor_pointer() |
| 82 | .hover(|button| button.bg(theme::row_hover())) |
| 83 | .on_click(move |_, _, cx| { |
| 84 | cx.stop_propagation(); |
| 85 | handler(cx) |
| 86 | }), |
| 87 | None => button |
| 88 | .opacity(DISABLED_OPACITY) |
| 89 | .on_click(|_, _, cx| cx.stop_propagation()), |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | fn dot( |
| 94 | id: &str, |
| 95 | index: usize, |
| 96 | current: usize, |
| 97 | page_key: &str, |
| 98 | context: &CardContext, |
| 99 | ) -> Stateful<Div> { |
| 100 | let handler = slot_handler(context, page_key, index); |
| 101 | div() |
| 102 | .id(ElementId::Name(format!("{id}-dot-{index}").into())) |
| 103 | .size(px(DOT_SIZE)) |
| 104 | .rounded_full() |
| 105 | .cursor_pointer() |
| 106 | .bg(if index == current { |
| 107 | theme::accent_text() |
| 108 | } else { |
| 109 | theme::border_strong() |
| 110 | }) |
| 111 | .on_click(move |_, _, cx| { |
| 112 | cx.stop_propagation(); |
| 113 | handler(cx) |
| 114 | }) |
| 115 | } |