From 89c8a7c2426a426fdda1af36e8121d77a01cc128 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 6 Dec 2023 13:52:33 -0800 Subject: [PATCH] Enable buffer font size adjustment in zed2 Co-authored-by: Nathan --- crates/editor2/src/editor.rs | 2 +- crates/gpui2/src/app.rs | 1 - crates/theme2/src/settings.rs | 31 +++++++++++++++---------------- crates/ui2/src/styled_ext.rs | 3 +-- crates/zed2/src/zed2.rs | 15 +++++++-------- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/crates/editor2/src/editor.rs b/crates/editor2/src/editor.rs index 94ae8abc71..8f9d22e6c9 100644 --- a/crates/editor2/src/editor.rs +++ b/crates/editor2/src/editor.rs @@ -9291,7 +9291,7 @@ impl Render for Editor { color: cx.theme().colors().text, font_family: settings.buffer_font.family.clone(), font_features: settings.buffer_font.features, - font_size: settings.buffer_font_size.into(), + font_size: settings.buffer_font_size(cx).into(), font_weight: FontWeight::NORMAL, font_style: FontStyle::Normal, line_height: relative(settings.buffer_line_height.value()), diff --git a/crates/gpui2/src/app.rs b/crates/gpui2/src/app.rs index 0715ace9ea..9293302938 100644 --- a/crates/gpui2/src/app.rs +++ b/crates/gpui2/src/app.rs @@ -860,7 +860,6 @@ impl AppContext { } /// Remove the global of the given type from the app context. Does not notify global observers. - #[cfg(any(test, feature = "test-support"))] pub fn remove_global(&mut self) -> G { let global_type = TypeId::of::(); *self diff --git a/crates/theme2/src/settings.rs b/crates/theme2/src/settings.rs index 15b578d4b0..624b14fe33 100644 --- a/crates/theme2/src/settings.rs +++ b/crates/theme2/src/settings.rs @@ -27,7 +27,7 @@ pub struct ThemeSettings { } #[derive(Default)] -pub struct AdjustedBufferFontSize(Option); +pub struct AdjustedBufferFontSize(Pixels); #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)] pub struct ThemeSettingsContent { @@ -69,12 +69,10 @@ impl BufferLineHeight { } impl ThemeSettings { - pub fn buffer_font_size(&self, cx: &mut AppContext) -> Pixels { - let font_size = *cx - .default_global::() - .0 - .get_or_insert(self.buffer_font_size.into()); - font_size.max(MIN_FONT_SIZE) + pub fn buffer_font_size(&self, cx: &AppContext) -> Pixels { + cx.try_global::() + .map_or(self.buffer_font_size, |size| size.0) + .max(MIN_FONT_SIZE) } pub fn line_height(&self) -> f32 { @@ -83,9 +81,9 @@ impl ThemeSettings { } pub fn adjusted_font_size(size: Pixels, cx: &mut AppContext) -> Pixels { - if let Some(adjusted_size) = cx.default_global::().0 { + if let Some(AdjustedBufferFontSize(adjusted_size)) = cx.try_global::() { let buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size; - let delta = adjusted_size - buffer_font_size; + let delta = *adjusted_size - buffer_font_size; size + delta } else { size @@ -95,18 +93,19 @@ pub fn adjusted_font_size(size: Pixels, cx: &mut AppContext) -> Pixels { pub fn adjust_font_size(cx: &mut AppContext, f: fn(&mut Pixels)) { let buffer_font_size = ThemeSettings::get_global(cx).buffer_font_size; - let adjusted_size = cx - .default_global::() - .0 - .get_or_insert(buffer_font_size); - f(adjusted_size); - *adjusted_size = (*adjusted_size).max(MIN_FONT_SIZE - buffer_font_size); + let mut adjusted_size = cx + .try_global::() + .map_or(buffer_font_size, |adjusted_size| adjusted_size.0); + + f(&mut adjusted_size); + adjusted_size = adjusted_size.max(MIN_FONT_SIZE); + cx.set_global(AdjustedBufferFontSize(adjusted_size)); cx.refresh(); } pub fn reset_font_size(cx: &mut AppContext) { if cx.has_global::() { - cx.global_mut::().0 = None; + cx.remove_global::(); cx.refresh(); } } diff --git a/crates/ui2/src/styled_ext.rs b/crates/ui2/src/styled_ext.rs index e567830d6c..0f882b4963 100644 --- a/crates/ui2/src/styled_ext.rs +++ b/crates/ui2/src/styled_ext.rs @@ -70,8 +70,7 @@ pub trait StyledExt: Styled + Sized { /// or other places that text needs to match the user's buffer font size. fn text_buffer(self, cx: &mut WindowContext) -> Self { let settings = ThemeSettings::get_global(cx); - - self.text_size(settings.buffer_font_size) + self.text_size(settings.buffer_font_size(cx)) } /// The [`Surface`](ui2::ElevationIndex::Surface) elevation level, located above the app background, is the standard level for all elements diff --git a/crates/zed2/src/zed2.rs b/crates/zed2/src/zed2.rs index d220250b3d..45ace75ebc 100644 --- a/crates/zed2/src/zed2.rs +++ b/crates/zed2/src/zed2.rs @@ -235,14 +235,13 @@ pub fn initialize_workspace(app_state: Arc, cx: &mut AppContext) { .open_urls(&[action.url.clone()]) }) .register_action(|_, action: &OpenBrowser, cx| cx.open_url(&action.url)) - //todo!(buffer font size) - // cx.add_global_action(move |_: &IncreaseBufferFontSize, cx| { - // theme::adjust_font_size(cx, |size| *size += 1.0) - // }); - // cx.add_global_action(move |_: &DecreaseBufferFontSize, cx| { - // theme::adjust_font_size(cx, |size| *size -= 1.0) - // }); - // cx.add_global_action(move |_: &ResetBufferFontSize, cx| theme::reset_font_size(cx)); + .register_action(move |_, _: &IncreaseBufferFontSize, cx| { + theme::adjust_font_size(cx, |size| *size += px(1.0)) + }) + .register_action(move |_, _: &DecreaseBufferFontSize, cx| { + theme::adjust_font_size(cx, |size| *size -= px(1.0)) + }) + .register_action(move |_, _: &ResetBufferFontSize, cx| theme::reset_font_size(cx)) .register_action(|_, _: &install_cli::Install, cx| { cx.spawn(|_, cx| async move { install_cli::install_cli(cx.deref())