Skip to main content

gpui_deck/
theme.rs

1// Modified for GPUI Deck: retain only input colors, caret, and scrollbar defaults.
2use gpui::{App, Global, Hsla, hsla};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use crate::{ScrollbarMode, ScrollbarMotion, ScrollbarStyles, input::CursorBlink};
7
8/// Baseline colors for text editing, independent of any application design system.
9///
10/// Explicit colors in [`crate::InputEditorStyle`] take precedence. These defaults
11/// make unconfigured input text and its caret visible on a light surface; they
12/// do not style a control frame, choose typography, or set layout dimensions.
13#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
14#[non_exhaustive]
15pub struct InputColors {
16    pub foreground: Hsla,
17    pub muted_foreground: Hsla,
18    pub background: Hsla,
19    pub border: Hsla,
20    pub selection: Hsla,
21}
22
23impl Default for InputColors {
24    fn default() -> Self {
25        Self {
26            foreground: hsla(0., 0., 0.039, 1.),
27            muted_foreground: hsla(0., 0., 0.451, 1.),
28            background: hsla(0., 0., 1., 1.),
29            border: hsla(0., 0., 0.898, 1.),
30            selection: hsla(0., 0., 0.961, 0.4),
31        }
32    }
33}
34
35/// Construct with [`Theme::default`] and assign fields; new fields may be
36/// added without a breaking change.
37#[derive(Clone, Default)]
38#[non_exhaustive]
39pub struct Theme {
40    /// Baseline colors used when an input has no explicit projected color.
41    pub input_colors: InputColors,
42    pub scrollbar: ScrollbarTheme,
43    /// Application-wide caret blink behavior.
44    pub cursor_blink: CursorBlink,
45}
46
47impl Global for Theme {}
48
49impl Theme {
50    pub fn global(cx: &App) -> Self {
51        cx.try_global::<Self>().cloned().unwrap_or_default()
52    }
53
54    pub fn global_mut(cx: &mut App) -> &mut Self {
55        if !cx.has_global::<Self>() {
56            cx.set_global(Self::default());
57        }
58        cx.global_mut::<Self>()
59    }
60}
61
62/// Access to the active base theme through an application context.
63pub(crate) trait ActiveTheme {
64    fn theme(&self) -> Theme;
65}
66
67impl ActiveTheme for App {
68    #[inline(always)]
69    fn theme(&self) -> Theme {
70        Theme::global(self)
71    }
72}
73
74/// Global defaults used by [`crate::Scrollbar`].
75///
76/// `motion` defaults to motionless. Styled layers project their own timing;
77/// Base never installs a fade or slide of its own.
78#[derive(Clone, Default)]
79pub struct ScrollbarTheme {
80    mode: ScrollbarMode,
81    motion: ScrollbarMotion,
82    styles: ScrollbarStyles,
83}
84
85impl ScrollbarTheme {
86    pub fn new() -> Self {
87        Self::default()
88    }
89
90    pub fn with_mode(mut self, mode: ScrollbarMode) -> Self {
91        self.mode = mode;
92        self
93    }
94
95    pub fn with_motion(mut self, motion: ScrollbarMotion) -> Self {
96        self.motion = motion;
97        self
98    }
99
100    pub fn with_styles(mut self, styles: ScrollbarStyles) -> Self {
101        self.styles = styles;
102        self
103    }
104
105    pub fn mode(&self) -> ScrollbarMode {
106        self.mode
107    }
108
109    pub fn motion(&self) -> ScrollbarMotion {
110        self.motion
111    }
112
113    pub fn styles(&self) -> &ScrollbarStyles {
114        &self.styles
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121
122    #[test]
123    fn scrollbar_theme_supports_builder_construction() {
124        let mode = ScrollbarMode::Hover;
125        let motion = ScrollbarMotion::default().with_enter(std::time::Duration::from_millis(120));
126        let styles = ScrollbarStyles::default();
127        let theme = ScrollbarTheme::new()
128            .with_mode(mode)
129            .with_motion(motion)
130            .with_styles(styles);
131
132        assert_eq!(theme.mode(), mode);
133        assert_eq!(theme.motion(), motion);
134        let _ = theme.styles();
135    }
136}