Skip to main content

egui_pop_viewport/
gestures.rs

1//! The viewport's gestures, as data so an input profile can remap them.
2//! The pointer's come from the pointer profile in force
3//! (`egui_pop_core::pointer`, `docs/pointer-gestures.md`): what the
4//! wheel, a scroll, a pinch, each click, double click and drag does with
5//! each set of modifiers ([`ViewGestures`]); [`set_gestures`] sets them
6//! outright. They follow the shared interaction grammar:
7//!
8//! <div class="warning">Experimental: may change in any release
9//! (docs/stability.md).</div>
10//!
11//! - **Space held**: a drag pans, whatever is under the pointer (a handle
12//!   included);
13//! - **Escape** during a gesture puts things back as they were, with no
14//!   trace (a `Cancel` signal);
15//! - **double click** on the canvas: the profile's view action (fit by
16//!   default, or leave the view); on a handle, that handle's reset;
17//! - **the wheel** over the canvas zooms around the pointer or pans, as
18//!   the profile says; over a handle it adjusts that handle's quantity;
19//! - a primary drag on a handle moves it, whatever the profile.
20
21use egui::Context;
22use egui_pop_core::model::pointer::SCOPE_GRAPH;
23use egui_pop_core::pointer::{self as pointer_map};
24
25/// The views' gestures and their action, shared with every zoomable view
26/// (`egui_pop_core::pointer::view`).
27pub use egui_pop_core::pointer::view::{ViewAction, ViewGestures};
28
29/// The viewport's gestures: the image canvas's, shared with each pane of
30/// the library's comparison (`egui_pop_core::pointer::view`, which runs
31/// them).
32pub use egui_pop_core::pointer::view::CanvasGestures as Gestures;
33
34/// What the pointer does in a view's scope with the modifiers held now,
35/// in words (`Wheel: zoom ยท MiddleDrag: pan`): a canvas's hint, empty
36/// when nothing is bound with them.
37pub fn hint(ctx: &Context, scope: &str) -> String {
38    use crate::strings as s;
39    use egui_pop_core::strings::tr;
40    let modifiers = ctx.input(|i| i.modifiers);
41    let map = pointer_map::map(ctx);
42    pointer_map::hint(&map, scope, modifiers, |command| {
43        let key = match ViewAction::of(command) {
44            ViewAction::Nothing => return None,
45            ViewAction::Zoom => &s::HINT_ZOOM,
46            ViewAction::ZoomFree => &s::HINT_ZOOM_FREE,
47            ViewAction::Pan => &s::HINT_PAN,
48            ViewAction::PanX => &s::HINT_PAN_X,
49            ViewAction::PanY => &s::HINT_PAN_Y,
50            ViewAction::ZoomCycle => &s::HINT_ZOOM_CYCLE,
51            ViewAction::Fit => &s::HINT_FIT,
52            ViewAction::ZoomToggle => &s::HINT_ZOOM_TOGGLE,
53            ViewAction::Leave => &s::HINT_LEAVE,
54        };
55        Some(tr(ctx, key))
56    })
57}
58
59/// The node graph's pointer gestures in a context (its
60/// `canvas.graph` scope).
61pub fn graph_gestures(ctx: &Context) -> ViewGestures {
62    ViewGestures::from_map(&pointer_map::map(ctx), SCOPE_GRAPH)
63}
64
65/// The viewports' gestures in a context: those set, else the pointer
66/// profile's.
67pub fn gestures(ctx: &Context) -> Gestures {
68    egui_pop_core::pointer::view::canvas_gestures(ctx)
69}
70
71/// Sets the viewports' gestures in a context, over the pointer
72/// profile's (the comparison's panes follow them too).
73pub fn set_gestures(ctx: &Context, gestures: Gestures) {
74    egui_pop_core::pointer::view::set_canvas_gestures(ctx, gestures);
75}