mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 02:32:33 +00:00
ui-contract: the host's tool gesture holds only beside a modal body that keeps the canvas
The registry keeps the dialog's part and the host's part apart: the host's gestures join the reach only while a modal body keeps the same canvas, whatever order the two are made in, so a modal that keeps nothing (an alert) leaves the canvas blocked. Trials: the host's gesture lapses when the dialog stops keeping the canvas; under a dialog that keeps nothing it passes no press and no wheel. Also: the BlockingOverlay story's drive opens it; rows.rs doc wrapped; api-map TonePickerRow note names pass_canvas_gesture.
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
use super::{BodyCx, Component, Cx};
|
||||
pub use crate::runtime::sampler::CanvasGesture;
|
||||
use crate::runtime::sampler::{self, Owner, Reach};
|
||||
use crate::runtime::sampler::{self, Reach};
|
||||
use egui::{Context, Id};
|
||||
|
||||
mod sealed {
|
||||
@@ -30,8 +30,8 @@ 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`. A registration holds for this pass
|
||||
/// and the next and belongs to the modal layer whose body calls it: called outside every modal body it holds nothing,
|
||||
/// and a modal opened over that layer blocks the canvas. The registrations of one pass add up. The canvas's host passes
|
||||
/// its running tool's gestures with `runtime::sampler::pass_tool_gesture`.
|
||||
/// and a modal opened over that layer blocks the canvas. The registrations of one pass add up. The canvas's host adds
|
||||
/// its running tool's gestures to a canvas a modal keeps with `runtime::sampler::pass_tool_gesture`.
|
||||
pub trait Sampler: sealed::Host {
|
||||
/// 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
|
||||
@@ -39,7 +39,7 @@ pub trait Sampler: sealed::Host {
|
||||
/// 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), Owner::Body);
|
||||
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
|
||||
@@ -47,7 +47,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(), Owner::Body);
|
||||
sampler::register(self.egui(), canvas, Reach::view());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -153,63 +153,70 @@ impl Hole {
|
||||
}
|
||||
}
|
||||
|
||||
/// The registration, the root frame it was made in, and what the input hook follows of Space and the pointer.
|
||||
/// The registrations, each with the root frame it was made in, and what the input hook follows of Space and the
|
||||
/// pointer.
|
||||
#[derive(Default)]
|
||||
struct Registry {
|
||||
made: Option<(u64, Armed)>,
|
||||
/// The dialog's part: the canvas a modal body keeps, its reach and that modal.
|
||||
body: Option<(u64, Armed)>,
|
||||
/// The host's part: the canvas whose tool the host runs and the gestures the tool takes. It holds only beside a
|
||||
/// body part for the same canvas, whatever order the two are made in.
|
||||
tool: Option<(u64, Id, Reach)>,
|
||||
/// Space was pressed over the canvas and not released: the canvas has it.
|
||||
space: bool,
|
||||
/// The pointer stands where the canvas answers.
|
||||
over: bool,
|
||||
}
|
||||
|
||||
/// Who a registration belongs to.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(crate) enum Owner {
|
||||
/// A control: the modal layer whose body is running. Made outside every modal body, it is dropped, so a picker
|
||||
/// armed in a panel passes no press under a modal that opens later.
|
||||
Body,
|
||||
/// The canvas's host, for the tool it runs: the modal layer whose body is running, else the top modal layer, here
|
||||
/// or in another viewport.
|
||||
Tool,
|
||||
}
|
||||
|
||||
/// Registers `canvas` with `reach` for the modal layer `owner` names. `Sampler::pass_canvas_gesture` and
|
||||
/// `Sampler::keep_view_open` (contract layer) register as [`Owner::Body`], [`pass_tool_gesture`] as [`Owner::Tool`].
|
||||
pub(crate) fn register(ctx: &Context, canvas: Id, reach: Reach, owner: Owner) {
|
||||
/// Registers `canvas` with `reach` for the modal layer whose body is running. Made outside every modal body it is
|
||||
/// dropped, so a picker armed in a panel passes no press under a modal that opens later. `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;
|
||||
}
|
||||
let now = pass::root_frame_nr(ctx);
|
||||
let viewport = pass::current_viewport(ctx);
|
||||
let modal = overlay::modal_drawing(ctx).or_else(|| {
|
||||
let shown = (owner == Owner::Tool).then(|| {
|
||||
overlay::top_modal_in(ctx, viewport)
|
||||
.or_else(|| overlay::modal_viewport(ctx, viewport).and_then(|other| overlay::top_modal_in(ctx, other)))
|
||||
});
|
||||
shown.flatten().map(|top| top.id)
|
||||
});
|
||||
let Some(modal) = modal else { return };
|
||||
let Some(modal) = overlay::modal_drawing(ctx) else { return };
|
||||
Service::<Registry>::run(ctx, |registry| {
|
||||
let reach = match registry.made.take() {
|
||||
let reach = match registry.body.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 }));
|
||||
registry.body = Some((now, Armed { canvas, reach, modal }));
|
||||
});
|
||||
}
|
||||
|
||||
/// The canvas host's entry (`ToolHost`): passes `gesture` on `canvas`, the canvas whose tool the host runs, through the
|
||||
/// modal layer that holds the canvas: the one whose body is running, else the top modal. The host calls it in every
|
||||
/// pass for the gestures its running tool takes; controls use `Sampler::pass_canvas_gesture`.
|
||||
/// The canvas host's entry (`ToolHost`): passes `gesture` on `canvas`, the canvas whose tool the host runs. It takes
|
||||
/// effect only while a modal body keeps the same canvas (`Sampler::keep_view_open` or `pass_canvas_gesture`), and
|
||||
/// then for that modal: a modal that keeps nothing, an alert say, leaves the canvas blocked. The host calls it in
|
||||
/// every pass for the gestures its running tool takes; controls use `Sampler::pass_canvas_gesture`.
|
||||
pub fn pass_tool_gesture(ctx: &Context, canvas: Id, gesture: CanvasGesture) {
|
||||
register(ctx, canvas, Reach::gesture(gesture), Owner::Tool);
|
||||
if !crate::runtime::measure::effect(ctx, "sampler::pass_tool_gesture") {
|
||||
return;
|
||||
}
|
||||
let now = pass::root_frame_nr(ctx);
|
||||
let reach = Reach::gesture(gesture);
|
||||
Service::<Registry>::run(ctx, |registry| {
|
||||
let reach = match registry.tool.take() {
|
||||
Some((made, held, gestures)) if made == now && held == canvas => gestures.join(reach),
|
||||
_ => reach,
|
||||
};
|
||||
registry.tool = Some((now, canvas, reach));
|
||||
});
|
||||
}
|
||||
|
||||
/// The registration made in this root frame or the last: one per app, the latest wins.
|
||||
/// The body part made in this root frame or the last (one per app, the latest wins), with the host's gestures for the
|
||||
/// same canvas made in that window added.
|
||||
fn armed(ctx: &Context) -> Option<Armed> {
|
||||
let now = pass::root_frame_nr(ctx);
|
||||
Service::<Registry>::run(ctx, |registry| registry.made.clone().filter(|(made, _)| made + 1 >= now).map(|(_, armed)| armed))
|
||||
let live = |made: u64| made + 1 >= now;
|
||||
Service::<Registry>::run(ctx, |registry| {
|
||||
let (_, mut armed) = registry.body.clone().filter(|(made, _)| live(*made))?;
|
||||
if let Some((_, _, gestures)) = registry.tool.clone().filter(|(made, canvas, _)| live(*made) && *canvas == armed.canvas) {
|
||||
armed.reach = armed.reach.join(gestures);
|
||||
}
|
||||
Some(armed)
|
||||
})
|
||||
}
|
||||
|
||||
/// The armed canvas, while the modal layer that registered it is the top modal and no popup stands over a modal.
|
||||
|
||||
@@ -150,14 +150,25 @@ fn dialog_frame_with_footer(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
shown
|
||||
}
|
||||
|
||||
#[story(BlockingOverlay<'static>, covers = [Default], drive = Drive::Never)]
|
||||
/// The drive of the overlay story: the opener's click.
|
||||
#[doc(hidden)]
|
||||
pub fn open_export(fixture: &mut Fixture) {
|
||||
if let Some(open) = fixture.by_label(EXPORT) {
|
||||
fixture.click(&open);
|
||||
}
|
||||
}
|
||||
|
||||
/// The opener of the overlay story.
|
||||
const EXPORT: &str = "Export…";
|
||||
|
||||
#[story(BlockingOverlay<'static>, covers = [Default], drive = Drive::Custom(open_export))]
|
||||
fn blocking_overlay(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let scrim = *data.value("scrim", true);
|
||||
let card = *data.value("wait card", true);
|
||||
let overlay = BlockingOverlay::new("Exporting").scrim(scrim);
|
||||
let overlay = if card { overlay.message("Exporting 24 pages…").cancel("Cancel") } else { overlay };
|
||||
// Escape closes it too, so the story's overlay without a card can close.
|
||||
overlay.cancellable(true).opened(Bind::bound(opener(ui, data, "Export…"))).show(ui, |_, _| {}).into()
|
||||
overlay.cancellable(true).opened(Bind::bound(opener(ui, data, EXPORT))).show(ui, |_, _| {}).into()
|
||||
}
|
||||
|
||||
#[story(ModalDragStrip, covers = [Default], drive = Drive::Never)]
|
||||
|
||||
@@ -112,7 +112,8 @@ 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 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`.
|
||||
/// 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
|
||||
|
||||
@@ -317,26 +317,39 @@ fn a_gesture_with_a_modifier_passes_only_with_the_modifier_held() {
|
||||
scene.assert_dialog_holds(&frames);
|
||||
}
|
||||
|
||||
/// The gesture the canvas's host passes for its running tool belongs to the top modal: it reaches the canvas under
|
||||
/// the open dialog, and once the dialog closes it lapses with it, so the next dialog blocks the canvas while the host
|
||||
/// passes nothing.
|
||||
/// The gesture the canvas's host passes for its running tool joins the canvas a dialog keeps: the press and its drag
|
||||
/// reach the canvas under the dialog, and once the dialog stops keeping the canvas the host's gesture lapses with it.
|
||||
#[trial]
|
||||
fn a_tool_gesture_the_host_passes_reaches_the_canvas_and_lapses_with_the_dialog() {
|
||||
fn a_tool_gesture_the_host_passes_reaches_a_canvas_the_dialog_keeps_and_lapses_with_it() {
|
||||
let mut frames = Frames::new();
|
||||
let scene = Scene::new(Arm::Nothing);
|
||||
let scene = Scene::new(Arm::View);
|
||||
scene.tool.set(Some(CanvasGesture::PRIMARY));
|
||||
scene.open_dialog(&mut frames);
|
||||
scene.drag(&mut frames, 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);
|
||||
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");
|
||||
scene.tool.set(None);
|
||||
scene.arm.set(Arm::Nothing);
|
||||
scene.settle(&mut frames, 3);
|
||||
scene.forget_canvas();
|
||||
scene.drag(&mut frames, CANVAS.center(), Vec2::new(40.0, 10.0));
|
||||
assert!(!scene.took(PointerButton::Primary), "the host's gesture lapsed with the dialog's");
|
||||
}
|
||||
|
||||
/// A dialog that keeps nothing (an alert) leaves the canvas blocked while the host goes on passing its tool's
|
||||
/// gesture: no press and no wheel reach it.
|
||||
#[trial]
|
||||
fn a_tool_gesture_passes_nothing_under_a_dialog_that_keeps_no_canvas() {
|
||||
let mut frames = Frames::new();
|
||||
let scene = Scene::new(Arm::Nothing);
|
||||
scene.tool.set(Some(CanvasGesture::PRIMARY));
|
||||
scene.open_dialog(&mut frames);
|
||||
scene.forget_canvas();
|
||||
scene.drag(&mut frames, CANVAS.center(), Vec2::new(40.0, 10.0));
|
||||
assert!(!scene.took(PointerButton::Primary), "the registration lapsed with its dialog");
|
||||
scene.frame(&mut frames, vec![wheel(-40.0)]);
|
||||
scene.settle(&mut frames, 2);
|
||||
assert!(!scene.took(PointerButton::Primary), "the press stayed blocked");
|
||||
assert_eq!(scene.canvas.borrow().wheels, 0, "the wheel stayed blocked");
|
||||
scene.assert_dialog_holds(&frames);
|
||||
}
|
||||
|
||||
/// A picker armed in a panel, outside every modal body, passes no press under a modal that opens while it stays armed.
|
||||
|
||||
@@ -292,7 +292,7 @@ name = "TonePickerRow"
|
||||
old = "craft_imaging::ui::adjust::tone::TonePickerRow"
|
||||
new = "craft_imaging::ui::adjust::TonePickerRow"
|
||||
status = "moved"
|
||||
note = "A pattern over `Bind<Option<TonePicker>>`; `canvas(Id)` names the document canvas the armed picker samples through the sampler of the modal layer (`Sampler::arm_sampler`)."
|
||||
note = "A pattern over `Bind<Option<TonePicker>>`; `canvas(Id)` names the document canvas the armed picker samples through the modal layer: the row passes its primary press (`Sampler::pass_canvas_gesture` with `CanvasGesture::PRIMARY`)."
|
||||
|
||||
[[type]]
|
||||
name = "TonePickerResponse"
|
||||
|
||||
Reference in New Issue
Block a user