diff --git a/crates/gpui3/src/styled.rs b/crates/gpui3/src/styled.rs index 4d54ebb5ad..d511198c65 100644 --- a/crates/gpui3/src/styled.rs +++ b/crates/gpui3/src/styled.rs @@ -1,6 +1,6 @@ use crate::{ - self as gpui3, hsla, point, px, relative, rems, AbsoluteLength, AlignItems, Display, Fill, - FlexDirection, Hsla, JustifyContent, Length, Position, Rems, SharedString, StyleRefinement, + self as gpui3, hsla, point, px, relative, rems, AlignItems, Display, Fill, FlexDirection, Hsla, + JustifyContent, Length, Position, Rems, SharedString, StyleRefinement, }; use crate::{BoxShadow, TextStyleRefinement}; use smallvec::smallvec; @@ -350,13 +350,13 @@ pub trait Styled { self } - fn text_size(mut self, size: Rems) -> Self + fn text_size(mut self, size: impl Into) -> Self where Self: Sized, { self.text_style() .get_or_insert_with(Default::default) - .font_size = Some(size); + .font_size = Some(size.into()); self } diff --git a/crates/ui2/src/components/list.rs b/crates/ui2/src/components/list.rs index b3a54118b7..8ab1c64e86 100644 --- a/crates/ui2/src/components/list.rs +++ b/crates/ui2/src/components/list.rs @@ -405,7 +405,7 @@ impl ListEntry { // .ml(rems(0.75 * self.indent_level as f32)) .children((0..self.indent_level).map(|_| { div() - .w(setting.list_indent_depth()) + .w(*setting.list_indent_depth) .h_full() .flex() .justify_center() diff --git a/crates/ui2/src/components/panel.rs b/crates/ui2/src/components/panel.rs index 7aaf98598f..52e36b2f14 100644 --- a/crates/ui2/src/components/panel.rs +++ b/crates/ui2/src/components/panel.rs @@ -61,7 +61,7 @@ impl Panel { scroll_state, current_side: PanelSide::default(), allowed_sides: PanelAllowedSides::default(), - initial_width: setting.default_panel_size(), + initial_width: *setting.default_panel_size, width: None, children: SmallVec::new(), } diff --git a/crates/ui2/src/components/title_bar.rs b/crates/ui2/src/components/title_bar.rs index b13982f2a7..f1d7d42711 100644 --- a/crates/ui2/src/components/title_bar.rs +++ b/crates/ui2/src/components/title_bar.rs @@ -126,7 +126,7 @@ impl TitleBar { .flex() .items_center() .gap_1() - .when(setting.titlebar_show_project_owner(), |this| { + .when(*setting.titlebar.show_project_owner, |this| { this.child(Button::new("iamnbutler")) }) .child(Button::new("zed")) diff --git a/crates/ui2/src/elements/button.rs b/crates/ui2/src/elements/button.rs index 5fe901aa6c..b4d9d8afa6 100644 --- a/crates/ui2/src/elements/button.rs +++ b/crates/ui2/src/elements/button.rs @@ -1,7 +1,8 @@ use std::marker::PhantomData; use std::sync::Arc; -use gpui3::{rems, DefiniteLength, Hsla, Interactive, MouseButton, WindowContext}; +use gpui3::rems; +use gpui3::{DefiniteLength, Hsla, Interactive, MouseButton, WindowContext}; use crate::prelude::*; use crate::settings::user_settings; @@ -153,7 +154,7 @@ impl Button { let mut el = h_stack() .p_1() - .text_size(rems(1.125 * setting.ui_scale())) + .text_size(ui_size(1.125)) .rounded_md() .border() .border_color(border_color) diff --git a/crates/ui2/src/prelude.rs b/crates/ui2/src/prelude.rs index 015aeadcf4..db484b6cfa 100644 --- a/crates/ui2/src/prelude.rs +++ b/crates/ui2/src/prelude.rs @@ -3,9 +3,10 @@ pub use gpui3::{ ViewContext, WindowContext, }; +use crate::settings::user_settings; pub use crate::{theme, ButtonVariant, ElementExt, Theme}; -use gpui3::{hsla, rems, rgb, AbsoluteLength, Hsla}; +use gpui3::{hsla, rems, rgb, AbsoluteLength, Hsla, Rems}; use strum::EnumIter; #[derive(Clone, Copy)] @@ -156,6 +157,12 @@ impl HighlightColor { } } +pub fn ui_size(size: f32) -> Rems { + let setting = user_settings(); + + rems(*setting.ui_scale * size) +} + #[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, EnumIter)] pub enum FileSystemStatus { #[default] diff --git a/crates/ui2/src/settings.rs b/crates/ui2/src/settings.rs index 4c105f66e1..a387b8ddd9 100644 --- a/crates/ui2/src/settings.rs +++ b/crates/ui2/src/settings.rs @@ -1,102 +1,70 @@ +use std::ops::Deref; + use gpui3::{rems, AbsoluteLength}; use crate::DisclosureControlStyle; // This is a fake static example of user settings overriding the default settings pub fn user_settings() -> Settings { - let mut settings = Settings::new(); - settings.list_indent_depth = Some(rems(0.5).into()); + let mut settings = Settings::default(); + settings.list_indent_depth = SettingValue::UserDefined(rems(0.5).into()); settings } -#[derive(Clone, Copy)] +#[derive(Clone)] +pub enum SettingValue { + UserDefined(T), + Default(T), +} + +impl Deref for SettingValue { + type Target = T; + + fn deref(&self) -> &Self::Target { + match self { + Self::UserDefined(value) => value, + Self::Default(value) => value, + } + } +} + +#[derive(Clone)] pub struct TitlebarSettings { - pub show_project_owner: Option, - pub show_git_status: Option, - pub show_git_controls: Option, + pub show_project_owner: SettingValue, + pub show_git_status: SettingValue, + pub show_git_controls: SettingValue, } impl Default for TitlebarSettings { fn default() -> Self { Self { - show_project_owner: Some(true), - show_git_status: Some(true), - show_git_controls: Some(true), + show_project_owner: SettingValue::Default(true), + show_git_status: SettingValue::Default(true), + show_git_controls: SettingValue::Default(true), } } } -#[derive(Clone, Copy)] -pub struct Settings { - pub default_panel_size: Option, - pub list_disclosure_style: Option, - pub list_indent_depth: Option, - pub titlebar: TitlebarSettings, - pub ui_scale: Option, -} - // These should be merged into settings -impl Settings { - pub fn new() -> Self { - Self { - titlebar: TitlebarSettings::default(), - list_disclosure_style: None, - list_indent_depth: None, - default_panel_size: None, - ui_scale: None, - } - } - - pub fn titlebar_show_project_owner(&self) -> bool { - self.titlebar.show_project_owner.unwrap_or( - Settings::default() - .titlebar - .show_project_owner - .expect("titlebar_show_project_owner default not set."), - ) - } - - pub fn list_disclosure_style(&self) -> DisclosureControlStyle { - self.list_disclosure_style.unwrap_or( - Settings::default() - .list_disclosure_style - .expect("list_disclosure_style default not set."), - ) - } - - pub fn list_indent_depth(&self) -> AbsoluteLength { - self.list_indent_depth.unwrap_or( - Settings::default() - .list_indent_depth - .expect("list_indent_depth default not set."), - ) - } - - pub fn default_panel_size(&self) -> AbsoluteLength { - self.default_panel_size.unwrap_or( - Settings::default() - .default_panel_size - .expect("default_panel_size default not set."), - ) - } - - pub fn ui_scale(&self) -> f32 { - self.ui_scale.unwrap_or( - Settings::default() - .ui_scale - .expect("ui_scale default not set."), - ) - } +#[derive(Clone)] +pub struct Settings { + pub default_panel_size: SettingValue, + pub list_disclosure_style: SettingValue, + pub list_indent_depth: SettingValue, + pub titlebar: TitlebarSettings, + pub ui_scale: SettingValue, } impl Default for Settings { fn default() -> Self { Self { titlebar: TitlebarSettings::default(), - list_disclosure_style: Some(DisclosureControlStyle::ChevronOnHover), - list_indent_depth: Some(rems(0.3).into()), - default_panel_size: Some(rems(16.).into()), - ui_scale: Some(1.), + list_disclosure_style: SettingValue::Default(DisclosureControlStyle::ChevronOnHover), + list_indent_depth: SettingValue::Default(rems(0.3).into()), + default_panel_size: SettingValue::Default(rems(16.).into()), + ui_scale: SettingValue::Default(1.), } } } + +impl Settings {}