| 1 | use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex}; |
| 2 | use gpui_kit::prelude::FluentBuilder as _; |
| 3 | use gpui_kit::*; |
| 4 | use teams_core::{ |
| 5 | AdaptiveCard, CardAction, CardActionKind, CardColumn, CardElement, CardFact, CardImage, |
| 6 | CardItem, CardSpacing, CardText, ColumnWidth, ContainerStyle, ImageSize, TextColor, TextSize, |
| 7 | VerticalAlignment, |
| 8 | }; |
| 9 | |
| 10 | use crate::render::{layout_blocks, render_blocks}; |
| 11 | use crate::theme; |
| 12 | |
| 13 | const CARD_WIDTH: f32 = 480.; |
| 14 | const CARD_FULL_WIDTH: f32 = 720.; |
| 15 | const CARD_PADDING: f32 = 12.; |
| 16 | const CARD_RADIUS: f32 = 10.; |
| 17 | const CARD_BORDER_WIDTH: f32 = 1.; |
| 18 | const COLUMN_GAP: f32 = 8.; |
| 19 | const SEPARATOR_PADDING: f32 = 8.; |
| 20 | const ACTION_GAP: f32 = 8.; |
| 21 | const ACTION_RADIUS: f32 = 6.; |
| 22 | const ACTION_TEXT_SIZE: f32 = 13.; |
| 23 | const ACTION_DISABLED_OPACITY: f32 = 0.5; |
| 24 | const ACTION_DISABLED_TOOLTIP: &str = "Not supported in this app"; |
| 25 | const FACT_TITLE_WIDTH: f32 = 96.; |
| 26 | const FACT_GAP: f32 = 2.; |
| 27 | const CONTAINER_PADDING: f32 = 8.; |
| 28 | const CONTAINER_RADIUS: f32 = 6.; |
| 29 | const CONTAINER_TINT: f32 = 0.16; |
| 30 | const TEXT_LINE_HEIGHT: f32 = 1.35; |
| 31 | const IMAGE_RADIUS: f32 = 4.; |
| 32 | const IMAGE_SET_GAP: f32 = 6.; |
| 33 | const SMALL_IMAGE: f32 = 40.; |
| 34 | const MEDIUM_IMAGE: f32 = 80.; |
| 35 | const LARGE_IMAGE: f32 = 160.; |
| 36 | const EXTERNAL_IMAGE_SCHEME: &str = "https://"; |
| 37 | |
| 38 | pub fn cards_view(cards: &[AdaptiveCard], id: &str, cx: &App) -> Vec<AnyElement> { |
| 39 | cards |
| 40 | .iter() |
| 41 | .enumerate() |
| 42 | .map(|(index, card)| card_view(card, &format!("{id}-card-{index}"), cx)) |
| 43 | .collect() |
| 44 | } |
| 45 | |
| 46 | fn card_view(card: &AdaptiveCard, id: &str, cx: &App) -> AnyElement { |
| 47 | let width = if card.full_width { |
| 48 | CARD_FULL_WIDTH |
| 49 | } else { |
| 50 | CARD_WIDTH |
| 51 | }; |
| 52 | v_flex() |
| 53 | .w(px(width)) |
| 54 | .max_w(relative(1.)) |
| 55 | .p(px(CARD_PADDING)) |
| 56 | .rounded(px(CARD_RADIUS)) |
| 57 | .bg(theme::surface()) |
| 58 | .border(px(CARD_BORDER_WIDTH)) |
| 59 | .border_color(theme::border()) |
| 60 | .text_color(theme::text_strong()) |
| 61 | .child(items_view(&card.items, id, cx)) |
| 62 | .when(!card.actions.is_empty(), |card_element| { |
| 63 | card_element.child( |
| 64 | div() |
| 65 | .mt(px(spacing_pixels(CardSpacing::Default))) |
| 66 | .child(actions_view(&card.actions, &format!("{id}-actions"))), |
| 67 | ) |
| 68 | }) |
| 69 | .into_any_element() |
| 70 | } |
| 71 | |
| 72 | fn items_view(items: &[CardItem], id: &str, cx: &App) -> Div { |
| 73 | v_flex() |
| 74 | .w_full() |
| 75 | .children(items.iter().enumerate().map(|(index, item)| { |
| 76 | let item_id = format!("{id}-{index}"); |
| 77 | div() |
| 78 | .w_full() |
| 79 | .when(index > 0, |wrapper| { |
| 80 | wrapper.mt(px(spacing_pixels(item.spacing))) |
| 81 | }) |
| 82 | .when(index > 0 && item.separator, |wrapper| { |
| 83 | wrapper |
| 84 | .pt(px(SEPARATOR_PADDING)) |
| 85 | .border_t_1() |
| 86 | .border_color(theme::border()) |
| 87 | }) |
| 88 | .child(element_view(&item.element, &item_id, cx)) |
| 89 | })) |
| 90 | } |
| 91 | |
| 92 | fn spacing_pixels(spacing: CardSpacing) -> f32 { |
| 93 | match spacing { |
| 94 | CardSpacing::None => 0., |
| 95 | CardSpacing::Small => 4., |
| 96 | CardSpacing::Default => 8., |
| 97 | CardSpacing::Medium => 12., |
| 98 | CardSpacing::Large | CardSpacing::Padding => 16., |
| 99 | CardSpacing::ExtraLarge => 24., |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn element_view(element: &CardElement, id: &str, cx: &App) -> AnyElement { |
| 104 | match element { |
| 105 | CardElement::Text(text) => text_view(text, id, cx), |
| 106 | CardElement::Image(image) => image_view(image, id), |
| 107 | CardElement::ImageSet(images) => h_flex() |
| 108 | .flex_wrap() |
| 109 | .gap(px(IMAGE_SET_GAP)) |
| 110 | .children( |
| 111 | images |
| 112 | .iter() |
| 113 | .enumerate() |
| 114 | .map(|(index, image)| image_view(image, &format!("{id}-{index}"))), |
| 115 | ) |
| 116 | .into_any_element(), |
| 117 | CardElement::Columns(columns) => columns_view(columns, id, cx), |
| 118 | CardElement::Container { style, items } => { |
| 119 | let tint = container_tint(*style); |
| 120 | items_view(items, id, cx) |
| 121 | .when_some(tint, |container, tint| { |
| 122 | container |
| 123 | .p(px(CONTAINER_PADDING)) |
| 124 | .rounded(px(CONTAINER_RADIUS)) |
| 125 | .bg(tint) |
| 126 | }) |
| 127 | .into_any_element() |
| 128 | } |
| 129 | CardElement::Facts(facts) => facts_view(facts, id, cx), |
| 130 | CardElement::Actions(actions) => actions_view(actions, id).into_any_element(), |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | fn container_tint(style: ContainerStyle) -> Option<Hsla> { |
| 135 | match style { |
| 136 | ContainerStyle::Default => None, |
| 137 | ContainerStyle::Emphasis => Some(theme::surface_raised()), |
| 138 | ContainerStyle::Accent => Some(theme::accent().opacity(CONTAINER_TINT)), |
| 139 | ContainerStyle::Good => Some(theme::green().opacity(CONTAINER_TINT)), |
| 140 | ContainerStyle::Warning => Some(theme::amber().opacity(CONTAINER_TINT)), |
| 141 | ContainerStyle::Attention => Some(theme::red().opacity(CONTAINER_TINT)), |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fn text_size(size: TextSize) -> f32 { |
| 146 | match size { |
| 147 | TextSize::Small => 11.5, |
| 148 | TextSize::Default => 13.5, |
| 149 | TextSize::Medium => 15., |
| 150 | TextSize::Large => 17.5, |
| 151 | TextSize::ExtraLarge => 21., |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | fn text_color(text: &CardText) -> Hsla { |
| 156 | match text.color { |
| 157 | TextColor::Accent => theme::accent_text(), |
| 158 | TextColor::Good => theme::green(), |
| 159 | TextColor::Warning => theme::amber(), |
| 160 | TextColor::Attention => theme::red_soft(), |
| 161 | TextColor::Default | TextColor::Dark | TextColor::Light if text.subtle => { |
| 162 | theme::text_muted() |
| 163 | } |
| 164 | TextColor::Default | TextColor::Dark | TextColor::Light => theme::text_strong(), |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | fn text_view(text: &CardText, id: &str, cx: &App) -> AnyElement { |
| 169 | div() |
| 170 | .w_full() |
| 171 | .text_size(px(text_size(text.size))) |
| 172 | .line_height(relative(TEXT_LINE_HEIGHT)) |
| 173 | .text_color(text_color(text)) |
| 174 | .when(text.bold, |element| { |
| 175 | element.font_weight(FontWeight::SEMIBOLD) |
| 176 | }) |
| 177 | .when(!text.wrap, |element| element.whitespace_nowrap()) |
| 178 | .child(render_blocks(&layout_blocks(&text.spans), id, false, cx)) |
| 179 | .into_any_element() |
| 180 | } |
| 181 | |
| 182 | fn facts_view(facts: &[CardFact], id: &str, cx: &App) -> AnyElement { |
| 183 | v_flex() |
| 184 | .w_full() |
| 185 | .gap(px(FACT_GAP)) |
| 186 | .children(facts.iter().enumerate().map(|(index, fact)| { |
| 187 | h_flex() |
| 188 | .w_full() |
| 189 | .items_start() |
| 190 | .gap(px(COLUMN_GAP)) |
| 191 | .text_size(px(text_size(TextSize::Default))) |
| 192 | .line_height(relative(TEXT_LINE_HEIGHT)) |
| 193 | .child( |
| 194 | div() |
| 195 | .w(px(FACT_TITLE_WIDTH)) |
| 196 | .flex_none() |
| 197 | .font_weight(FontWeight::SEMIBOLD) |
| 198 | .text_color(theme::text_muted()) |
| 199 | .child(fact.title.clone()), |
| 200 | ) |
| 201 | .child(div().flex_1().min_w(px(0.)).child(render_blocks( |
| 202 | &layout_blocks(&fact.value), |
| 203 | &format!("{id}-{index}"), |
| 204 | false, |
| 205 | cx, |
| 206 | ))) |
| 207 | })) |
| 208 | .into_any_element() |
| 209 | } |
| 210 | |
| 211 | fn columns_view(columns: &[CardColumn], id: &str, cx: &App) -> AnyElement { |
| 212 | div() |
| 213 | .flex() |
| 214 | .flex_row() |
| 215 | .items_stretch() |
| 216 | .w_full() |
| 217 | .gap(px(COLUMN_GAP)) |
| 218 | .children(columns.iter().enumerate().map(|(index, column)| { |
| 219 | let content = items_view(&column.items, &format!("{id}-{index}"), cx); |
| 220 | let aligned = v_flex() |
| 221 | .h_full() |
| 222 | .map(|aligned| match column.vertical_alignment { |
| 223 | VerticalAlignment::Top => aligned.justify_start(), |
| 224 | VerticalAlignment::Center => aligned.justify_center(), |
| 225 | VerticalAlignment::Bottom => aligned.justify_end(), |
| 226 | }); |
| 227 | sized_column(aligned, column.width).child(content) |
| 228 | })) |
| 229 | .into_any_element() |
| 230 | } |
| 231 | |
| 232 | fn sized_column(column: Div, width: ColumnWidth) -> Div { |
| 233 | match width { |
| 234 | ColumnWidth::Auto => column.flex_none(), |
| 235 | ColumnWidth::Stretch => column.flex_1().min_w(px(0.)), |
| 236 | ColumnWidth::Weighted(weight) => column |
| 237 | .flex_basis(px(0.)) |
| 238 | .flex_grow(weight.max(0.)) |
| 239 | .min_w(px(0.)), |
| 240 | ColumnWidth::Pixels(pixels) => column.w(px(pixels)).flex_none(), |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | fn image_view(image: &CardImage, id: &str) -> AnyElement { |
| 245 | let width = match (image.width, image.height, named_image_width(image.size)) { |
| 246 | (Some(width), _, _) => width, |
| 247 | (None, Some(height), _) => height, |
| 248 | (None, None, Some(width)) => width, |
| 249 | (None, None, None) => MEDIUM_IMAGE, |
| 250 | } |
| 251 | .min(CARD_FULL_WIDTH - 2. * CARD_PADDING); |
| 252 | let height = image.height.unwrap_or(width); |
| 253 | let radius = px(if image.person { |
| 254 | width.min(height) / 2. |
| 255 | } else { |
| 256 | IMAGE_RADIUS |
| 257 | }); |
| 258 | let frame = div() |
| 259 | .id(ElementId::Name(id.to_owned().into())) |
| 260 | .w(px(width)) |
| 261 | .h(px(height)) |
| 262 | .max_w(relative(1.)) |
| 263 | .flex_none() |
| 264 | .overflow_hidden() |
| 265 | .rounded(radius); |
| 266 | if !image.url.starts_with(EXTERNAL_IMAGE_SCHEME) { |
| 267 | return frame.bg(theme::surface_raised()).into_any_element(); |
| 268 | } |
| 269 | frame |
| 270 | .child( |
| 271 | img(image.url.clone()) |
| 272 | .size_full() |
| 273 | .rounded(radius) |
| 274 | .object_fit(ObjectFit::Cover) |
| 275 | .with_loading(move || image_placeholder(radius)) |
| 276 | .with_fallback(move || image_placeholder(radius)), |
| 277 | ) |
| 278 | .into_any_element() |
| 279 | } |
| 280 | |
| 281 | fn image_placeholder(radius: Pixels) -> AnyElement { |
| 282 | div() |
| 283 | .size_full() |
| 284 | .rounded(radius) |
| 285 | .bg(theme::surface_raised()) |
| 286 | .into_any_element() |
| 287 | } |
| 288 | |
| 289 | fn named_image_width(size: ImageSize) -> Option<f32> { |
| 290 | match size { |
| 291 | ImageSize::Small => Some(SMALL_IMAGE), |
| 292 | ImageSize::Medium => Some(MEDIUM_IMAGE), |
| 293 | ImageSize::Large => Some(LARGE_IMAGE), |
| 294 | ImageSize::Stretch => Some(CARD_WIDTH - 2. * CARD_PADDING), |
| 295 | ImageSize::Auto => None, |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | fn actions_view(actions: &[CardAction], id: &str) -> Div { |
| 300 | h_flex().w_full().flex_wrap().gap(px(ACTION_GAP)).children( |
| 301 | actions |
| 302 | .iter() |
| 303 | .enumerate() |
| 304 | .map(|(index, action)| action_button(action, format!("{id}-{index}"))), |
| 305 | ) |
| 306 | } |
| 307 | |
| 308 | fn action_button(action: &CardAction, id: String) -> Stateful<Div> { |
| 309 | let button = div() |
| 310 | .id(ElementId::Name(id.into())) |
| 311 | .px(px(12.)) |
| 312 | .py(px(6.)) |
| 313 | .rounded(px(ACTION_RADIUS)) |
| 314 | .border_1() |
| 315 | .border_color(theme::border_strong()) |
| 316 | .text_size(px(ACTION_TEXT_SIZE)) |
| 317 | .font_weight(FontWeight::SEMIBOLD) |
| 318 | .child(action.title.clone()); |
| 319 | match &action.kind { |
| 320 | CardActionKind::OpenUrl(url) => { |
| 321 | let url = url.clone(); |
| 322 | button |
| 323 | .text_color(theme::accent_text()) |
| 324 | .cursor_pointer() |
| 325 | .hover(|button| button.bg(theme::row_hover())) |
| 326 | .on_click(move |_, _, cx| { |
| 327 | cx.stop_propagation(); |
| 328 | cx.open_url(&url); |
| 329 | }) |
| 330 | } |
| 331 | CardActionKind::Unsupported => button |
| 332 | .text_color(theme::text_muted()) |
| 333 | .opacity(ACTION_DISABLED_OPACITY) |
| 334 | .tooltip(|window, cx| Tooltip::new(ACTION_DISABLED_TOOLTIP).build(window, cx)) |
| 335 | .on_click(|_, _, cx| cx.stop_propagation()), |
| 336 | } |
| 337 | } |