egui_pop_viewport/hooks.rs
1//! The application's way into each step of the viewport's lifecycle.
2//!
3//! <div class="warning">Experimental: may change in any release
4//! (docs/stability.md).</div>
5//!
6//! The widget does the plumbing; at each step an application may:
7//!
8//! - **observe**: every change is a [`Signal`](crate::Signal) in
9//! [`ViewportOutput::signals`](crate::ViewportOutput) (forward them to
10//! the host's journal);
11//! - **veto or redirect**: a *before* hook answers a [`Verdict`] (clamp
12//! the zoom, keep the view inside the image, snap a handle);
13//! - **replace**: a *run* hook takes the place of the default action and
14//! may call it ([`Hooks::on_double_click`]);
15//! - **extend**: an *after* hook adds to what was done
16//! ([`Hooks::under_overlays`] draws over the image and under the
17//! overlays, [`Hooks::after_paint`] over everything).
18//!
19//! Every hook is optional; without one the default applies. What hooks
20//! decided is kept in the state's [`trace`](crate::ViewportState::trace)
21//! and shown by the debug bridge's `viewport` collector.
22
23use std::fmt;
24
25use egui::{Painter, Pos2};
26
27use crate::display::{DisplayTarget, Resolution};
28use crate::overlay::HandleKind;
29use crate::source::Readout;
30use crate::view::{Mapping, View};
31use crate::widget::Compare;
32
33/// What a *before* hook decides.
34#[derive(Clone, Debug, PartialEq)]
35pub enum Verdict<T> {
36 /// Go ahead as proposed.
37 Allow,
38 /// Do nothing: the step does not happen.
39 Veto,
40 /// Go ahead with this value instead (a clamped zoom, a snapped point).
41 Redirect(T),
42}
43
44/// What made the view change (shared with every image canvas,
45/// `egui_pop_core::pointer::view`).
46pub use egui_pop_core::pointer::view::Cause;
47
48/// A change of view the widget proposes.
49#[derive(Clone, Copy, Debug, PartialEq)]
50pub struct ViewChange {
51 pub from: View,
52 pub to: View,
53 pub cause: Cause,
54}
55
56/// A handle move the widget proposes.
57#[derive(Clone, Debug, PartialEq)]
58pub struct HandleMove {
59 pub overlay: String,
60 pub handle: u32,
61 pub kind: HandleKind,
62 /// Where the handle was when the gesture began (image pixels).
63 pub start: [f64; 2],
64 /// Where the handle is now (image pixels).
65 pub from: [f64; 2],
66 /// Where the pointer would put it.
67 pub to: [f64; 2],
68 /// The keys held (Shift to keep a direction, Alt to move a control
69 /// alone...).
70 pub modifiers: egui::Modifiers,
71}
72
73type BeforeHandle = Box<dyn FnMut(&HandleMove, &Mapping) -> Verdict<[f64; 2]>>;
74type BeforeCompare = Box<dyn FnMut(&Compare, &Compare) -> Verdict<Compare>>;
75type OnReadout = Box<dyn FnMut(&Readout) -> String>;
76type BeforeView = Box<dyn FnMut(&ViewChange, &Mapping) -> Verdict<View>>;
77type RunDoubleClick =
78 Box<dyn FnMut(Pos2, &Mapping, &mut dyn FnMut() -> Option<View>) -> Option<View>>;
79type AfterPaint = Box<dyn FnMut(&Painter, &Mapping)>;
80type BeforeDisplay = Box<dyn FnMut(&Resolution) -> Verdict<DisplayTarget>>;
81
82/// The hooks of one viewport (all optional). Set them on the
83/// [`ViewportState`](crate::ViewportState) with
84/// [`hooks_mut`](crate::ViewportState::hooks_mut).
85#[derive(Default)]
86pub struct Hooks {
87 /// Before the view changes: allow, veto or redirect (clamp the zoom,
88 /// keep the image in view). Not asked when a gesture is cancelled:
89 /// that puts back a view already allowed.
90 pub before_view: Option<BeforeView>,
91 /// Replaces the double click on the canvas (not one a handle took).
92 /// Gets the pointer, the mapping and the profile's default action;
93 /// returns the view to show, or `None` for no change. A hook that
94 /// handles the click itself does not call the default; one that
95 /// passes it on calls it and returns its view. Calling it runs the
96 /// whole default: when the profile's action is to leave the view it
97 /// returns `None` and the viewport sends `Signal::Leave`, exactly as
98 /// without a hook.
99 pub on_double_click: Option<RunDoubleClick>,
100 /// Before a handle moves: allow, veto (it stays) or redirect (snap
101 /// it to a grid, keep it inside the image).
102 pub before_handle: Option<BeforeHandle>,
103 /// Before the comparison changes (the current one, the proposed
104 /// one): allow, veto or redirect. Over the `before` key it is asked
105 /// once when the key goes down and once when it comes up, the
106 /// latter only after a press it let through.
107 pub before_compare: Option<BeforeCompare>,
108 /// Replaces the readout's text.
109 pub on_readout: Option<OnReadout>,
110 /// After the image, before the overlays: draw between them (a
111 /// mask's tint, a selection's veil), in image coordinates through
112 /// the mapping; the overlays and their handles stay on top.
113 pub under_overlays: Option<AfterPaint>,
114 /// After the image, the overlays and their handles are drawn: draw
115 /// more on top, in image coordinates through the mapping.
116 pub after_paint: Option<AfterPaint>,
117 /// Before the viewport draws for the target it resolved (what is
118 /// wanted against what the surface carries): allow, veto (draw
119 /// sRGB) or redirect (draw for another target the surface carries,
120 /// a soft proof for example). Asked when the resolution changes.
121 pub before_display: Option<BeforeDisplay>,
122}
123
124impl fmt::Debug for Hooks {
125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126 f.debug_struct("Hooks")
127 .field("before_view", &self.before_view.is_some())
128 .field("on_double_click", &self.on_double_click.is_some())
129 .field("before_handle", &self.before_handle.is_some())
130 .field("before_compare", &self.before_compare.is_some())
131 .field("on_readout", &self.on_readout.is_some())
132 .field("under_overlays", &self.under_overlays.is_some())
133 .field("after_paint", &self.after_paint.is_some())
134 .field("before_display", &self.before_display.is_some())
135 .finish()
136 }
137}
138
139impl Hooks {
140 /// The hooks that are set, by name.
141 pub fn names(&self) -> Vec<&'static str> {
142 let mut v = Vec::new();
143 if self.before_view.is_some() {
144 v.push("before_view");
145 }
146 if self.on_double_click.is_some() {
147 v.push("on_double_click");
148 }
149 if self.before_handle.is_some() {
150 v.push("before_handle");
151 }
152 if self.before_compare.is_some() {
153 v.push("before_compare");
154 }
155 if self.on_readout.is_some() {
156 v.push("on_readout");
157 }
158 if self.under_overlays.is_some() {
159 v.push("under_overlays");
160 }
161 if self.after_paint.is_some() {
162 v.push("after_paint");
163 }
164 if self.before_display.is_some() {
165 v.push("before_display");
166 }
167 v
168 }
169}
170
171/// One decision of a hook, kept for the debug bridge.
172#[derive(Clone, Debug, PartialEq)]
173pub struct TraceEntry {
174 /// Input time, in seconds.
175 pub time: f64,
176 /// The hook (`before_view`...).
177 pub hook: &'static str,
178 /// `allow`, `veto`, `redirect` or `run`.
179 pub verdict: &'static str,
180 /// What was proposed and what came of it.
181 pub detail: String,
182}
183
184/// How many trace entries a state keeps.
185pub const TRACE_KEPT: usize = 64;