gpui_component/text/
style.rs

1use std::sync::Arc;
2
3use gpui::{px, rems, Pixels, Rems, StyleRefinement};
4
5use crate::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    pub is_dark: bool,
24}
25
26impl PartialEq for TextViewStyle {
27    fn eq(&self, other: &Self) -> bool {
28        self.paragraph_gap == other.paragraph_gap
29            && self.heading_base_font_size == other.heading_base_font_size
30            && self.highlight_theme == other.highlight_theme
31    }
32}
33
34impl Default for TextViewStyle {
35    fn default() -> Self {
36        Self {
37            paragraph_gap: rems(1.),
38            heading_base_font_size: px(14.),
39            heading_font_size: None,
40            highlight_theme: HighlightTheme::default_light().clone(),
41            code_block: StyleRefinement::default(),
42            is_dark: false,
43        }
44    }
45}
46
47impl TextViewStyle {
48    /// Set paragraph gap, default is 1 rem.
49    pub fn paragraph_gap(mut self, gap: Rems) -> Self {
50        self.paragraph_gap = gap;
51        self
52    }
53
54    pub fn heading_font_size<F>(mut self, f: F) -> Self
55    where
56        F: Fn(u8, Pixels) -> Pixels + Send + Sync + 'static,
57    {
58        self.heading_font_size = Some(Arc::new(f));
59        self
60    }
61
62    /// Set style for code blocks.
63    pub fn code_block(mut self, style: StyleRefinement) -> Self {
64        self.code_block = style;
65        self
66    }
67}