input: Refactor common logic into update_zoom

This commit is contained in:
Victoria Brekenfeld 2025-02-13 21:03:06 +01:00 committed by Victoria Brekenfeld
parent b7d4a66c22
commit e0530d2723
2 changed files with 48 additions and 63 deletions

View file

@ -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<State>, 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(

View file

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