Skip to main content

gpui_component/text/
style.rs

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