BlueGit

ict rusty-teams

Files

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

draft_style.rs

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