From e0530d2723b461b98f63d48891a848516cc00bfd Mon Sep 17 00:00:00 2001 From: Victoria Brekenfeld Date: Thu, 13 Feb 2025 21:03:06 +0100 Subject: [PATCH] input: Refactor common logic into `update_zoom` --- src/input/actions.rs | 79 ++++++++++++++++++++++++++------------------ src/input/mod.rs | 32 ++---------------- 2 files changed, 48 insertions(+), 63 deletions(-) diff --git a/src/input/actions.rs b/src/input/actions.rs index 5961ef49..8b709e6c 100644 --- a/src/input/actions.rs +++ b/src/input/actions.rs @@ -1027,46 +1027,17 @@ impl State { Action::Spawn(command) => self.spawn_command(command), x @ Action::ZoomIn | x @ Action::ZoomOut => { - let mut shell = self.common.shell.write().unwrap(); - let (zoom_seat, current_level) = shell - .zoom_level(None) - .map(|(s, _, l)| (s, l)) - .unwrap_or_else(|| (seat.clone(), 1.0)); - - if current_level == 1. && matches!(x, Action::ZoomOut) { - return; - } - let new_level = if current_level == 1. && matches!(x, Action::ZoomIn) { - self.common.config.dynamic_conf.zoom_state().last_level - } else { + let change = { let increment = self.common.config.cosmic_conf.accessibility_zoom.increment as f64 / 100.0; match x { - Action::ZoomIn => current_level + increment, - Action::ZoomOut => (current_level - increment).max(1.0), + Action::ZoomIn => increment, + Action::ZoomOut => -increment, _ => unreachable!(), } }; - if &zoom_seat == seat { - shell.trigger_zoom( - seat, - new_level, - self.common.config.cosmic_conf.accessibility_zoom.view_moves, - true, - ); - - if new_level > 1. - && self - .common - .config - .cosmic_conf - .accessibility_zoom - .start_on_login - { - self.common.config.dynamic_conf.zoom_state_mut().last_level = new_level; - } - } + self.update_zoom(seat, change, true); } // Do nothing @@ -1116,6 +1087,48 @@ impl State { } }); } + + pub fn update_zoom(&mut self, seat: &Seat, change: f64, animate: bool) { + let mut shell = self.common.shell.write().unwrap(); + let (zoom_seat, current_level) = shell + .zoom_state() + .map(|state| (state.current_seat(), state.current_level())) + .unwrap_or_else(|| (seat.clone(), 1.0)); + let change = if current_level == 1.0 && animate { + self.common.config.dynamic_conf.zoom_state().last_level + } else { + change + }; + + if current_level == 1. && change <= 0. { + return; + } + + if zoom_seat == *seat { + let new_level = (current_level + change).max(1.0); + shell.trigger_zoom( + &seat, + new_level, + &self.common.config.cosmic_conf.accessibility_zoom, + animate, + &self.common.event_loop_handle, + ); + + /* TODO: debounce + if new_level > 1. + && self + .common + .config + .cosmic_conf + .accessibility_zoom + .start_on_login + { + self.common.config.dynamic_conf.zoom_state_mut().last_level = + new_level; + } + */ + } + } } fn to_next_workspace( diff --git a/src/input/mod.rs b/src/input/mod.rs index ed9c2712..d719f5c0 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -842,40 +842,12 @@ impl State { .or_else(|| event.amount(Axis::Vertical)) .map(|val| val * scroll_factor) { - let mut shell = self.common.shell.write().unwrap(); - let (zoom_seat, current_level) = shell - .zoom_level(None) - .map(|(s, _, l)| (s, l)) - .unwrap_or_else(|| (seat.clone(), 1.0)); - - if current_level == 1. && percentage.is_sign_positive() { - return; - } if event.source() == AxisSource::Wheel { percentage *= 5.; } - let new_level = (current_level - percentage as f64 / 100.).max(1.0); - if zoom_seat == seat { - shell.trigger_zoom( - &seat, - new_level, - self.common.config.cosmic_conf.accessibility_zoom.view_moves, - event.source() == AxisSource::Wheel, - ); - - if new_level > 1. - && self - .common - .config - .cosmic_conf - .accessibility_zoom - .start_on_login - { - self.common.config.dynamic_conf.zoom_state_mut().last_level = - new_level; - } - } + let change = -(percentage as f64 / 100.); + self.update_zoom(&seat, change, event.source() == AxisSource::Wheel); } } else { let mut frame = AxisFrame::new(event.time_msec()).source(event.source());