| 1 | use std::collections::HashMap; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::component::tooltip::{PopupTriggerTooltip as _, Tooltip}; |
| 5 | use gpui_kit::component::{ActiveTheme as _, h_flex, v_flex}; |
| 6 | use gpui_kit::prelude::FluentBuilder as _; |
| 7 | use gpui_kit::*; |
| 8 | use teams_core::{ |
| 9 | AdaptiveCard, CardAction, CardActionKind, CardColumn, CardElement, CardFact, CardImage, |
| 10 | CardInput, CardItem, CardSpacing, CardText, ColumnWidth, ContainerLayout, ContainerStyle, |
| 11 | HorizontalAlignment, ImageSize, TextColor, TextSize, VerticalAlignment, WidthClass, |
| 12 | split_overflow, |
| 13 | }; |
| 14 | |
| 15 | use super::card_carousel::carousel_view; |
| 16 | use super::card_chart::chart_view; |
| 17 | use super::card_code_block::code_block_view; |
| 18 | use super::card_icon::{action_icon_view, icon_view}; |
| 19 | use super::card_input::{input_view, rating_display_view}; |
| 20 | use super::card_media::media_view; |
| 21 | use super::card_table::table_view; |
| 22 | use super::card_widgets::{ |
| 23 | badge_view, compound_button_view, leading, progress_bar_view, progress_ring_view, |
| 24 | }; |
| 25 | use super::widgets::symbol; |
| 26 | use crate::app_state::{AppHandle, AppState}; |
| 27 | use crate::card_state::{ActionPhase, CardScope, CardState}; |
| 28 | use crate::render::{layout_card_blocks, render_blocks}; |
| 29 | use crate::theme; |
| 30 | |
| 31 | const CARD_WIDTH: f32 = 480.; |
| 32 | const CARD_FULL_WIDTH: f32 = 720.; |
| 33 | const CARD_PADDING: f32 = 12.; |
| 34 | const CARD_RADIUS: f32 = 10.; |
| 35 | const CARD_BORDER_WIDTH: f32 = 1.; |
| 36 | const COLUMN_GAP: f32 = 8.; |
| 37 | const SEPARATOR_PADDING: f32 = 8.; |
| 38 | const ACTION_GAP: f32 = 8.; |
| 39 | const ACTION_RADIUS: f32 = 6.; |
| 40 | const ACTION_TEXT_SIZE: f32 = 13.; |
| 41 | const ACTION_DISABLED_OPACITY: f32 = 0.5; |
| 42 | const ACTION_DISABLED_TOOLTIP: &str = "Not supported in this app"; |
| 43 | const ACTION_BUSY_OPACITY: f32 = 0.6; |
| 44 | const ACTION_CHECK_SIZE: f32 = 14.; |
| 45 | const ACTION_ICON_GAP: f32 = 4.; |
| 46 | const NOTE_TEXT_SIZE: f32 = 12.; |
| 47 | const FACT_TITLE_WIDTH: f32 = 96.; |
| 48 | const FACT_GAP: f32 = 2.; |
| 49 | const CONTAINER_PADDING: f32 = 8.; |
| 50 | const CONTAINER_RADIUS: f32 = 6.; |
| 51 | const CONTAINER_TINT: f32 = 0.16; |
| 52 | const TEXT_LINE_HEIGHT: f32 = 1.35; |
| 53 | const IMAGE_RADIUS: f32 = 4.; |
| 54 | const IMAGE_SET_GAP: f32 = 6.; |
| 55 | const SMALL_IMAGE: f32 = 40.; |
| 56 | const MEDIUM_IMAGE: f32 = 80.; |
| 57 | const LARGE_IMAGE: f32 = 160.; |
| 58 | const EXTERNAL_IMAGE_SCHEMES: [&str; 2] = ["https://", "http://"]; |
| 59 | const ACTION_OVERFLOW_LABEL: &str = "..."; |
| 60 | const ACTION_OVERFLOW_TOOLTIP: &str = "More actions"; |
| 61 | const OVERFLOW_PANEL_PADDING: f32 = 4.; |
| 62 | const SELECT_TINT: f32 = 0.07; |
| 63 | |
| 64 | pub(super) type ClickHandler = Rc<dyn Fn(&mut App)>; |
| 65 | |
| 66 | pub(super) struct CardContext<'a> { |
| 67 | pub(super) cx: &'a App, |
| 68 | pub(super) scope: &'a CardScope, |
| 69 | pub(super) state: &'a CardState, |
| 70 | root_key: String, |
| 71 | initial_visibility: Rc<HashMap<String, bool>>, |
| 72 | inputs: Vec<CardInput>, |
| 73 | width_class: WidthClass, |
| 74 | } |
| 75 | |
| 76 | impl<'a> CardContext<'a> { |
| 77 | fn new(card: &AdaptiveCard, scope: &'a CardScope, cx: &'a App) -> Self { |
| 78 | CardContext { |
| 79 | cx, |
| 80 | scope, |
| 81 | state: &cx.global::<AppHandle>().0.read(cx).cards, |
| 82 | root_key: scope.card_key(), |
| 83 | initial_visibility: Rc::new(card.element_visibility()), |
| 84 | inputs: card.own_inputs(), |
| 85 | width_class: WidthClass::from_pixels(card_width(card)), |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | fn nested(&self, card: &AdaptiveCard) -> CardContext<'a> { |
| 90 | let mut inputs = self.inputs.clone(); |
| 91 | inputs.extend(card.own_inputs()); |
| 92 | CardContext { |
| 93 | cx: self.cx, |
| 94 | scope: self.scope, |
| 95 | state: self.state, |
| 96 | root_key: self.root_key.clone(), |
| 97 | initial_visibility: self.initial_visibility.clone(), |
| 98 | inputs, |
| 99 | width_class: self.width_class, |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | pub(super) fn visible(&self, id: Option<&String>, initial: bool) -> bool { |
| 104 | match id { |
| 105 | Some(id) => self |
| 106 | .state |
| 107 | .is_visible(&format!("{}/{id}", self.root_key), initial), |
| 108 | None => initial, |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | pub fn cards_view( |
| 114 | cards: &[AdaptiveCard], |
| 115 | conversation_id: &str, |
| 116 | message_id: &str, |
| 117 | cx: &App, |
| 118 | ) -> Vec<AnyElement> { |
| 119 | cards |
| 120 | .iter() |
| 121 | .enumerate() |
| 122 | .map(|(index, card)| { |
| 123 | let scope = CardScope::message(conversation_id, message_id, index); |
| 124 | card_view(card, &scope, cx) |
| 125 | }) |
| 126 | .collect() |
| 127 | } |
| 128 | |
| 129 | pub fn ensure_cards_inputs( |
| 130 | app: &Entity<AppState>, |
| 131 | cards: &[AdaptiveCard], |
| 132 | conversation_id: &str, |
| 133 | message_id: &str, |
| 134 | window: &mut Window, |
| 135 | cx: &mut App, |
| 136 | ) { |
| 137 | if cards.is_empty() { |
| 138 | return; |
| 139 | } |
| 140 | app.update(cx, |state, cx| { |
| 141 | for (index, card) in cards.iter().enumerate() { |
| 142 | let scope = CardScope::message(conversation_id, message_id, index); |
| 143 | state.ensure_card_inputs(&scope, card, window, cx); |
| 144 | } |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | pub fn request_card_refreshes( |
| 149 | app: &Entity<AppState>, |
| 150 | cards: &[AdaptiveCard], |
| 151 | conversation_id: &str, |
| 152 | message_id: &str, |
| 153 | cx: &mut App, |
| 154 | ) { |
| 155 | if cards.iter().all(|card| card.refresh.is_none()) { |
| 156 | return; |
| 157 | } |
| 158 | app.update(cx, |state, cx| { |
| 159 | state.request_card_refreshes(conversation_id, message_id, cards, cx) |
| 160 | }); |
| 161 | } |
| 162 | |
| 163 | pub fn card_view(card: &AdaptiveCard, scope: &CardScope, cx: &App) -> AnyElement { |
| 164 | let context = CardContext::new(card, scope, cx); |
| 165 | let id = scope.card_key(); |
| 166 | let note = context.state.note(&id).map(str::to_owned); |
| 167 | let select_handler = card |
| 168 | .select_action |
| 169 | .as_ref() |
| 170 | .filter(|action| action.is_clickable()) |
| 171 | .and_then(|action| click_handler(action, &format!("{id}-select"), None, &context)); |
| 172 | let visible_actions = card |
| 173 | .actions |
| 174 | .iter() |
| 175 | .any(|action| context.visible(action.id.as_ref(), action.visible)); |
| 176 | v_flex() |
| 177 | .id(ElementId::Name(format!("{id}-root").into())) |
| 178 | .relative() |
| 179 | .w(px(card_width(card))) |
| 180 | .max_w(relative(1.)) |
| 181 | .p(px(CARD_PADDING)) |
| 182 | .rounded(px(CARD_RADIUS)) |
| 183 | .bg(theme::surface()) |
| 184 | .border(px(CARD_BORDER_WIDTH)) |
| 185 | .border_color(theme::border()) |
| 186 | .text_color(theme::text_strong()) |
| 187 | .when_some(select_handler, |root, handler| { |
| 188 | root.cursor_pointer() |
| 189 | .hover(|root| root.bg(theme::row_hover())) |
| 190 | .on_click(move |_, _, cx| { |
| 191 | cx.stop_propagation(); |
| 192 | handler(cx) |
| 193 | }) |
| 194 | }) |
| 195 | .when_some(card.layout.background_image.clone(), |root, url| { |
| 196 | root.child(background_layer(&url, CARD_RADIUS)) |
| 197 | }) |
| 198 | .child(aligned_body(&card.items, &id, &context, &card.layout, true)) |
| 199 | .when(visible_actions, |card_element| { |
| 200 | card_element.child(div().mt(px(spacing_pixels(CardSpacing::Default))).child( |
| 201 | actions_view(&card.actions, &format!("{id}-actions"), &context), |
| 202 | )) |
| 203 | }) |
| 204 | .when_some(note, |card_element, note| { |
| 205 | card_element.child( |
| 206 | div() |
| 207 | .mt(px(spacing_pixels(CardSpacing::Default))) |
| 208 | .text_size(px(NOTE_TEXT_SIZE)) |
| 209 | .text_color(theme::text_muted()) |
| 210 | .child(note), |
| 211 | ) |
| 212 | }) |
| 213 | .into_any_element() |
| 214 | } |
| 215 | |
| 216 | fn card_width(card: &AdaptiveCard) -> f32 { |
| 217 | if card.full_width { |
| 218 | CARD_FULL_WIDTH |
| 219 | } else { |
| 220 | CARD_WIDTH |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | fn background_layer(url: &str, radius: f32) -> AnyElement { |
| 225 | div() |
| 226 | .absolute() |
| 227 | .inset_0() |
| 228 | .rounded(px(radius)) |
| 229 | .overflow_hidden() |
| 230 | .child( |
| 231 | img(url.to_owned()) |
| 232 | .size_full() |
| 233 | .object_fit(ObjectFit::Cover) |
| 234 | .with_loading(|| div().into_any_element()) |
| 235 | .with_fallback(|| div().into_any_element()), |
| 236 | ) |
| 237 | .into_any_element() |
| 238 | } |
| 239 | |
| 240 | fn aligned_body( |
| 241 | items: &[CardItem], |
| 242 | id: &str, |
| 243 | context: &CardContext, |
| 244 | layout: &ContainerLayout, |
| 245 | top_level: bool, |
| 246 | ) -> Div { |
| 247 | items_view(items, id, context, top_level) |
| 248 | .when_some(layout.min_height, |body, height| body.min_h(px(height))) |
| 249 | .map(|body| match layout.vertical_alignment { |
| 250 | VerticalAlignment::Top => body.justify_start(), |
| 251 | VerticalAlignment::Center => body.justify_center(), |
| 252 | VerticalAlignment::Bottom => body.justify_end(), |
| 253 | }) |
| 254 | .when(layout.rtl, |body| body.text_right()) |
| 255 | } |
| 256 | |
| 257 | pub(super) fn items_view( |
| 258 | items: &[CardItem], |
| 259 | id: &str, |
| 260 | context: &CardContext, |
| 261 | top_level: bool, |
| 262 | ) -> Div { |
| 263 | let shown: Vec<(usize, &CardItem)> = items |
| 264 | .iter() |
| 265 | .enumerate() |
| 266 | .filter(|(_, item)| { |
| 267 | context.visible(item.id.as_ref(), item.visible) |
| 268 | && item |
| 269 | .target_width |
| 270 | .is_none_or(|target| target.matches(context.width_class)) |
| 271 | }) |
| 272 | .collect(); |
| 273 | let mut column = v_flex().w_full(); |
| 274 | for (position, (index, item)) in shown.iter().enumerate() { |
| 275 | let item_id = format!("{id}-{index}"); |
| 276 | let first = position == 0; |
| 277 | let bleed = top_level |
| 278 | .then(|| bleed_edges(item, first, position + 1 == shown.len())) |
| 279 | .flatten(); |
| 280 | column = column.child( |
| 281 | div() |
| 282 | .map(|wrapper| match bleed { |
| 283 | Some(edges) => wrapper |
| 284 | .mx(px(-CARD_PADDING)) |
| 285 | .when(edges.top, |wrapper| wrapper.mt(px(-CARD_PADDING))) |
| 286 | .when(edges.bottom, |wrapper| wrapper.mb(px(-CARD_PADDING))), |
| 287 | None => wrapper.w_full(), |
| 288 | }) |
| 289 | .when(item.stretch, |wrapper| wrapper.flex_grow(1.)) |
| 290 | .when(!first && bleed.is_none_or(|edges| !edges.top), |wrapper| { |
| 291 | wrapper.mt(px(spacing_pixels(item.spacing))) |
| 292 | }) |
| 293 | .when(!first && item.separator, |wrapper| { |
| 294 | wrapper |
| 295 | .pt(px(SEPARATOR_PADDING)) |
| 296 | .border_t_1() |
| 297 | .border_color(theme::border()) |
| 298 | }) |
| 299 | .child(element_view(&item.element, &item_id, context, bleed)), |
| 300 | ); |
| 301 | } |
| 302 | column |
| 303 | } |
| 304 | |
| 305 | #[derive(Clone, Copy)] |
| 306 | pub(super) struct BleedEdges { |
| 307 | top: bool, |
| 308 | bottom: bool, |
| 309 | } |
| 310 | |
| 311 | fn bleed_edges(item: &CardItem, first: bool, last: bool) -> Option<BleedEdges> { |
| 312 | match &item.element { |
| 313 | CardElement::Container { layout, .. } if layout.bleed => Some(BleedEdges { |
| 314 | top: first, |
| 315 | bottom: last, |
| 316 | }), |
| 317 | _ => None, |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | fn spacing_pixels(spacing: CardSpacing) -> f32 { |
| 322 | match spacing { |
| 323 | CardSpacing::None => 0., |
| 324 | CardSpacing::Small => 4., |
| 325 | CardSpacing::Default => 8., |
| 326 | CardSpacing::Medium => 12., |
| 327 | CardSpacing::Large | CardSpacing::Padding => 16., |
| 328 | CardSpacing::ExtraLarge => 24., |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | fn element_view( |
| 333 | element: &CardElement, |
| 334 | id: &str, |
| 335 | context: &CardContext, |
| 336 | bleed: Option<BleedEdges>, |
| 337 | ) -> AnyElement { |
| 338 | match element { |
| 339 | CardElement::Text(text) => text_view(text, id, context.cx), |
| 340 | CardElement::Image(image) => aligned_image(image, id, context), |
| 341 | CardElement::ImageSet(images) => { |
| 342 | h_flex() |
| 343 | .flex_wrap() |
| 344 | .gap(px(IMAGE_SET_GAP)) |
| 345 | .children(images.iter().enumerate().map(|(index, image)| { |
| 346 | selectable_image(image, &format!("{id}-{index}"), context) |
| 347 | })) |
| 348 | .into_any_element() |
| 349 | } |
| 350 | CardElement::Columns { columns, layout } => columns_view(columns, layout, id, context), |
| 351 | CardElement::Container { |
| 352 | layout, |
| 353 | items, |
| 354 | select_action, |
| 355 | } => selectable( |
| 356 | container_view(items, id, context, layout, bleed).into_any_element(), |
| 357 | select_action.as_ref(), |
| 358 | id, |
| 359 | false, |
| 360 | context, |
| 361 | ), |
| 362 | CardElement::Facts(facts) => facts_view(facts, id, context.cx), |
| 363 | CardElement::Media(media) => media_view(media, id), |
| 364 | CardElement::Icon(icon) => selectable( |
| 365 | icon_view(icon), |
| 366 | icon.select_action.as_ref(), |
| 367 | id, |
| 368 | true, |
| 369 | context, |
| 370 | ), |
| 371 | CardElement::Table(table) => table_view(table, id, context), |
| 372 | CardElement::CodeBlock(block) => code_block_view(block, id, context), |
| 373 | CardElement::Badge(badge) => leading(badge_view(badge, id)), |
| 374 | CardElement::ProgressBar(bar) => progress_bar_view(bar, id), |
| 375 | CardElement::ProgressRing(ring) => leading(progress_ring_view(ring, id)), |
| 376 | CardElement::CompoundButton(button) => compound_button_view(button, id, context), |
| 377 | CardElement::Carousel(carousel) => carousel_view(carousel, id, context), |
| 378 | CardElement::Actions(actions) => actions_view(actions, id, context).into_any_element(), |
| 379 | CardElement::Input(input) => { |
| 380 | input_view(input, context.scope, context.state, &context.inputs) |
| 381 | } |
| 382 | CardElement::Rating(display) => rating_display_view(display), |
| 383 | CardElement::Chart(chart) => chart_view(chart), |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | pub(super) fn container_view( |
| 388 | items: &[CardItem], |
| 389 | id: &str, |
| 390 | context: &CardContext, |
| 391 | layout: &ContainerLayout, |
| 392 | bleed: Option<BleedEdges>, |
| 393 | ) -> Div { |
| 394 | let tint = container_tint(layout.style); |
| 395 | let padded = tint.is_some() || layout.background_image.is_some(); |
| 396 | let inset = |edge: bool| { |
| 397 | if edge { |
| 398 | CARD_PADDING |
| 399 | } else if padded { |
| 400 | CONTAINER_PADDING |
| 401 | } else { |
| 402 | 0. |
| 403 | } |
| 404 | }; |
| 405 | v_flex() |
| 406 | .w_full() |
| 407 | .relative() |
| 408 | .map(|container| match bleed { |
| 409 | Some(edges) => container |
| 410 | .px(px(CARD_PADDING)) |
| 411 | .pt(px(inset(edges.top))) |
| 412 | .pb(px(inset(edges.bottom))) |
| 413 | .when(edges.top, |container| { |
| 414 | container.rounded_t(px(CARD_RADIUS - CARD_BORDER_WIDTH)) |
| 415 | }) |
| 416 | .when(edges.bottom, |container| { |
| 417 | container.rounded_b(px(CARD_RADIUS - CARD_BORDER_WIDTH)) |
| 418 | }), |
| 419 | None => container.when(padded, |container| { |
| 420 | container |
| 421 | .p(px(CONTAINER_PADDING)) |
| 422 | .rounded(px(CONTAINER_RADIUS)) |
| 423 | }), |
| 424 | }) |
| 425 | .when_some(tint, |container, tint| container.bg(tint)) |
| 426 | .when_some(layout.background_image.clone(), |container, url| { |
| 427 | container.child(background_layer(&url, CONTAINER_RADIUS)) |
| 428 | }) |
| 429 | .child(aligned_body(items, id, context, layout, false)) |
| 430 | } |
| 431 | |
| 432 | pub(super) fn container_tint(style: ContainerStyle) -> Option<Hsla> { |
| 433 | match style { |
| 434 | ContainerStyle::Default => None, |
| 435 | ContainerStyle::Emphasis => Some(theme::surface_raised()), |
| 436 | ContainerStyle::Accent => Some(theme::accent().opacity(CONTAINER_TINT)), |
| 437 | ContainerStyle::Good => Some(theme::green().opacity(CONTAINER_TINT)), |
| 438 | ContainerStyle::Warning => Some(theme::amber().opacity(CONTAINER_TINT)), |
| 439 | ContainerStyle::Attention => Some(theme::red().opacity(CONTAINER_TINT)), |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | fn text_size(size: TextSize) -> f32 { |
| 444 | match size { |
| 445 | TextSize::Small => 11.5, |
| 446 | TextSize::Default => 13.5, |
| 447 | TextSize::Medium => 15., |
| 448 | TextSize::Large => 17.5, |
| 449 | TextSize::ExtraLarge => 21., |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | fn text_color(text: &CardText) -> Hsla { |
| 454 | card_color(text.color, text.subtle) |
| 455 | } |
| 456 | |
| 457 | pub(super) fn card_color(color: TextColor, subtle: bool) -> Hsla { |
| 458 | match color { |
| 459 | TextColor::Accent => theme::accent_text(), |
| 460 | TextColor::Good => theme::green(), |
| 461 | TextColor::Warning => theme::amber(), |
| 462 | TextColor::Attention => theme::red_soft(), |
| 463 | TextColor::Default | TextColor::Dark | TextColor::Light if subtle => theme::text_muted(), |
| 464 | TextColor::Default | TextColor::Dark | TextColor::Light => theme::text_strong(), |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | fn text_view(text: &CardText, id: &str, cx: &App) -> AnyElement { |
| 469 | div() |
| 470 | .w_full() |
| 471 | .min_w(px(0.)) |
| 472 | .text_size(px(text_size(text.size))) |
| 473 | .line_height(relative(TEXT_LINE_HEIGHT)) |
| 474 | .text_color(text_color(text)) |
| 475 | .when(text.bold, |element| { |
| 476 | element.font_weight(FontWeight::SEMIBOLD) |
| 477 | }) |
| 478 | .when(text.monospace, |element| { |
| 479 | element.font_family(cx.theme().mono_font_family.clone()) |
| 480 | }) |
| 481 | .map(|element| match text.alignment { |
| 482 | Some(HorizontalAlignment::Left) => element.text_left(), |
| 483 | Some(HorizontalAlignment::Center) => element.text_center(), |
| 484 | Some(HorizontalAlignment::Right) => element.text_right(), |
| 485 | None => element, |
| 486 | }) |
| 487 | .map(|element| match (text.wrap, text.max_lines) { |
| 488 | (false, _) => element |
| 489 | .whitespace_nowrap() |
| 490 | .overflow_hidden() |
| 491 | .text_ellipsis(), |
| 492 | (true, Some(lines)) => element.text_ellipsis().line_clamp(lines), |
| 493 | (true, None) => element, |
| 494 | }) |
| 495 | .child(render_blocks(&layout_card_blocks(&text.spans), id, false, cx)) |
| 496 | .into_any_element() |
| 497 | } |
| 498 | |
| 499 | fn facts_view(facts: &[CardFact], id: &str, cx: &App) -> AnyElement { |
| 500 | v_flex() |
| 501 | .w_full() |
| 502 | .gap(px(FACT_GAP)) |
| 503 | .children(facts.iter().enumerate().map(|(index, fact)| { |
| 504 | h_flex() |
| 505 | .w_full() |
| 506 | .items_start() |
| 507 | .gap(px(COLUMN_GAP)) |
| 508 | .text_size(px(text_size(TextSize::Default))) |
| 509 | .line_height(relative(TEXT_LINE_HEIGHT)) |
| 510 | .child( |
| 511 | div() |
| 512 | .w(px(FACT_TITLE_WIDTH)) |
| 513 | .flex_none() |
| 514 | .font_weight(FontWeight::SEMIBOLD) |
| 515 | .text_color(theme::text_muted()) |
| 516 | .child(fact.title.clone()), |
| 517 | ) |
| 518 | .child(div().flex_1().min_w(px(0.)).child(render_blocks( |
| 519 | &layout_card_blocks(&fact.value), |
| 520 | &format!("{id}-{index}"), |
| 521 | false, |
| 522 | cx, |
| 523 | ))) |
| 524 | })) |
| 525 | .into_any_element() |
| 526 | } |
| 527 | |
| 528 | fn columns_view( |
| 529 | columns: &[CardColumn], |
| 530 | layout: &ContainerLayout, |
| 531 | id: &str, |
| 532 | context: &CardContext, |
| 533 | ) -> AnyElement { |
| 534 | let tint = container_tint(layout.style); |
| 535 | div() |
| 536 | .flex() |
| 537 | .map(|row| { |
| 538 | if layout.rtl { |
| 539 | row.flex_row_reverse() |
| 540 | } else { |
| 541 | row.flex_row() |
| 542 | } |
| 543 | }) |
| 544 | .items_stretch() |
| 545 | .w_full() |
| 546 | .gap(px(COLUMN_GAP)) |
| 547 | .when_some(layout.min_height, |row, height| row.min_h(px(height))) |
| 548 | .when_some(tint, |row, tint| { |
| 549 | row.p(px(CONTAINER_PADDING)) |
| 550 | .rounded(px(CONTAINER_RADIUS)) |
| 551 | .bg(tint) |
| 552 | }) |
| 553 | .children( |
| 554 | columns |
| 555 | .iter() |
| 556 | .enumerate() |
| 557 | .filter(|(_, column)| { |
| 558 | column |
| 559 | .target_width |
| 560 | .is_none_or(|target| target.matches(context.width_class)) |
| 561 | }) |
| 562 | .map(|(index, column)| { |
| 563 | let content = container_view( |
| 564 | &column.items, |
| 565 | &format!("{id}-{index}"), |
| 566 | context, |
| 567 | &column.layout, |
| 568 | None, |
| 569 | ); |
| 570 | let content = selectable( |
| 571 | content.into_any_element(), |
| 572 | column.select_action.as_ref(), |
| 573 | &format!("{id}-{index}"), |
| 574 | false, |
| 575 | context, |
| 576 | ); |
| 577 | let aligned = |
| 578 | v_flex() |
| 579 | .h_full() |
| 580 | .map(|aligned| match column.layout.vertical_alignment { |
| 581 | VerticalAlignment::Top => aligned.justify_start(), |
| 582 | VerticalAlignment::Center => aligned.justify_center(), |
| 583 | VerticalAlignment::Bottom => aligned.justify_end(), |
| 584 | }); |
| 585 | sized_column(aligned, column.width).child(content) |
| 586 | }), |
| 587 | ) |
| 588 | .into_any_element() |
| 589 | } |
| 590 | |
| 591 | pub(super) fn sized_column(column: Div, width: ColumnWidth) -> Div { |
| 592 | match width { |
| 593 | ColumnWidth::Auto => column.flex_none(), |
| 594 | ColumnWidth::Stretch => column.flex_1().min_w(px(0.)), |
| 595 | ColumnWidth::Weighted(weight) => column |
| 596 | .flex_basis(px(0.)) |
| 597 | .flex_grow(weight.max(0.)) |
| 598 | .min_w(px(0.)), |
| 599 | ColumnWidth::Pixels(pixels) => column.w(px(pixels)).flex_none(), |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | fn aligned_image(image: &CardImage, id: &str, context: &CardContext) -> AnyElement { |
| 604 | h_flex() |
| 605 | .w_full() |
| 606 | .map(|row| match image.alignment { |
| 607 | HorizontalAlignment::Left => row.justify_start(), |
| 608 | HorizontalAlignment::Center => row.justify_center(), |
| 609 | HorizontalAlignment::Right => row.justify_end(), |
| 610 | }) |
| 611 | .child(selectable_image(image, id, context)) |
| 612 | .into_any_element() |
| 613 | } |
| 614 | |
| 615 | fn selectable_image(image: &CardImage, id: &str, context: &CardContext) -> AnyElement { |
| 616 | selectable( |
| 617 | image_view(image, id), |
| 618 | image.select_action.as_ref(), |
| 619 | id, |
| 620 | true, |
| 621 | context, |
| 622 | ) |
| 623 | } |
| 624 | |
| 625 | pub(super) fn selectable( |
| 626 | content: AnyElement, |
| 627 | select_action: Option<&CardAction>, |
| 628 | id: &str, |
| 629 | fit: bool, |
| 630 | context: &CardContext, |
| 631 | ) -> AnyElement { |
| 632 | let select_key = format!("{id}-select"); |
| 633 | let Some(handler) = select_action |
| 634 | .filter(|action| action.is_clickable()) |
| 635 | .and_then(|action| click_handler(action, &select_key, None, context)) |
| 636 | else { |
| 637 | return content; |
| 638 | }; |
| 639 | let group = SharedString::from(select_key.clone()); |
| 640 | div() |
| 641 | .id(ElementId::Name(select_key.into())) |
| 642 | .group(group.clone()) |
| 643 | .relative() |
| 644 | .cursor_pointer() |
| 645 | .map(|wrapper| { |
| 646 | if fit { |
| 647 | wrapper.self_start().flex_none() |
| 648 | } else { |
| 649 | wrapper.w_full() |
| 650 | } |
| 651 | }) |
| 652 | .on_click(move |_, _, cx| { |
| 653 | cx.stop_propagation(); |
| 654 | handler(cx) |
| 655 | }) |
| 656 | .child(content) |
| 657 | .child( |
| 658 | div() |
| 659 | .absolute() |
| 660 | .inset_0() |
| 661 | .rounded(px(CONTAINER_RADIUS)) |
| 662 | .group_hover(group, |tint| tint.bg(white().opacity(SELECT_TINT))), |
| 663 | ) |
| 664 | .into_any_element() |
| 665 | } |
| 666 | |
| 667 | fn image_view(image: &CardImage, id: &str) -> AnyElement { |
| 668 | let width = match (image.width, image.height, named_image_width(image.size)) { |
| 669 | (Some(width), _, _) => width, |
| 670 | (None, Some(height), _) => height, |
| 671 | (None, None, Some(width)) => width, |
| 672 | (None, None, None) => MEDIUM_IMAGE, |
| 673 | } |
| 674 | .min(CARD_FULL_WIDTH - 2. * CARD_PADDING); |
| 675 | let height = image.height.unwrap_or(width); |
| 676 | let radius = px(if image.person { |
| 677 | width.min(height) / 2. |
| 678 | } else { |
| 679 | IMAGE_RADIUS |
| 680 | }); |
| 681 | let frame = div() |
| 682 | .id(ElementId::Name(id.to_owned().into())) |
| 683 | .w(px(width)) |
| 684 | .h(px(height)) |
| 685 | .max_w(relative(1.)) |
| 686 | .flex_none() |
| 687 | .overflow_hidden() |
| 688 | .rounded(radius); |
| 689 | if !EXTERNAL_IMAGE_SCHEMES |
| 690 | .iter() |
| 691 | .any(|scheme| image.url.starts_with(scheme)) |
| 692 | { |
| 693 | return frame.bg(theme::surface_raised()).into_any_element(); |
| 694 | } |
| 695 | let alt_text = image.alt_text.clone(); |
| 696 | frame |
| 697 | .when_some(image.background_color, |frame, color| frame.bg(rgba(color))) |
| 698 | .when_some(alt_text, |frame, alt_text| { |
| 699 | frame.tooltip(move |window, cx| Tooltip::new(alt_text.clone()).build(window, cx)) |
| 700 | }) |
| 701 | .child( |
| 702 | img(image.url.clone()) |
| 703 | .size_full() |
| 704 | .rounded(radius) |
| 705 | .object_fit(ObjectFit::Cover) |
| 706 | .with_loading(move || image_placeholder(radius)) |
| 707 | .with_fallback(move || image_placeholder(radius)), |
| 708 | ) |
| 709 | .into_any_element() |
| 710 | } |
| 711 | |
| 712 | fn image_placeholder(radius: Pixels) -> AnyElement { |
| 713 | div() |
| 714 | .size_full() |
| 715 | .rounded(radius) |
| 716 | .bg(theme::surface_raised()) |
| 717 | .into_any_element() |
| 718 | } |
| 719 | |
| 720 | fn named_image_width(size: ImageSize) -> Option<f32> { |
| 721 | match size { |
| 722 | ImageSize::Small => Some(SMALL_IMAGE), |
| 723 | ImageSize::Medium => Some(MEDIUM_IMAGE), |
| 724 | ImageSize::Large => Some(LARGE_IMAGE), |
| 725 | ImageSize::Stretch => Some(CARD_WIDTH - 2. * CARD_PADDING), |
| 726 | ImageSize::Auto => None, |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | fn actions_view(actions: &[CardAction], id: &str, context: &CardContext) -> Div { |
| 731 | let open_card = context |
| 732 | .state |
| 733 | .open_card(id) |
| 734 | .and_then(|index| actions.get(index)) |
| 735 | .and_then(|action| match &action.kind { |
| 736 | CardActionKind::ShowCard(card) => Some(card), |
| 737 | _ => None, |
| 738 | }); |
| 739 | let split = split_overflow(actions, |action| { |
| 740 | context.visible(action.id.as_ref(), action.visible) |
| 741 | }); |
| 742 | let overflow_key = format!("{id}-overflow"); |
| 743 | let overflow_open = !split.overflow.is_empty() && context.state.slot(&overflow_key, 0) == 1; |
| 744 | v_flex() |
| 745 | .w_full() |
| 746 | .gap(px(ACTION_GAP)) |
| 747 | .child( |
| 748 | h_flex() |
| 749 | .w_full() |
| 750 | .flex_wrap() |
| 751 | .gap(px(ACTION_GAP)) |
| 752 | .children( |
| 753 | split |
| 754 | .primary |
| 755 | .iter() |
| 756 | .map(|&index| action_button(&actions[index], index, id, context)), |
| 757 | ) |
| 758 | .when(!split.overflow.is_empty(), |row| { |
| 759 | row.child(overflow_button(&overflow_key, overflow_open, context)) |
| 760 | }), |
| 761 | ) |
| 762 | .when(overflow_open, |column| { |
| 763 | column.child( |
| 764 | v_flex() |
| 765 | .self_start() |
| 766 | .items_start() |
| 767 | .gap(px(OVERFLOW_PANEL_PADDING)) |
| 768 | .p(px(OVERFLOW_PANEL_PADDING)) |
| 769 | .rounded(px(CONTAINER_RADIUS)) |
| 770 | .bg(theme::surface_raised()) |
| 771 | .children( |
| 772 | split |
| 773 | .overflow |
| 774 | .iter() |
| 775 | .map(|&index| action_button(&actions[index], index, id, context)), |
| 776 | ), |
| 777 | ) |
| 778 | }) |
| 779 | .when_some(open_card, |column, card| { |
| 780 | let nested_id = format!("{id}-show"); |
| 781 | let nested_context = context.nested(card); |
| 782 | column.child( |
| 783 | v_flex() |
| 784 | .w_full() |
| 785 | .p(px(CONTAINER_PADDING)) |
| 786 | .gap(px(ACTION_GAP)) |
| 787 | .rounded(px(CONTAINER_RADIUS)) |
| 788 | .bg(theme::surface_raised()) |
| 789 | .child(items_view(&card.items, &nested_id, &nested_context, false)) |
| 790 | .when(!card.actions.is_empty(), |nested| { |
| 791 | nested.child(actions_view( |
| 792 | &card.actions, |
| 793 | &format!("{nested_id}-actions"), |
| 794 | &nested_context, |
| 795 | )) |
| 796 | }), |
| 797 | ) |
| 798 | }) |
| 799 | } |
| 800 | |
| 801 | fn overflow_button(overflow_key: &str, open: bool, context: &CardContext) -> Stateful<Div> { |
| 802 | let handler = slot_handler(context, overflow_key, usize::from(!open)); |
| 803 | action_frame(overflow_key, theme::border_strong()) |
| 804 | .text_color(theme::accent_text()) |
| 805 | .cursor_pointer() |
| 806 | .hover(|button| button.bg(theme::row_hover())) |
| 807 | .when(open, |button| button.bg(theme::row_hover())) |
| 808 | .popup_trigger_tooltip_text(open, ACTION_OVERFLOW_TOOLTIP) |
| 809 | .child(ACTION_OVERFLOW_LABEL) |
| 810 | .on_click(move |_, _, cx| { |
| 811 | cx.stop_propagation(); |
| 812 | handler(cx) |
| 813 | }) |
| 814 | } |
| 815 | |
| 816 | fn action_frame(button_id: &str, border: Hsla) -> Stateful<Div> { |
| 817 | div() |
| 818 | .id(ElementId::Name(button_id.to_owned().into())) |
| 819 | .px(px(12.)) |
| 820 | .py(px(6.)) |
| 821 | .rounded(px(ACTION_RADIUS)) |
| 822 | .border_1() |
| 823 | .border_color(border) |
| 824 | .text_size(px(ACTION_TEXT_SIZE)) |
| 825 | .font_weight(FontWeight::SEMIBOLD) |
| 826 | } |
| 827 | |
| 828 | pub(super) fn slot_handler(context: &CardContext, slot_key: &str, value: usize) -> ClickHandler { |
| 829 | let scope = context.scope.clone(); |
| 830 | let slot_key = slot_key.to_owned(); |
| 831 | Rc::new(move |cx| { |
| 832 | let (scope, slot_key) = (scope.clone(), slot_key.clone()); |
| 833 | cx.global::<AppHandle>().0.clone().update(cx, |state, cx| { |
| 834 | state.set_card_slot(&scope, &slot_key, value, cx) |
| 835 | }); |
| 836 | }) |
| 837 | } |
| 838 | |
| 839 | fn action_button( |
| 840 | action: &CardAction, |
| 841 | index: usize, |
| 842 | actions_id: &str, |
| 843 | context: &CardContext, |
| 844 | ) -> Stateful<Div> { |
| 845 | let button_id = format!("{actions_id}-{index}"); |
| 846 | let phase = context.state.phase(&button_id).cloned(); |
| 847 | let label = action_label( |
| 848 | action, |
| 849 | &phase, |
| 850 | context.state.open_card(actions_id) == Some(index), |
| 851 | ); |
| 852 | let border = match phase { |
| 853 | Some(ActionPhase::Failed(_)) => theme::red(), |
| 854 | _ => theme::border_strong(), |
| 855 | }; |
| 856 | let button = action_frame(&button_id, border).child(label); |
| 857 | if !action.is_clickable() { |
| 858 | let hint = match action.kind { |
| 859 | CardActionKind::Unavailable(reason) if action.enabled => Some(reason.to_owned()), |
| 860 | _ if action.enabled => Some(ACTION_DISABLED_TOOLTIP.to_owned()), |
| 861 | _ => action.tooltip.clone(), |
| 862 | }; |
| 863 | return button |
| 864 | .text_color(theme::text_muted()) |
| 865 | .opacity(ACTION_DISABLED_OPACITY) |
| 866 | .when_some(hint, |button, hint| { |
| 867 | button.tooltip(move |window, cx| Tooltip::new(hint.clone()).build(window, cx)) |
| 868 | }) |
| 869 | .on_click(|_, _, cx| cx.stop_propagation()); |
| 870 | } |
| 871 | let enabled = button.text_color(theme::accent_text()); |
| 872 | if phase == Some(ActionPhase::Busy) { |
| 873 | return enabled |
| 874 | .opacity(ACTION_BUSY_OPACITY) |
| 875 | .on_click(|_, _, cx| cx.stop_propagation()); |
| 876 | } |
| 877 | let hint = failure_reason(&phase).or_else(|| action.tooltip.clone()); |
| 878 | let enabled = enabled |
| 879 | .cursor_pointer() |
| 880 | .hover(|button| button.bg(theme::row_hover())) |
| 881 | .when_some(hint, |button, hint| { |
| 882 | button.tooltip(move |window, cx| Tooltip::new(hint.clone()).build(window, cx)) |
| 883 | }); |
| 884 | match click_handler(action, &button_id, Some((actions_id, index)), context) { |
| 885 | Some(handler) => enabled.on_click(move |_, _, cx| { |
| 886 | cx.stop_propagation(); |
| 887 | handler(cx) |
| 888 | }), |
| 889 | None => enabled, |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | fn click_handler( |
| 894 | action: &CardAction, |
| 895 | action_key: &str, |
| 896 | show_card: Option<(&str, usize)>, |
| 897 | context: &CardContext, |
| 898 | ) -> Option<ClickHandler> { |
| 899 | let scope = context.scope.clone(); |
| 900 | match &action.kind { |
| 901 | CardActionKind::OpenUrl(url) => { |
| 902 | let url = url.clone(); |
| 903 | Some(Rc::new(move |cx| cx.open_url(&url))) |
| 904 | } |
| 905 | CardActionKind::Submit(_) | CardActionKind::Execute(_) => { |
| 906 | let action = action.clone(); |
| 907 | let action_key = action_key.to_owned(); |
| 908 | let inputs = context.inputs.clone(); |
| 909 | Some(Rc::new(move |cx| { |
| 910 | let (scope, action, action_key, inputs) = ( |
| 911 | scope.clone(), |
| 912 | action.clone(), |
| 913 | action_key.clone(), |
| 914 | inputs.clone(), |
| 915 | ); |
| 916 | cx.global::<AppHandle>().0.clone().update(cx, |state, cx| { |
| 917 | state.run_card_action(scope, action_key, action, inputs, cx) |
| 918 | }); |
| 919 | })) |
| 920 | } |
| 921 | CardActionKind::ShowCard(_) => { |
| 922 | let (actions_id, index) = show_card?; |
| 923 | let actions_id = actions_id.to_owned(); |
| 924 | Some(Rc::new(move |cx| { |
| 925 | let (scope, actions_id) = (scope.clone(), actions_id.clone()); |
| 926 | cx.global::<AppHandle>().0.clone().update(cx, |state, cx| { |
| 927 | state.toggle_show_card(&scope, &actions_id, index, cx) |
| 928 | }); |
| 929 | })) |
| 930 | } |
| 931 | CardActionKind::ToggleVisibility(targets) => { |
| 932 | let elements: Vec<(String, bool, Option<bool>)> = targets |
| 933 | .iter() |
| 934 | .map(|target| { |
| 935 | let initial = context |
| 936 | .initial_visibility |
| 937 | .get(&target.element_id) |
| 938 | .copied() |
| 939 | .unwrap_or(true); |
| 940 | (target.element_id.clone(), initial, target.visible) |
| 941 | }) |
| 942 | .collect(); |
| 943 | let root_key = context.root_key.clone(); |
| 944 | Some(Rc::new(move |cx| { |
| 945 | let (scope, root_key, elements) = |
| 946 | (scope.clone(), root_key.clone(), elements.clone()); |
| 947 | cx.global::<AppHandle>().0.clone().update(cx, |state, cx| { |
| 948 | state.toggle_card_elements(&scope, &root_key, elements, cx) |
| 949 | }); |
| 950 | })) |
| 951 | } |
| 952 | CardActionKind::Unavailable(_) | CardActionKind::Unsupported => None, |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | fn failure_reason(phase: &Option<ActionPhase>) -> Option<String> { |
| 957 | match phase { |
| 958 | Some(ActionPhase::Failed(reason)) => Some(reason.clone()), |
| 959 | _ => None, |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | fn action_label(action: &CardAction, phase: &Option<ActionPhase>, open: bool) -> AnyElement { |
| 964 | match phase { |
| 965 | Some(ActionPhase::Busy) => format!("{}...", action.title).into_any_element(), |
| 966 | Some(ActionPhase::Done) => h_flex() |
| 967 | .gap(px(ACTION_ICON_GAP)) |
| 968 | .items_center() |
| 969 | .child(symbol("done", ACTION_CHECK_SIZE, theme::green())) |
| 970 | .child(action.title.clone()) |
| 971 | .into_any_element(), |
| 972 | _ if open => titled(action, format!("{} ^", action.title)), |
| 973 | _ => titled(action, action.title.clone()), |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | fn titled(action: &CardAction, text: String) -> AnyElement { |
| 978 | match action.icon.as_ref().map(action_icon_view) { |
| 979 | Some(icon) => h_flex() |
| 980 | .gap(px(ACTION_ICON_GAP)) |
| 981 | .items_center() |
| 982 | .child(icon) |
| 983 | .child(text) |
| 984 | .into_any_element(), |
| 985 | None => text.into_any_element(), |
| 986 | } |
| 987 | } |