mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
Allow mouse wheel zoom in terminals and diff views (#66296)
## Summary
I like to use the mouse wheel zoom functionality on the code to change
the size in Zed. It's very useful when switching between screens of
different sizes for example.
Unfortunately it only works when a file is opened, not in the diff view
or the terminal, and diff view is pretty much the only view I use
nowadays. So whenever I want to change the font sizes, I have to click
"Open file" to zoom there.
This has been annoying me for months so I fixed it with the help of
Claude Opus 5.5 High.
The agent diff and terminals in the agent panel still don't zoom on
purpose (decision made in #53598 apparently).
### Before
https://github.com/user-attachments/assets/eb47fae2-d82f-4bfc-adbe-3a3c7223bacb
### After
https://github.com/user-attachments/assets/84cd86ae-d589-457b-8ab0-733cfb60177f
## Testing
I tested on macOS 27.0.1 (26A434) and Zed 1.27.0 (37faef81f7).
### Reproduction steps
- Enable `mouse_wheel_zoom` in settings.
- Open any file diff and the terminal view.
- Do Cmd + Scroll (or Ctrl + Scroll on non-Macs) in the code and
terminal.
- Previously, they didn't change the font sizes. After this commit they
do.
## Self-review
- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.
Release Notes:
- Added support for mouse-wheel font zoom in diff views and terminals.
---------
Co-authored-by: dino <dinojoaocosta@gmail.com>
This commit is contained in:
@@ -510,17 +510,7 @@ impl EditorElement {
|
||||
&& editor.read(cx).enable_mouse_wheel_zoom
|
||||
&& EditorSettings::get_global(cx).mouse_wheel_zoom
|
||||
{
|
||||
let delta_y = match event.delta {
|
||||
ScrollDelta::Pixels(pixels) => pixels.y.into(),
|
||||
ScrollDelta::Lines(lines) => lines.y,
|
||||
};
|
||||
|
||||
if delta_y > 0.0 {
|
||||
theme_settings::increase_buffer_font_size(cx);
|
||||
} else if delta_y < 0.0 {
|
||||
theme_settings::decrease_buffer_font_size(cx);
|
||||
}
|
||||
|
||||
theme_settings::adjust_buffer_font_size_for_scroll(event.delta, cx);
|
||||
cx.stop_propagation();
|
||||
} else {
|
||||
let scroll_sensitivity = {
|
||||
|
||||
@@ -627,7 +627,6 @@ impl SplittableEditor {
|
||||
editor.disable_runnables();
|
||||
editor.disable_code_lens(cx);
|
||||
editor.disable_inline_diagnostics();
|
||||
editor.disable_mouse_wheel_zoom();
|
||||
editor.set_minimap_visibility(crate::MinimapVisibility::Disabled, window, cx);
|
||||
editor.set_diff_hunk_renderer(Some(Arc::new(DefaultDiffHunkRenderer)), cx);
|
||||
editor
|
||||
@@ -728,6 +727,7 @@ impl SplittableEditor {
|
||||
});
|
||||
|
||||
let splittable = cx.weak_entity();
|
||||
let enable_mouse_wheel_zoom = self.rhs_editor.read(cx).enable_mouse_wheel_zoom;
|
||||
let rhs_editor = self.rhs_editor.downgrade();
|
||||
let lhs_editor = cx.new(|cx| {
|
||||
let mut editor =
|
||||
@@ -746,8 +746,12 @@ impl SplittableEditor {
|
||||
editor.disable_lsp_data();
|
||||
editor.disable_runnables();
|
||||
editor.disable_diagnostics(cx);
|
||||
editor.disable_mouse_wheel_zoom();
|
||||
editor.set_minimap_visibility(crate::MinimapVisibility::Disabled, window, cx);
|
||||
|
||||
if !enable_mouse_wheel_zoom {
|
||||
editor.disable_mouse_wheel_zoom();
|
||||
}
|
||||
|
||||
editor
|
||||
});
|
||||
|
||||
@@ -2340,7 +2344,7 @@ mod tests {
|
||||
use pretty_assertions::assert_eq;
|
||||
use project::Project;
|
||||
use rand::rngs::StdRng;
|
||||
use settings::{DiffViewStyle, SettingsStore};
|
||||
use settings::{DiffViewStyle, Settings as _, SettingsStore};
|
||||
use ui::{VisualContext as _, div, px};
|
||||
use util::rel_path::rel_path;
|
||||
use workspace::{Item, MultiWorkspace};
|
||||
@@ -2700,6 +2704,93 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_lhs_mouse_wheel_zoom_respects_rhs_configuration(cx: &mut gpui::TestAppContext) {
|
||||
let (editor, cx) = init_test(cx, SoftWrap::None, DiffViewStyle::Split).await;
|
||||
|
||||
cx.update(|_, cx| {
|
||||
cx.update_global::<SettingsStore, _>(|store, cx| {
|
||||
store.update_user_settings(cx, |settings| {
|
||||
settings.editor.minimum_split_diff_width = Some(0.);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
let set_mouse_wheel_zoom = |enabled: bool, cx: &mut VisualTestContext| {
|
||||
cx.update(|_, cx| {
|
||||
cx.update_global::<SettingsStore, _>(|store, cx| {
|
||||
store.update_user_settings(cx, |settings| {
|
||||
settings.editor.mouse_wheel_zoom = Some(enabled);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
let buffer_font_size = |cx: &VisualTestContext| {
|
||||
cx.read(|cx| theme_settings::ThemeSettings::get_global(cx).buffer_font_size(cx))
|
||||
};
|
||||
|
||||
let zoom_scroll =
|
||||
|position: gpui::Point<Pixels>, lines: f32, cx: &mut VisualTestContext| {
|
||||
let modifiers = cx.read(|cx| gpui::Modifiers::secondary_key(cx));
|
||||
cx.simulate_event(gpui::ScrollEvent {
|
||||
position,
|
||||
delta: gpui::ScrollDelta::Lines(gpui::point(0., lines)),
|
||||
modifiers,
|
||||
..Default::default()
|
||||
});
|
||||
};
|
||||
|
||||
let lhs_position = |cx: &VisualTestContext| {
|
||||
editor.read_with(cx, |editor, cx| {
|
||||
editor
|
||||
.lhs_editor()
|
||||
.expect("split editor should have a LHS editor")
|
||||
.read(cx)
|
||||
.last_bounds()
|
||||
.expect("LHS editor should have been rendered")
|
||||
.center()
|
||||
})
|
||||
};
|
||||
|
||||
let workspace = editor.read_with(cx, |editor, _| editor.workspace.clone());
|
||||
workspace
|
||||
.update_in(cx, |workspace, window, cx| {
|
||||
workspace.add_item_to_center(Box::new(editor.clone()), window, cx);
|
||||
})
|
||||
.expect("workspace should exist");
|
||||
cx.run_until_parked();
|
||||
set_mouse_wheel_zoom(true, cx);
|
||||
let position = lhs_position(cx);
|
||||
let initial_font_size = buffer_font_size(cx);
|
||||
zoom_scroll(position, 1., cx);
|
||||
assert!(buffer_font_size(cx) > initial_font_size);
|
||||
zoom_scroll(position, -1., cx);
|
||||
assert_eq!(buffer_font_size(cx), initial_font_size);
|
||||
zoom_scroll(position, -1., cx);
|
||||
assert!(buffer_font_size(cx) < initial_font_size);
|
||||
|
||||
set_mouse_wheel_zoom(false, cx);
|
||||
let initial_font_size = buffer_font_size(cx);
|
||||
zoom_scroll(position, 1., cx);
|
||||
assert_eq!(buffer_font_size(cx), initial_font_size);
|
||||
|
||||
set_mouse_wheel_zoom(true, cx);
|
||||
editor.update_in(cx, |editor, window, cx| {
|
||||
// Recreate the LHS editor to exercise inheritance rather than
|
||||
// disabling it directly.
|
||||
editor.unsplit(window, cx);
|
||||
editor.update_editors(cx, |editor, _| editor.disable_mouse_wheel_zoom());
|
||||
editor.split(window, cx);
|
||||
});
|
||||
|
||||
cx.run_until_parked();
|
||||
let position = lhs_position(cx);
|
||||
let initial_font_size = buffer_font_size(cx);
|
||||
zoom_scroll(position, 1., cx);
|
||||
assert_eq!(buffer_font_size(cx), initial_font_size);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_expand_excerpt_with_hunk_before_excerpt_start(cx: &mut gpui::TestAppContext) {
|
||||
use rope::Point;
|
||||
|
||||
@@ -769,6 +769,14 @@ impl TerminalView {
|
||||
}
|
||||
|
||||
fn scroll_wheel(&mut self, event: &ScrollEvent, cx: &mut Context<Self>) {
|
||||
if event.modifiers.secondary(cx)
|
||||
&& matches!(self.mode, TerminalMode::Standalone)
|
||||
&& EditorSettings::get_global(cx).mouse_wheel_zoom
|
||||
{
|
||||
theme_settings::adjust_buffer_font_size_for_scroll(event.delta, cx);
|
||||
return cx.stop_propagation();
|
||||
}
|
||||
|
||||
let terminal_content = self.terminal.read(cx).last_content();
|
||||
|
||||
if self.block_below_cursor.is_some() && terminal_content.display_offset == 0 {
|
||||
@@ -2313,7 +2321,7 @@ fn first_project_directory(workspace: &Workspace, cx: &App) -> Option<PathBuf> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use gpui::{TestAppContext, UpdateGlobal, VisualTestContext};
|
||||
use gpui::{ScrollDelta, TestAppContext, UpdateGlobal, VisualTestContext};
|
||||
use project::{Entry, Project, ProjectPath, Worktree};
|
||||
use remote::RemoteClient;
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -2413,6 +2421,90 @@ mod tests {
|
||||
});
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn test_mouse_wheel_zoom(cx: &mut TestAppContext) {
|
||||
let (project, _workspace, window_handle) = init_test_with_window(cx).await;
|
||||
let (_pane, _terminal, terminal_view) =
|
||||
add_display_only_terminal(&project, window_handle, true, false, cx);
|
||||
|
||||
let mut cx = VisualTestContext::from_window(window_handle.into(), cx);
|
||||
|
||||
let set_mouse_wheel_zoom = |enabled: bool, cx: &mut VisualTestContext| {
|
||||
cx.update(|_window, cx| {
|
||||
SettingsStore::update_global(cx, |store, cx| {
|
||||
store.update_user_settings(cx, |settings| {
|
||||
settings.editor.mouse_wheel_zoom = Some(enabled);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
let buffer_font_size = |cx: &VisualTestContext| {
|
||||
cx.read(|cx| theme_settings::ThemeSettings::get_global(cx).buffer_font_size(cx))
|
||||
};
|
||||
|
||||
let zoom_scroll =
|
||||
|position: gpui::Point<Pixels>, lines: f32, cx: &mut VisualTestContext| {
|
||||
let modifiers = cx.read(|cx| gpui::Modifiers::secondary_key(cx));
|
||||
|
||||
cx.simulate_event(ScrollEvent {
|
||||
position,
|
||||
delta: ScrollDelta::Lines(gpui::point(0., lines)),
|
||||
modifiers,
|
||||
..Default::default()
|
||||
});
|
||||
};
|
||||
|
||||
cx.simulate_resize(gpui::size(px(400.), px(200.)));
|
||||
cx.draw(
|
||||
gpui::Point::default(),
|
||||
gpui::size(px(400.), px(200.)),
|
||||
|_, _| terminal_view.clone().into_any_element(),
|
||||
);
|
||||
cx.run_until_parked();
|
||||
|
||||
let position =
|
||||
terminal_view.read_with(&cx, |view, cx| view.terminal_bounds(cx).bounds.center());
|
||||
|
||||
set_mouse_wheel_zoom(true, &mut cx);
|
||||
let initial_font_size = buffer_font_size(&cx);
|
||||
zoom_scroll(position, 1., &mut cx);
|
||||
assert!(buffer_font_size(&cx) > initial_font_size);
|
||||
zoom_scroll(position, -1., &mut cx);
|
||||
assert_eq!(buffer_font_size(&cx), initial_font_size);
|
||||
zoom_scroll(position, -1., &mut cx);
|
||||
assert!(buffer_font_size(&cx) < initial_font_size);
|
||||
|
||||
set_mouse_wheel_zoom(false, &mut cx);
|
||||
let initial_font_size = buffer_font_size(&cx);
|
||||
zoom_scroll(position, 1., &mut cx);
|
||||
assert_eq!(buffer_font_size(&cx), initial_font_size);
|
||||
|
||||
set_mouse_wheel_zoom(true, &mut cx);
|
||||
cx.update(|window, cx| {
|
||||
terminal_view.update(cx, |terminal_view, cx| {
|
||||
terminal_view.set_embedded_mode(None, cx);
|
||||
|
||||
// Embedded terminals ignore scroll events unless focused.
|
||||
window.focus(&terminal_view.focus_handle, cx)
|
||||
});
|
||||
});
|
||||
|
||||
cx.draw(
|
||||
gpui::Point::default(),
|
||||
gpui::size(px(400.), px(200.)),
|
||||
|_, _| terminal_view.clone().into_any_element(),
|
||||
);
|
||||
cx.run_until_parked();
|
||||
|
||||
let position =
|
||||
terminal_view.read_with(&cx, |view, cx| view.terminal_bounds(cx).bounds.center());
|
||||
|
||||
let initial_font_size = buffer_font_size(&cx);
|
||||
zoom_scroll(position, 1., &mut cx);
|
||||
assert_eq!(buffer_font_size(&cx), initial_font_size);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn edit_menu_copy_and_paste_are_available_when_terminal_is_focused(
|
||||
cx: &mut TestAppContext,
|
||||
|
||||
@@ -12,7 +12,7 @@ use std::sync::Arc;
|
||||
|
||||
use ::settings::{IntoGpui, Settings, SettingsStore};
|
||||
use anyhow::{Context as _, Result};
|
||||
use gpui::{App, Font, HighlightStyle, Pixels, Refineable, px};
|
||||
use gpui::{App, Font, HighlightStyle, Pixels, Refineable, ScrollDelta, px};
|
||||
use gpui_util::ResultExt;
|
||||
use theme::{
|
||||
AccentColors, Appearance, AppearanceContent, DEFAULT_DARK_THEME, DEFAULT_ICON_THEME_NAME,
|
||||
@@ -434,6 +434,22 @@ pub fn merge_accent_colors(
|
||||
}
|
||||
}
|
||||
|
||||
/// Increases or decreases the buffer font size based on the scroll delta,
|
||||
/// without persisting the result in the settings.
|
||||
/// This will be effective until the app is restarted.
|
||||
pub fn adjust_buffer_font_size_for_scroll(delta: ScrollDelta, cx: &mut App) {
|
||||
let delta_y = match delta {
|
||||
ScrollDelta::Pixels(pixels) => f32::from(pixels.y),
|
||||
ScrollDelta::Lines(lines) => lines.y,
|
||||
};
|
||||
|
||||
if delta_y > 0.0 {
|
||||
increase_buffer_font_size(cx);
|
||||
} else if delta_y < 0.0 {
|
||||
decrease_buffer_font_size(cx);
|
||||
}
|
||||
}
|
||||
|
||||
/// Increases the buffer font size by 1 pixel, without persisting the result in the settings.
|
||||
/// This will be effective until the app is restarted.
|
||||
pub fn increase_buffer_font_size(cx: &mut App) {
|
||||
|
||||
Reference in New Issue
Block a user