| 1 | use gpui_kit::component::{h_flex, v_flex}; |
| 2 | use gpui_kit::*; |
| 3 | |
| 4 | use super::blocks::{Block, Inline, StyleFlags}; |
| 5 | use super::selectable::SelectableRichText; |
| 6 | use crate::theme; |
| 7 | |
| 8 | pub fn render_blocks(blocks: &[Block], id: &str, own: bool, cx: &App) -> AnyElement { |
| 9 | v_flex() |
| 10 | .w_full() |
| 11 | .gap(px(6.)) |
| 12 | .children( |
| 13 | blocks |
| 14 | .iter() |
| 15 | .enumerate() |
| 16 | .map(|(index, block)| render_block(block, format!("{id}-{index}"), own, cx)), |
| 17 | ) |
| 18 | .into_any_element() |
| 19 | } |
| 20 | |
| 21 | fn render_block(block: &Block, id: String, own: bool, cx: &App) -> AnyElement { |
| 22 | match block { |
| 23 | Block::Paragraph(inline) => render_inline(inline, id, own), |
| 24 | Block::Code(code) => div() |
| 25 | .w_full() |
| 26 | .px(px(10.)) |
| 27 | .py(px(8.)) |
| 28 | .rounded(px(6.)) |
| 29 | .bg(theme::code_background()) |
| 30 | .border_1() |
| 31 | .border_color(theme::border_strong()) |
| 32 | .text_color(theme::text_strong()) |
| 33 | .font_family(code_font(cx)) |
| 34 | .text_size(px(12.5)) |
| 35 | .line_height(relative(1.5)) |
| 36 | .child(SelectableRichText::new( |
| 37 | ElementId::Name(id.into()), |
| 38 | code.clone(), |
| 39 | Vec::new(), |
| 40 | )) |
| 41 | .into_any_element(), |
| 42 | Block::Quote(children) => div() |
| 43 | .w_full() |
| 44 | .px(px(8.)) |
| 45 | .py(px(3.)) |
| 46 | .border_l(px(3.)) |
| 47 | .border_color(theme::accent()) |
| 48 | .text_size(px(12.5)) |
| 49 | .text_color(theme::text_muted()) |
| 50 | .child(render_blocks(children, &id, own, cx)) |
| 51 | .into_any_element(), |
| 52 | Block::ListItem(inline) => h_flex() |
| 53 | .w_full() |
| 54 | .gap(px(8.)) |
| 55 | .items_start() |
| 56 | .child(div().child("\u{2022}")) |
| 57 | .child( |
| 58 | div() |
| 59 | .flex_1() |
| 60 | .min_w_0() |
| 61 | .child(render_inline(inline, id, own)), |
| 62 | ) |
| 63 | .into_any_element(), |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | fn code_font(cx: &App) -> SharedString { |
| 68 | use gpui_kit::component::ActiveTheme as _; |
| 69 | cx.theme().mono_font_family.clone() |
| 70 | } |
| 71 | |
| 72 | fn render_inline(inline: &Inline, id: String, own: bool) -> AnyElement { |
| 73 | let highlights = inline |
| 74 | .segments |
| 75 | .iter() |
| 76 | .filter(|segment| !segment.style.is_plain()) |
| 77 | .map(|segment| (segment.range.clone(), highlight_for(segment.style, own))) |
| 78 | .collect::<Vec<_>>(); |
| 79 | div() |
| 80 | .w_full() |
| 81 | .text_color(theme::text()) |
| 82 | .child( |
| 83 | SelectableRichText::new(ElementId::Name(id.into()), inline.text.clone(), highlights) |
| 84 | .links(inline.links()), |
| 85 | ) |
| 86 | .into_any_element() |
| 87 | } |
| 88 | |
| 89 | fn highlight_for(style: StyleFlags, own: bool) -> HighlightStyle { |
| 90 | let mut highlight = HighlightStyle::default(); |
| 91 | if style.bold { |
| 92 | highlight.font_weight = Some(FontWeight::BOLD); |
| 93 | } |
| 94 | if style.italic { |
| 95 | highlight.font_style = Some(FontStyle::Italic); |
| 96 | } |
| 97 | if style.code { |
| 98 | highlight.background_color = Some(theme::code_background()); |
| 99 | } |
| 100 | let accent = if own { |
| 101 | theme::accent_soft() |
| 102 | } else { |
| 103 | theme::accent_text() |
| 104 | }; |
| 105 | if style.mention { |
| 106 | highlight.font_weight = Some(FontWeight::BOLD); |
| 107 | highlight.color = Some(accent); |
| 108 | } |
| 109 | if style.link { |
| 110 | highlight.color = Some(accent); |
| 111 | highlight.underline = Some(UnderlineStyle { |
| 112 | thickness: px(1.), |
| 113 | color: Some(accent), |
| 114 | wavy: false, |
| 115 | }); |
| 116 | } |
| 117 | highlight |
| 118 | } |