1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::sync::Arc;
use gpui::{App, HighlightStyle, Pixels, Rems, StyleRefinement, px, rems};
use crate::{ActiveTheme as _, highlighter::HighlightTheme};
/// TextViewStyle used to customize the style for [`TextView`].
#[derive(Clone)]
pub struct TextViewStyle {
/// Gap of each paragraphs, default is 1 rem.
pub paragraph_gap: Rems,
/// Base font size for headings, default is 14px.
pub heading_base_font_size: Pixels,
/// Function to calculate heading font size based on heading level (1-6).
///
/// The first parameter is the heading level (1-6), the second parameter is the base font size.
/// The second parameter is the base font size.
pub heading_font_size: Option<Arc<dyn Fn(u8, Pixels) -> Pixels + Send + Sync + 'static>>,
/// Highlight theme for code blocks. Default: [`HighlightTheme::default_light()`]
pub highlight_theme: Arc<HighlightTheme>,
/// The style refinement for code blocks.
pub code_block: StyleRefinement,
/// Style refinement applied to the table container (the bordered wrapper
/// in wrap mode, the scroll viewport in horizontal-scroll mode).
///
/// Set `overflow_x: scroll` here for adaptive table layout: columns fit
/// their content when space allows, shrink (wrapping cell text) down to a
/// per-column floor when the frame is narrower, and below that the table
/// scrolls horizontally instead of squeezing further, e.g.
/// `TextViewStyle::default().table({ let mut s = StyleRefinement::default(); s.overflow.x = Some(Overflow::Scroll); s })`.
pub table: StyleRefinement,
/// Style refinement applied to the header row (the first row) of a table,
/// on top of the `table_head` background and foreground from the theme.
pub table_head: StyleRefinement,
/// Style refinement applied to each table cell.
///
/// With the scroll layout, set `white_space: nowrap` here to keep cells
/// on a single line — columns then never shrink and the table scrolls as
/// soon as the content is wider than the frame.
pub table_cell: StyleRefinement,
/// The highlight style for inline code.
///
/// Default is [`HighlightStyle::default()`], the `background_color` will
/// fallback to `cx.theme().accent`, if it is `None`.
pub inline_code: HighlightStyle,
pub is_dark: bool,
}
impl PartialEq for TextViewStyle {
fn eq(&self, other: &Self) -> bool {
self.paragraph_gap == other.paragraph_gap
&& self.heading_base_font_size == other.heading_base_font_size
&& match (&self.heading_font_size, &other.heading_font_size) {
(Some(left), Some(right)) => (1..=6).all(|level| {
left(level, self.heading_base_font_size)
== right(level, other.heading_base_font_size)
}),
(None, None) => true,
_ => false,
}
&& self.highlight_theme == other.highlight_theme
&& self.code_block == other.code_block
&& self.table == other.table
&& self.table_head == other.table_head
&& self.table_cell == other.table_cell
&& self.inline_code == other.inline_code
&& self.is_dark == other.is_dark
}
}
impl Default for TextViewStyle {
fn default() -> Self {
Self {
paragraph_gap: rems(1.),
heading_base_font_size: px(14.),
heading_font_size: None,
highlight_theme: HighlightTheme::default_light().clone(),
code_block: StyleRefinement::default(),
table: StyleRefinement::default(),
table_head: StyleRefinement::default(),
table_cell: StyleRefinement::default(),
inline_code: HighlightStyle::default(),
is_dark: false,
}
}
}
impl TextViewStyle {
/// Set paragraph gap, default is 1 rem.
pub fn paragraph_gap(mut self, gap: Rems) -> Self {
self.paragraph_gap = gap;
self
}
pub fn heading_font_size<F>(mut self, f: F) -> Self
where
F: Fn(u8, Pixels) -> Pixels + Send + Sync + 'static,
{
self.heading_font_size = Some(Arc::new(f));
self
}
/// Set style for code blocks.
pub fn code_block(mut self, style: StyleRefinement) -> Self {
self.code_block = style;
self
}
/// Set style for inline code spans.
pub fn inline_code(mut self, style: HighlightStyle) -> Self {
self.inline_code = style;
self
}
/// Set extra style for the table container.
///
/// Set `overflow_x: scroll` on the refinement for adaptive layout: cells
/// wrap as the frame narrows, and once columns reach their minimum width
/// the table scrolls horizontally instead of shrinking further.
pub fn table(mut self, style: StyleRefinement) -> Self {
self.table = style;
self
}
/// Set extra style for the table header row.
pub fn table_head(mut self, style: StyleRefinement) -> Self {
self.table_head = style;
self
}
/// Set extra style for each table cell.
///
/// With the scroll table layout, `white_space: nowrap` here keeps cells
/// on a single line and the table scrolls whenever the content is wider
/// than the frame.
pub fn table_cell(mut self, style: StyleRefinement) -> Self {
self.table_cell = style;
self
}
/// Returns the [`HighlightStyle`] to use for inline code,
/// fallback `background_color` to `cx.theme().accent`, if it is `None`.
pub(crate) fn inline_code_highlight(&self, cx: &App) -> HighlightStyle {
let mut style = self.inline_code;
if style.background_color.is_none() {
style.background_color = Some(cx.theme().accent);
}
style
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn selection_layout_fingerprint_covers_callback_table_and_theme_fields() {
let base = TextViewStyle::default();
let heading = base.clone().heading_font_size(|_, size| size);
assert!(heading == base.clone().heading_font_size(|_, size| size));
assert!(heading != base.clone().heading_font_size(|_, size| size * 2.));
let mut table = StyleRefinement::default();
table.text.white_space = Some(gpui::WhiteSpace::Nowrap);
assert!(base != base.clone().table_cell(table));
let mut dark = base.clone();
dark.is_dark = true;
assert!(base != dark);
}
#[test]
fn cloning_preserves_the_same_heading_callback_fingerprint() {
let style = TextViewStyle::default().heading_font_size(|_, size| size);
assert!(style == style.clone());
}
}