egui_pop_core/canvas.rs
1//! The canvas in use, and the navigation commands sent to it.
2//!
3//! <div class="warning">Experimental: may change in any release
4//! (docs/stability.md).</div>
5//!
6//! A canvas (the node graph, the image viewport, a plot) marks itself
7//! active each frame the pointer is over it or it has the keyboard
8//! ([`mark_active`]). The application's `view.*` commands
9//! (`view.fit`, `view.frame_selection`, `view.zoom_in`, ...) go to the
10//! active canvas through [`request`], and the canvas takes them at its
11//! next frame ([`take`]); the `canvasActive` context key says whether
12//! one is active, so the commands' keys reach nothing else. A canvas
13//! publishes its zoom ([`set_zoom`]) for a zoom control to show; hand
14//! mode ([`hand`]) is shared by every canvas. The navigation model
15//! itself is the viewport crate's (`egui_pop_viewport::navigation`).
16
17use egui::{Context, Id};
18
19use crate::state::Slot;
20
21/// The canvas marked last, and the frame it was marked in.
22#[derive(Clone, Copy, Debug, Default, PartialEq)]
23struct Active {
24 id: Option<Id>,
25 frame: u64,
26}
27
28const ACTIVE: Slot<Active> = Slot::new("core.canvas.active");
29/// Commands waiting for a canvas, by its id.
30const QUEUE: Slot<Vec<String>> = Slot::new("core.canvas.queue");
31
32/// Marks a canvas as the one in use this frame (the pointer is over it,
33/// or it has the keyboard).
34pub fn mark_active(ctx: &Context, id: Id) {
35 let frame = ctx.cumulative_frame_nr();
36 ACTIVE.set(
37 ctx,
38 Active {
39 id: Some(id),
40 frame,
41 },
42 );
43}
44
45/// The canvas in use: one marked this frame or the one before.
46pub fn active(ctx: &Context) -> Option<Id> {
47 let now = ctx.cumulative_frame_nr();
48 let a = ACTIVE.get_or_default(ctx);
49 a.id.filter(|_| a.frame + 1 >= now)
50}
51
52/// The canvas marked last, however long ago (where an agent's command
53/// goes when no canvas is in use).
54pub fn last(ctx: &Context) -> Option<Id> {
55 ACTIVE.get_or_default(ctx).id
56}
57
58/// Sends a navigation command (`view.fit`, ...) to the canvas in use,
59/// else to the one marked last. Whether there was one.
60pub fn request(ctx: &Context, command: &str) -> bool {
61 let Some(id) = active(ctx).or_else(|| last(ctx)) else {
62 return false;
63 };
64 request_to(ctx, id, command);
65 true
66}
67
68/// Sends a navigation command to a canvas by its id.
69pub fn request_to(ctx: &Context, id: Id, command: &str) {
70 let command = command.to_string();
71 QUEUE.keyed(id).update(ctx, |q| q.push(command));
72 ctx.request_repaint();
73}
74
75/// The commands waiting for a canvas, taken.
76pub fn take(ctx: &Context, id: Id) -> Vec<String> {
77 let slot = QUEUE.keyed(id);
78 if slot.get(ctx).is_none_or(|q| q.is_empty()) {
79 return Vec::new();
80 }
81 let mut out = Vec::new();
82 slot.update(ctx, |q| out = std::mem::take(q));
83 out
84}
85
86const HAND: Slot<bool> = Slot::new("core.canvas.hand");
87
88/// Whether hand mode is on: a primary drag pans every canvas (for a pen,
89/// a touch screen or a mouse without a middle button). `view.hand`
90/// toggles it on whichever canvas takes it.
91pub fn hand(ctx: &Context) -> bool {
92 HAND.get_or_default(ctx)
93}
94
95/// Turns hand mode on or off.
96pub fn set_hand(ctx: &Context, on: bool) {
97 HAND.set(ctx, on);
98 ctx.request_repaint();
99}
100
101/// A canvas's zoom, by its id: what a zoom control shows.
102const ZOOM: Slot<f32> = Slot::new("core.canvas.zoom");
103
104/// Publishes a canvas's zoom (1 is 100 %: one content unit, or one
105/// image pixel, to a point), each frame it draws.
106pub fn set_zoom(ctx: &Context, id: Id, zoom: f32) {
107 let slot = ZOOM.keyed(id);
108 if slot.get(ctx) != Some(zoom) {
109 slot.set(ctx, zoom);
110 }
111}
112
113/// A canvas's zoom as it last published it.
114pub fn zoom(ctx: &Context, id: Id) -> Option<f32> {
115 ZOOM.keyed(id).get(ctx)
116}