| 1 | use std::collections::HashMap; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::component::{h_flex, tooltip::Tooltip, v_flex}; |
| 5 | use gpui_kit::prelude::FluentBuilder as _; |
| 6 | use gpui_kit::*; |
| 7 | use teams_core::{ |
| 8 | AdaptiveCard, CardAction, CardActionKind, CardColumn, CardElement, CardFact, CardImage, |
| 9 | CardInput, CardItem, CardSpacing, CardText, ColumnWidth, ContainerStyle, ImageSize, TextColor, TextSize, |
| 10 | VerticalAlignment, |
| 11 | }; |
| 12 | |
| 13 | use super::card_input::input_view; |
| 14 | use super::widgets::symbol; |
| 15 | use crate::app_state::{AppHandle, AppState}; |
| 16 | use crate::card_state::{ActionPhase, CardScope, CardState}; |
| 17 | use crate::render::{layout_blocks, render_blocks}; |
| 18 | use crate::theme; |
| 19 | |
| 20 | const CARD_WIDTH: f32 = 480.; |
| 21 | const CARD_FULL_WIDTH: f32 = 720.; |
| 22 | const CARD_PADDING: f32 = 12.; |
| 23 | const CARD_RADIUS: f32 = 10.; |
| 24 | const CARD_BORDER_WIDTH: f32 = 1.; |
| 25 | const COLUMN_GAP: f32 = 8.; |
| 26 | const SEPARATOR_PADDING: f32 = 8.; |
| 27 | const ACTION_GAP: f32 = 8.; |
| 28 | const ACTION_RADIUS: f32 = 6.; |
| 29 | const ACTION_TEXT_SIZE: f32 = 13.; |
| 30 | const ACTION_DISABLED_OPACITY: f32 = 0.5; |
| 31 | const ACTION_DISABLED_TOOLTIP: &str = "Not supported in this app"; |
| 32 | const ACTION_BUSY_OPACITY: f32 = 0.6; |
| 33 | const ACTION_CHECK_SIZE: f32 = 14.; |
| 34 | const ACTION_ICON_GAP: f32 = 4.; |
| 35 | const NOTE_TEXT_SIZE: f32 = 12.; |
| 36 | const FACT_TITLE_WIDTH: f32 = 96.; |
| 37 | const FACT_GAP: f32 = 2.; |
| 38 | const CONTAINER_PADDING: f32 = 8.; |
| 39 | const CONTAINER_RADIUS: f32 = 6.; |
| 40 | const CONTAINER_TINT: f32 = 0.16; |
| 41 | const TEXT_LINE_HEIGHT: f32 = 1.35; |
| 42 | const IMAGE_RADIUS: f32 = 4.; |
| 43 | const IMAGE_SET_GAP: f32 = 6.; |
| 44 | const SMALL_IMAGE: f32 = 40.; |
| 45 | const MEDIUM_IMAGE: f32 = 80.; |
| 46 | const LARGE_IMAGE: f32 = 160.; |
| 47 | const EXTERNAL_IMAGE_SCHEME: &str = "https://"; |
| 48 | |
| 49 | struct CardContext<'a> { |
| 50 | cx: &'a App, |
| 51 | scope: &'a CardScope, |
| 52 | state: &'a CardState, |
| 53 | root_key: String, |
| 54 | initial_visibility: Rc<HashMap<String, bool>>, |
| 55 | inputs: Vec<CardInput>, |
| 56 | } |
| 57 | |
| 58 | impl<'a> CardContext<'a> { |
| 59 | fn new(card: &AdaptiveCard, scope: &'a CardScope, cx: &'a App) -> Self { |
| 60 | CardContext { |
| 61 | cx, |
| 62 | scope, |
| 63 | state: &cx.global::<AppHandle>().0.read(cx).cards, |
| 64 | root_key: scope.card_key(), |
| 65 | initial_visibility: Rc::new(card.element_visibility()), |
| 66 | inputs: card.own_inputs(), |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn nested(&self, card: &AdaptiveCard) -> CardContext<'a> { |
| 71 | let mut inputs = self.inputs.clone(); |
| 72 | inputs.extend(card.own_inputs()); |
| 73 | CardContext { |
| 74 | cx: self.cx, |
| 75 | scope: self.scope, |
| 76 | state: self.state, |
| 77 | root_key: self.root_key.clone(), |
| 78 | initial_visibility: self.initial_visibility.clone(), |
| 79 | inputs, |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | fn visible(&self, id: Option<&String>, initial: bool) -> bool { |
| 84 | match id { |
| 85 | Some(id) => self |
| 86 | .state |
| 87 | .is_visible(&format!("{}/{id}", self.root_key), initial), |
| 88 | None => initial, |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | pub fn cards_view( |
| 94 | cards: &[AdaptiveCard], |
| 95 | conversation_id: &str, |
| 96 | message_id: &str, |
| 97 | cx: &App, |
| 98 | ) -> Vec<AnyElement> { |
| 99 | cards |
| 100 | .iter() |
| 101 | .enumerate() |
| 102 | .map(|(index, card)| { |
| 103 | let scope = CardScope::message(conversation_id, message_id, index); |
| 104 | card_view(card, &scope, cx) |
| 105 | }) |
| 106 | .collect() |
| 107 | } |
| 108 | |
| 109 | pub fn ensure_cards_inputs( |
| 110 | app: &Entity<AppState>, |
| 111 | cards: &[AdaptiveCard], |
| 112 | conversation_id: &str, |
| 113 | message_id: &str, |
| 114 | window: &mut Window, |
| 115 | cx: &mut App, |
| 116 | ) { |
| 117 | if cards.is_empty() { |
| 118 | return; |
| 119 | } |
| 120 | app.update(cx, |state, cx| { |
| 121 | for (index, card) in cards.iter().enumerate() { |
| 122 | let scope = CardScope::message(conversation_id, message_id, index); |
| 123 | state.ensure_card_inputs(&scope, card, window, cx); |
| 124 | } |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | pub fn card_view(card: &AdaptiveCard, scope: &CardScope, cx: &App) -> AnyElement { |
| 129 | let context = CardContext::new(card, scope, cx); |
| 130 | let id = scope.card_key(); |
| 131 | let width = if card.full_width { |
| 132 | CARD_FULL_WIDTH |
| 133 | } else { |
| 134 | CARD_WIDTH |
| 135 | }; |
| 136 | let note = context.state.note(&id).map(str::to_owned); |
| 137 | let visible_actions = card |
| 138 | .actions |
| 139 | .iter() |
| 140 | .any(|action| context.visible(action.id.as_ref(), action.visible)); |
| 141 | v_flex() |
| 142 | .w(px(width)) |
| 143 | .max_w(relative(1.)) |
| 144 | .p(px(CARD_PADDING)) |
| 145 | .rounded(px(CARD_RADIUS)) |
| 146 | .bg(theme::surface()) |
| 147 | .border(px(CARD_BORDER_WIDTH)) |
| 148 | .border_color(theme::border()) |
| 149 | .text_color(theme::text_strong()) |
| 150 | .child(items_view(&card.items, &id, &context)) |
| 151 | .when(visible_actions, |card_element| { |
| 152 | card_element.child(div().mt(px(spacing_pixels(CardSpacing::Default))).child( |
| 153 | actions_view(&card.actions, &format!("{id}-actions"), &context), |
| 154 | )) |
| 155 | }) |
| 156 | .when_some(note, |card_element, note| { |
| 157 | card_element.child( |
| 158 | div() |
| 159 | .mt(px(spacing_pixels(CardSpacing::Default))) |
| 160 | .text_size(px(NOTE_TEXT_SIZE)) |
| 161 | .text_color(theme::text_muted()) |
| 162 | .child(note), |
| 163 | ) |
| 164 | }) |
| 165 | .into_any_element() |
| 166 | } |
| 167 | |
| 168 | fn items_view(items: &[CardItem], id: &str, context: &CardContext) -> Div { |
| 169 | let mut shown = 0; |
| 170 | let mut column = v_flex().w_full(); |
| 171 | for (index, item) in items.iter().enumerate() { |
| 172 | if !context.visible(item.id.as_ref(), item.visible) { |
| 173 | continue; |
| 174 | } |
| 175 | let item_id = format!("{id}-{index}"); |
| 176 | let first = shown == 0; |
| 177 | shown += 1; |
| 178 | column = column.child( |
| 179 | div() |
| 180 | .w_full() |
| 181 | .when(!first, |wrapper| { |
| 182 | wrapper.mt(px(spacing_pixels(item.spacing))) |
| 183 | }) |
| 184 | .when(!first && item.separator, |wrapper| { |
| 185 | wrapper |
| 186 | .pt(px(SEPARATOR_PADDING)) |
| 187 | .border_t_1() |
| 188 | .border_color(theme::border()) |
| 189 | }) |
| 190 | .child(element_view(&item.element, &item_id, context)), |
| 191 | ); |
| 192 | } |
| 193 | column |
| 194 | } |
| 195 | |
| 196 | fn spacing_pixels(spacing: CardSpacing) -> f32 { |
| 197 | match spacing { |
| 198 | CardSpacing::None => 0., |
| 199 | CardSpacing::Small => 4., |
| 200 | CardSpacing::Default => 8., |
| 201 | CardSpacing::Medium => 12., |
| 202 | CardSpacing::Large | CardSpacing::Padding => 16., |
| 203 | CardSpacing::ExtraLarge => 24., |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | fn element_view(element: &CardElement, id: &str, context: &CardContext) -> AnyElement { |
| 208 | match element { |
| 209 | CardElement::Text(text) => text_view(text, id, context.cx), |
| 210 | CardElement::Image(image) => image_view(image, id), |
| 211 | CardElement::ImageSet(images) => h_flex() |
| 212 | .flex_wrap() |
| 213 | .gap(px(IMAGE_SET_GAP)) |
| 214 | .children( |
| 215 | images |
| 216 | .iter() |
| 217 | .enumerate() |
| 218 | .map(|(index, image)| image_view(image, &format!("{id}-{index}"))), |
| 219 | ) |
| 220 | .into_any_element(), |
| 221 | CardElement::Columns(columns) => columns_view(columns, id, context), |
| 222 | CardElement::Container { style, items } => { |
| 223 | let tint = container_tint(*style); |
| 224 | items_view(items, id, context) |
| 225 | .when_some(tint, |container, tint| { |
| 226 | container |
| 227 | .p(px(CONTAINER_PADDING)) |
| 228 | .rounded(px(CONTAINER_RADIUS)) |
| 229 | .bg(tint) |
| 230 | }) |
| 231 | .into_any_element() |
| 232 | } |
| 233 | CardElement::Facts(facts) => facts_view(facts, id, context.cx), |
| 234 | CardElement::Actions(actions) => actions_view(actions, id, context).into_any_element(), |
| 235 | CardElement::Input(input) => input_view(input, context.scope, context.state), |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | fn container_tint(style: ContainerStyle) -> Option<Hsla> { |
| 240 | match style { |
| 241 | ContainerStyle::Default => None, |
| 242 | ContainerStyle::Emphasis => Some(theme::surface_raised()), |
| 243 | ContainerStyle::Accent => Some(theme::accent().opacity(CONTAINER_TINT)), |
| 244 | ContainerStyle::Good => Some(theme::green().opacity(CONTAINER_TINT)), |
| 245 | ContainerStyle::Warning => Some(theme::amber().opacity(CONTAINER_TINT)), |
| 246 | ContainerStyle::Attention => Some(theme::red().opacity(CONTAINER_TINT)), |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | fn text_size(size: TextSize) -> f32 { |
| 251 | match size { |
| 252 | TextSize::Small => 11.5, |
| 253 | TextSize::Default => 13.5, |
| 254 | TextSize::Medium => 15., |
| 255 | TextSize::Large => 17.5, |
| 256 | TextSize::ExtraLarge => 21., |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | fn text_color(text: &CardText) -> Hsla { |
| 261 | match text.color { |
| 262 | TextColor::Accent => theme::accent_text(), |
| 263 | TextColor::Good => theme::green(), |
| 264 | TextColor::Warning => theme::amber(), |
| 265 | TextColor::Attention => theme::red_soft(), |
| 266 | TextColor::Default | TextColor::Dark | TextColor::Light if text.subtle => { |
| 267 | theme::text_muted() |
| 268 | } |
| 269 | TextColor::Default | TextColor::Dark | TextColor::Light => theme::text_strong(), |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | fn text_view(text: &CardText, id: &str, cx: &App) -> AnyElement { |
| 274 | div() |
| 275 | .w_full() |
| 276 | .text_size(px(text_size(text.size))) |
| 277 | .line_height(relative(TEXT_LINE_HEIGHT)) |
| 278 | .text_color(text_color(text)) |
| 279 | .when(text.bold, |element| { |
| 280 | element.font_weight(FontWeight::SEMIBOLD) |
| 281 | }) |
| 282 | .when(!text.wrap, |element| element.whitespace_nowrap()) |
| 283 | .child(render_blocks(&layout_blocks(&text.spans), id, false, cx)) |
| 284 | .into_any_element() |
| 285 | } |
| 286 | |
| 287 | fn facts_view(facts: &[CardFact], id: &str, cx: &App) -> AnyElement { |
| 288 | v_flex() |
| 289 | .w_full() |
| 290 | .gap(px(FACT_GAP)) |
| 291 | .children(facts.iter().enumerate().map(|(index, fact)| { |
| 292 | h_flex() |
| 293 | .w_full() |
| 294 | .items_start() |
| 295 | .gap(px(COLUMN_GAP)) |
| 296 | .text_size(px(text_size(TextSize::Default))) |
| 297 | .line_height(relative(TEXT_LINE_HEIGHT)) |
| 298 | .child( |
| 299 | div() |
| 300 | .w(px(FACT_TITLE_WIDTH)) |
| 301 | .flex_none() |
| 302 | .font_weight(FontWeight::SEMIBOLD) |
| 303 | .text_color(theme::text_muted()) |
| 304 | .child(fact.title.clone()), |
| 305 | ) |
| 306 | .child(div().flex_1().min_w(px(0.)).child(render_blocks( |
| 307 | &layout_blocks(&fact.value), |
| 308 | &format!("{id}-{index}"), |
| 309 | false, |
| 310 | cx, |
| 311 | ))) |
| 312 | })) |
| 313 | .into_any_element() |
| 314 | } |
| 315 | |
| 316 | fn columns_view(columns: &[CardColumn], id: &str, context: &CardContext) -> AnyElement { |
| 317 | div() |
| 318 | .flex() |
| 319 | .flex_row() |
| 320 | .items_stretch() |
| 321 | .w_full() |
| 322 | .gap(px(COLUMN_GAP)) |
| 323 | .children(columns.iter().enumerate().map(|(index, column)| { |
| 324 | let content = items_view(&column.items, &format!("{id}-{index}"), context); |
| 325 | let aligned = v_flex() |
| 326 | .h_full() |
| 327 | .map(|aligned| match column.vertical_alignment { |
| 328 | VerticalAlignment::Top => aligned.justify_start(), |
| 329 | VerticalAlignment::Center => aligned.justify_center(), |
| 330 | VerticalAlignment::Bottom => aligned.justify_end(), |
| 331 | }); |
| 332 | sized_column(aligned, column.width).child(content) |
| 333 | })) |
| 334 | .into_any_element() |
| 335 | } |
| 336 | |
| 337 | fn sized_column(column: Div, width: ColumnWidth) -> Div { |
| 338 | match width { |
| 339 | ColumnWidth::Auto => column.flex_none(), |
| 340 | ColumnWidth::Stretch => column.flex_1().min_w(px(0.)), |
| 341 | ColumnWidth::Weighted(weight) => column |
| 342 | .flex_basis(px(0.)) |
| 343 | .flex_grow(weight.max(0.)) |
| 344 | .min_w(px(0.)), |
| 345 | ColumnWidth::Pixels(pixels) => column.w(px(pixels)).flex_none(), |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | fn image_view(image: &CardImage, id: &str) -> AnyElement { |
| 350 | let width = match (image.width, image.height, named_image_width(image.size)) { |
| 351 | (Some(width), _, _) => width, |
| 352 | (None, Some(height), _) => height, |
| 353 | (None, None, Some(width)) => width, |
| 354 | (None, None, None) => MEDIUM_IMAGE, |
| 355 | } |
| 356 | .min(CARD_FULL_WIDTH - 2. * CARD_PADDING); |
| 357 | let height = image.height.unwrap_or(width); |
| 358 | let radius = px(if image.person { |
| 359 | width.min(height) / 2. |
| 360 | } else { |
| 361 | IMAGE_RADIUS |
| 362 | }); |
| 363 | let frame = div() |
| 364 | .id(ElementId::Name(id.to_owned().into())) |
| 365 | .w(px(width)) |
| 366 | .h(px(height)) |
| 367 | .max_w(relative(1.)) |
| 368 | .flex_none() |
| 369 | .overflow_hidden() |
| 370 | .rounded(radius); |
| 371 | if !image.url.starts_with(EXTERNAL_IMAGE_SCHEME) { |
| 372 | return frame.bg(theme::surface_raised()).into_any_element(); |
| 373 | } |
| 374 | frame |
| 375 | .child( |
| 376 | img(image.url.clone()) |
| 377 | .size_full() |
| 378 | .rounded(radius) |
| 379 | .object_fit(ObjectFit::Cover) |
| 380 | .with_loading(move || image_placeholder(radius)) |
| 381 | .with_fallback(move || image_placeholder(radius)), |
| 382 | ) |
| 383 | .into_any_element() |
| 384 | } |
| 385 | |
| 386 | fn image_placeholder(radius: Pixels) -> AnyElement { |
| 387 | div() |
| 388 | .size_full() |
| 389 | .rounded(radius) |
| 390 | .bg(theme::surface_raised()) |
| 391 | .into_any_element() |
| 392 | } |
| 393 | |
| 394 | fn named_image_width(size: ImageSize) -> Option<f32> { |
| 395 | match size { |
| 396 | ImageSize::Small => Some(SMALL_IMAGE), |
| 397 | ImageSize::Medium => Some(MEDIUM_IMAGE), |
| 398 | ImageSize::Large => Some(LARGE_IMAGE), |
| 399 | ImageSize::Stretch => Some(CARD_WIDTH - 2. * CARD_PADDING), |
| 400 | ImageSize::Auto => None, |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | fn actions_view(actions: &[CardAction], id: &str, context: &CardContext) -> Div { |
| 405 | let open_card = context |
| 406 | .state |
| 407 | .open_card(id) |
| 408 | .and_then(|index| actions.get(index)) |
| 409 | .and_then(|action| match &action.kind { |
| 410 | CardActionKind::ShowCard(card) => Some(card), |
| 411 | _ => None, |
| 412 | }); |
| 413 | v_flex() |
| 414 | .w_full() |
| 415 | .gap(px(ACTION_GAP)) |
| 416 | .child( |
| 417 | h_flex().w_full().flex_wrap().gap(px(ACTION_GAP)).children( |
| 418 | actions |
| 419 | .iter() |
| 420 | .enumerate() |
| 421 | .filter(|(_, action)| context.visible(action.id.as_ref(), action.visible)) |
| 422 | .map(|(index, action)| action_button(action, index, id, context)), |
| 423 | ), |
| 424 | ) |
| 425 | .when_some(open_card, |column, card| { |
| 426 | let nested_id = format!("{id}-show"); |
| 427 | let nested_context = context.nested(card); |
| 428 | column.child( |
| 429 | v_flex() |
| 430 | .w_full() |
| 431 | .p(px(CONTAINER_PADDING)) |
| 432 | .gap(px(ACTION_GAP)) |
| 433 | .rounded(px(CONTAINER_RADIUS)) |
| 434 | .bg(theme::surface_raised()) |
| 435 | .child(items_view(&card.items, &nested_id, &nested_context)) |
| 436 | .when(!card.actions.is_empty(), |nested| { |
| 437 | nested.child(actions_view( |
| 438 | &card.actions, |
| 439 | &format!("{nested_id}-actions"), |
| 440 | &nested_context, |
| 441 | )) |
| 442 | }), |
| 443 | ) |
| 444 | }) |
| 445 | } |
| 446 | |
| 447 | fn action_button( |
| 448 | action: &CardAction, |
| 449 | index: usize, |
| 450 | actions_id: &str, |
| 451 | context: &CardContext, |
| 452 | ) -> Stateful<Div> { |
| 453 | let button_id = format!("{actions_id}-{index}"); |
| 454 | let phase = context.state.phase(&button_id).cloned(); |
| 455 | let label = action_label( |
| 456 | action, |
| 457 | &phase, |
| 458 | context.state.open_card(actions_id) == Some(index), |
| 459 | ); |
| 460 | let button = div() |
| 461 | .id(ElementId::Name(button_id.clone().into())) |
| 462 | .px(px(12.)) |
| 463 | .py(px(6.)) |
| 464 | .rounded(px(ACTION_RADIUS)) |
| 465 | .border_1() |
| 466 | .border_color(match phase { |
| 467 | Some(ActionPhase::Failed(_)) => theme::red(), |
| 468 | _ => theme::border_strong(), |
| 469 | }) |
| 470 | .text_size(px(ACTION_TEXT_SIZE)) |
| 471 | .font_weight(FontWeight::SEMIBOLD) |
| 472 | .child(label); |
| 473 | if !action.is_clickable() { |
| 474 | return button |
| 475 | .text_color(theme::text_muted()) |
| 476 | .opacity(ACTION_DISABLED_OPACITY) |
| 477 | .when(action.enabled, |button| { |
| 478 | button.tooltip(|window, cx| Tooltip::new(ACTION_DISABLED_TOOLTIP).build(window, cx)) |
| 479 | }) |
| 480 | .on_click(|_, _, cx| cx.stop_propagation()); |
| 481 | } |
| 482 | let enabled = button.text_color(theme::accent_text()); |
| 483 | if phase == Some(ActionPhase::Busy) { |
| 484 | return enabled |
| 485 | .opacity(ACTION_BUSY_OPACITY) |
| 486 | .on_click(|_, _, cx| cx.stop_propagation()); |
| 487 | } |
| 488 | let enabled = enabled |
| 489 | .cursor_pointer() |
| 490 | .hover(|button| button.bg(theme::row_hover())) |
| 491 | .when_some(failure_reason(&phase), |button, reason| { |
| 492 | button.tooltip(move |window, cx| Tooltip::new(reason.clone()).build(window, cx)) |
| 493 | }); |
| 494 | let scope = context.scope.clone(); |
| 495 | match &action.kind { |
| 496 | CardActionKind::OpenUrl(url) => { |
| 497 | let url = url.clone(); |
| 498 | enabled.on_click(move |_, _, cx| { |
| 499 | cx.stop_propagation(); |
| 500 | cx.open_url(&url); |
| 501 | }) |
| 502 | } |
| 503 | CardActionKind::Submit(_) | CardActionKind::Execute(_) => { |
| 504 | let action = action.clone(); |
| 505 | let inputs = context.inputs.clone(); |
| 506 | enabled.on_click(move |_, _, cx| { |
| 507 | cx.stop_propagation(); |
| 508 | let (scope, action, button_id, inputs) = ( |
| 509 | scope.clone(), |
| 510 | action.clone(), |
| 511 | button_id.clone(), |
| 512 | inputs.clone(), |
| 513 | ); |
| 514 | cx.global::<AppHandle>().0.clone().update(cx, |state, cx| { |
| 515 | state.run_card_action(scope, button_id, action, inputs, cx) |
| 516 | }); |
| 517 | }) |
| 518 | } |
| 519 | CardActionKind::ShowCard(_) => { |
| 520 | let actions_id = actions_id.to_owned(); |
| 521 | enabled.on_click(move |_, _, cx| { |
| 522 | cx.stop_propagation(); |
| 523 | let (scope, actions_id) = (scope.clone(), actions_id.clone()); |
| 524 | cx.global::<AppHandle>().0.clone().update(cx, |state, cx| { |
| 525 | state.toggle_show_card(&scope, &actions_id, index, cx) |
| 526 | }); |
| 527 | }) |
| 528 | } |
| 529 | CardActionKind::ToggleVisibility(targets) => { |
| 530 | let elements: Vec<(String, bool, Option<bool>)> = targets |
| 531 | .iter() |
| 532 | .map(|target| { |
| 533 | let initial = context |
| 534 | .initial_visibility |
| 535 | .get(&target.element_id) |
| 536 | .copied() |
| 537 | .unwrap_or(true); |
| 538 | (target.element_id.clone(), initial, target.visible) |
| 539 | }) |
| 540 | .collect(); |
| 541 | let root_key = context.root_key.clone(); |
| 542 | enabled.on_click(move |_, _, cx| { |
| 543 | cx.stop_propagation(); |
| 544 | let (scope, root_key, elements) = |
| 545 | (scope.clone(), root_key.clone(), elements.clone()); |
| 546 | cx.global::<AppHandle>().0.clone().update(cx, |state, cx| { |
| 547 | state.toggle_card_elements(&scope, &root_key, elements, cx) |
| 548 | }); |
| 549 | }) |
| 550 | } |
| 551 | CardActionKind::Unsupported => enabled, |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | fn failure_reason(phase: &Option<ActionPhase>) -> Option<String> { |
| 556 | match phase { |
| 557 | Some(ActionPhase::Failed(reason)) => Some(reason.clone()), |
| 558 | _ => None, |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | fn action_label(action: &CardAction, phase: &Option<ActionPhase>, open: bool) -> AnyElement { |
| 563 | match phase { |
| 564 | Some(ActionPhase::Busy) => format!("{}...", action.title).into_any_element(), |
| 565 | Some(ActionPhase::Done) => h_flex() |
| 566 | .gap(px(ACTION_ICON_GAP)) |
| 567 | .items_center() |
| 568 | .child(symbol("done", ACTION_CHECK_SIZE, theme::green())) |
| 569 | .child(action.title.clone()) |
| 570 | .into_any_element(), |
| 571 | _ if open => format!("{} ^", action.title).into_any_element(), |
| 572 | _ => action.title.clone().into_any_element(), |
| 573 | } |
| 574 | } |