Skip to main content

markdown/
source_style.rs

1//! Host styling for the plain-text source view.
2
3use gpui::{App, Global, Hsla};
4use theme::Theme;
5
6#[derive(Clone, Copy, Debug, PartialEq)]
7pub struct SourceStyle {
8    pub line_numbers: bool,
9    /// Minimum number of digit slots; grows to fit the line count.
10    pub gutter_min_digits: usize,
11    /// Space after the numbers, in multiples of the code font size.
12    pub gutter_gap: f32,
13    /// `None` uses the current theme's faint text color.
14    pub gutter_color: Option<Hsla>,
15}
16
17impl Default for SourceStyle {
18    fn default() -> Self {
19        Self {
20            line_numbers: true,
21            gutter_min_digits: 1,
22            gutter_gap: 1.0,
23            gutter_color: None,
24        }
25    }
26}
27
28impl SourceStyle {
29    pub fn of(cx: &App) -> Self {
30        cx.try_global::<Installed>()
31            .map_or_else(Self::default, |installed| (installed.0)(Theme::of(cx)))
32    }
33}
34
35struct Installed(Box<dyn Fn(&Theme) -> SourceStyle>);
36
37impl Global for Installed {}
38
39/// Resolve styles at paint so host colors follow theme changes.
40pub fn set_source_style(cx: &mut App, style: impl Fn(&Theme) -> SourceStyle + 'static) {
41    cx.set_global(Installed(Box::new(style)));
42    cx.refresh_windows();
43}