BlueGit

ict rusty-teams

Files

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

draft_style.rs

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 | MarkKind::Subscript | MarkKind::Size(_) | MarkKind::Highlight(_) => {}
73 MarkKind::Code => style.color = Some(theme::text_strong()),
74 MarkKind::Link(_) => {
75 style.color = Some(theme::accent_text());
76 style.underline = Some(underline(Some(theme::accent_text())));
77 }
78 }
79 style
80}
81
82fn size_flags(kind: &MarkKind) -> StyleFlags {
83 let mut flags = StyleFlags::default();
84 match kind {
85 MarkKind::Superscript => flags.script = Some(Script::Super),
86 MarkKind::Subscript => flags.script = Some(Script::Sub),
87 MarkKind::Size(SizeStep::Small) => flags.size = Some(FontSize::Small),
88 MarkKind::Size(SizeStep::Large) => flags.size = Some(FontSize::Large),
89 _ => {}
90 }
91 flags
92}
93
94fn heading_decoration(content: Range<usize>, level: u8) -> TextDecoration {
95 let scale = HEADING_SCALES[usize::from(level.clamp(1, 3) - 1)];
96 let style = HighlightStyle {
97 font_weight: Some(FontWeight::BOLD),
98 color: Some(theme::text_strong()),
99 ..Default::default()
100 };
101 TextDecoration::new(content, style).with_font_scale(scale)
102}
103
104/// From the start of the first line of `lines` to the end of the last; Block and Bar cover
105/// every line starting inside it, ends included, so empty lines count too.
106fn block_range(draft: &Draft, lines: Range<usize>) -> Range<usize> {
107 let line_ranges = draft.line_ranges();
108 line_ranges[lines.start].start..line_ranges[lines.end - 1].end
109}
110
111pub fn draft_style(draft: &Draft, mono: SharedString) -> DraftStyle {
112 let mut text = Vec::new();
113 let mut ranges = Vec::new();
114 let mut markers = Vec::new();
115 let indents = draft
116 .line_ranges()
117 .into_iter()
118 .zip(draft.lines())
119 .map(|(line, kind)| (line.start, px(line_indent(kind))))
120 .filter(|(_, indent)| *indent > px(0.))
121 .collect();
122 for mark in draft.marks() {
123 let mut decoration = TextDecoration::new(mark.range.clone(), mark_style(&mark.kind));
124 let flags = size_flags(&mark.kind);
125 if flags.is_flowed() {
126 decoration = decoration.with_font_scale(flags.font_scale());
127 if flags.baseline_raise() != 0. {
128 decoration = decoration.with_baseline_shift(flags.baseline_raise());
129 }
130 }
131 if let MarkKind::Highlight(value) = mark.kind {
132 ranges.push(
133 RangeDecoration::new(mark.range.clone())
134 .with_style(RangeDecorationStyle::Pill)
135 .with_color(highlight_fill(value))
136 .with_radius(px(HIGHLIGHT_RADIUS)),
137 );
138 }
139 if mark.kind == MarkKind::Code {
140 text.push(decoration.with_font_family(mono.clone()));
141 ranges.push(
142 RangeDecoration::new(mark.range.clone())
143 .with_style(RangeDecorationStyle::Pill)
144 .with_color(theme::inline_code_fill(false))
145 .with_border(theme::inline_code_border())
146 .with_radius(px(CODE_PILL_RADIUS)),
147 );
148 } else {
149 text.push(decoration);
150 }
151 }
152 let kinds = draft.lines();
153 let mut index = 0;
154 while index < kinds.len() {
155 let kind = &kinds[index];
156 let run_end = index
157 + kinds[index..]
158 .iter()
159 .take_while(|other| *other == kind)
160 .count();
161 for line in index..run_end {
162 let marker = draft.marker_range(line);
163 let content = draft.content_range(line);
164 if !marker.is_empty() {
165 markers.push(marker.clone());
166 if kind.is_list() {
167 text.push(TextDecoration::new(marker, color(theme::text_soft())));
168 }
169 }
170 if content.is_empty() {
171 continue;
172 }
173 match kind {
174 LineKind::Quote => {
175 text.push(TextDecoration::new(content, color(theme::text_soft())))
176 }
177 LineKind::Code(_) => text.push(
178 TextDecoration::new(content, color(theme::text_strong()))
179 .with_font_family(mono.clone()),
180 ),
181 LineKind::Heading(level) => text.push(heading_decoration(content, *level)),
182 _ => {}
183 }
184 }
185 let block = match kind {
186 LineKind::Code(_) => Some(
187 RangeDecoration::new(block_range(draft, index..run_end))
188 .with_style(RangeDecorationStyle::Block)
189 .with_color(theme::code_surface())
190 .with_radius(px(CODE_BLOCK_RADIUS)),
191 ),
192 LineKind::Quote => Some(
193 RangeDecoration::new(block_range(draft, index..run_end))
194 .with_style(RangeDecorationStyle::Bar)
195 .with_color(theme::border_strong())
196 .with_radius(px(QUOTE_BAR_WIDTH)),
197 ),
198 _ => None,
199 };
200 ranges.extend(block);
201 index = run_end;
202 }
203 DraftStyle {
204 text,
205 ranges,
206 markers,
207 indents,
208 }
209}
210
211#[cfg(test)]
212mod tests {
213 use gpui_kit::component::input::RangeDecorationStyle;
214 use teams_core::{Draft, DraftLine, LineKind};
215
216 use super::{block_range, draft_style};
217
218 fn line(kind: LineKind, content: &str) -> DraftLine {
219 DraftLine {
220 kind,
221 number: None,
222 content: content.into(),
223 marks: Vec::new(),
224 }
225 }
226
227 #[test]
228 fn list_markers_hang_and_code_gets_a_pill() {
229 let draft = Draft::from_markdown("- one `x`\n```\nfn\n```");
230 let style = draft_style(&draft, "Mono".into());
231 assert_eq!(style.markers, [draft.marker_range(0)]);
232 assert_eq!(
233 style
234 .ranges
235 .iter()
236 .map(|range| range.style())
237 .collect::<Vec<_>>(),
238 [RangeDecorationStyle::Pill, RangeDecorationStyle::Block]
239 );
240 }
241
242 #[test]
243 fn nesting_quotes_and_code_become_line_indents() {
244 let draft = Draft::from_markdown("- a\n - b\n> q\n```\nx\n```");
245 let style = draft_style(&draft, "Mono".into());
246 let starts = draft.line_ranges();
247 assert_eq!(
248 style.indents,
249 [
250 (starts[1].start, gpui_kit::px(18.)),
251 (starts[2].start, gpui_kit::px(12.)),
252 (starts[3].start, gpui_kit::px(10.)),
253 ]
254 );
255 assert!(!draft.text().contains('\u{2003}'));
256 }
257
258 #[test]
259 fn a_code_block_covers_its_empty_last_line_before_text() {
260 let draft = Draft::from_lines(&[
261 line(LineKind::Code(None), "x"),
262 line(LineKind::Code(None), ""),
263 line(LineKind::Text, "after"),
264 ]);
265 let block = block_range(&draft, 0..2);
266 let empty_line_start = draft.line_ranges()[1].start;
267 assert!(block.contains(&empty_line_start) || block.end == empty_line_start);
268 assert!(block.end < draft.line_ranges()[2].start);
269 }
270
271 #[test]
272 fn a_lone_code_line_still_gets_a_block() {
273 let draft = Draft::from_lines(&[line(LineKind::Code(None), "")]);
274 let style = draft_style(&draft, "Mono".into());
275 assert_eq!(style.ranges.len(), 1);
276 assert_eq!(style.ranges[0].style(), RangeDecorationStyle::Block);
277 }
278}