Enable buffer font size adjustment in zed2

Max Brunsfeld and Nathan created

Co-authored-by: Nathan <nathan@zed.dev>

Change summary

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(-)

Detailed changes

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()),

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<G: Any>(&mut self) -> G {
         let global_type = TypeId::of::<G>();
         *self

crates/theme2/src/settings.rs 🔗

@@ -27,7 +27,7 @@ pub struct ThemeSettings {
 }
 
 #[derive(Default)]
-pub struct AdjustedBufferFontSize(Option<Pixels>);
+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::<AdjustedBufferFontSize>()
-            .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::<AdjustedBufferFontSize>()
+            .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::<AdjustedBufferFontSize>().0 {
+    if let Some(AdjustedBufferFontSize(adjusted_size)) = cx.try_global::<AdjustedBufferFontSize>() {
         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::<AdjustedBufferFontSize>()
-        .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::<AdjustedBufferFontSize>()
+        .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::<AdjustedBufferFontSize>() {
-        cx.global_mut::<AdjustedBufferFontSize>().0 = None;
+        cx.remove_global::<AdjustedBufferFontSize>();
         cx.refresh();
     }
 }

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

crates/zed2/src/zed2.rs 🔗

@@ -235,14 +235,13 @@ pub fn initialize_workspace(app_state: Arc<AppState>, 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())