Skip to main content

gpui_base/text/
style.rs

1use std::sync::Arc;
2
3use gpui::{HighlightStyle, Hsla, Rems, StyleRefinement, rems};
4
5use crate::ColorTokens;
6
7/// TextViewStyle used to customize the style for [`super::TextView`].
8///
9/// The fields are private because this type crosses the `gpui-base` seam:
10/// build one with the `with_*` methods and read it back through the accessors
11/// of the same name, so a later field is an additive change rather than a
12/// breaking one.
13#[derive(Clone)]
14pub struct TextViewStyle {
15    foreground: Hsla,
16    muted_foreground: Hsla,
17    link: Hsla,
18    /// The color of the link under the pointer, or [`Self::link`] when `None`.
19    link_hover: Option<Hsla>,
20    selection: Hsla,
21    code_background: Hsla,
22    border: Hsla,
23    paragraph_gap: Rems,
24    heading: Arc<dyn Fn(u8) -> StyleRefinement + Send + Sync + 'static>,
25    code_block: StyleRefinement,
26    table: StyleRefinement,
27    table_head: StyleRefinement,
28    table_cell: StyleRefinement,
29    inline_code: HighlightStyle,
30    /// Explicit inline code refinements, applied after theme and surface colors.
31    inline_code_style: StyleRefinement,
32    /// Explicit list marker refinements, applied after theme and surface colors.
33    list_style: StyleRefinement,
34    /// Explicit blockquote refinements, applied after theme and surface colors.
35    blockquote_style: StyleRefinement,
36    /// The table body background, or the theme surface when `None`. Only
37    /// [`Self::on_text_color`] sets it, to let a table on an inverted surface
38    /// show that surface.
39    table_background: Option<Hsla>,
40    /// The background of a hovered table body row, or no hover highlight
41    /// when `None`.
42    table_row_hover: Option<Hsla>,
43    is_dark: bool,
44}
45
46impl PartialEq for TextViewStyle {
47    fn eq(&self, other: &Self) -> bool {
48        self.paragraph_gap == other.paragraph_gap
49            && self.foreground == other.foreground
50            && self.muted_foreground == other.muted_foreground
51            && self.link == other.link
52            && self.link_hover == other.link_hover
53            && self.selection == other.selection
54            && self.code_background == other.code_background
55            && self.border == other.border
56            && (Arc::ptr_eq(&self.heading, &other.heading)
57                || (1..=6).all(|level| (self.heading)(level) == (other.heading)(level)))
58            && self.code_block == other.code_block
59            && self.table == other.table
60            && self.table_head == other.table_head
61            && self.table_cell == other.table_cell
62            && self.inline_code == other.inline_code
63            && self.inline_code_style == other.inline_code_style
64            && self.list_style == other.list_style
65            && self.blockquote_style == other.blockquote_style
66            && self.table_background == other.table_background
67            && self.table_row_hover == other.table_row_hover
68            && self.is_dark == other.is_dark
69    }
70}
71
72impl Default for TextViewStyle {
73    fn default() -> Self {
74        Self::from_colors(&ColorTokens::light(), false)
75    }
76}
77
78impl TextViewStyle {
79    /// Derives rich-text colors from Base semantic theme tokens.
80    pub fn from_theme(theme: &crate::Theme) -> Self {
81        Self::from_colors(
82            &theme.tokens.colors,
83            theme.appearance == crate::ThemeAppearance::Dark,
84        )
85    }
86
87    /// Derives rich-text colors from one palette.
88    ///
89    /// Rich text needs a handful of roles the palette does not name directly —
90    /// a code background, a link color — so they are mapped here once instead
91    /// of at every call site.
92    fn from_colors(colors: &ColorTokens, is_dark: bool) -> Self {
93        Self {
94            foreground: colors.foreground,
95            muted_foreground: colors.muted_foreground,
96            link: colors.primary,
97            link_hover: None,
98            selection: colors.selection,
99            code_background: colors.accent,
100            border: colors.border,
101            paragraph_gap: rems(0.75),
102            heading: Arc::new(|_| StyleRefinement::default()),
103            code_block: StyleRefinement::default(),
104            table: StyleRefinement::default(),
105            table_head: StyleRefinement::default(),
106            table_cell: StyleRefinement::default(),
107            inline_code: HighlightStyle {
108                background_color: Some(colors.accent),
109                ..Default::default()
110            },
111            inline_code_style: StyleRefinement::default(),
112            list_style: StyleRefinement::default(),
113            blockquote_style: StyleRefinement::default(),
114            table_background: None,
115            table_row_hover: None,
116            is_dark,
117        }
118    }
119
120    /// Sets the default body-text color.
121    pub fn with_foreground(mut self, color: Hsla) -> Self {
122        self.foreground = color;
123        self
124    }
125
126    /// Sets the secondary text color.
127    pub fn with_muted_foreground(mut self, color: Hsla) -> Self {
128        self.muted_foreground = color;
129        self
130    }
131
132    /// Sets the link text color.
133    pub fn with_link(mut self, color: Hsla) -> Self {
134        self.link = color;
135        self
136    }
137
138    /// Sets the color of the link under the pointer. Unset, a hovered link
139    /// keeps the [link color](Self::with_link) and only the cursor changes.
140    pub fn with_link_hover(mut self, color: Hsla) -> Self {
141        self.link_hover = Some(color);
142        self
143    }
144
145    /// Sets the background painted behind selected text.
146    ///
147    /// Selection quads are painted under the glyphs, so this is normally a
148    /// translucent wash rather than a solid fill.
149    pub fn with_selection(mut self, color: Hsla) -> Self {
150        self.selection = color;
151        self
152    }
153
154    /// Sets the background of fenced code blocks and table header rows.
155    pub fn with_code_background(mut self, color: Hsla) -> Self {
156        self.code_background = color;
157        self
158    }
159
160    /// Sets the color of borders and horizontal rules.
161    pub fn with_border(mut self, color: Hsla) -> Self {
162        self.border = color;
163        self
164    }
165
166    /// Sets the gap between paragraphs. Defaults to 0.75 rem.
167    pub fn with_paragraph_gap(mut self, gap: Rems) -> Self {
168        self.paragraph_gap = gap;
169        self
170    }
171
172    /// Sets the style refinement for headings, selected by heading level (1-6).
173    pub fn with_heading<F>(mut self, heading: F) -> Self
174    where
175        F: Fn(u8) -> StyleRefinement + Send + Sync + 'static,
176    {
177        self.heading = Arc::new(heading);
178        self
179    }
180
181    /// Sets the style refinement for code blocks.
182    ///
183    /// Set `overflow.y` to `Overflow::Scroll` together with a max height to
184    /// scroll long code inside the block: it gets its own scrollbar, and wheel
185    /// input over it no longer scrolls an ancestor list until the code reaches
186    /// its edge.
187    pub fn with_code_block(mut self, style: StyleRefinement) -> Self {
188        self.code_block = style;
189        self
190    }
191
192    /// Sets the highlight style for inline code spans.
193    ///
194    /// When `background_color` is `None`, the neutral code background is used,
195    /// which keeps [`TextViewStyle::default`] usable without a theme.
196    pub fn with_inline_code(mut self, style: HighlightStyle) -> Self {
197        self.inline_code = style;
198        self
199    }
200
201    /// Overrides inline code typography and its box presentation.
202    ///
203    /// Supports font family, size, features, fallbacks, weight, style, color, underline and
204    /// strikethrough, background, border widths/color/style, corner radii, and
205    /// padding. Sizes and padding resolve pixels/rem against the window.
206    /// Unspecified properties keep the existing defaults. Box layout, margin,
207    /// positioning, overflow, and interaction properties are not used.
208    /// Line height remains owned by the paragraph. Non-finite or non-positive
209    /// font sizes are ignored. Explicit refinements also take precedence on
210    /// inverted surfaces.
211    pub fn with_inline_code_style(mut self, style: StyleRefinement) -> Self {
212        self.inline_code_style = normalize_inline_code_style(style);
213        self
214    }
215
216    /// Refines the marker of a bullet or numbered list item.
217    ///
218    /// Applied to the marker column after its defaults: the muted text color,
219    /// a minimum width of the list indent, right-aligned text, and a right
220    /// padding of 0.3 times that indent. Text properties such as color, font
221    /// family, size, and weight style the bullet or number. The text color
222    /// also outlines an unchecked task-list checkbox; a checked one keeps the
223    /// link color. Unspecified properties keep the existing defaults, and
224    /// explicit refinements also take precedence on inverted surfaces.
225    pub fn with_list_style(mut self, style: StyleRefinement) -> Self {
226        self.list_style = style;
227        self
228    }
229
230    /// Refines the blockquote container.
231    ///
232    /// Applied after its defaults: the muted text color, a 2px left border in
233    /// the border color, and a left padding of 0.9 times the body font size.
234    /// Border widths and color style the quote bar; text properties,
235    /// background, padding, and corner radii style the quote and its
236    /// contents. Child blocks keep properties they set explicitly, including
237    /// heading and table font sizes. Unspecified properties keep the existing
238    /// defaults, and explicit refinements also take precedence on inverted surfaces.
239    pub fn with_blockquote_style(mut self, style: StyleRefinement) -> Self {
240        self.blockquote_style = style;
241        self
242    }
243
244    /// Sets the style refinement for the table container (the bordered wrapper
245    /// in wrap mode, the scroll viewport in horizontal-scroll mode).
246    ///
247    /// Set `overflow_x: scroll` on the refinement for adaptive table layout:
248    /// columns fit their content when space allows, shrink (wrapping cell
249    /// text) down to a per-column floor when the frame is narrower, and below
250    /// that the table scrolls horizontally instead of squeezing further.
251    pub fn with_table(mut self, style: StyleRefinement) -> Self {
252        self.table = style;
253        self
254    }
255
256    /// Sets the style refinement for the header row (the first row) of a
257    /// table, applied on top of the header background and foreground.
258    pub fn with_table_head(mut self, style: StyleRefinement) -> Self {
259        self.table_head = style;
260        self
261    }
262
263    /// Sets the style refinement for each table cell.
264    ///
265    /// With the scroll table layout, `white_space: nowrap` here keeps cells on
266    /// a single line — columns then never shrink and the table scrolls as soon
267    /// as the content is wider than the frame.
268    pub fn with_table_cell(mut self, style: StyleRefinement) -> Self {
269        self.table_cell = style;
270        self
271    }
272
273    /// Sets the background of a table body row while the pointer is over it.
274    ///
275    /// The header row does not highlight. Without this, table rows have no
276    /// hover highlight.
277    pub fn with_table_row_hover(mut self, color: Hsla) -> Self {
278        self.table_row_hover = Some(color);
279        self
280    }
281
282    /// Sets whether content-specific assets should use their dark variant.
283    pub fn with_dark(mut self, is_dark: bool) -> Self {
284        self.is_dark = is_dark;
285        self
286    }
287
288    /// The default body-text color.
289    pub fn foreground(&self) -> Hsla {
290        self.foreground
291    }
292
293    /// The secondary text color.
294    pub fn muted_foreground(&self) -> Hsla {
295        self.muted_foreground
296    }
297
298    fn without_link_hover(mut self) -> Self {
299        self.link_hover = None;
300        self
301    }
302
303    /// The link text color.
304    pub fn link(&self) -> Hsla {
305        self.link
306    }
307
308    /// The color of the link under the pointer: the hover color when one is
309    /// set, the link color otherwise.
310    pub fn link_hover(&self) -> Hsla {
311        self.link_hover.unwrap_or(self.link)
312    }
313
314    /// The background painted behind selected text.
315    pub fn selection(&self) -> Hsla {
316        self.selection
317    }
318
319    /// The background of fenced code blocks and table header rows.
320    pub fn code_background(&self) -> Hsla {
321        self.code_background
322    }
323
324    /// The color of borders and horizontal rules.
325    pub fn border(&self) -> Hsla {
326        self.border
327    }
328
329    /// The gap between paragraphs.
330    pub fn paragraph_gap(&self) -> Rems {
331        self.paragraph_gap
332    }
333
334    /// The style refinement for a heading at `level` (1-6).
335    pub fn heading(&self, level: u8) -> StyleRefinement {
336        (self.heading)(level)
337    }
338
339    /// The style refinement for code blocks.
340    pub fn code_block(&self) -> &StyleRefinement {
341        &self.code_block
342    }
343
344    /// The style refinement for the table container.
345    pub fn table(&self) -> &StyleRefinement {
346        &self.table
347    }
348
349    /// The style refinement for table header rows.
350    pub fn table_head(&self) -> &StyleRefinement {
351        &self.table_head
352    }
353
354    /// The style refinement for table cells.
355    pub fn table_cell(&self) -> &StyleRefinement {
356        &self.table_cell
357    }
358
359    /// The background of a hovered table body row, if rows highlight on hover.
360    pub fn table_row_hover(&self) -> Option<Hsla> {
361        self.table_row_hover
362    }
363
364    /// The highlight style for inline code, before the code-background
365    /// fallback in [`Self::inline_code_highlight`] applies.
366    pub fn inline_code(&self) -> HighlightStyle {
367        self.inline_code
368    }
369
370    /// Explicit inline code typography and box refinements.
371    pub fn inline_code_style(&self) -> &StyleRefinement {
372        &self.inline_code_style
373    }
374
375    /// Explicit list marker refinements.
376    pub fn list_style(&self) -> &StyleRefinement {
377        &self.list_style
378    }
379
380    /// Explicit blockquote refinements.
381    pub fn blockquote_style(&self) -> &StyleRefinement {
382        &self.blockquote_style
383    }
384
385    /// Whether content-specific assets should use their dark variant.
386    pub fn is_dark(&self) -> bool {
387        self.is_dark
388    }
389
390    /// The table body background, when it is not the theme surface.
391    pub(crate) fn table_background(&self) -> Option<Hsla> {
392        self.table_background
393    }
394
395    /// Returns the [`HighlightStyle`] to use for inline code, falling back to
396    /// the code background when no custom background was supplied.
397    pub(crate) fn inline_code_highlight(&self) -> HighlightStyle {
398        let mut style = self.inline_code;
399        if style.background_color.is_none() {
400            style.background_color = Some(self.code_background);
401        }
402        style
403    }
404
405    /// This style adapted to body text drawn in `color`, the text color a
406    /// container sets for its surface.
407    ///
408    /// The body text always takes `color`. When `color` is far from this
409    /// style's foreground in lightness, the surface is inverted from the one
410    /// this style was made for (a `primary` fill, say): the link, muted text,
411    /// code, border, selection and table row hover colors would vanish on it,
412    /// so they are all derived from `color`, and [`Self::is_dark`] flips.
413    pub(crate) fn on_text_color(&self, color: Hsla) -> Self {
414        let style = self.clone().with_foreground(color);
415        if !self.is_inverted_by(color) {
416            return style;
417        }
418
419        let code_background = color.opacity(0.12);
420        let mut table_head = self.table_head.clone();
421        table_head.background = Some(code_background.into());
422        table_head.text.color = Some(color);
423        let mut style = style
424            .with_muted_foreground(color.opacity(0.7))
425            .with_link(color)
426            .without_link_hover()
427            .with_selection(color.opacity(0.25))
428            .with_code_background(code_background)
429            .with_border(color.opacity(0.2))
430            .with_inline_code(HighlightStyle {
431                background_color: Some(code_background),
432                ..self.inline_code
433            })
434            .with_table_head(table_head)
435            .with_dark(!self.is_dark);
436        style.table_background = Some(gpui::transparent_black());
437        style.table_row_hover = self.table_row_hover.map(|_| color.opacity(0.08));
438        style
439    }
440
441    /// Whether body text in `color` sits on a surface inverted from the one
442    /// this style was made for.
443    ///
444    /// Mid-tone text such as a destructive red reads on either kind of surface,
445    /// so only a lightness gap wider than that counts as inverted.
446    pub(crate) fn is_inverted_by(&self, color: Hsla) -> bool {
447        const INVERTED_LIGHTNESS_GAP: f32 = 0.6;
448        (oklab_lightness(color) - oklab_lightness(self.foreground)).abs() > INVERTED_LIGHTNESS_GAP
449    }
450}
451
452/// The perceptual (Oklab) lightness of `color`, from 0 (black) to 1 (white).
453fn oklab_lightness(color: Hsla) -> f32 {
454    let rgb = color.to_rgb();
455    let linear = |c: f32| {
456        if c <= 0.04045 {
457            c / 12.92
458        } else {
459            ((c + 0.055) / 1.055).powf(2.4)
460        }
461    };
462    let (r, g, b) = (linear(rgb.r), linear(rgb.g), linear(rgb.b));
463    let l = (0.412_221_46 * r + 0.536_332_55 * g + 0.051_445_995 * b).cbrt();
464    let m = (0.211_903_5 * r + 0.680_699_5 * g + 0.107_396_96 * b).cbrt();
465    let s = (0.088_302_46 * r + 0.281_718_85 * g + 0.629_978_7 * b).cbrt();
466    0.210_454_26 * l + 0.793_617_8 * m - 0.004_072_047 * s
467}
468
469/// Drop an invalid font size before it can replace an inherited value or
470/// make retained style equality fail every frame (NaN is never equal).
471pub(super) fn normalize_inline_code_style(mut style: StyleRefinement) -> StyleRefinement {
472    if style.text.font_size.is_some_and(|size| {
473        let size = size.to_pixels(gpui::px(1.)).as_f32();
474        !size.is_finite() || size <= 0.
475    }) {
476        style.text.font_size = None;
477    }
478    style
479}
480
481#[cfg(test)]
482mod tests {
483    use gpui::{Styled as _, px};
484
485    use super::*;
486
487    #[test]
488    fn a_hovered_link_keeps_the_link_color_until_given_its_own() {
489        let style = TextViewStyle::default().with_link(gpui::blue());
490        assert_eq!(style.link_hover(), gpui::blue());
491
492        let style = style.with_link_hover(gpui::red());
493        assert_eq!(style.link_hover(), gpui::red());
494        assert!(style != TextViewStyle::default().with_link(gpui::blue()));
495
496        // On an inverted surface links take the surface text color, and a
497        // hovered link stays with it.
498        let colors = ColorTokens::light();
499        let inverted = style.on_text_color(colors.primary_foreground);
500        assert_eq!(inverted.link_hover(), inverted.link());
501    }
502
503    #[test]
504    fn selection_layout_fingerprint_covers_callback_table_and_theme_fields() {
505        let base = TextViewStyle::default();
506        let heading = base
507            .clone()
508            .with_heading(|_| StyleRefinement::default().text_size(px(14.)));
509        assert!(
510            heading
511                == base
512                    .clone()
513                    .with_heading(|_| StyleRefinement::default().text_size(px(14.)))
514        );
515        assert!(
516            heading
517                != base
518                    .clone()
519                    .with_heading(|_| StyleRefinement::default().text_size(px(28.)))
520        );
521
522        let mut table = StyleRefinement::default();
523        table.text.white_space = Some(gpui::WhiteSpace::Nowrap);
524        assert!(base != base.clone().with_table_cell(table));
525        assert!(base != base.clone().with_table_row_hover(gpui::red()));
526
527        assert!(base != base.clone().with_dark(true));
528        assert!(
529            base != base
530                .clone()
531                .with_inline_code_style(StyleRefinement::default().border_color(gpui::red()))
532        );
533        assert!(
534            base != base
535                .clone()
536                .with_list_style(StyleRefinement::default().text_color(gpui::red()))
537        );
538        assert!(
539            base != base
540                .clone()
541                .with_blockquote_style(StyleRefinement::default().border_color(gpui::red()))
542        );
543    }
544
545    #[test]
546    fn heading_equality_skips_shared_callbacks_and_compares_distinct_ones() {
547        use std::sync::atomic::{AtomicUsize, Ordering};
548
549        let calls = Arc::new(AtomicUsize::new(0));
550        let calls_for_heading = calls.clone();
551        let style = TextViewStyle::default().with_heading(move |_| {
552            calls_for_heading.fetch_add(1, Ordering::Relaxed);
553            StyleRefinement::default().text_size(px(14.))
554        });
555
556        assert!(style == style.clone());
557        assert_eq!(calls.load(Ordering::Relaxed), 0);
558        assert!(style != style.clone().with_paragraph_gap(rems(1.)));
559        assert!(style != style.clone().with_dark(!style.is_dark()));
560        assert_eq!(calls.load(Ordering::Relaxed), 0);
561
562        let equal = style
563            .clone()
564            .with_heading(|_| StyleRefinement::default().text_size(px(14.)));
565        assert!(style == equal);
566        assert_eq!(calls.load(Ordering::Relaxed), 6);
567
568        calls.store(0, Ordering::Relaxed);
569        let different = style.clone().with_heading(|level| {
570            StyleRefinement::default().text_size(px(if level == 6 { 28. } else { 14. }))
571        });
572        assert!(style != different);
573        assert_eq!(calls.load(Ordering::Relaxed), 6);
574    }
575
576    #[test]
577    fn default_style_is_readable_without_an_application_theme() {
578        let style = TextViewStyle::default();
579
580        assert_eq!(style.foreground().a, 1.0);
581        assert_eq!(style.link().a, 1.0);
582        assert!(style.selection().a > 0.0);
583        assert!(style.inline_code().background_color.is_some());
584        assert!(style.code_background().a > 0.0);
585        assert!(style.border().a > 0.0);
586        assert_eq!(style.table_row_hover(), None);
587        assert_eq!(style.code_block().corner_radii.top_left, None);
588        assert_eq!(style.code_block().corner_radii.top_right, None);
589        assert_eq!(style.code_block().corner_radii.bottom_left, None);
590        assert_eq!(style.code_block().corner_radii.bottom_right, None);
591    }
592
593    #[test]
594    fn heading_refinement_defaults_empty_and_resolves_by_level() {
595        let base = TextViewStyle::default();
596        assert_eq!(base.heading(1), StyleRefinement::default());
597
598        let style = base.clone().with_heading(|level| match level {
599            1 => StyleRefinement::default().pt(rems(1.)).pb(rems(0.5)),
600            _ => StyleRefinement::default().pb(rems(0.25)),
601        });
602
603        assert_eq!(
604            style.heading(1),
605            StyleRefinement::default().pt(rems(1.)).pb(rems(0.5))
606        );
607        assert_eq!(style.heading(2), StyleRefinement::default().pb(rems(0.25)));
608        assert!(style != base);
609    }
610
611    #[test]
612    fn inline_code_falls_back_to_the_code_background() {
613        let style = TextViewStyle::default()
614            .with_code_background(gpui::rgb(0x123456).into())
615            .with_inline_code(HighlightStyle::default());
616
617        assert_eq!(
618            style.inline_code_highlight().background_color,
619            Some(gpui::rgb(0x123456).into())
620        );
621    }
622
623    #[test]
624    fn inline_code_refinements_remain_explicit_on_an_inverted_surface() {
625        let colors = ColorTokens::light();
626        let plain = TextViewStyle::from_colors(&colors, false);
627        assert_eq!(plain.inline_code_style(), &StyleRefinement::default());
628        for size in [f32::NAN, f32::INFINITY, 0., -1.] {
629            assert!(
630                plain
631                    .clone()
632                    .with_inline_code_style(StyleRefinement::default().text_size(px(size)))
633                    .inline_code_style()
634                    .text
635                    .font_size
636                    .is_none()
637            );
638        }
639        let refinement = StyleRefinement::default()
640            .text_size(gpui::rems(1.))
641            .bg(gpui::red())
642            .border_1()
643            .border_color(gpui::blue());
644        let style = plain.with_inline_code_style(refinement.clone());
645        assert_eq!(style.inline_code_style(), &refinement);
646        assert_eq!(
647            style
648                .on_text_color(colors.primary_foreground)
649                .inline_code_style(),
650            &refinement
651        );
652    }
653
654    #[test]
655    fn list_and_blockquote_refinements_remain_explicit_on_an_inverted_surface() {
656        let colors = ColorTokens::light();
657        let plain = TextViewStyle::from_colors(&colors, false);
658        assert_eq!(plain.list_style(), &StyleRefinement::default());
659        assert_eq!(plain.blockquote_style(), &StyleRefinement::default());
660
661        let marker = StyleRefinement::default()
662            .text_color(gpui::red())
663            .font_weight(gpui::FontWeight::BOLD);
664        let quote = StyleRefinement::default()
665            .border_l_4()
666            .border_color(gpui::blue())
667            .bg(gpui::green());
668        let style = plain
669            .with_list_style(marker.clone())
670            .with_blockquote_style(quote.clone());
671        assert_eq!(style.list_style(), &marker);
672        assert_eq!(style.blockquote_style(), &quote);
673
674        let inverted = style.on_text_color(colors.primary_foreground);
675        assert_eq!(inverted.list_style(), &marker);
676        assert_eq!(inverted.blockquote_style(), &quote);
677    }
678
679    #[test]
680    fn text_color_of_a_matching_surface_only_replaces_the_body_text() {
681        let style = TextViewStyle::from_colors(&ColorTokens::light(), false)
682            .with_table_row_hover(ColorTokens::light().muted);
683        let destructive = ColorTokens::light().destructive;
684
685        let adapted = style.on_text_color(destructive);
686        assert_eq!(adapted.foreground(), destructive);
687        assert_eq!(adapted.link(), style.link());
688        assert_eq!(adapted.muted_foreground(), style.muted_foreground());
689        assert_eq!(adapted.code_background(), style.code_background());
690        assert_eq!(adapted.table_background(), None);
691        assert_eq!(adapted.table_row_hover(), style.table_row_hover());
692        assert!(!adapted.is_dark());
693    }
694
695    #[test]
696    fn text_color_of_an_inverted_surface_derives_every_color_from_it() {
697        for (colors, is_dark) in [(ColorTokens::light(), false), (ColorTokens::dark(), true)] {
698            let style = TextViewStyle::from_colors(&colors, is_dark);
699            let text = colors.primary_foreground;
700            // Rows without a hover highlight stay without one.
701            assert_eq!(style.on_text_color(text).table_row_hover(), None);
702
703            let style = style.with_table_row_hover(colors.muted);
704            let adapted = style.on_text_color(text);
705            assert_eq!(adapted.foreground(), text);
706            assert_eq!(adapted.link(), text);
707            assert_eq!(adapted.muted_foreground(), text.opacity(0.7));
708            assert_eq!(adapted.code_background(), text.opacity(0.12));
709            assert_eq!(
710                adapted.inline_code_highlight().background_color,
711                Some(text.opacity(0.12))
712            );
713            assert_eq!(adapted.border(), text.opacity(0.2));
714            assert_eq!(adapted.selection(), text.opacity(0.25));
715            assert_eq!(adapted.table_background(), Some(gpui::transparent_black()));
716            assert_eq!(adapted.table_row_hover(), Some(text.opacity(0.08)));
717            assert_eq!(adapted.table_head().text.color, Some(text));
718            assert_eq!(adapted.is_dark(), !is_dark);
719        }
720    }
721
722    #[test]
723    fn from_theme_maps_base_semantic_tokens() {
724        let mut theme = crate::Theme::default();
725        theme.tokens.colors.foreground = gpui::rgb(0x112233).into();
726        theme.tokens.colors.muted_foreground = gpui::rgb(0x445566).into();
727        theme.tokens.colors.primary = gpui::rgb(0x3366ff).into();
728        theme.tokens.colors.accent = gpui::rgb(0xddeeff).into();
729        theme.tokens.colors.border = gpui::rgb(0x778899).into();
730        theme.tokens.colors.selection = gpui::rgb(0x55a0fc).into();
731
732        let style = TextViewStyle::from_theme(&theme);
733        assert_eq!(style.foreground(), theme.tokens.colors.foreground);
734        assert_eq!(style.link(), theme.tokens.colors.primary);
735        assert_eq!(style.selection(), theme.tokens.colors.selection);
736        assert_eq!(style.code_background(), theme.tokens.colors.accent);
737        assert_eq!(style.border(), theme.tokens.colors.border);
738    }
739}