BlueGit

ict rusty-teams

Files

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

draft_style.rs

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