Skip to content

Commit

Permalink
Merge pull request #228 from Urcra/urcra/no-ctrl-zoom
Browse files Browse the repository at this point in the history
Allow zooming without ctrl key
  • Loading branch information
podusowski authored Nov 17, 2024
2 parents dc07d5b + 0029466 commit ea956ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.

## Unreleased

* Add option to zoom without holding ctrl on native and web
* Fixed zoom not following cursor if the map wasn't moved.
* Fixed spurious "Error from IO runtime" at shutdown.

Expand Down
30 changes: 28 additions & 2 deletions walkers/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub struct Map<'a, 'b, 'c> {

zoom_gesture_enabled: bool,
drag_gesture_enabled: bool,
zoom_with_ctrl: bool,
}

impl<'a, 'b, 'c> Map<'a, 'b, 'c> {
Expand All @@ -64,6 +65,7 @@ impl<'a, 'b, 'c> Map<'a, 'b, 'c> {
plugins: Vec::default(),
zoom_gesture_enabled: true,
drag_gesture_enabled: true,
zoom_with_ctrl: true,
}
}

Expand All @@ -87,6 +89,20 @@ impl<'a, 'b, 'c> Map<'a, 'b, 'c> {
self.drag_gesture_enabled = enabled;
self
}

/// Sets the zoom behaviour
///
/// When enabled zoom is done with mouse wheel while holding <kbd>ctrl</kbd> key on native
/// and web. Panning is done with mouse wheel without <kbd>ctrl</kbd> key
///
/// When disabled, zooming can be done without holding <kbd>ctrl</kbd> key
/// but panning with mouse wheel is disabled
///
/// Has no effect on Android
pub fn zoom_with_ctrl(mut self, enabled: bool) -> Self {
self.zoom_with_ctrl = enabled;
self
}
}

/// Projects geographical position into pixels on the viewport, suitable for [`egui::Painter`].
Expand Down Expand Up @@ -154,7 +170,14 @@ impl Map<'_, '_, '_> {
/// Handle zoom and drag inputs, and recalculate everything accordingly.
/// Returns `false` if no gesture handled.
fn handle_gestures(&mut self, ui: &mut Ui, response: &Response) -> bool {
let zoom_delta = ui.input(|input| input.zoom_delta()) as f64;
let mut zoom_delta = ui.input(|input| input.zoom_delta()) as f64;

if !self.zoom_with_ctrl && zoom_delta == 1.0 {
// We only use the raw scroll values, if we are zooming without ctrl,
// and zoom_delta is not already over/under 1.0 (eg. a ctrl + scroll event or a pinch zoom)
// These values seem to corrospond to the same values as one would get in `zoom_delta()`
zoom_delta = ui.input(|input| (1.0 + input.smooth_scroll_delta.y / 200.0)) as f64
};

let mut changed = false;

Expand Down Expand Up @@ -207,7 +230,10 @@ impl Map<'_, '_, '_> {
.recalculate_drag(response, self.my_position);
}

if ui.ui_contains_pointer() {
// Only enable panning with mouse_wheel if we are zooming with ctrl. But always allow touch devices to pan
let panning_enabled = ui.input(|i| i.any_touches()) || self.zoom_with_ctrl;

if ui.ui_contains_pointer() && panning_enabled {
// Panning by scrolling, e.g. two-finger drag on a touchpad:
let scroll_delta = ui.input(|i| i.smooth_scroll_delta);
if scroll_delta != Vec2::ZERO {
Expand Down

0 comments on commit ea956ec

Please sign in to comment.