| 1 | use gpui_kit::assets::IconName; |
| 2 | use gpui_kit::component::tooltip::Tooltip; |
| 3 | use gpui_kit::component::{ActiveTheme as _, h_flex, v_flex}; |
| 4 | use gpui_kit::prelude::FluentBuilder as _; |
| 5 | use gpui_kit::*; |
| 6 | use teams_core::CardCodeBlock; |
| 7 | |
| 8 | use super::adaptive_card::{CardContext, slot_handler}; |
| 9 | use super::widgets::icon; |
| 10 | use crate::theme; |
| 11 | |
| 12 | const COLLAPSED_LINES: usize = 10; |
| 13 | const CODE_TEXT_SIZE: f32 = 12.5; |
| 14 | const CODE_LINE_HEIGHT: f32 = 1.45; |
| 15 | const CODE_RADIUS: f32 = 6.; |
| 16 | const CODE_PADDING: f32 = 8.; |
| 17 | const HEADER_PADDING_X: f32 = 10.; |
| 18 | const HEADER_PADDING_Y: f32 = 4.; |
| 19 | const GUTTER_DIGIT_WIDTH: f32 = 8.; |
| 20 | const GUTTER_GAP: f32 = 12.; |
| 21 | const COPY_ICON_SIZE: f32 = 14.; |
| 22 | const TAB_REPLACEMENT: &str = " "; |
| 23 | const COPY_TOOLTIP: &str = "Copy code"; |
| 24 | |
| 25 | pub(super) fn code_block_view( |
| 26 | block: &CardCodeBlock, |
| 27 | id: &str, |
| 28 | context: &CardContext, |
| 29 | ) -> AnyElement { |
| 30 | let lines: Vec<String> = block |
| 31 | .code |
| 32 | .lines() |
| 33 | .map(|line| line.replace('\t', TAB_REPLACEMENT)) |
| 34 | .collect(); |
| 35 | let expanded_key = format!("{id}-expanded"); |
| 36 | let expanded = context.state.slot(&expanded_key, 0) == 1; |
| 37 | let hidden = lines.len().saturating_sub(COLLAPSED_LINES); |
| 38 | let shown = if expanded { |
| 39 | lines.len() |
| 40 | } else { |
| 41 | lines.len().min(COLLAPSED_LINES) |
| 42 | }; |
| 43 | let last_number = block.start_line + lines.len().saturating_sub(1); |
| 44 | let gutter_width = last_number.to_string().len() as f32 * GUTTER_DIGIT_WIDTH; |
| 45 | let code = block.code.clone(); |
| 46 | let toggle = slot_handler(context, &expanded_key, usize::from(!expanded)); |
| 47 | v_flex() |
| 48 | .w_full() |
| 49 | .overflow_hidden() |
| 50 | .rounded(px(CODE_RADIUS)) |
| 51 | .bg(theme::code_surface()) |
| 52 | .border_1() |
| 53 | .border_color(theme::code_header_border()) |
| 54 | .child( |
| 55 | h_flex() |
| 56 | .w_full() |
| 57 | .justify_between() |
| 58 | .items_center() |
| 59 | .px(px(HEADER_PADDING_X)) |
| 60 | .py(px(HEADER_PADDING_Y)) |
| 61 | .border_b_1() |
| 62 | .border_color(theme::code_header_border()) |
| 63 | .text_size(px(11.5)) |
| 64 | .text_color(theme::text_muted()) |
| 65 | .child(block.language.clone().unwrap_or_default()) |
| 66 | .child( |
| 67 | div() |
| 68 | .id(ElementId::Name(format!("{id}-copy").into())) |
| 69 | .cursor_pointer() |
| 70 | .p(px(2.)) |
| 71 | .rounded(px(4.)) |
| 72 | .hover(|button| button.bg(theme::row_hover())) |
| 73 | .tooltip(|window, cx| Tooltip::new(COPY_TOOLTIP).build(window, cx)) |
| 74 | .on_click(move |_, _, cx| { |
| 75 | cx.stop_propagation(); |
| 76 | cx.write_to_clipboard(ClipboardItem::new_string(code.clone())); |
| 77 | }) |
| 78 | .child(icon(IconName::Copy, COPY_ICON_SIZE, theme::text_muted())), |
| 79 | ), |
| 80 | ) |
| 81 | .child( |
| 82 | v_flex() |
| 83 | .w_full() |
| 84 | .p(px(CODE_PADDING)) |
| 85 | .font_family(context.cx.theme().mono_font_family.clone()) |
| 86 | .text_size(px(CODE_TEXT_SIZE)) |
| 87 | .line_height(relative(CODE_LINE_HEIGHT)) |
| 88 | .children(lines.iter().take(shown).enumerate().map(|(offset, line)| { |
| 89 | h_flex() |
| 90 | .w_full() |
| 91 | .gap(px(GUTTER_GAP)) |
| 92 | .child( |
| 93 | div() |
| 94 | .w(px(gutter_width)) |
| 95 | .flex_none() |
| 96 | .text_right() |
| 97 | .text_color(theme::text_faint()) |
| 98 | .child((block.start_line + offset).to_string()), |
| 99 | ) |
| 100 | .child( |
| 101 | div() |
| 102 | .flex_1() |
| 103 | .min_w(px(0.)) |
| 104 | .overflow_hidden() |
| 105 | .whitespace_nowrap() |
| 106 | .text_color(theme::text_strong()) |
| 107 | .child(if line.is_empty() { |
| 108 | " ".to_owned() |
| 109 | } else { |
| 110 | line.clone() |
| 111 | }), |
| 112 | ) |
| 113 | })), |
| 114 | ) |
| 115 | .when(hidden > 0, |frame| { |
| 116 | frame.child( |
| 117 | div() |
| 118 | .id(ElementId::Name(format!("{id}-expand").into())) |
| 119 | .w_full() |
| 120 | .px(px(HEADER_PADDING_X)) |
| 121 | .py(px(HEADER_PADDING_Y)) |
| 122 | .border_t_1() |
| 123 | .border_color(theme::code_header_border()) |
| 124 | .text_size(px(12.)) |
| 125 | .text_color(theme::accent_text()) |
| 126 | .cursor_pointer() |
| 127 | .hover(|footer| footer.bg(theme::row_hover())) |
| 128 | .on_click(move |_, _, cx| { |
| 129 | cx.stop_propagation(); |
| 130 | toggle(cx) |
| 131 | }) |
| 132 | .child(if expanded { |
| 133 | "Collapse".to_owned() |
| 134 | } else { |
| 135 | format!("Expand ({hidden} more lines)") |
| 136 | }), |
| 137 | ) |
| 138 | }) |
| 139 | .into_any_element() |
| 140 | } |