diff --git a/crates/ui-contract/src/contract/body/presses.rs b/crates/ui-contract/src/contract/body/presses.rs index 54e203e75..ca96137d0 100644 --- a/crates/ui-contract/src/contract/body/presses.rs +++ b/crates/ui-contract/src/contract/body/presses.rs @@ -8,7 +8,7 @@ use crate::runtime::egui::areas; use crate::runtime::memory::sub; use crate::runtime::pointer; use craft_behavior::{Cursor, CursorPriority, PartRef, PointerButton, PressState}; -use egui::Pos2; +use egui::{Pos2, Ui}; /// Whether the second press of a double click went down on the container's move band in this pass. #[derive(Clone, Default)] @@ -47,6 +47,16 @@ impl BodyCx<'_> { self.memory.recall::(sub(self.id, "surface_press")).pressed } + /// Whether the primary press of this pass went down in the body's rect, on the layer `ui` (a `Ui` of this body) + /// paints on, whatever takes it: a child, a part, an app's own egui widget or the surface itself. A press on a + /// popup or a window above the body is not in it. The container decides what it means (a dock group whose body is + /// the app's own egui code names its panel as the one pressed into); it moves no focus by itself. A measuring run + /// sees none, for the reason [`band_double_pressed`](Self::band_double_pressed) gives. + pub fn pressed_within(&self, ui: &Ui) -> bool { + let ctx = self.memory.ctx(); + !self.memory.is_peek() && pointer::primary_press_in(ctx, ui.layer_id(), self.rect).is_some() + } + /// Whether the press of [`surface_pressed`](Self::surface_pressed) is the second press of a double click: no child /// or part takes it and its click count is two or more. One own-surface signal for every container (an empty /// workspace opens a file on it). A measuring run sees none, for the reason @@ -85,15 +95,10 @@ impl BodyHooks<'_> { let ctx = core.ctx.clone(); let pass = pointer::pass_pointer(&ctx); let (layer, rect) = (core.response.layer_id, core.rect); - // Whether `pos` lies in the container's rect, on its layer. - let inside = |pos: Pos2| { - let at = areas::layer_map(&ctx, layer).map_or(pos, |map| map.to_layer(pos)); - areas::layer_at(&ctx, pos) == Some(layer) && rect.contains(at) - }; + let inside = |pos: Pos2| areas::lies_in(&ctx, layer, rect, pos); let surface = if sensed { - pass.frame.as_ref().and_then(|frame| frame.press).filter(|press| press.button == PointerButton::Primary).filter(|press| { - inside(press.origin) - && pointer::press_winner(&ctx, press.origin, pass.press_targets).is_none() + pointer::primary_press_in(&ctx, layer, rect).filter(|press| { + pointer::press_winner(&ctx, press.origin, pass.press_targets).is_none() && !pointer::text_takes_press(&ctx, pass.press_targets) && ![pass.press_targets.click, pass.press_targets.drag].into_iter().flatten().any(|widget| pointer::foreign_target(&ctx, widget)) }) diff --git a/crates/ui-contract/src/runtime/egui/areas.rs b/crates/ui-contract/src/runtime/egui/areas.rs index f9a3b58a0..090a019ec 100644 --- a/crates/ui-contract/src/runtime/egui/areas.rs +++ b/crates/ui-contract/src/runtime/egui/areas.rs @@ -53,6 +53,12 @@ pub(crate) fn layer_position(ctx: &Context, layer: LayerId) -> usize { ctx.memory(|memory| memory.layer_ids().position(|listed| listed == layer).map_or(0, |at| at + 1)) } +/// Whether the screen position `pos` lies in `rect`, given in the space of `layer`, with `layer` the topmost layer there. +pub(crate) fn lies_in(ctx: &Context, layer: LayerId, rect: Rect, pos: Pos2) -> bool { + let at = layer_map(ctx, layer).map_or(pos, |map| map.to_layer(pos)); + layer_at(ctx, pos) == Some(layer) && rect.contains(at) +} + /// The topmost layer under a screen position. Inside the canvas a modal layer leaves open (`runtime::sampler`) and /// where nothing but the canvas lies over it, it answers the canvas's own layer as if the modal's gate and its scrim /// were not there. diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index 5c3f11500..36e734e57 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -411,6 +411,13 @@ pub(crate) fn pass_of(ctx: &Context) -> u64 { pass::pass_nr(ctx, pass::current_viewport(ctx)) } +/// The primary press of this pass when it went down in `rect` on `layer` (`areas::lies_in`), whatever takes it. Read +/// as the press step reads it, without noting a pointer input: the press engages the sensing of what it lands on. +pub(crate) fn primary_press_in(ctx: &Context, layer: Layer, rect: egui::Rect) -> Option { + with_pass_raw(ctx, |pass| pass.frame.as_ref().and_then(|frame| frame.press)) + .filter(|press| press.button == PointerButton::Primary && areas::lies_in(ctx, layer, rect, press.origin)) +} + /// What the begin hook read of the running pass. pub(crate) fn pass_pointer(ctx: &Context) -> Arc { let viewport = pass::current_viewport(ctx); diff --git a/crates/ui-patterns/src/patterns/dock/area.rs b/crates/ui-patterns/src/patterns/dock/area.rs index c9e48b04e..1f19d184f 100644 --- a/crates/ui-patterns/src/patterns/dock/area.rs +++ b/crates/ui-patterns/src/patterns/dock/area.rs @@ -80,6 +80,11 @@ pub struct DockAreaOutput

