diff --git a/crates/behavior/src/hit.rs b/crates/behavior/src/hit.rs index a131f52ee..b8e32eaff 100644 --- a/crates/behavior/src/hit.rs +++ b/crates/behavior/src/hit.rs @@ -223,12 +223,14 @@ pub struct Shape { /// The named stacking order: stacked parts of one priority rank by it before the nearest centre, the higher order /// winning. pub stacking: u32, + /// Whether a passed drag targets the arena root instead of the next shape. + pub drag_to_root: bool, } impl Shape { /// A shape of `owner` at stack `position`. pub const fn new(shape: HitShape, sense: Sense, owner: O, part: Option, priority: HitPriority, position: u32) -> Self { - Shape { shape, sense, owner, part, priority, position, local: false, min_across: 0.0, stacking: 0 } + Shape { shape, sense, owner, part, priority, position, local: false, min_across: 0.0, stacking: 0, drag_to_root: false } } /// The hit area is at least `across` points wide and high. @@ -280,6 +282,8 @@ pub struct Winner { pub sense: Sense, /// Its shape lies in the body layer's space. pub local: bool, + /// A passed drag targets the arena root. + pub drag_to_root: bool, } /// The winner among `shapes` at `point`: stack position first, then level, sub-level, stacking order, the nearest @@ -303,7 +307,7 @@ pub fn resolve_mapped(shapes: &[Shape], point: Pos2, what: ResolveFo .then_with(|| distance(b).partial_cmp(&distance(a)).unwrap_or(Ordering::Equal)) .then(ia.cmp(ib)) }) - .map(|(index, shape)| Winner { index, owner: shape.owner, part: shape.part, sense: shape.sense, local: shape.local }) + .map(|(index, shape)| Winner { index, owner: shape.owner, part: shape.part, sense: shape.sense, local: shape.local, drag_to_root: shape.drag_to_root }) } #[cfg(test)] diff --git a/crates/ui-canvas/src/canvas/tool_host.rs b/crates/ui-canvas/src/canvas/tool_host.rs index cafcec80e..0ed77d2f3 100644 --- a/crates/ui-canvas/src/canvas/tool_host.rs +++ b/crates/ui-canvas/src/canvas/tool_host.rs @@ -1288,10 +1288,10 @@ impl<'a, 't: 'a> Run<'a, 't> { /// The keys the canvas holds: intents routed to it while it has focus, the arrows, Enter and Escape while nothing /// in the window has focus, and the chords the tool claimed at the end of the last pass. A session control in the /// canvas body (`KeyUse::session`) has taken the arrows, Enter and Escape it acts on before this runs. The Escape - /// of the focused canvas reaches the tool once, through the window's intents (`with_window_intents` offers the + /// of the focused canvas reaches the tool once, through the window's intents (`with_canvas_window_intents` offers the /// focused document canvas's Escape); one the tool leaves stays the canvas's, for the scopes around it. fn keys(&mut self, ctx: &Context, canvas: Id, rtl: bool) { - keys::with_window_intents(ctx, rtl, |window| { + keys::with_canvas_window_intents(ctx, rtl, |window| { let mut taken = self.offer(window, |intents| intents.take_nudge().map(|(direction, mods)| ToolKey::Nudge { direction, mods })); taken |= self.offer(window, |intents| intents.take(Intent::Submit).then_some(ToolKey::Submit)); if !self.cancelled { diff --git a/crates/ui-contract/src/contract/cx/parts.rs b/crates/ui-contract/src/contract/cx/parts.rs index 223d4325f..1fab11e0d 100644 --- a/crates/ui-contract/src/contract/cx/parts.rs +++ b/crates/ui-contract/src/contract/cx/parts.rs @@ -96,6 +96,13 @@ pub(crate) struct PartExtras { } impl PartBuilder<'_> { + /// Send a passed drag to the enclosing arena root, retaining clicks on this part. + /// Declare `CLICK | PASSES_DRAG` as the part's sense. + pub fn drag_to_root(self) -> Self { + self.decl.drag_to_root = true; + self + } + /// What the part senses; hover only by default. pub fn sense(self, sense: Sense) -> Self { self.decl.sense = sense; diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index 2dbb6a3d6..8767d4a6a 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -131,6 +131,7 @@ pub(crate) struct PartDecl { pub held: bool, /// The named stacking order among parts of one priority (default 0). pub stacking: u32, + pub drag_to_root: bool, } impl PartDecl { @@ -148,6 +149,7 @@ impl PartDecl { inactive: false, held: false, stacking: 0, + drag_to_root: false, } } @@ -318,7 +320,9 @@ fn rotate(state: &mut ArenaState, pass_nr: u64) { } fn own_part_shape(part: &PartDecl, root: Id, position: u32, target: f32) -> Shape { - Shape::new(part.shape.clone(), part.sense, root, Some(part.part_ref()), part.priority, position).stacking(part.stacking).min_across(target) + let mut shape = Shape::new(part.shape.clone(), part.sense, root, Some(part.part_ref()), part.priority, position).stacking(part.stacking).min_across(target); + shape.drag_to_root = part.drag_to_root; + shape } /// The root's own parts but the `over` ones, at position 0. @@ -343,10 +347,12 @@ fn over_position(count: usize) -> u32 { } fn member_part_shape(member: &MemberDecl, part: &PartDecl, position: u32, target: f32) -> Shape { - Shape::new(part.shape.clone(), part.sense, member.id, Some(part.part_ref()), part.priority, position) + let mut shape = Shape::new(part.shape.clone(), part.sense, member.id, Some(part.part_ref()), part.priority, position) .stacking(part.stacking) .local(member.local) - .min_across(target) + .min_across(target); + shape.drag_to_root = part.drag_to_root; + shape } /// The member's root shape and its parts but the `over` ones, at its stack position. diff --git a/crates/ui-contract/src/runtime/keys.rs b/crates/ui-contract/src/runtime/keys.rs index d4a806820..a113f01ee 100644 --- a/crates/ui-contract/src/runtime/keys.rs +++ b/crates/ui-contract/src/runtime/keys.rs @@ -37,7 +37,7 @@ pub(crate) use intents::lent_taken; pub use intents::settle; #[doc(hidden)] pub use intents::take_submit; -pub use intents::with_window_intents; +pub use intents::{with_canvas_window_intents, with_window_intents}; pub(crate) use intents::{ Lent, context_part, context_request, give_back, lend, request_context, route_whole_reset, scope_escape, take_cancel, take_every_escape, take_window_cancel, diff --git a/crates/ui-contract/src/runtime/keys/intents.rs b/crates/ui-contract/src/runtime/keys/intents.rs index f513bc0ed..0a5ca227c 100644 --- a/crates/ui-contract/src/runtime/keys/intents.rs +++ b/crates/ui-contract/src/runtime/keys/intents.rs @@ -266,38 +266,43 @@ pub fn intents_of(ctx: &Context, id: Option, rtl: bool) -> Intents { } } -/// Offers the window's intents to `take` while nothing in the window has framework focus or the focused stop is the -/// window's document canvas (`KeyUse::chrome_key`) or one of its canvas controls (`focus::is_canvas_member`), and -/// writes back what it left untaken. `take` returns whether it took any. `false` when another stop is focused or -/// nothing was taken. +/// Offers the window's intents and the focused framework stop's unused Escape to `take`, and writes back what it +/// left untaken. `take` returns whether it took any. /// /// The window's intents are the keys the runtime routes to no component this pass: an Escape (`Intent::Cancel`) -/// or a plain Enter (`Intent::Submit`) while no framework stop is focused, and the arrows' nudge; with the document -/// canvas or a canvas control focused, the Escape that stop, the canvas's sessions, its tool and every scope around -/// it left. A component or a +/// or a plain Enter (`Intent::Submit`) while no framework stop is focused, and the arrows' nudge; with a framework +/// stop focused, its unused Escape. A component or a /// scope that runs earlier in the pass takes them first (an open dialog takes the Escape that closes it, a canvas /// session the one that ends it), so an app that hosts the runtime reads them last in its UI to see what no /// component took. `rtl` reads horizontal moves in the window's direction. pub fn with_window_intents(ctx: &Context, rtl: bool, take: impl FnOnce(&mut Intents) -> bool) -> bool { - let canvas = match focus::focused_rec(ctx) { - Some(rec) if rec.stop.keys.chrome_key || focus::is_canvas_member(ctx, &rec) => Some(rec.stop.id), - Some(_) => return false, - None => None, - }; + offer_window_intents(ctx, rtl, take) +} + +/// Offers window intents to a canvas while the canvas or one of its controls has focus, or nothing has focus. +/// Other focused controls get the opportunity to use their Escape before the window reads it. +pub fn with_canvas_window_intents(ctx: &Context, rtl: bool, take: impl FnOnce(&mut Intents) -> bool) -> bool { + if focus::focused_rec(ctx).is_some_and(|rec| !rec.stop.keys.chrome_key && !focus::is_canvas_member(ctx, &rec)) { + return false; + } + offer_window_intents(ctx, rtl, take) +} + +fn offer_window_intents(ctx: &Context, rtl: bool, take: impl FnOnce(&mut Intents) -> bool) -> bool { + let focused = focus::focused_rec(ctx).map(|rec| rec.stop.id); let mut window = intents_of(ctx, None, rtl); - // The focused canvas's or canvas control's leftover Escape is offered as the window's; it stays in that stop's - // slot until taken. - let lent_cancel = canvas - .filter(|canvas| !window.items.iter().any(|(item, _)| *item == Intent::Cancel) && holds(ctx, Some(*canvas), &Intent::Cancel)) - .map(|canvas| { + // The focused stop's leftover Escape stays in its slot until taken. + let lent_cancel = focused + .filter(|id| !window.items.iter().any(|(item, _)| *item == Intent::Cancel) && holds(ctx, Some(*id), &Intent::Cancel)) + .map(|id| { window.items.push((Intent::Cancel, false)); - (canvas, window.items.len() - 1) + (id, window.items.len() - 1) }); let taken = take(&mut window); - if let Some((canvas, at)) = lent_cancel { + if let Some((id, at)) = lent_cancel { let (_, cancel_taken) = window.items.remove(at); if cancel_taken && !crate::runtime::measure::active(ctx) { - current(ctx, |stage| take_from(stage, Some(canvas), &Intent::Cancel)); + current(ctx, |stage| take_from(stage, Some(id), &Intent::Cancel)); } } if taken { @@ -403,7 +408,7 @@ pub(crate) fn lent_taken(ctx: &Context) -> u64 { /// Takes the window's untaken `Cancel` (Escape) while nothing has focus. pub(crate) fn take_window_cancel(ctx: &Context) -> bool { - with_window_intents(ctx, false, |window| window.take(Intent::Cancel)) + with_canvas_window_intents(ctx, false, |window| window.take(Intent::Cancel)) } /// Writes back what a component left untaken, so a scope's `Submit` and `Cancel` rules see it. diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index 1c1981941..2ae250732 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -788,7 +788,11 @@ fn find_under(ctx: &Context, viewport: ViewportId, at: Pos2, targets: PressTarge let dragging: Vec = pressable .iter() .copied() - .filter(|&index| index != winner.index && entry.shapes[index].sense.contains(craft_behavior::Sense::DRAG)) + .filter(|&index| { + let shape = &entry.shapes[index]; + index != winner.index && shape.sense.contains(craft_behavior::Sense::DRAG) + && (!winner.drag_to_root || (shape.owner == root && shape.part.is_none())) + }) .collect(); pick(&dragging, ResolveFor::Press) }) @@ -839,7 +843,8 @@ fn winners_of(mut ranked: Vec<((u8, u32), Winner, Id, bool)>) -> PressWinner // shape that passes its drag, the next shape that drags takes it, in the same root or one below. (true, false) => { let passes = winners.click.is_some_and(|(top, _)| top.sense.contains(craft_behavior::Sense::PASSES_DRAG)); - if drags && (passes || !(clicks || root_clicks)) { + let root_target = winners.click.is_some_and(|(top, owner)| top.drag_to_root && (root != owner || winner.part.is_some())); + if drags && !root_target && (passes || !(clicks || root_clicks)) { winners.drag = Some((winner, root)); } break; diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index a29a40590..fdc6a7bb6 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -46,6 +46,37 @@ fn press_key(frames: &mut Frames, show: &mut impl FnMut(&mut Ui), key: Key) { frames.frame(vec![key_press(key, Modifiers::NONE)], &mut *show); } +#[trial] +fn escape_unused_by_a_focused_control_reaches_the_window() { + let (host, mut show) = host_window(); + let mut frames = Frames::new(); + frames.idle(2, &mut show); + request(&frames.ctx, FocusTarget::Id(named_id("button"))); + frames.idle(2, &mut show); + press_key(&mut frames, &mut show, Key::Escape); + assert_eq!(seen(&host).1, 1, "the focused button leaves Escape for the window"); +} + +#[trial] +fn a_canvas_cannot_take_escape_before_an_unrelated_focused_control() { + let (host, mut base) = host_window(); + let stolen = Rc::new(std::cell::Cell::new(false)); + let canvas_seen = Rc::clone(&stolen); + let mut show = move |ui: &mut Ui| { + if crate::runtime::keys::with_canvas_window_intents(ui.ctx(), false, |intents| intents.take(Intent::Cancel)) { + canvas_seen.set(true); + } + base(ui); + }; + let mut frames = Frames::new(); + frames.idle(2, &mut show); + request(&frames.ctx, FocusTarget::Id(named_id("button"))); + frames.idle(2, &mut show); + press_key(&mut frames, &mut show, Key::Escape); + assert!(!stolen.get()); + assert_eq!(seen(&host).1, 1); +} + /// A window with a framework button and the host's own egui text field, with what the host saw. fn host_window() -> (Rc>, impl FnMut(&mut Ui)) { let host = Rc::new(RefCell::new(Host { text: String::new(), rect: Rect::NOTHING, reached: Vec::new(), cancels: 0, start_drag: false })); diff --git a/crates/ui-contract/src/testing/sensing.rs b/crates/ui-contract/src/testing/sensing.rs index f9d54b99c..3bdf93017 100644 --- a/crates/ui-contract/src/testing/sensing.rs +++ b/crates/ui-contract/src/testing/sensing.rs @@ -23,12 +23,13 @@ pub struct ProbePart { priority: HitPriority, over: bool, cursor: Option, + drag_to_root: bool, } impl ProbePart { /// A part `key` over `rect` sensing `sense`. pub fn new(key: PartKey, rect: Rect, sense: Sense) -> Self { - ProbePart { key, index: None, rect, sense, priority: HitPriority::BODY, over: false, cursor: None } + ProbePart { key, index: None, rect, sense, priority: HitPriority::BODY, over: false, cursor: None, drag_to_root: false } } /// The part's tier among the shapes of its stack position. @@ -37,6 +38,12 @@ impl ProbePart { self } + /// Send a passed drag to the arena root. + pub fn drag_to_root(mut self) -> Self { + self.drag_to_root = true; + self + } + /// The part sits above the body members. pub fn over(mut self) -> Self { self.over = true; @@ -61,6 +68,7 @@ impl ProbePart { decl.priority = self.priority; decl.over = self.over; decl.cursor = self.cursor.map(Cursor::System); + decl.drag_to_root = self.drag_to_root; decl } } diff --git a/crates/ui-contract/tests/it/runtime/pointer.rs b/crates/ui-contract/tests/it/runtime/pointer.rs index b59d11096..3c2e3a252 100644 --- a/crates/ui-contract/tests/it/runtime/pointer.rs +++ b/crates/ui-contract/tests/it/runtime/pointer.rs @@ -501,6 +501,25 @@ fn one_pointer_resolution_per_pass_whatever_the_number_of_roots() { const LINK: PartKey = PartKey::new("link"); const SURFACE: PartKey = PartKey::new("surface"); +/// A clickable overlay can send its drag to the canvas root past a draggable field. +#[trial] +fn passed_drag_can_target_the_arena_root_past_a_field() { + let page = rect(100.0, 100.0, 400.0, 300.0); + let area = rect(150.0, 150.0, 60.0, 40.0); + let field = ProbePart::new(SURFACE, area, Sense::CLICK | Sense::DRAG); + let comment = ProbePart::new(LINK, area, Sense::CLICK | Sense::PASSES_DRAG).level(HitLevel::Edge, 0).drag_to_root(); + let root = ProbeRoot::new("canvas", page, Sense::DRAG).arena().member(ProbeMember::new("parts", page, Sense::empty()).part(field).part(comment)); + let (mut show_frame, log) = scene(vec![root]); + let mut frames = Frames::new(); + frames.idle(2, &mut show_frame); + drag(&mut frames, area.center(), Vec2::new(40.0, 30.0), &mut show_frame); + assert_eq!(drags(&log.borrow().root_phases(0, None)), 1, "canvas takes the drag"); + assert_eq!(drags(&log.borrow().member_phases(0, "parts", Some(SURFACE))), 0, "field does not drag"); + log.borrow_mut().clear(); + pointer::click(&mut frames, area.center(), &mut show_frame); + assert!(log.borrow().member_phases(0, "parts", Some(LINK)).iter().any(|phase| matches!(phase, PressPhase::Release { click: true, .. }))); +} + /// L-19: a link on a page clicks, but a drag that starts on it belongs to the page under it (production's /// `Hit::declines_drag`). A press past slop on a click part that passes its drag gives the `DragStart` to the next /// shape below that drags, whether that shape is the same root's own part or a root shown below; the link sees no diff --git a/crates/ui-panels/src/rails/drawer.rs b/crates/ui-panels/src/rails/drawer.rs index 33f7420a7..8d3e1ced8 100644 --- a/crates/ui-panels/src/rails/drawer.rs +++ b/crates/ui-panels/src/rails/drawer.rs @@ -58,13 +58,13 @@ pub enum ToolbarEdit { /// Where it goes. at: DropAt, }, - /// Take the tool off the toolbar. + /// Take every occurrence of the tool off the toolbar. Use `RemoveFrom` to remove one occurrence. #[non_exhaustive] Remove { /// The tool. tool: T, }, - /// Move a tool that is on the toolbar to `at`. + /// Move a tool by id to `at`, removing its other occurrences. Use `MoveFrom` to move one occurrence. #[non_exhaustive] Move { /// The tool. @@ -72,6 +72,22 @@ pub enum ToolbarEdit { /// Where it goes. at: DropAt, }, + /// Remove one occurrence, identified by its slot and index. + RemoveFrom { + /// The source slot. + slot: SlotId, + /// The index in the source slot. + index: usize, + }, + /// Move one occurrence, identified by its slot and index. + MoveFrom { + /// The source slot. + slot: SlotId, + /// The index in the source slot. + index: usize, + /// The destination. + at: DropAt, + }, /// Go back to the default toolbar. Reset, } @@ -91,6 +107,16 @@ impl ToolbarEdit { pub fn move_to(tool: T, at: DropAt) -> Self { ToolbarEdit::Move { tool, at } } + + /// Remove the occurrence at `index` in `slot`. + pub fn remove_from(slot: SlotId, index: usize) -> Self { + ToolbarEdit::RemoveFrom { slot, index } + } + + /// Move the occurrence at `index` in `slot` to `at`. + pub fn move_from(slot: SlotId, index: usize, at: DropAt) -> Self { + ToolbarEdit::MoveFrom { slot, index, at } + } } /// What the drawer reports. @@ -240,9 +266,9 @@ fn match_texts(command: &ResolvedCommand, tool: &RailTool) -> Vec