BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / draft_style.rs

draft_style.rs

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