Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Allow mouse wheel zoom in terminals and diff views (#66296)

Andri Soone504a7f2cf25b
diff --git a/crates/editor/src/element/mouse.rs b/crates/editor/src/element/mouse.rs
index 6b84ce18ce9..35edcc48aae 100644
--- a/crates/editor/src/element/mouse.rs
+++ b/crates/editor/src/element/mouse.rs
@@ -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 = {
diff --git a/crates/editor/src/split.rs b/crates/editor/src/split.rs
index 555a7ea407e..577b8cd6497 100644
--- a/crates/editor/src/split.rs
+++ b/crates/editor/src/split.rs
@@ -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;
diff --git a/crates/terminal_view/src/terminal_view.rs b/crates/terminal_view/src/terminal_view.rs
index 0eb62438a52..cc121674c16 100644
--- a/crates/terminal_view/src/terminal_view.rs
+++ b/crates/terminal_view/src/terminal_view.rs
@@ -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,
diff --git a/crates/theme_settings/src/theme_settings.rs b/crates/theme_settings/src/theme_settings.rs
index f56a46b4050..c18212c597f 100644
--- a/crates/theme_settings/src/theme_settings.rs
+++ b/crates/theme_settings/src/theme_settings.rs
@@ -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) {