BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-ui-kit / src / draft_style.rs

draft_style.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
draft_style.rs 9.6 KiB Download Raw
1use std::ops::Range;
2
3use gpui_kit::component::input::{RangeDecoration, RangeDecorationStyle, TextDecoration};
4use gpui_kit::*;
5use teams_core::{Draft, FontSize, LineKind, MarkKind, SizeStep};
6
7use crate::render::blocks::{Script, StyleFlags};
8use crate::render::elements::{highlight_fill, readable_on};
9use app_base::theme;
10
11const CODE_PILL_RADIUS: f32 = 4.;
12const CODE_BLOCK_RADIUS: f32 = 6.;
13const QUOTE_BAR_WIDTH: f32 = 3.;
14const QUOTE_INDENT: f32 = 12.;
15const CODE_INDENT: f32 = 10.;
16const LIST_LEVEL_INDENT: f32 = 18.;
17const HEADING_SCALES: [f32; 3] = [1.6, 1.35, 1.15];
18const INDENT_STEP: f32 = teams_core::INDENT_PIXELS as f32;
19const HIGHLIGHT_RADIUS: f32 = 3.;
20
21/// How the composer paints a draft: text styles, fills and the list markers to hang under.
22pub struct DraftStyle {
23 pub text: Vec<TextDecoration>,
24 pub ranges: Vec<RangeDecoration>,
25 pub markers: Vec<Range<usize>>,
26 pub indents: Vec<(usize, Pixels)>,
27}
28
29fn line_indent(kind: &LineKind) -> f32 {
30 match kind {
31 LineKind::Text | LineKind::Heading(_) | LineKind::Table(_) | LineKind::Rule => 0.,
32 LineKind::Indent(level) => INDENT_STEP * f32::from(*level),
33 LineKind::Quote => QUOTE_INDENT,
34 LineKind::Code(_) => CODE_INDENT,
35 LineKind::Bullet(depth) | LineKind::Numbered(depth) => {
36 LIST_LEVEL_INDENT * f32::from(*depth)
37 }
38 }
39}
40
41fn color(color: Hsla) -> HighlightStyle {
42 HighlightStyle {
43 color: Some(color),
44 ..Default::default()
45 }
46}
47
48fn underline(color: Option<Hsla>) -> UnderlineStyle {
49 UnderlineStyle {
50 thickness: px(1.),
51 color,
52 wavy: false,
53 }
54}
55
56fn mark_style(kind: &MarkKind) -> HighlightStyle {
57 let mut style = HighlightStyle::default();
58 match kind {
59 MarkKind::Bold => style.font_weight = Some(FontWeight::BOLD),
60 MarkKind::Italic => style.font_style = Some(FontStyle::Italic),
61 MarkKind::Underline => style.underline = Some(underline(None)),
62 MarkKind::Strike => {
63 style.color = Some(theme::text_muted());
64 style.strikethrough = Some(StrikethroughStyle {
65 thickness: px(1.),
66 color: None,
67 });
68 }
69 MarkKind::Color(value) => {
70 style.color = Some(readable_on(*value, theme::surface_raised()));
71 }
72 MarkKind::Superscript
73 | MarkKind::Subscript
74 | MarkKind::Size(_)
75 | MarkKind::Highlight(_) => {}
76 MarkKind::Code => style.color = Some(theme::text_strong()),
77 MarkKind::Link(_) => {
78 style.color = Some(theme::accent_text());
79 style.underline = Some(underline(Some(theme::accent_text())));
80 }
81 }
82 style
83}
84
85fn size_flags(kind: &MarkKind) -> StyleFlags {
86 let mut flags = StyleFlags::default();
87 match kind {
88 MarkKind::Superscript => flags.script = Some(Script::Super),
89 MarkKind::Subscript => flags.script = Some(Script::Sub),
90 MarkKind::Size(SizeStep::Small) => flags.size = Some(FontSize::Small),
91 MarkKind::Size(SizeStep::Large) => flags.size = Some(FontSize::Large),
92 _ => {}
93 }
94 flags
95}
96
97fn heading_decoration(content: Range<usize>, level: u8) -> TextDecoration {
98 let scale = HEADING_SCALES[usize::from(level.clamp(1, 3) - 1)];
99 let style = HighlightStyle {
100 font_weight: Some(FontWeight::BOLD),
101 color: Some(theme::text_strong()),
102 ..Default::default()
103 };
104 TextDecoration::new(content, style).with_font_scale(scale)
105}
106
107/// From the start of the first line of `lines` to the end of the last; Block and Bar cover
108/// every line starting inside it, ends included, so empty lines count too.
109fn block_range(draft: &Draft, lines: Range<usize>) -> Range<usize> {
110 let line_ranges = draft.line_ranges();
111 line_ranges[lines.start].start..line_ranges[lines.end - 1].end
112}
113
114pub fn draft_style(draft: &Draft, mono: SharedString) -> DraftStyle {
115 let mut text = Vec::new();
116 let mut ranges = Vec::new();
117 let mut markers = Vec::new();
118 let indents = draft
119 .line_ranges()
120 .into_iter()
121 .zip(draft.lines())
122 .map(|(line, kind)| (line.start, px(line_indent(kind))))
123 .filter(|(_, indent)| *indent > px(0.))
124 .collect();
125 for mark in draft.marks() {
126 let mut decoration = TextDecoration::new(mark.range.clone(), mark_style(&mark.kind));
127 let flags = size_flags(&mark.kind);
128 if flags.is_flowed() {
129 decoration = decoration.with_font_scale(flags.font_scale());
130 if flags.baseline_raise() != 0. {
131 decoration = decoration.with_baseline_shift(flags.baseline_raise());
132 }
133 }
134 if let MarkKind::Highlight(value) = mark.kind {
135 ranges.push(
136 RangeDecoration::new(mark.range.clone())
137 .with_style(RangeDecorationStyle::Pill)
138 .with_color(highlight_fill(value))
139 .with_radius(px(HIGHLIGHT_RADIUS)),
140 );
141 }
142 if mark.kind == MarkKind::Code {
143 text.push(decoration.with_font_family(mono.clone()));
144 ranges.push(
145 RangeDecoration::new(mark.range.clone())
146 .with_style(RangeDecorationStyle::Pill)
147 .with_color(theme::inline_code_fill(false))
148 .with_border(theme::inline_code_border())
149 .with_radius(px(CODE_PILL_RADIUS)),
150 );
151 } else {
152 text.push(decoration);
153 }
154 }
155 let kinds = draft.lines();
156 let mut index = 0;
157 while index < kinds.len() {
158 let kind = &kinds[index];
159 let run_end = index
160 + kinds[index..]
161 .iter()
162 .take_while(|other| *other == kind)
163 .count();
164 for line in index..run_end {
165 let marker = draft.marker_range(line);
166 let content = draft.content_range(line);
167 if !marker.is_empty() {
168 markers.push(marker.clone());
169 if kind.is_list() {
170 text.push(TextDecoration::new(marker, color(theme::text_soft())));
171 }
172 }
173 if content.is_empty() {
174 continue;
175 }
176 match kind {
177 LineKind::Quote => {
178 text.push(TextDecoration::new(content, color(theme::text_soft())))
179 }
180 LineKind::Code(_) => text.push(
181 TextDecoration::new(content, color(theme::text_strong()))
182 .with_font_family(mono.clone()),
183 ),
184 LineKind::Heading(level) => text.push(heading_decoration(content, *level)),
185 _ => {}
186 }
187 }
188 let block = match kind {
189 LineKind::Code(_) => Some(
190 RangeDecoration::new(block_range(draft, index..run_end))
191 .with_style(RangeDecorationStyle::Block)
192 .with_color(theme::code_surface())
193 .with_radius(px(CODE_BLOCK_RADIUS)),
194 ),
195 LineKind::Quote => Some(
196 RangeDecoration::new(block_range(draft, index..run_end))
197 .with_style(RangeDecorationStyle::Bar)
198 .with_color(theme::border_strong())
199 .with_radius(px(QUOTE_BAR_WIDTH)),
200 ),
201 _ => None,
202 };
203 ranges.extend(block);
204 index = run_end;
205 }
206 DraftStyle {
207 text,
208 ranges,
209 markers,
210 indents,
211 }
212}
213
214#[cfg(test)]
215mod tests {
216 use gpui_kit::component::input::RangeDecorationStyle;
217 use teams_core::{Draft, DraftLine, LineKind};
218
219 use super::{block_range, draft_style};
220
221 fn line(kind: LineKind, content: &str) -> DraftLine {
222 DraftLine {
223 kind,
224 number: None,
225 content: content.into(),
226 marks: Vec::new(),
227 }
228 }
229
230 #[test]
231 fn list_markers_hang_and_code_gets_a_pill() {
232 let draft = Draft::from_markdown("- one `x`\n```\nfn\n```");
233 let style = draft_style(&draft, "Mono".into());
234 assert_eq!(style.markers, [draft.marker_range(0)]);
235 assert_eq!(
236 style
237 .ranges
238 .iter()
239 .map(|range| range.style())
240 .collect::<Vec<_>>(),
241 [RangeDecorationStyle::Pill, RangeDecorationStyle::Block]
242 );
243 }
244
245 #[test]
246 fn nesting_quotes_and_code_become_line_indents() {
247 let draft = Draft::from_markdown("- a\n - b\n> q\n```\nx\n```");
248 let style = draft_style(&draft, "Mono".into());
249 let starts = draft.line_ranges();
250 assert_eq!(
251 style.indents,
252 [
253 (starts[1].start, gpui_kit::px(18.)),
254 (starts[2].start, gpui_kit::px(12.)),
255 (starts[3].start, gpui_kit::px(10.)),
256 ]
257 );
258 assert!(!draft.text().contains('\u{2003}'));
259 }
260
261 #[test]
262 fn a_code_block_covers_its_empty_last_line_before_text() {
263 let draft = Draft::from_lines(&[
264 line(LineKind::Code(None), "x"),
265 line(LineKind::Code(None), ""),
266 line(LineKind::Text, "after"),
267 ]);
268 let block = block_range(&draft, 0..2);
269 let empty_line_start = draft.line_ranges()[1].start;
270 assert!(block.contains(&empty_line_start) || block.end == empty_line_start);
271 assert!(block.end < draft.line_ranges()[2].start);
272 }
273
274 #[test]
275 fn a_lone_code_line_still_gets_a_block() {
276 let draft = Draft::from_lines(&[line(LineKind::Code(None), "")]);
277 let style = draft_style(&draft, "Mono".into());
278 assert_eq!(style.ranges.len(), 1);
279 assert_eq!(style.ranges[0].style(), RangeDecorationStyle::Block);
280 }
281}