mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 04:04:25 +00:00
ui-contract: a modal's canvas reach names the gestures it passes
The canvas a modal dialog keeps open (runtime::sampler) passes the view navigation and, beside it, only the presses the dialog names: a CanvasGesture is a button with the modifiers it needs held, its drag included. Sampler::pass_canvas_gesture replaces arm_sampler; the eyedropper rows, the Color Picker and the tone pickers name a primary press, Curves names its Cmd-click, and every other press stays blocked. Trials: the kept view passes the wheel (scroll and zoom) and the middle pan and blocks the primary press; a modifier gesture passes only with its modifier. Story: Canvas "under a dialog", a live region under an open modal.
This commit is contained in:
@@ -15,7 +15,7 @@ use craft_host::Host;
|
||||
use craft_host::target::Caps;
|
||||
use craft_settings::Remembered;
|
||||
use craft_theme::{ColorModel, Srgba, builtin_models};
|
||||
use craft_ui::contract::{Bind, Sampler as _, ShowContainer as _};
|
||||
use craft_ui::contract::{Bind, CanvasGesture, Sampler as _, ShowContainer as _};
|
||||
use craft_ui::dialog::{Dialog, DialogId, DialogResult, DialogStack, StackDialog, dialog_id};
|
||||
use craft_ui::patterns::form_dialogs::{HostRequests, ResetFooter};
|
||||
use craft_ui::runtime::env::{Env, Label};
|
||||
@@ -246,7 +246,7 @@ impl ColorPickerDialog {
|
||||
}
|
||||
|
||||
/// The canvas whose presses pass through the dialog's modal layer while the dropper is armed
|
||||
/// (`Sampler::arm_sampler`), and whose view stays usable (`keep_view_open`).
|
||||
/// (`Sampler::pass_canvas_gesture`), and whose view stays usable (`keep_view_open`).
|
||||
#[must_use]
|
||||
pub fn canvas(mut self, canvas: Id) -> ColorPickerDialog {
|
||||
self.canvas = Some(canvas);
|
||||
@@ -422,7 +422,7 @@ impl StackDialog for ColorPickerDialog {
|
||||
cx.keep_view_open(canvas);
|
||||
}
|
||||
if let Some(canvas) = arm {
|
||||
cx.arm_sampler(canvas);
|
||||
cx.pass_canvas_gesture(canvas, CanvasGesture::PRIMARY);
|
||||
}
|
||||
let mut models: Vec<&dyn ColorModel> = builtin_models().into_iter().filter(|model| model.default_on().full).collect();
|
||||
models.push(&cmyk);
|
||||
|
||||
@@ -12,7 +12,9 @@ use craft_behavior::{EditEvent, EditEvents, IconId, Mixed};
|
||||
use craft_keymap::{Chord, Key, Mods};
|
||||
use craft_theme::Srgba;
|
||||
use craft_ui::buttons::{Button, ToggleButton};
|
||||
use craft_ui::contract::{Bind, Entry, NoValue, Outcome, Pattern, Props, Remember as _, Sampler as _, Show as _, ShowContainer as _, clicked};
|
||||
use craft_ui::contract::{
|
||||
Bind, CanvasGesture, Entry, NoValue, Outcome, Pattern, Props, Remember as _, Sampler as _, Show as _, ShowContainer as _, clicked,
|
||||
};
|
||||
use craft_ui::display::{Shade, Text};
|
||||
use craft_ui::layout_components::{Row, Stack};
|
||||
use craft_ui::pickers::Select;
|
||||
@@ -148,8 +150,8 @@ type SamplerFn<'a> = Box<dyn Fn(&[[f32; 2]]) -> Vec<f32> + 'a>;
|
||||
/// - **Mode icons** report [`CurvesPanelOutput::mode`]; the current mode's icon reports nothing.
|
||||
/// - **Selection**: the selected point is the panel's own state ([`CurvesPanelOutput::selected`]); the host sets
|
||||
/// it with [`select`](Self::select), after [`add_point_from_sample`](Self::add_point_from_sample).
|
||||
/// - **Sampling under the dialog**: with a [`canvas`](Self::canvas) the panel arms the canvas's sampler in every
|
||||
/// pass, so a ⌘/Ctrl-click on the image reaches the host's tool, which calls `add_point_from_sample`.
|
||||
/// - **Sampling under the dialog**: with a [`canvas`](Self::canvas) the panel lets the ⌘/Ctrl-click on the image
|
||||
/// through the dialog in every pass, so it reaches the host's tool, which calls `add_point_from_sample`.
|
||||
#[must_use = "show the panel with `show`"]
|
||||
#[derive(Pattern, Props)]
|
||||
pub struct CurvesPanel<'a> {
|
||||
@@ -165,7 +167,7 @@ pub struct CurvesPanel<'a> {
|
||||
/// The armed point picker; the picker row shows when it is given.
|
||||
#[prop]
|
||||
pickers: Option<Bind<'a, Option<TonePicker>>>,
|
||||
/// The document canvas the sampler is armed for.
|
||||
/// The document canvas the ⌘/Ctrl-click and the eyedroppers reach under the dialog.
|
||||
#[prop]
|
||||
canvas: Option<Id>,
|
||||
picker_enabled: [bool; 3],
|
||||
@@ -318,7 +320,7 @@ impl<'a> CurvesPanel<'a> {
|
||||
let hint = template.fill(&[("key", &key)]);
|
||||
// A Curves panel in a dialog keeps the image under the dialog usable for the ⌘/Ctrl-click point.
|
||||
if let Some(canvas) = canvas {
|
||||
cx.arm_sampler(canvas);
|
||||
cx.pass_canvas_gesture(canvas, CanvasGesture::PRIMARY.with(Mods::CMD));
|
||||
}
|
||||
|
||||
let mut picked = channel;
|
||||
|
||||
@@ -12,7 +12,7 @@ use craft_behavior::{CancelReason, Cursor, IconId, PaintedCursor};
|
||||
use craft_theme::Srgba;
|
||||
use craft_ui::buttons::ToggleButton;
|
||||
use craft_ui::canvas::{CanvasTool, ToolAction, ToolContext, ToolPath, ToolPointer};
|
||||
use craft_ui::contract::{Bind, Held, NoValue, Outcome, Pattern, Props, Sampler as _, Show as _, ShowContainer as _};
|
||||
use craft_ui::contract::{Bind, CanvasGesture, Held, NoValue, Outcome, Pattern, Props, Sampler as _, Show as _, ShowContainer as _};
|
||||
use craft_ui::display::{Shade, Text};
|
||||
use craft_ui::layout_components::Row;
|
||||
use craft_ui::primitives::{Draw, IconSpec};
|
||||
@@ -104,7 +104,7 @@ impl Outcome for TonePickerRowOutput {
|
||||
/// - A button is pressed while its picker is armed. A click on a disarmed button arms it; a click on
|
||||
/// the armed one disarms it.
|
||||
/// - While a picker is armed and [`canvas`](Self::canvas) names the document's canvas, the row
|
||||
/// arms the canvas's sampler in every pass: a primary press on the canvas under the modal dialog
|
||||
/// keeps the canvas reachable in every pass: a primary press on the canvas under the modal dialog
|
||||
/// reaches the canvas's tool (a [`TonePickTool`]), and the wheel and middle-drag navigate it.
|
||||
/// Keys, focus and Escape stay the dialog's.
|
||||
/// - A picker the host disabled ([`enabled_picker`](Self::enabled_picker)) shows disabled.
|
||||
@@ -147,7 +147,7 @@ impl<'a> TonePickerRow<'a> {
|
||||
let shown = Row::new().label(words.eyedroppers.clone()).show(ui, |ui, cx| {
|
||||
let current = held.read(cx).flatten();
|
||||
if let (Some(canvas), Some(_)) = (canvas, current) {
|
||||
cx.arm_sampler(canvas);
|
||||
cx.pass_canvas_gesture(canvas, CanvasGesture::PRIMARY);
|
||||
}
|
||||
Text::new(words.eyedroppers.clone()).shade(Shade::Muted).role(TypeRole::Label).show(ui);
|
||||
for picker in TonePicker::ALL {
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
//! Stories of the canvas family.
|
||||
|
||||
use super::{Canvas, CropOverlay};
|
||||
use crate::buttons::Button;
|
||||
use crate::canvas::testing::canvas_story::{StoryDocument, show_document};
|
||||
use crate::contract::{Bind, Show as _, ShowContainer as _};
|
||||
use crate::testing::{StoryData, StoryOutput, story};
|
||||
use crate::contract::{Bind, BodyCx, CanvasGesture, Gap, Sampler as _, Show as _, ShowContainer as _, clicked};
|
||||
use crate::dialog::Dialog;
|
||||
use crate::display::Text;
|
||||
use crate::layout_components::Stack;
|
||||
use crate::testing::{Drive, StoryData, StoryOutput, stage, story};
|
||||
use craft_behavior::geometry::CropBox;
|
||||
use craft_behavior::kurbo::Rect as DocRect;
|
||||
use craft_behavior::kurbo::{Point, Rect as DocRect};
|
||||
use egui::Ui;
|
||||
|
||||
/// The zoomed story's zoom, in percent of one device pixel per document pixel.
|
||||
@@ -17,7 +21,7 @@ fn crop() -> CropBox {
|
||||
|
||||
#[story(Canvas, covers = [Default])]
|
||||
fn crop_tool(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
let room = crate::testing::stage(ui);
|
||||
let room = stage(ui);
|
||||
let view = StoryDocument::STANDARD.view(room, ui.pixels_per_point(), 100.0);
|
||||
let shown = Canvas::new("Document").view(view).show_at(ui, room, |ui, body| {
|
||||
show_document(ui, body.rect, &StoryDocument::STANDARD);
|
||||
@@ -28,7 +32,7 @@ fn crop_tool(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
|
||||
#[story(Canvas, covers = [Default])]
|
||||
fn zoomed(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
let room = crate::testing::stage(ui);
|
||||
let room = stage(ui);
|
||||
let view = StoryDocument::STANDARD.view(room, ui.pixels_per_point(), ZOOMED);
|
||||
let shown = Canvas::new("Document").view(view).show_at(ui, room, |ui, body| {
|
||||
show_document(ui, body.rect, &StoryDocument::STANDARD);
|
||||
@@ -39,10 +43,47 @@ fn zoomed(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
|
||||
#[story(Canvas, covers = [Default])]
|
||||
fn freehand(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
let room = crate::testing::stage(ui);
|
||||
let room = stage(ui);
|
||||
Canvas::new("Painting")
|
||||
.view(StoryDocument::STANDARD.view(room, ui.pixels_per_point(), 100.0))
|
||||
.show_at(ui, room, |ui, body| show_document(ui, body.rect, &StoryDocument::STANDARD))
|
||||
.output
|
||||
.into()
|
||||
}
|
||||
|
||||
/// The canvas under an open modal dialog that keeps it live: the wheel scrolls and zooms it, the middle button and
|
||||
/// Space pan it, and a click on the image picks a point into the dialog. Every other input stays the dialog's.
|
||||
#[story(Canvas, covers = [], drive = Drive::Never)]
|
||||
fn under_a_dialog(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let room = stage(ui);
|
||||
let pixels_per_point = ui.pixels_per_point();
|
||||
let mut open = *data.value("open", false);
|
||||
let mut picked = *data.value::<Option<Point>>("picked", None);
|
||||
let row = |ui: &mut Ui, _: &mut BodyCx<'_>| {
|
||||
if clicked(&Button::new("Pick a Point…").show(ui)) {
|
||||
open = true;
|
||||
}
|
||||
};
|
||||
let stage = Stack::new().fill().footer(row).footer_gap(Gap::Stack).show_at(ui, room, |ui, stack| {
|
||||
let view = data.value("view", StoryDocument::STANDARD.view(stack.rect, pixels_per_point, 100.0));
|
||||
Canvas::new("Document")
|
||||
.view(Bind::bound(view))
|
||||
.show_at(ui, stack.rect, |ui, body| show_document(ui, body.rect, &StoryDocument::STANDARD))
|
||||
.output
|
||||
});
|
||||
let Some(canvas) = stage.inner else { return StoryOutput::default() };
|
||||
// The press the dialog lets through reaches the canvas's background, where the app's tool would take it.
|
||||
if open && let (Some(at), Some(space)) = (canvas.click_at(), canvas.space) {
|
||||
picked = Some(space.view.to_doc(at));
|
||||
}
|
||||
let id = canvas.response.id;
|
||||
Dialog::new("Pick a Point").opened(Bind::bound(&mut open)).show(ui, |ui, cx| {
|
||||
cx.keep_view_open(id);
|
||||
cx.pass_canvas_gesture(id, CanvasGesture::PRIMARY);
|
||||
let text = picked.map_or_else(|| "Click the image to pick a point.".to_owned(), |at| format!("Picked {:.0}, {:.0}", at.x, at.y));
|
||||
Text::new(text).show(ui);
|
||||
});
|
||||
*data.value("open", false) = open;
|
||||
*data.value::<Option<Point>>("picked", None) = picked;
|
||||
canvas.into()
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ pub use props::{Common, Props, Resolved, Tooltip, TooltipAt, TooltipSpec, Toolti
|
||||
pub use registry::registrations;
|
||||
#[doc(hidden)]
|
||||
pub use registry::{CollectionEntry, ContainerEntry, Exemption, LeafEntry, PatternKind, PatternProbe, Probe, Registration, ShowEntry};
|
||||
pub use sampler::Sampler;
|
||||
pub use sampler::{CanvasGesture, Sampler};
|
||||
pub use settle::SettleCx;
|
||||
pub use text::EDIT_MENU;
|
||||
pub use values::{Bind, BindMut, EditResponse, ItemOutput, NoValue, Outcome, Validity, ValueOf, clicked, pressed};
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//! `runtime::sampler`.
|
||||
|
||||
use super::{BodyCx, Component, Cx};
|
||||
pub use crate::runtime::sampler::CanvasGesture;
|
||||
use crate::runtime::sampler::{self, Reach};
|
||||
use egui::{Context, Id};
|
||||
|
||||
@@ -27,14 +28,17 @@ mod sealed {
|
||||
}
|
||||
|
||||
/// The canvas reach under a modal layer (15.5, 8.3): a control of a dialog keeps the canvas below it usable. It is
|
||||
/// implemented for `Cx`, `BodyCx` and so for `FormCx`, which derefs to `BodyCx`. The registration holds for this pass
|
||||
/// implemented for `Cx`, `BodyCx` and so for `FormCx`, which derefs to `BodyCx`. A registration holds for this pass
|
||||
/// and the next and belongs to the modal layer whose body calls it: a modal opened over that layer blocks the canvas.
|
||||
/// The registrations of one pass add up.
|
||||
pub trait Sampler: sealed::Host {
|
||||
/// Arms a sampler for `canvas` (the canvas component's id): while the modal layer is open, primary presses on the
|
||||
/// canvas pass through it to the canvas, and so does the view navigation (`keep_view_open`). The arming control
|
||||
/// calls it in every pass while it is armed.
|
||||
fn arm_sampler(&mut self, canvas: Id) {
|
||||
sampler::register(self.egui(), canvas, Reach::Sample);
|
||||
/// Lets `gesture` on `canvas` (the canvas component's id) through the modal layer, and the view navigation with it
|
||||
/// (`keep_view_open`): an armed eyedropper's press ([`CanvasGesture::PRIMARY`]), a dialog's drag on the image, a
|
||||
/// ⌘-click (`CanvasGesture::PRIMARY.with(Mods::CMD)`). A press that starts no named gesture stays blocked. The
|
||||
/// app runs the dialog's own tool on the canvas meanwhile. The control calls it in every pass while it takes the
|
||||
/// gesture.
|
||||
fn pass_canvas_gesture(&mut self, canvas: Id, gesture: CanvasGesture) {
|
||||
sampler::register(self.egui(), canvas, Reach::gesture(gesture));
|
||||
}
|
||||
|
||||
/// Keeps the view of `canvas` (the canvas component's id) usable under the modal layer: the wheel scrolls and
|
||||
@@ -42,7 +46,7 @@ pub trait Sampler: sealed::Host {
|
||||
/// canvas adds the primary press for the pan. Its other input stays blocked. A dialog body calls it in every pass
|
||||
/// while the dialog is open.
|
||||
fn keep_view_open(&mut self, canvas: Id) {
|
||||
sampler::register(self.egui(), canvas, Reach::View);
|
||||
sampler::register(self.egui(), canvas, Reach::view());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -228,7 +228,7 @@ const ALL_BUTTONS: [egui::PointerButton; 5] = [
|
||||
egui::PointerButton::Extra2,
|
||||
];
|
||||
|
||||
fn button_of(button: egui::PointerButton) -> PointerButton {
|
||||
pub(crate) fn button_of(button: egui::PointerButton) -> PointerButton {
|
||||
match button {
|
||||
egui::PointerButton::Primary => PointerButton::Primary,
|
||||
egui::PointerButton::Secondary => PointerButton::Secondary,
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
//! The canvas reach under a modal layer (15.5, 8.3): the one exception to "a modal blocks the input below it".
|
||||
//!
|
||||
//! A modal dialog that shows a canvas keeps it usable, as Photoshop's Color Picker, Levels and Curves do. A control
|
||||
//! of the dialog registers the canvas every pass with one of two reaches. `Reach::View` lets the view navigation
|
||||
//! through: the wheel (scroll and zoom) and the middle press that pans. `Reach::Sample` adds the primary press, for
|
||||
//! the eyedropper a control of the dialog armed. Space held over the canvas adds the primary press under either
|
||||
//! reach, for the pan. Focus, keys and Escape stay with the dialog. One registration holds per app (the latest), for
|
||||
//! the root frame it is made in and the next, and only while the modal layer that made it is the top modal: a modal
|
||||
//! opened over it blocks the canvas again.
|
||||
//! of the dialog registers the canvas every pass with its reach: the view navigation always passes (the wheel that
|
||||
//! scrolls and zooms, the middle press that pans, and the primary press while Space is held over the canvas, for the
|
||||
//! pan), and so do the gestures the dialog names ([`CanvasGesture`]: an eyedropper's press, a shadow drag, Curves'
|
||||
//! ⌘-click), each a button with the modifiers it needs, its drag included. Every other press stays blocked. Focus,
|
||||
//! keys and Escape stay with the dialog. One registration holds per app (the latest; the controls of one pass add
|
||||
//! up), for the root frame it is made in and the next, and only while the modal layer that made it is the top modal: a
|
||||
//! modal opened over it blocks the canvas again.
|
||||
//!
|
||||
//! One test answers a pointer input ([`Hole::answers`]): the point lies in the canvas and no layer lies over the
|
||||
//! canvas's own layer there (the dialog, a popup above it, a floating panel). The input hook keeps the events the
|
||||
@@ -19,20 +20,77 @@ use crate::runtime::egui::memory::Service;
|
||||
use crate::runtime::egui::pass;
|
||||
use crate::runtime::frame::{PerViewport, Stored};
|
||||
use crate::runtime::{overlay, pointer};
|
||||
use craft_keymap::KeyInput;
|
||||
use egui::{Context, Event, Id, PointerButton, Pos2, Rect, ViewportId};
|
||||
use craft_behavior::PointerButton;
|
||||
use craft_host::target::Os;
|
||||
use craft_keymap::{KeyInput, Mods};
|
||||
use egui::{Context, Event, Id, Pos2, Rect, ViewportId};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
/// What a modal leaves of the canvas below it.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub(crate) enum Reach {
|
||||
/// View navigation: the wheel and the middle press.
|
||||
View,
|
||||
/// View navigation and the primary press, for the sampler an armed control runs on the canvas.
|
||||
Sample,
|
||||
/// A gesture on the canvas that a dialog takes under its modal layer: a press of `button` with at least `mods` held,
|
||||
/// and the drag that follows it. Other modifiers may be held too: an eyedropper's Shift and Alt add and subtract.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub struct CanvasGesture {
|
||||
/// The button that starts it.
|
||||
pub button: PointerButton,
|
||||
/// The modifiers it needs held at the press.
|
||||
pub mods: Mods,
|
||||
}
|
||||
|
||||
impl CanvasGesture {
|
||||
/// A press of the primary button, with any modifiers: an eyedropper, a shadow or pattern drag.
|
||||
pub const PRIMARY: CanvasGesture = CanvasGesture::press(PointerButton::Primary);
|
||||
|
||||
/// A press of `button`, with any modifiers.
|
||||
pub const fn press(button: PointerButton) -> CanvasGesture {
|
||||
CanvasGesture { button, mods: Mods::NONE }
|
||||
}
|
||||
|
||||
/// The same gesture, needing `mods` held at the press.
|
||||
#[must_use]
|
||||
pub const fn with(mut self, mods: Mods) -> CanvasGesture {
|
||||
self.mods = mods;
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether a press of `button` with `held` (read for `os`) starts this gesture.
|
||||
fn starts(self, button: PointerButton, held: Mods, os: Os) -> bool {
|
||||
let need = self.mods.effective(os);
|
||||
let held = held.effective(os);
|
||||
button == self.button && (!need.cmd || held.cmd) && (!need.ctrl || held.ctrl) && (!need.alt || held.alt) && (!need.shift || held.shift)
|
||||
}
|
||||
}
|
||||
|
||||
/// What a modal leaves of the canvas below it: the view navigation, and the gestures it names.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub(crate) struct Reach {
|
||||
gestures: SmallVec<[CanvasGesture; 2]>,
|
||||
}
|
||||
|
||||
impl Reach {
|
||||
/// The view navigation alone.
|
||||
pub(crate) fn view() -> Reach {
|
||||
Reach::default()
|
||||
}
|
||||
|
||||
/// The view navigation and `gesture`.
|
||||
pub(crate) fn gesture(gesture: CanvasGesture) -> Reach {
|
||||
Reach { gestures: SmallVec::from_slice(&[gesture]) }
|
||||
}
|
||||
|
||||
/// What this reach and `other` let through together.
|
||||
fn join(mut self, other: Reach) -> Reach {
|
||||
for gesture in other.gestures {
|
||||
if !self.gestures.contains(&gesture) {
|
||||
self.gestures.push(gesture);
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// The canvas a dialog keeps open, how far, and the modal layer that made the registration.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
struct Armed {
|
||||
canvas: Id,
|
||||
reach: Reach,
|
||||
@@ -52,6 +110,8 @@ pub(crate) struct Hole {
|
||||
/// The canvas's screen rect.
|
||||
pub rect: Rect,
|
||||
pub reach: Reach,
|
||||
/// The platform the gestures' modifiers are read for.
|
||||
os: Os,
|
||||
/// Space is held over the canvas.
|
||||
pub space: bool,
|
||||
/// A Space press that the hook read this pass belongs to the canvas: the key stage takes it as the canvas's
|
||||
@@ -68,12 +128,16 @@ impl Hole {
|
||||
self.rect.contains(at) && !self.covers.iter().any(|cover| cover.contains(at))
|
||||
}
|
||||
|
||||
/// Whether the canvas takes a press of `button` at `at`: the reach admits the button and the canvas answers.
|
||||
pub(crate) fn admits_press(&self, button: PointerButton, at: Pos2) -> bool {
|
||||
/// Whether the canvas takes a press of `button` with `modifiers` at `at`: the middle press and, with Space held,
|
||||
/// the primary one pan; any other press starts a gesture the dialog named; and the canvas answers.
|
||||
pub(crate) fn admits_press(&self, button: egui::PointerButton, modifiers: &egui::Modifiers, at: Pos2) -> bool {
|
||||
let reached = match button {
|
||||
PointerButton::Primary => self.reach == Reach::Sample || self.space,
|
||||
PointerButton::Middle => true,
|
||||
_ => false,
|
||||
egui::PointerButton::Middle => true,
|
||||
egui::PointerButton::Primary if self.space => true,
|
||||
_ => {
|
||||
let (button, held) = (crate::runtime::egui::input::button_of(button), Mods::from_egui(modifiers, self.os));
|
||||
self.reach.gestures.iter().any(|gesture| gesture.starts(button, held, self.os))
|
||||
}
|
||||
};
|
||||
reached && self.answers(at)
|
||||
}
|
||||
@@ -82,7 +146,7 @@ impl Hole {
|
||||
/// `pointer` is where the pointer stood before the event.
|
||||
pub(crate) fn admits(&self, event: &Event, pointer: Option<Pos2>) -> bool {
|
||||
match event {
|
||||
Event::PointerButton { pos, button, pressed: true, .. } => self.admits_press(*button, *pos),
|
||||
Event::PointerButton { pos, button, pressed: true, modifiers } => self.admits_press(*button, modifiers, *pos),
|
||||
Event::MouseWheel { .. } => pointer.is_some_and(|at| self.answers(at)),
|
||||
_ => false,
|
||||
}
|
||||
@@ -99,8 +163,8 @@ struct Registry {
|
||||
over: bool,
|
||||
}
|
||||
|
||||
/// Registers `canvas` with `reach` for the modal layer whose body is running. `Cx::arm_sampler` and
|
||||
/// `Cx::keep_view_open` (contract layer) call it.
|
||||
/// Registers `canvas` with `reach` for the modal layer whose body is running. `Sampler::pass_canvas_gesture` and
|
||||
/// `Sampler::keep_view_open` (contract layer) call it.
|
||||
pub(crate) fn register(ctx: &Context, canvas: Id, reach: Reach) {
|
||||
if !crate::runtime::measure::effect(ctx, "sampler::register") {
|
||||
return;
|
||||
@@ -108,8 +172,8 @@ pub(crate) fn register(ctx: &Context, canvas: Id, reach: Reach) {
|
||||
let now = pass::root_frame_nr(ctx);
|
||||
let modal = overlay::modal_drawing(ctx);
|
||||
Service::<Registry>::run(ctx, |registry| {
|
||||
let reach = match registry.made {
|
||||
Some((made, held)) if made == now && held.canvas == canvas && held.modal == modal => held.reach.max(reach),
|
||||
let reach = match registry.made.take() {
|
||||
Some((made, held)) if made == now && held.canvas == canvas && held.modal == modal => held.reach.join(reach),
|
||||
_ => reach,
|
||||
};
|
||||
registry.made = Some((now, Armed { canvas, reach, modal }));
|
||||
@@ -119,7 +183,7 @@ pub(crate) fn register(ctx: &Context, canvas: Id, reach: Reach) {
|
||||
/// The registration made in this root frame or the last: one per app, the latest wins.
|
||||
fn armed(ctx: &Context) -> Option<Armed> {
|
||||
let now = pass::root_frame_nr(ctx);
|
||||
Service::<Registry>::run(ctx, |registry| registry.made.filter(|(made, _)| made + 1 >= now).map(|(_, armed)| armed))
|
||||
Service::<Registry>::run(ctx, |registry| registry.made.clone().filter(|(made, _)| made + 1 >= now).map(|(_, armed)| armed))
|
||||
}
|
||||
|
||||
/// The armed canvas, while the modal layer that registered it is the top modal and no popup stands over a modal.
|
||||
@@ -132,7 +196,8 @@ fn open_hole(ctx: &Context) -> Option<Hole> {
|
||||
&& overlay::top_modal_in(ctx, viewport).is_none_or(|top| top.id == modal)
|
||||
&& !overlay::popup_over_modal(ctx);
|
||||
let space = Service::<Registry>::run(ctx, |registry| registry.space);
|
||||
held.then_some(Hole { viewport, canvas: armed.canvas, layer, rect, reach: armed.reach, space, space_pressed: false, covers: Vec::new() })
|
||||
let os = crate::runtime::windows::os_of(ctx);
|
||||
held.then_some(Hole { viewport, canvas: armed.canvas, layer, rect, reach: armed.reach, os, space, space_pressed: false, covers: Vec::new() })
|
||||
}
|
||||
|
||||
/// The canvas a modal layer keeps open in this pass, while the registration holds.
|
||||
@@ -178,7 +243,18 @@ pub(crate) fn layer_at(ctx: &Context, at: Pos2) -> Option<Layer> {
|
||||
|
||||
/// Whether the canvas took the primary press at `at` in the running pass: it moves no key focus.
|
||||
pub(crate) fn admitted_press(ctx: &Context, at: Pos2) -> bool {
|
||||
hole_of_pass(ctx).is_some_and(|hole| hole.admits_press(PointerButton::Primary, at))
|
||||
let Some(hole) = hole_of_pass(ctx) else { return false };
|
||||
let modifiers = ctx.input(|input| {
|
||||
input
|
||||
.events
|
||||
.iter()
|
||||
.find_map(|event| match event {
|
||||
Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: true, modifiers } if *pos == at => Some(*modifiers),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or(input.modifiers)
|
||||
});
|
||||
hole.admits_press(egui::PointerButton::Primary, &modifiers, at)
|
||||
}
|
||||
|
||||
/// Whether the root `root` may start a press that egui gave no target at `at`: where the canvas answers, only the
|
||||
|
||||
@@ -8,7 +8,9 @@ use super::path_row::PathRow;
|
||||
use super::words::ParamWords;
|
||||
use crate::buttons::{Button, ToggleButton};
|
||||
use crate::choice::{Checkbox, Radio, RadioGroup, Swatch};
|
||||
use crate::contract::{Bind, Entry, NoValue, Outcome, Props as _, Remember as _, Sampler as _, Show as _, ShowContainer as _, clicked};
|
||||
use crate::contract::{
|
||||
Bind, CanvasGesture, Entry, NoValue, Outcome, Props as _, Remember as _, Sampler as _, Show as _, ShowContainer as _, clicked,
|
||||
};
|
||||
use crate::form::{Field, FieldGroup, SliderField, SliderFieldLayout};
|
||||
|
||||
use crate::layout_components::{Row, Stack};
|
||||
@@ -110,8 +112,7 @@ impl<'a> ParamRows<'a> {
|
||||
}
|
||||
|
||||
/// The canvas (the canvas component's id) the eyedropper rows sample while the rows are inside a modal dialog: an
|
||||
/// armed eyedropper registers it with `Sampler::arm_sampler`, so primary presses on it pass through the dialog's
|
||||
/// modal layer. The dialog body keeps the view usable itself with `Sampler::keep_view_open`.
|
||||
/// armed eyedropper lets its press through the dialog's modal layer (`Sampler::pass_canvas_gesture`). The dialog body keeps the view usable itself with `Sampler::keep_view_open`.
|
||||
pub fn canvas(mut self, canvas: Id) -> Self {
|
||||
self.canvas = Some(canvas);
|
||||
self
|
||||
@@ -148,7 +149,7 @@ impl<'a> ParamRows<'a> {
|
||||
pass.begin(&filled, state, cx.keep::<Memo>().clone());
|
||||
pass.rows(ui, params);
|
||||
if let Some(canvas) = canvas.filter(|_| pass.sampling && !read_only) {
|
||||
cx.arm_sampler(canvas);
|
||||
cx.pass_canvas_gesture(canvas, CanvasGesture::PRIMARY);
|
||||
}
|
||||
*cx.keep::<Memo>() = pass.memo.clone();
|
||||
changed = pass.work != filled;
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
//! A canvas under a modal dialog (15.5, 8.3): an armed sampler lets primary presses through. The dialog's body arms
|
||||
//! the canvas the way a dialog does, through `BodyCx`.
|
||||
//! A canvas under a modal dialog (15.5, 8.3): the view navigation passes, and so do the gestures the dialog names;
|
||||
//! every other press stays blocked. The dialog's body keeps the canvas open the way a dialog does, through `BodyCx`.
|
||||
|
||||
use super::kit::{Leaf, Opened, PlainStyle, Seen};
|
||||
use craft_behavior::Sense;
|
||||
use craft_ui::contract::{Common, Component, Cx, MeasureCx, Overlay, OverlayUi, Overlays as _, Props as _, Role, Sampler as _, Show as _, Trigger};
|
||||
use craft_behavior::{PointerButton, Sense};
|
||||
use craft_keymap::Mods;
|
||||
use craft_ui::contract::{
|
||||
CanvasGesture, Common, Component, Cx, MeasureCx, Overlay, OverlayUi, Overlays as _, Pointer as _, Props as _, Role, Sampler as _, Show as _,
|
||||
Trigger,
|
||||
};
|
||||
use craft_ui::runtime::focus::Initial;
|
||||
use craft_ui::style::StyleCx;
|
||||
use craft_ui::testing::focus::is_focused;
|
||||
use craft_ui::testing::input::key_press;
|
||||
use craft_ui::testing::pointer::{moved, press, release};
|
||||
use craft_ui::testing::pointer::{middle, moved, wheel, zoom_wheel};
|
||||
use craft_ui::testing::{Frames, trial};
|
||||
use craft_ui_test_kit::contract::{Probe, Seen as ProbeSeen};
|
||||
use egui::{Event, Id, Key, Modifiers, Pos2, Rect, Response, Ui, Vec2};
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
@@ -21,7 +24,67 @@ const CANVAS: Rect = Rect { min: Pos2::new(20.0, 20.0), max: Pos2::new(320.0, 26
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum Arm {
|
||||
Nothing,
|
||||
Sampler,
|
||||
View,
|
||||
Gesture(CanvasGesture),
|
||||
}
|
||||
|
||||
/// What reached the canvas.
|
||||
#[derive(Default)]
|
||||
struct Took {
|
||||
id: Option<Id>,
|
||||
/// The buttons of the presses it took.
|
||||
buttons: Vec<PointerButton>,
|
||||
dragged: bool,
|
||||
/// The passes that gave it a wheel share.
|
||||
wheels: u32,
|
||||
/// The passes whose wheel share zoomed.
|
||||
zooms: u32,
|
||||
}
|
||||
|
||||
/// The canvas: a surface that takes presses of any button, their drags and the wheel.
|
||||
#[derive(Component)]
|
||||
struct Surface<'a> {
|
||||
#[common]
|
||||
common: Common,
|
||||
took: &'a RefCell<Took>,
|
||||
}
|
||||
|
||||
impl Component for Surface<'_> {
|
||||
const ROLE: Role = Role::GROUP;
|
||||
type Style = PlainStyle;
|
||||
type Output = Response;
|
||||
|
||||
fn style(_: &StyleCx<'_>, _: SurfaceStyleKey) -> PlainStyle {
|
||||
PlainStyle::default()
|
||||
}
|
||||
|
||||
fn measure(&mut self, _: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
CANVAS.size()
|
||||
}
|
||||
|
||||
fn sense(&self) -> Sense {
|
||||
Sense::PRESS | Sense::WHEEL
|
||||
}
|
||||
|
||||
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
||||
let mut took = self.took.borrow_mut();
|
||||
took.id = Some(cx.id);
|
||||
let phase = cx.press().phase;
|
||||
if let Some(start) = phase.start()
|
||||
&& !took.buttons.contains(&start.button)
|
||||
{
|
||||
took.buttons.push(start.button);
|
||||
}
|
||||
took.dragged |= phase.is_drag();
|
||||
if let Some(wheel) = cx.take_wheel() {
|
||||
took.wheels += 1;
|
||||
took.zooms += u32::from(wheel.zoom != 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> Response {
|
||||
cx.response.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// A button that opens a modal dialog whose body arms the canvas, holds a focusable leaf.
|
||||
@@ -69,7 +132,8 @@ impl Component for Dialog<'_> {
|
||||
let shown = ui.show(popup, overlay, |ui, body| {
|
||||
match scene.arm.get() {
|
||||
Arm::Nothing => {}
|
||||
Arm::Sampler => body.arm_sampler(canvas),
|
||||
Arm::View => body.keep_view_open(canvas),
|
||||
Arm::Gesture(gesture) => body.pass_canvas_gesture(canvas, gesture),
|
||||
}
|
||||
let leaf = Leaf::new(&scene.leaf, Vec2::new(80.0, 30.0)).id_salt("armer").show(ui);
|
||||
scene.armer_id.set(leaf.id);
|
||||
@@ -84,10 +148,9 @@ impl Component for Dialog<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The scene: a canvas probe and the dialog's opener.
|
||||
/// The scene: the canvas and the dialog's opener.
|
||||
struct Scene {
|
||||
area: Rect,
|
||||
canvas: RefCell<ProbeSeen>,
|
||||
canvas: RefCell<Took>,
|
||||
leaf: RefCell<Seen>,
|
||||
armer_id: Cell<Id>,
|
||||
open: Cell<bool>,
|
||||
@@ -98,7 +161,6 @@ struct Scene {
|
||||
impl Scene {
|
||||
fn new(arm: Arm) -> Self {
|
||||
Scene {
|
||||
area: CANVAS,
|
||||
canvas: RefCell::default(),
|
||||
leaf: RefCell::default(),
|
||||
armer_id: Cell::new(Id::NULL),
|
||||
@@ -111,7 +173,7 @@ impl Scene {
|
||||
fn show(&self, ui: &mut Ui) {
|
||||
self.dialog.shown.set(false);
|
||||
let canvas = self.canvas.borrow().id.unwrap_or(Id::NULL);
|
||||
Probe::new(&self.canvas).size(self.area.size()).sense(Sense::CLICK | Sense::DRAG).id_salt("canvas").show_at(ui, self.area);
|
||||
Surface { common: Common::new(), took: &self.canvas }.id_salt("canvas").show_at(ui, CANVAS);
|
||||
Dialog { common: Common::new(), scene: self, canvas }.id_salt("dialog").show_at(ui, OPENER);
|
||||
}
|
||||
|
||||
@@ -135,42 +197,52 @@ impl Scene {
|
||||
}
|
||||
|
||||
fn forget_canvas(&self) {
|
||||
self.canvas.borrow_mut().press.clear();
|
||||
self.leaf.borrow_mut().presses = 0;
|
||||
self.leaf.borrow_mut().activations = 0;
|
||||
let id = self.canvas.borrow().id;
|
||||
*self.canvas.borrow_mut() = Took { id, ..Took::default() };
|
||||
}
|
||||
|
||||
fn pressed_canvas(&self) -> bool {
|
||||
!self.canvas.borrow().press.is_empty()
|
||||
fn took(&self, button: PointerButton) -> bool {
|
||||
self.canvas.borrow().buttons.contains(&button)
|
||||
}
|
||||
|
||||
fn dragged_canvas(&self) -> bool {
|
||||
self.canvas.borrow().press.iter().any(|phase| phase.is_drag())
|
||||
self.canvas.borrow().dragged
|
||||
}
|
||||
|
||||
/// A press of `button` at `at` with `modifiers` held, a move by `by` and the release.
|
||||
fn drag_with(&self, frames: &mut Frames, button: egui::PointerButton, modifiers: Modifiers, at: Pos2, by: Vec2) {
|
||||
let event = |pos, pressed| Event::PointerButton { pos, button, pressed, modifiers };
|
||||
self.frame(frames, vec![moved(at)]);
|
||||
self.frame(frames, vec![event(at, true)]);
|
||||
self.frame(frames, vec![moved(at + by / 2.0)]);
|
||||
self.frame(frames, vec![moved(at + by)]);
|
||||
self.frame(frames, vec![event(at + by, false)]);
|
||||
self.settle(frames, 2);
|
||||
}
|
||||
|
||||
/// A primary press at `at`, a move by `by` and the release.
|
||||
fn drag(&self, frames: &mut Frames, at: Pos2, by: Vec2) {
|
||||
self.frame(frames, vec![moved(at)]);
|
||||
self.frame(frames, vec![press(at)]);
|
||||
self.frame(frames, vec![moved(at + by / 2.0)]);
|
||||
self.frame(frames, vec![moved(at + by)]);
|
||||
self.frame(frames, vec![release(at + by)]);
|
||||
self.settle(frames, 2);
|
||||
self.drag_with(frames, egui::PointerButton::Primary, Modifiers::NONE, at, by);
|
||||
}
|
||||
|
||||
/// The dialog is open and keeps key focus.
|
||||
fn assert_dialog_holds(&self, frames: &Frames) {
|
||||
assert!(self.dialog.shown.get(), "input on the canvas does not close the dialog");
|
||||
assert!(is_focused(&frames.ctx, self.armer_id.get()), "the dialog keeps key focus");
|
||||
}
|
||||
}
|
||||
|
||||
/// With a sampler armed, a press and a drag on the canvas under the dialog reach it, the dialog keeps key focus and
|
||||
/// Escape closes it; once it is closed the arm lapses, so a dialog that no longer arms blocks the canvas again.
|
||||
/// With a gesture named, its press and drag on the canvas under the dialog reach it, the dialog keeps key focus and
|
||||
/// Escape closes it; once it is closed the registration lapses, so a dialog that names nothing blocks the canvas again.
|
||||
#[trial]
|
||||
fn an_armed_sampler_passes_a_press_and_a_drag_to_the_canvas_and_lapses_with_the_dialog() {
|
||||
fn a_named_gesture_passes_its_press_and_drag_to_the_canvas_and_lapses_with_the_dialog() {
|
||||
let mut frames = Frames::new();
|
||||
let scene = Scene::new(Arm::Sampler);
|
||||
let scene = Scene::new(Arm::Gesture(CanvasGesture::PRIMARY));
|
||||
scene.open_dialog(&mut frames);
|
||||
scene.drag(&mut frames, CANVAS.center(), Vec2::new(40.0, 10.0));
|
||||
assert!(scene.pressed_canvas(), "the press reached the canvas");
|
||||
assert!(scene.took(PointerButton::Primary), "the press reached the canvas");
|
||||
assert!(scene.dragged_canvas(), "the drag stayed the canvas's");
|
||||
assert!(scene.dialog.shown.get(), "a press on the canvas does not close the dialog");
|
||||
assert!(is_focused(&frames.ctx, scene.armer_id.get()), "the dialog keeps key focus");
|
||||
scene.assert_dialog_holds(&frames);
|
||||
scene.frame(&mut frames, vec![key_press(Key::Escape, Modifiers::NONE)]);
|
||||
scene.settle(&mut frames, 30);
|
||||
assert!(!scene.dialog.shown.get(), "Escape closes the dialog");
|
||||
@@ -179,5 +251,51 @@ fn an_armed_sampler_passes_a_press_and_a_drag_to_the_canvas_and_lapses_with_the_
|
||||
scene.open_dialog(&mut frames);
|
||||
scene.forget_canvas();
|
||||
scene.drag(&mut frames, CANVAS.center(), Vec2::new(40.0, 10.0));
|
||||
assert!(!scene.pressed_canvas(), "the old arm has lapsed");
|
||||
assert!(!scene.took(PointerButton::Primary), "the old registration has lapsed");
|
||||
}
|
||||
|
||||
/// A dialog that keeps the view open passes the wheel (scroll and zoom) and the middle-button pan to the canvas under
|
||||
/// it, and blocks the primary press, which starts no gesture it named.
|
||||
#[trial]
|
||||
fn a_kept_view_passes_the_wheel_and_the_middle_pan_and_blocks_the_primary_press() {
|
||||
let mut frames = Frames::new();
|
||||
let scene = Scene::new(Arm::View);
|
||||
scene.open_dialog(&mut frames);
|
||||
scene.frame(&mut frames, vec![moved(CANVAS.center())]);
|
||||
scene.frame(&mut frames, vec![wheel(-40.0)]);
|
||||
scene.frame(&mut frames, vec![zoom_wheel(-40.0)]);
|
||||
scene.settle(&mut frames, 2);
|
||||
assert!(scene.canvas.borrow().wheels > scene.canvas.borrow().zooms, "the scroll reached the canvas");
|
||||
assert!(scene.canvas.borrow().zooms > 0, "the zoom reached the canvas");
|
||||
scene.drag_with(&mut frames, egui::PointerButton::Middle, Modifiers::NONE, CANVAS.center(), Vec2::new(40.0, 10.0));
|
||||
assert!(scene.took(PointerButton::Middle) && scene.dragged_canvas(), "the middle-button pan reached the canvas");
|
||||
scene.forget_canvas();
|
||||
scene.drag(&mut frames, CANVAS.center(), Vec2::new(40.0, 10.0));
|
||||
assert!(!scene.took(PointerButton::Primary), "the primary press stayed blocked");
|
||||
scene.assert_dialog_holds(&frames);
|
||||
// With nothing kept open the dialog blocks the view navigation as well.
|
||||
scene.arm.set(Arm::Nothing);
|
||||
scene.settle(&mut frames, 3);
|
||||
scene.forget_canvas();
|
||||
scene.frame(&mut frames, vec![wheel(-40.0)]);
|
||||
scene.frame(&mut frames, vec![middle(CANVAS.center(), true)]);
|
||||
scene.frame(&mut frames, vec![middle(CANVAS.center(), false)]);
|
||||
scene.settle(&mut frames, 2);
|
||||
assert!(scene.canvas.borrow().wheels == 0 && !scene.took(PointerButton::Middle), "the canvas is blocked");
|
||||
}
|
||||
|
||||
/// A gesture that needs a modifier passes only with it held at the press: Curves' ⌘-click reaches the canvas, a plain
|
||||
/// press does not.
|
||||
#[trial]
|
||||
fn a_gesture_with_a_modifier_passes_only_with_the_modifier_held() {
|
||||
let mut frames = Frames::new();
|
||||
let scene = Scene::new(Arm::Gesture(CanvasGesture::PRIMARY.with(Mods::CMD)));
|
||||
scene.open_dialog(&mut frames);
|
||||
scene.drag(&mut frames, CANVAS.center(), Vec2::new(40.0, 10.0));
|
||||
assert!(!scene.took(PointerButton::Primary), "a plain press stayed blocked");
|
||||
scene.drag_with(&mut frames, egui::PointerButton::Primary, Modifiers::SHIFT, CANVAS.center(), Vec2::new(40.0, 10.0));
|
||||
assert!(!scene.took(PointerButton::Primary), "a press with another modifier stayed blocked");
|
||||
scene.drag_with(&mut frames, egui::PointerButton::Primary, Modifiers::COMMAND, CANVAS.center(), Vec2::new(40.0, 10.0));
|
||||
assert!(scene.took(PointerButton::Primary) && scene.dragged_canvas(), "the ⌘-press and its drag reached the canvas");
|
||||
scene.assert_dialog_holds(&frames);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user