| 1 | use std::time::Duration; |
| 2 | |
| 3 | use gpui_kit::assets::IconName; |
| 4 | use gpui_kit::component::tooltip::Tooltip; |
| 5 | use gpui_kit::component::{h_flex, v_flex}; |
| 6 | use gpui_kit::prelude::FluentBuilder as _; |
| 7 | use gpui_kit::*; |
| 8 | use teams_core::{ |
| 9 | BadgeAppearance, BadgeShape, BadgeSize, BadgeStyle, CardBadge, CardCompoundButton, |
| 10 | CardProgressBar, CardProgressRing, IconPosition, LabelPosition, RingSize, TextColor, |
| 11 | }; |
| 12 | |
| 13 | use super::adaptive_card::{CardContext, card_color, selectable}; |
| 14 | use super::card_icon::glyph_view; |
| 15 | use super::widgets::icon; |
| 16 | use crate::theme; |
| 17 | |
| 18 | const BADGE_TINT: f32 = 0.18; |
| 19 | const BADGE_ICON_GAP: f32 = 4.; |
| 20 | const BAR_HEIGHT: f32 = 6.; |
| 21 | const INDETERMINATE_WIDTH: f32 = 0.3; |
| 22 | const INDETERMINATE_PERIOD: Duration = Duration::from_millis(1400); |
| 23 | const RING_PERIOD: Duration = Duration::from_millis(1000); |
| 24 | const RING_LABEL_GAP: f32 = 8.; |
| 25 | const COMPOUND_PADDING: f32 = 10.; |
| 26 | const COMPOUND_GAP: f32 = 10.; |
| 27 | const COMPOUND_ICON_SIZE: f32 = 24.; |
| 28 | const COMPOUND_RADIUS: f32 = 6.; |
| 29 | |
| 30 | struct BadgePalette { |
| 31 | fill: Hsla, |
| 32 | foreground: Hsla, |
| 33 | border: Option<Hsla>, |
| 34 | } |
| 35 | |
| 36 | fn badge_palette(style: BadgeStyle, appearance: BadgeAppearance) -> BadgePalette { |
| 37 | let tinted = appearance == BadgeAppearance::Tint; |
| 38 | let semantic = |fill: Hsla, on_fill: Hsla, tint_text: Hsla| { |
| 39 | if tinted { |
| 40 | BadgePalette { |
| 41 | fill: fill.opacity(BADGE_TINT), |
| 42 | foreground: tint_text, |
| 43 | border: Some(fill.opacity(BADGE_TINT * 2.)), |
| 44 | } |
| 45 | } else { |
| 46 | BadgePalette { |
| 47 | fill, |
| 48 | foreground: on_fill, |
| 49 | border: None, |
| 50 | } |
| 51 | } |
| 52 | }; |
| 53 | match style { |
| 54 | BadgeStyle::Accent => semantic(theme::accent(), theme::on_accent(), theme::accent_text()), |
| 55 | BadgeStyle::Good => semantic(theme::green(), theme::background(), theme::green()), |
| 56 | BadgeStyle::Attention => semantic(theme::red(), theme::white(), theme::red_soft()), |
| 57 | BadgeStyle::Warning => semantic(theme::amber(), theme::background(), theme::amber()), |
| 58 | BadgeStyle::Default => neutral(theme::badge_muted(), theme::text_strong(), tinted), |
| 59 | BadgeStyle::Informative => neutral(theme::surface_raised(), theme::text_soft(), tinted), |
| 60 | BadgeStyle::Subtle => BadgePalette { |
| 61 | fill: if tinted { |
| 62 | theme::surface_raised() |
| 63 | } else { |
| 64 | transparent_black() |
| 65 | }, |
| 66 | foreground: theme::text_muted(), |
| 67 | border: Some(theme::border_strong()), |
| 68 | }, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn neutral(fill: Hsla, foreground: Hsla, tinted: bool) -> BadgePalette { |
| 73 | BadgePalette { |
| 74 | fill: if tinted { fill.opacity(0.5) } else { fill }, |
| 75 | foreground, |
| 76 | border: tinted.then(theme::border_strong), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | fn badge_metrics(size: BadgeSize) -> (f32, f32, f32, f32) { |
| 81 | match size { |
| 82 | BadgeSize::Medium => (20., 12., 6., 12.), |
| 83 | BadgeSize::Large => (24., 13., 8., 14.), |
| 84 | BadgeSize::ExtraLarge => (32., 14., 10., 18.), |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | pub(super) fn leading(element: AnyElement) -> AnyElement { |
| 89 | div() |
| 90 | .w_full() |
| 91 | .flex() |
| 92 | .flex_row() |
| 93 | .child(element) |
| 94 | .into_any_element() |
| 95 | } |
| 96 | |
| 97 | pub(super) fn badge_view(badge: &CardBadge, id: &str) -> AnyElement { |
| 98 | let palette = badge_palette(badge.style, badge.appearance); |
| 99 | let (height, text_size, padding, icon_size) = badge_metrics(badge.size); |
| 100 | let glyph = badge |
| 101 | .icon |
| 102 | .as_deref() |
| 103 | .map(|name| glyph_view(name, icon_size, palette.foreground)); |
| 104 | let (icon_before, icon_after) = match badge.icon_position { |
| 105 | IconPosition::Before => (glyph, None), |
| 106 | IconPosition::After => (None, glyph), |
| 107 | }; |
| 108 | let text = (!badge.text.is_empty()).then(|| badge.text.clone()); |
| 109 | let tooltip = badge.tooltip.clone(); |
| 110 | h_flex() |
| 111 | .id(ElementId::Name(format!("{id}-badge").into())) |
| 112 | .flex_none() |
| 113 | .h(px(height)) |
| 114 | .min_w(px(height)) |
| 115 | .px(px(padding)) |
| 116 | .gap(px(BADGE_ICON_GAP)) |
| 117 | .items_center() |
| 118 | .justify_center() |
| 119 | .map(|frame| match badge.shape { |
| 120 | BadgeShape::Square => frame.rounded(px(2.)), |
| 121 | BadgeShape::Rounded => frame.rounded(px(height / 4.)), |
| 122 | BadgeShape::Circular => frame.rounded_full(), |
| 123 | }) |
| 124 | .bg(palette.fill) |
| 125 | .when_some(palette.border, |frame, border| { |
| 126 | frame.border_1().border_color(border) |
| 127 | }) |
| 128 | .text_color(palette.foreground) |
| 129 | .text_size(px(text_size)) |
| 130 | .font_weight(FontWeight::SEMIBOLD) |
| 131 | .when_some(tooltip, |frame, tooltip| { |
| 132 | frame.tooltip(move |window, cx| Tooltip::new(tooltip.clone()).build(window, cx)) |
| 133 | }) |
| 134 | .children(icon_before) |
| 135 | .children(text) |
| 136 | .children(icon_after) |
| 137 | .into_any_element() |
| 138 | } |
| 139 | |
| 140 | pub(super) fn progress_bar_view(bar: &CardProgressBar, id: &str) -> AnyElement { |
| 141 | let fill = match bar.color { |
| 142 | TextColor::Default | TextColor::Dark | TextColor::Light => theme::accent(), |
| 143 | other => card_color(other, false), |
| 144 | }; |
| 145 | let track = div() |
| 146 | .relative() |
| 147 | .w_full() |
| 148 | .h(px(BAR_HEIGHT)) |
| 149 | .overflow_hidden() |
| 150 | .rounded_full() |
| 151 | .bg(theme::border_strong()); |
| 152 | match bar.value { |
| 153 | Some(value) => track |
| 154 | .child( |
| 155 | div() |
| 156 | .h_full() |
| 157 | .w(relative(value / 100.)) |
| 158 | .rounded_full() |
| 159 | .bg(fill), |
| 160 | ) |
| 161 | .into_any_element(), |
| 162 | None => track |
| 163 | .child( |
| 164 | div() |
| 165 | .absolute() |
| 166 | .top_0() |
| 167 | .h_full() |
| 168 | .w(relative(INDETERMINATE_WIDTH)) |
| 169 | .rounded_full() |
| 170 | .bg(fill) |
| 171 | .with_animation( |
| 172 | ElementId::Name(format!("{id}-pulse").into()), |
| 173 | Animation::new(INDETERMINATE_PERIOD).repeat(), |
| 174 | |segment, delta| { |
| 175 | let travel = 1. + INDETERMINATE_WIDTH; |
| 176 | segment.left(relative( |
| 177 | (delta + 0.3).fract() * travel - INDETERMINATE_WIDTH, |
| 178 | )) |
| 179 | }, |
| 180 | ), |
| 181 | ) |
| 182 | .into_any_element(), |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | fn ring_pixels(size: RingSize) -> f32 { |
| 187 | match size { |
| 188 | RingSize::Tiny => 16., |
| 189 | RingSize::Small => 20., |
| 190 | RingSize::Medium => 28., |
| 191 | RingSize::Large => 40., |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | pub(super) fn progress_ring_view(ring: &CardProgressRing, id: &str) -> AnyElement { |
| 196 | let pixels = ring_pixels(ring.size); |
| 197 | let spinner = icon(IconName::Loader, pixels, theme::accent_text()).with_animation( |
| 198 | ElementId::Name(format!("{id}-spin").into()), |
| 199 | Animation::new(RING_PERIOD).repeat(), |
| 200 | |icon, delta| icon.transform(Transformation::rotate(percentage(delta))), |
| 201 | ); |
| 202 | let label = ring.label.clone().map(|label| { |
| 203 | div() |
| 204 | .text_size(px(13.)) |
| 205 | .text_color(theme::text_soft()) |
| 206 | .child(label) |
| 207 | }); |
| 208 | let vertical = matches!( |
| 209 | ring.label_position, |
| 210 | LabelPosition::Above | LabelPosition::Below |
| 211 | ); |
| 212 | let (label_before, label_after) = match ring.label_position { |
| 213 | LabelPosition::Before | LabelPosition::Above => (label, None), |
| 214 | LabelPosition::After | LabelPosition::Below => (None, label), |
| 215 | }; |
| 216 | div() |
| 217 | .flex() |
| 218 | .items_center() |
| 219 | .gap(px(RING_LABEL_GAP)) |
| 220 | .map(|frame| { |
| 221 | if vertical { |
| 222 | frame.flex_col() |
| 223 | } else { |
| 224 | frame.flex_row() |
| 225 | } |
| 226 | }) |
| 227 | .children(label_before) |
| 228 | .child(spinner) |
| 229 | .children(label_after) |
| 230 | .into_any_element() |
| 231 | } |
| 232 | |
| 233 | pub(super) fn compound_button_view( |
| 234 | button: &CardCompoundButton, |
| 235 | id: &str, |
| 236 | context: &CardContext, |
| 237 | ) -> AnyElement { |
| 238 | let clickable = button |
| 239 | .select_action |
| 240 | .as_ref() |
| 241 | .is_some_and(|action| action.is_clickable()); |
| 242 | let face = h_flex() |
| 243 | .w_full() |
| 244 | .items_center() |
| 245 | .gap(px(COMPOUND_GAP)) |
| 246 | .p(px(COMPOUND_PADDING)) |
| 247 | .rounded(px(COMPOUND_RADIUS)) |
| 248 | .border_1() |
| 249 | .border_color(theme::border_strong()) |
| 250 | .when(!clickable, |face| face.opacity(0.6)) |
| 251 | .children( |
| 252 | button |
| 253 | .icon |
| 254 | .as_deref() |
| 255 | .map(|name| glyph_view(name, COMPOUND_ICON_SIZE, theme::accent_text())), |
| 256 | ) |
| 257 | .child( |
| 258 | v_flex() |
| 259 | .flex_1() |
| 260 | .min_w(px(0.)) |
| 261 | .child( |
| 262 | div() |
| 263 | .text_size(px(13.5)) |
| 264 | .font_weight(FontWeight::SEMIBOLD) |
| 265 | .text_color(theme::text_strong()) |
| 266 | .child(button.title.clone()), |
| 267 | ) |
| 268 | .children(button.description.clone().map(|description| { |
| 269 | div() |
| 270 | .text_size(px(12.)) |
| 271 | .text_color(theme::text_muted()) |
| 272 | .child(description) |
| 273 | })), |
| 274 | ) |
| 275 | .children(button.badge.clone().map(|badge| { |
| 276 | div() |
| 277 | .flex_none() |
| 278 | .px(px(6.)) |
| 279 | .h(px(18.)) |
| 280 | .flex() |
| 281 | .items_center() |
| 282 | .rounded_full() |
| 283 | .bg(theme::badge_muted()) |
| 284 | .text_size(px(11.)) |
| 285 | .font_weight(FontWeight::BOLD) |
| 286 | .text_color(theme::text_strong()) |
| 287 | .child(badge) |
| 288 | })); |
| 289 | selectable( |
| 290 | face.into_any_element(), |
| 291 | button.select_action.as_ref(), |
| 292 | id, |
| 293 | false, |
| 294 | context, |
| 295 | ) |
| 296 | } |