BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / render / elements.rs

elements.rs

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