BlueGit

ict rusty-teams

Files

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

elements.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 History
elements.rs 5.6 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 if inline.segments.iter().any(|segment| segment.style.mention) {
69 return render_inline_with_chips(inline, &id, own);
70 }
71 let highlights = inline
72 .segments
73 .iter()
74 .filter(|segment| !segment.style.is_plain())
75 .map(|segment| (segment.range.clone(), highlight_for(segment.style, own)))
76 .collect::<Vec<_>>();
77 let styled = StyledText::new(inline.text.clone()).with_highlights(highlights);
78 let links = inline.links();
79 if links.is_empty() {
80 return div()
81 .w_full()
82 .text_color(theme::text())
83 .child(styled)
84 .into_any_element();
85 }
86 let ranges = links
87 .iter()
88 .map(|(range, _)| range.clone())
89 .collect::<Vec<_>>();
90 let urls = links.into_iter().map(|(_, url)| url).collect::<Vec<_>>();
91 div()
92 .w_full()
93 .text_color(theme::text())
94 .child(
95 InteractiveText::new(ElementId::Name(id.into()), styled).on_click(
96 ranges,
97 move |range_index, _, cx| {
98 if let Some(url) = urls.get(range_index) {
99 cx.open_url(url);
100 }
101 },
102 ),
103 )
104 .into_any_element()
105}
106
107fn highlight_for(style: StyleFlags, own: bool) -> HighlightStyle {
108 let mut highlight = HighlightStyle::default();
109 if style.bold {
110 highlight.font_weight = Some(FontWeight::BOLD);
111 }
112 if style.italic {
113 highlight.font_style = Some(FontStyle::Italic);
114 }
115 if style.code {
116 highlight.background_color = Some(theme::code_background());
117 }
118 if style.link {
119 let color = if own {
120 theme::accent_soft()
121 } else {
122 theme::accent_text()
123 };
124 highlight.color = Some(color);
125 highlight.underline = Some(UnderlineStyle {
126 thickness: px(1.),
127 color: Some(color),
128 wavy: false,
129 });
130 }
131 highlight
132}
133
134fn render_inline_with_chips(inline: &Inline, id: &str, own: bool) -> AnyElement {
135 let mut pieces: Vec<AnyElement> = Vec::new();
136 for (segment_index, segment) in inline.segments.iter().enumerate() {
137 let text = &inline.text[segment.range.clone()];
138 if segment.style.mention {
139 pieces.push(mention_chip(text.trim(), own));
140 continue;
141 }
142 for (word_index, word) in text.split_inclusive(' ').enumerate() {
143 let mut piece = div()
144 .id(ElementId::Name(
145 format!("{id}-{segment_index}-{word_index}").into(),
146 ))
147 .whitespace_nowrap()
148 .child(StyledText::new(word.to_owned()).with_highlights([(
149 0..word.len(),
150 highlight_for(segment.style, own),
151 )]));
152 if let Some(url) = segment.link.clone() {
153 piece = piece
154 .cursor_pointer()
155 .on_click(move |_, _, cx| cx.open_url(&url));
156 }
157 pieces.push(piece.into_any_element());
158 }
159 }
160 div()
161 .w_full()
162 .flex()
163 .flex_wrap()
164 .items_center()
165 .text_color(theme::text())
166 .children(pieces)
167 .into_any_element()
168}
169
170fn mention_chip(text: &str, own: bool) -> AnyElement {
171 div()
172 .mx(px(1.))
173 .px(px(6.))
174 .rounded(px(6.))
175 .bg(theme::mention_background(own))
176 .text_color(theme::mention_text())
177 .font_weight(FontWeight::SEMIBOLD)
178 .whitespace_nowrap()
179 .child(text.to_owned())
180 .into_any_element()
181}