{ pub events: Vec>, /// The shown panel of the group that holds key focus: focus is on the group or on a control inside it. pub focused: Option

, + /// The shown panel of the group whose body the primary press of this pass went down in, whatever took the press: a + /// framework control, the group's empty body or the app's own egui widget in a host body + /// ([`PanelSpec::host_body`](crate::docking::PanelSpec::host_body)), which keeps its presses and focus, so + /// `focused` does not name its group. A host that keeps a focused panel of its own makes it this one. + pub pressed: Option

, /// The panel whose tab the user clicked or picked this pass; a pick of the host's `DockArea::focus` is not the /// user's and shows only in `actions`. pub picked: Option

, @@ -321,6 +326,7 @@ struct Report

{ actions: Vec>, events: Vec>, focused: Option

, + pressed: Option

, picked: Option

, flyout: Vec>>, placed: Vec<(P, Rect)>, @@ -488,6 +494,7 @@ struct Passed

{ actions: Vec>, events: Vec>, focused: Option

, + pressed: Option

, picked: Option

, edit: EditEvents>>, space: Option>, @@ -657,19 +664,21 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { passed = Some(result); }); let requested = close_requests::

(&ctx); - let Passed { geometry, actions, events, focused, picked, edit, space, columns, windows, files, sources } = passed.unwrap_or_else(|| Passed { - geometry: Vec::new(), - actions: Vec::new(), - events: Vec::new(), - focused: None, - picked: None, - edit: EditEvents::new(), - space: None, - columns: Vec::new(), - windows: Vec::new(), - files: Vec::new(), - sources: Vec::new(), - }); + let Passed { geometry, actions, events, focused, pressed, picked, edit, space, columns, windows, files, sources } = + passed.unwrap_or_else(|| Passed { + geometry: Vec::new(), + actions: Vec::new(), + events: Vec::new(), + focused: None, + pressed: None, + picked: None, + edit: EditEvents::new(), + space: None, + columns: Vec::new(), + windows: Vec::new(), + files: Vec::new(), + sources: Vec::new(), + }); if shown.output.dropped.is_some() { cross_window::accept_cross_window(&ctx); } @@ -679,6 +688,7 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> { requested, events, focused, + pressed, picked, error, edit, @@ -838,6 +848,7 @@ fn show_area( actions: Vec::new(), events: Vec::new(), focused: None, + pressed: None, picked: None, flyout: Vec::new(), placed: Vec::new(), @@ -943,6 +954,7 @@ fn show_area( actions: report.actions, events: report.events, focused: report.focused, + pressed: report.pressed, picked: report.picked, edit, space: Some(space), @@ -1274,6 +1286,7 @@ impl Pass<'_, P> { // the group's room, never in the room the area has left. Where the host leaves the group to the dock, the // group shows in its place. Both run every pass; the host memoizes inside its view, the group inside its // header and its bodies. + let pressed = Cell::new(false); let (strip, view) = if grip > 0.0 { rect.split_top_bottom_at_y(rect.top() + grip.min(rect.height())) } else { (Rect::NOTHING, rect) }; let output = ui .scope_builder(UiBuilder::new().max_rect(rect), |ui| { @@ -1288,7 +1301,14 @@ impl Pass<'_, P> { None => { let group = make(active, look.minimized); let group = if focus.is_some() { group.take_focus() } else { group }; - Some(group.show_at(ui, rect, |ui, body| panel_body(scene, &panels, shown, ui, body)).output) + Some( + group + .show_at(ui, rect, |ui, body| { + pressed.set(pressed.get() || body.pressed_within(ui)); + panel_body(scene, &panels, shown, ui, body); + }) + .output, + ) } } }) @@ -1361,6 +1381,9 @@ impl Pass<'_, P> { for (panel, document) in output.closed_documents.into_iter().filter_map(|(key, document)| Some((scene.panel(key)?, document))) { self.report.events.push(DockEvent::DocumentClosed { panel: panel.clone(), document }); } + if pressed.get() { + self.report.pressed = Some(shown.clone()); + } // A tab picked in this pass is the group's shown panel from now on, though this pass drew the one before. if focus_within(ui.ctx(), output.response.id) { self.report.focused = Some(picked.unwrap_or(shown).clone()); diff --git a/crates/ui-patterns/tests/it/docking/tab_focus.rs b/crates/ui-patterns/tests/it/docking/tab_focus.rs index c4c21d266..fb3377679 100644 --- a/crates/ui-patterns/tests/it/docking/tab_focus.rs +++ b/crates/ui-patterns/tests/it/docking/tab_focus.rs @@ -1,5 +1,6 @@ //! A pointer press on a dock's panel tab leaves key focus where it was, so a hosted app's own shortcuts keep working -//! after a tab click, a tab drag or the tab menu; a press on an app's own egui body is the app's. +//! after a tab click, a tab drag or the tab menu; a press on an app's own egui body is the app's, and the area names +//! the panel pressed into (`DockAreaOutput::pressed`). use craft_behavior::Axis; use craft_behavior::dock::{Layout, Node, SplitSize, Workspace}; @@ -29,21 +30,32 @@ impl PanelSpecs for Specs { struct Panels; impl DockPanels for Panels { - fn body(&mut self, _: &usize, _: &mut Ui, _: &mut BodyCx<'_>) {} + /// The hosted body holds an app's own egui button. + fn body(&mut self, panel: &usize, ui: &mut Ui, _: &mut BodyCx<'_>) { + if *panel == HOSTED { + let _ = ui.button("App button"); + } + } } -/// A dock of the plain group [0, 1] beside the hosted group [2], and the workspace it edits. -fn dock() -> (Fixture, Rc>>>) { +/// The panels the area named as pressed into, one entry per pass that named one. +type Pressed = Rc>>; + +/// A dock of the plain group [0, 1] beside the hosted group [2], the workspace it edits and the panels it reported +/// pressed. +fn dock() -> (Fixture, Rc>>>, Pressed) { let root = Node::split(Axis::X, SplitSize::Ratio(0.5), Node::tabs(vec![0, 1], 0), Node::tabs(vec![HOSTED], 0)); let workspace = Rc::new(RefCell::new(Arc::new(Workspace::new(Layout::new(Some(root), Vec::new()))))); - let (held, specs) = (Rc::clone(&workspace), Tracked::new(Specs)); + let pressed: Pressed = Rc::default(); + let (held, seen, specs) = (Rc::clone(&workspace), Rc::clone(&pressed), Tracked::new(Specs)); let mut fixture = Fixture::new(move |ui| { let mut shown = held.borrow().clone(); - DockArea::new("Dock", Bind::bound(&mut shown), &specs, &mut Panels, DockConfig::default()).show(ui); + let out = DockArea::new("Dock", Bind::bound(&mut shown), &specs, &mut Panels, DockConfig::default()).show(ui); + seen.borrow_mut().extend(out.pressed); *held.borrow_mut() = shown; }); fixture.room(Vec2::new(1200.0, 600.0)).settle(); - (fixture, workspace) + (fixture, workspace, pressed) } /// A secondary press and release at `at`, then an idle frame. @@ -54,7 +66,7 @@ fn secondary_click(fixture: &mut Fixture, at: Pos2) { #[trial] fn a_press_on_a_panel_tab_leaves_key_focus_where_it_was() { - let (mut fixture, workspace) = dock(); + let (mut fixture, workspace, _) = dock(); let tab = fixture.by_label("Panel 1").expect("the first group's second tab"); fixture.click(&tab).idle(1); let shows_one = @@ -78,7 +90,7 @@ fn a_press_on_a_panel_tab_leaves_key_focus_where_it_was() { #[trial] fn a_press_on_an_app_body_is_the_apps_and_one_on_a_plain_body_focuses_its_panel() { - let (mut fixture, _) = dock(); + let (mut fixture, ..) = dock(); let hosted = fixture.by_label("Panel 2").expect("the hosted group's tab").rect; fixture.click_at(Pos2::new(hosted.center().x, hosted.bottom() + 200.0)).idle(1); assert!(fixture.focused_stop().is_none(), "a press on the app's own body takes no framework focus"); @@ -86,3 +98,21 @@ fn a_press_on_an_app_body_is_the_apps_and_one_on_a_plain_body_focuses_its_panel( fixture.click_at(Pos2::new(plain.center().x, plain.bottom() + 200.0)).idle(1); assert!(fixture.focused_stop().is_some(), "a press on a framework panel's empty body focuses its panel"); } + +/// A press in a group's body names the group's shown panel, whatever takes it: the app's own button in a host body +/// (which keeps the press and takes no framework focus) or a plain panel's empty body. A press on a tab is no body +/// press. +#[trial] +fn a_press_in_a_groups_body_names_its_panel_whatever_takes_it() { + let (mut fixture, _, pressed) = dock(); + let button = fixture.by_label("App button").expect("the app's own button in the hosted body"); + fixture.click(&button).idle(1); + assert_eq!(pressed.borrow().as_slice(), [HOSTED], "a press on the app's own widget names the hosted panel"); + pressed.borrow_mut().clear(); + let plain = fixture.by_label("Panel 0").expect("the plain group's tab"); + fixture.click_at(Pos2::new(plain.rect.center().x, plain.rect.bottom() + 200.0)).idle(1); + assert_eq!(pressed.borrow().as_slice(), [0], "a press on a plain body names its shown panel"); + pressed.borrow_mut().clear(); + fixture.click(&plain).idle(1); + assert!(pressed.borrow().is_empty(), "a press on a tab is no body press: {:?}", pressed.borrow()); +} diff --git a/docs/ui-docking.md b/docs/ui-docking.md index 958c0832b..c9dd4314d 100644 --- a/docs/ui-docking.md +++ b/docs/ui-docking.md @@ -82,7 +82,7 @@ Props: `hide` (`HidePanels::None`, `All` or `Except`), `maximized`, `auto_collap A panel in a column collapsed to icons shows in its flyout (`Layout::opens_in_flyout`). The host opens and closes it by writing the `flyout` binding, not the layout: `WindowMenuRows` checks such a panel's row while its flyout is open (`WindowMenuRows::flyout`) and reports a pick in `WindowMenuRowsOutput::flyout`. -`DockAreaOutput` holds the `actions` taken, the bodies' `requested` actions, the `events` for the host, the `focused` and `picked` panels, `edit`, `float_space`, `columns`, the OS `windows` the floating windows showed in (by a panel of each), `window_files` (one `WindowFiles` per drop on a floating OS window: the shown panel of the group under the drop, the first group's for a drop on the title row, and the files), and `error`: an `InvalidGeometry` when the workspace fails `validate`, in which case the area draws nothing. +`DockAreaOutput` holds the `actions` taken, the bodies' `requested` actions, the `events` for the host, the `focused` and `picked` panels, the `pressed` panel (the shown panel of the group whose body the primary press went down in, whatever took it, so a host whose bodies are its own egui code knows the panel clicked into), the gesture `sources` (a panel tab, a document tab, a group bar, window grips, dividers), `edit`, `float_space`, `columns`, the OS `windows` the floating windows showed in (by a panel of each), `window_files` (one `WindowFiles` per drop on a floating OS window: the shown panel of the group under the drop, the first group's for a drop on the title row, and the files), and `error`: an `InvalidGeometry` when the workspace fails `validate`, in which case the area draws nothing. The host handles these `DockEvent`s: