mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 20:25:06 +00:00
Blur Gallery pins leave a Cmd press to the host and report it as ignored_press
The pins are a canvas control (a component with parts in the canvas body), not a CanvasTool: their presses never pass through the ToolHost, so ToolAction::Ignored can't carry this one. The output reports the press with the same ToolPointer that ToolHostResponse::ignored_press uses, for the app's tool.
This commit is contained in:
@@ -6,7 +6,7 @@ use craft_behavior::kurbo::Point;
|
||||
use craft_behavior::{
|
||||
CanvasView, Granularity, HitLevel, HitPriority, HitShape, Intent, NavShape, PartKey, PartRef, PointerButton, PressPhase, Roving, Sense,
|
||||
};
|
||||
use craft_ui::canvas::canvas_space;
|
||||
use craft_ui::canvas::{ToolPointer, canvas_space};
|
||||
use craft_ui::contract::{
|
||||
Access, Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Focus as _, MeasureCx, Name, Numeric, Outcome, PaintUi, PartsCx,
|
||||
Pointer as _, Props as _, Remember as _, Role, WordSetCx as _,
|
||||
@@ -205,7 +205,8 @@ fn marks(pins: &BlurPins, view: &CanvasView, reach: &PinReach, canvas: Rect) ->
|
||||
/// - A press on a pin selects it and a drag moves it; a press on the selected pin's ring turns its amount, and on a
|
||||
/// handle or a Tilt-Shift line shapes it. A press on the canvas outside the pins adds a pin and drags it
|
||||
/// (Tilt-Shift has one band and adds none). A press that moves is one gesture; a click that only adds or selects
|
||||
/// is a set. Escape cancels the drag.
|
||||
/// is a set. Escape cancels the drag. A press with Cmd (Ctrl) held is the host's: the pins change nothing and report
|
||||
/// it as [`BlurPinsOutput::ignored_press`], for the app's tool.
|
||||
/// - The pins are one Tab stop. Page Up and Page Down select the previous or next pin, the arrow keys move it one
|
||||
/// document pixel (Shift ten) and Delete or Backspace remove it. [`BlurPinsCommand::AddPin`] adds a pin at the
|
||||
/// middle of the bounds.
|
||||
@@ -216,12 +217,13 @@ pub struct BlurPinsOverlay<'a> {
|
||||
#[common]
|
||||
common: Common,
|
||||
pins: Bind<'a, BlurPins>,
|
||||
ignored_press: Option<ToolPointer>,
|
||||
}
|
||||
|
||||
impl<'a> BlurPinsOverlay<'a> {
|
||||
/// The pins named `label` over `pins`.
|
||||
pub fn new(label: impl Into<Label>, pins: Bind<'a, BlurPins>) -> Self {
|
||||
BlurPinsOverlay { common: Common::new(), pins }.label(label)
|
||||
BlurPinsOverlay { common: Common::new(), pins, ignored_press: None }.label(label)
|
||||
}
|
||||
|
||||
/// The pointer on the pins: a press grabs, a drag shapes, the release commits.
|
||||
@@ -232,6 +234,9 @@ impl<'a> BlurPinsOverlay<'a> {
|
||||
let Some(result) = cx.part_if_declared(mark.part) else { continue };
|
||||
let press = result.press;
|
||||
match press.phase {
|
||||
PressPhase::Down { start, .. } if start.button == PointerButton::Primary && press.mods.cmd => {
|
||||
self.ignored_press = Some(ToolPointer::new(start.origin, start.button, start.time).mods(press.mods).clicks(start.clicks));
|
||||
}
|
||||
PressPhase::Down { start, .. } if start.button == PointerButton::Primary => {
|
||||
let from = view.to_doc(start.origin);
|
||||
let mut at = pins.clone();
|
||||
@@ -344,6 +349,8 @@ pub struct BlurPinsOutput {
|
||||
/// The edit of the pins: a press that moves is one `Begin` to `Commit`; a click that adds or selects, a nudge and
|
||||
/// a delete are a `Set`.
|
||||
pub edit: EditResponse<BlurPins>,
|
||||
/// A press with Cmd (Ctrl) held, which the pins leave to the host: its tool acts on it (the momentary Zoom).
|
||||
pub ignored_press: Option<ToolPointer>,
|
||||
}
|
||||
|
||||
impl Outcome for BlurPinsOutput {
|
||||
@@ -472,7 +479,7 @@ impl Component for BlurPinsOverlay<'_> {
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> BlurPinsOutput {
|
||||
BlurPinsOutput { edit: cx.edit_response() }
|
||||
BlurPinsOutput { edit: cx.edit_response(), ignored_press: self.ignored_press }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ use craft_imaging::ui::blur_pins::{
|
||||
use craft_ui::canvas::Canvas;
|
||||
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
|
||||
use craft_ui::testing::{Fixture, pointer, trial};
|
||||
use egui::{Pos2, Rect, pos2};
|
||||
use egui::{Modifiers, Pos2, Rect, pos2};
|
||||
|
||||
const BOUNDS: [f64; 4] = [0.0, 0.0, 1000.0, 800.0];
|
||||
|
||||
@@ -102,23 +102,29 @@ struct Overlay {
|
||||
fixture: Fixture,
|
||||
pins: Rc<RefCell<BlurPins>>,
|
||||
view: Rc<Cell<Option<CanvasView>>>,
|
||||
/// The presses the overlay left to the host.
|
||||
ignored: Rc<RefCell<Vec<Pos2>>>,
|
||||
}
|
||||
|
||||
fn overlay(pins: BlurPins) -> Overlay {
|
||||
let pins = Rc::new(RefCell::new(pins));
|
||||
let view = Rc::new(Cell::new(None));
|
||||
let (shown_pins, shown_view) = (Rc::clone(&pins), Rc::clone(&view));
|
||||
let ignored = Rc::new(RefCell::new(Vec::new()));
|
||||
let (shown_pins, shown_view, seen) = (Rc::clone(&pins), Rc::clone(&view), Rc::clone(&ignored));
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let canvas_view = CanvasView::new(room, Point::new(200.0, 150.0), 1.0);
|
||||
shown_view.set(Some(canvas_view));
|
||||
Canvas::new("Document").view(canvas_view).show(ui, |ui, body| {
|
||||
let shown = Canvas::new("Document").view(canvas_view).show(ui, |ui, body| {
|
||||
let mut held = shown_pins.borrow_mut();
|
||||
BlurPinsOverlay::new("Blur pins", Bind::bound(&mut *held)).show_at(ui, body.rect)
|
||||
});
|
||||
if let Some(press) = shown.inner.and_then(|output| output.ignored_press) {
|
||||
seen.borrow_mut().push(press.pos);
|
||||
}
|
||||
});
|
||||
fixture.settle();
|
||||
Overlay { fixture, pins, view }
|
||||
Overlay { fixture, pins, view, ignored }
|
||||
}
|
||||
|
||||
impl Overlay {
|
||||
@@ -177,3 +183,19 @@ fn blur_pins_ring_drag_turns_the_amount_over_several_frames() {
|
||||
let amount = o.pins.borrow().pins[0].amount;
|
||||
assert!((amount - 390.0).abs() < 1.0, "{amount}");
|
||||
}
|
||||
|
||||
// A press with Cmd (Ctrl) held is the host's: no pin is added and the overlay reports the press.
|
||||
#[trial]
|
||||
fn blur_pins_leave_a_cmd_press_to_the_host() {
|
||||
let mut o = overlay(pins_of(BlurKind::Field, &[15.0]));
|
||||
let empty = o.at(300.0, 250.0);
|
||||
o.fixture.hold(Modifiers::COMMAND);
|
||||
o.press(empty);
|
||||
o.release(empty);
|
||||
o.fixture.hold(Modifiers::NONE);
|
||||
assert_eq!(o.pins.borrow().pins.len(), 1, "no pin added");
|
||||
assert_eq!(*o.ignored.borrow(), [empty], "the press is reported for the host");
|
||||
o.press(empty);
|
||||
o.release(empty);
|
||||
assert_eq!(o.pins.borrow().pins.len(), 2, "a plain press adds a pin");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user