gpui_component/text/
style.rs1use std::sync::Arc;
2
3use gpui::{App, HighlightStyle, Pixels, Rems, StyleRefinement, px, rems};
4
5use crate::{ActiveTheme as _, highlighter::HighlightTheme};
6
7#[derive(Clone)]
9pub struct TextViewStyle {
10 pub paragraph_gap: Rems,
12 pub heading_base_font_size: Pixels,
14 pub heading_font_size: Option<Arc<dyn Fn(u8, Pixels) -> Pixels + Send + Sync + 'static>>,
19 pub highlight_theme: Arc<HighlightTheme>,
21 pub code_block: StyleRefinement,
23 pub table: StyleRefinement,
32 pub table_head: StyleRefinement,
35 pub table_cell: StyleRefinement,
41 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 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 pub fn code_block(mut self, style: StyleRefinement) -> Self {
105 self.code_block = style;
106 self
107 }
108
109 pub fn inline_code(mut self, style: HighlightStyle) -> Self {
111 self.inline_code = style;
112 self
113 }
114
115 pub fn table(mut self, style: StyleRefinement) -> Self {
121 self.table = style;
122 self
123 }
124
125 pub fn table_head(mut self, style: StyleRefinement) -> Self {
127 self.table_head = style;
128 self
129 }
130
131 pub fn table_cell(mut self, style: StyleRefinement) -> Self {
137 self.table_cell = style;
138 self
139 }
140
141 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}