1use std::sync::Arc;
2
3use gpui::{HighlightStyle, Hsla, Rems, StyleRefinement, rems};
4
5use crate::ColorTokens;
6
7#[derive(Clone)]
14pub struct TextViewStyle {
15 foreground: Hsla,
16 muted_foreground: Hsla,
17 link: Hsla,
18 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 inline_code_style: StyleRefinement,
32 list_style: StyleRefinement,
34 blockquote_style: StyleRefinement,
36 table_background: Option<Hsla>,
40 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 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 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 pub fn with_foreground(mut self, color: Hsla) -> Self {
122 self.foreground = color;
123 self
124 }
125
126 pub fn with_muted_foreground(mut self, color: Hsla) -> Self {
128 self.muted_foreground = color;
129 self
130 }
131
132 pub fn with_link(mut self, color: Hsla) -> Self {
134 self.link = color;
135 self
136 }
137
138 pub fn with_link_hover(mut self, color: Hsla) -> Self {
141 self.link_hover = Some(color);
142 self
143 }
144
145 pub fn with_selection(mut self, color: Hsla) -> Self {
150 self.selection = color;
151 self
152 }
153
154 pub fn with_code_background(mut self, color: Hsla) -> Self {
156 self.code_background = color;
157 self
158 }
159
160 pub fn with_border(mut self, color: Hsla) -> Self {
162 self.border = color;
163 self
164 }
165
166 pub fn with_paragraph_gap(mut self, gap: Rems) -> Self {
168 self.paragraph_gap = gap;
169 self
170 }
171
172 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 pub fn with_code_block(mut self, style: StyleRefinement) -> Self {
188 self.code_block = style;
189 self
190 }
191
192 pub fn with_inline_code(mut self, style: HighlightStyle) -> Self {
197 self.inline_code = style;
198 self
199 }
200
201 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 pub fn with_list_style(mut self, style: StyleRefinement) -> Self {
226 self.list_style = style;
227 self
228 }
229
230 pub fn with_blockquote_style(mut self, style: StyleRefinement) -> Self {
240 self.blockquote_style = style;
241 self
242 }
243
244 pub fn with_table(mut self, style: StyleRefinement) -> Self {
252 self.table = style;
253 self
254 }
255
256 pub fn with_table_head(mut self, style: StyleRefinement) -> Self {
259 self.table_head = style;
260 self
261 }
262
263 pub fn with_table_cell(mut self, style: StyleRefinement) -> Self {
269 self.table_cell = style;
270 self
271 }
272
273 pub fn with_table_row_hover(mut self, color: Hsla) -> Self {
278 self.table_row_hover = Some(color);
279 self
280 }
281
282 pub fn with_dark(mut self, is_dark: bool) -> Self {
284 self.is_dark = is_dark;
285 self
286 }
287
288 pub fn foreground(&self) -> Hsla {
290 self.foreground
291 }
292
293 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 pub fn link(&self) -> Hsla {
305 self.link
306 }
307
308 pub fn link_hover(&self) -> Hsla {
311 self.link_hover.unwrap_or(self.link)
312 }
313
314 pub fn selection(&self) -> Hsla {
316 self.selection
317 }
318
319 pub fn code_background(&self) -> Hsla {
321 self.code_background
322 }
323
324 pub fn border(&self) -> Hsla {
326 self.border
327 }
328
329 pub fn paragraph_gap(&self) -> Rems {
331 self.paragraph_gap
332 }
333
334 pub fn heading(&self, level: u8) -> StyleRefinement {
336 (self.heading)(level)
337 }
338
339 pub fn code_block(&self) -> &StyleRefinement {
341 &self.code_block
342 }
343
344 pub fn table(&self) -> &StyleRefinement {
346 &self.table
347 }
348
349 pub fn table_head(&self) -> &StyleRefinement {
351 &self.table_head
352 }
353
354 pub fn table_cell(&self) -> &StyleRefinement {
356 &self.table_cell
357 }
358
359 pub fn table_row_hover(&self) -> Option<Hsla> {
361 self.table_row_hover
362 }
363
364 pub fn inline_code(&self) -> HighlightStyle {
367 self.inline_code
368 }
369
370 pub fn inline_code_style(&self) -> &StyleRefinement {
372 &self.inline_code_style
373 }
374
375 pub fn list_style(&self) -> &StyleRefinement {
377 &self.list_style
378 }
379
380 pub fn blockquote_style(&self) -> &StyleRefinement {
382 &self.blockquote_style
383 }
384
385 pub fn is_dark(&self) -> bool {
387 self.is_dark
388 }
389
390 pub(crate) fn table_background(&self) -> Option<Hsla> {
392 self.table_background
393 }
394
395 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 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 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
452fn 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
469pub(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 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(), "e);
673
674 let inverted = style.on_text_color(colors.primary_foreground);
675 assert_eq!(inverted.list_style(), &marker);
676 assert_eq!(inverted.blockquote_style(), "e);
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 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}