diff --git a/crates/ui-contract/src/contract/overlay/opener.rs b/crates/ui-contract/src/contract/overlay/opener.rs index ee4ecf5c2..d34f4ac57 100644 --- a/crates/ui-contract/src/contract/overlay/opener.rs +++ b/crates/ui-contract/src/contract/overlay/opener.rs @@ -17,7 +17,8 @@ pub enum Trigger { /// pop-up buttons and dropdowns do, so a press held past the click time still opens it; the release that /// completes the click is part of that opening. Click, - /// A press on the opener toggles. + /// A primary press on the opener toggles, and a secondary click inside it toggles too, hanging the layer from the + /// opener as a press does (a pop-up menu button). Press, /// A context request (right click, the Menu key, AccessKit `ShowContextMenu`) opens. Context, @@ -75,8 +76,10 @@ impl Opener { let by_pointer = inside && frame.release == Some(PointerButton::Secondary); // A root opener opens on the primary press inside it, as a pop-up button does, however long the press is // then held; a `Click` opener's release completes the click that press began. + let primary = pressed_on && frame.press.is_some_and(|press| press.button == PointerButton::Primary); let fired = match trigger { - Trigger::Click | Trigger::Press => pressed_on && frame.press.is_some_and(|press| press.button == PointerButton::Primary), + Trigger::Click => primary, + Trigger::Press => primary || by_pointer, Trigger::Context => by_pointer || request.is_some(), Trigger::Hover | Trigger::Hold | Trigger::Manual => false, }; @@ -122,6 +125,19 @@ impl Opener { } } +/// Whether a secondary-button release lands inside `response`'s rect in this pass: the secondary click a +/// `Trigger::Press` opener opens on. +pub(super) fn secondary_release_in(response: &Response) -> bool { + pointer::with_pass_pointer(&response.ctx, |pass| { + pass.frame.as_ref().is_some_and(|frame| { + frame.release == Some(PointerButton::Secondary) + && frame.pos.is_some_and(|pos| { + response.rect.contains(areas::layer_map(&response.ctx, response.layer_id).map_or(pos, |map| map.to_layer(pos))) + }) + }) + }) +} + /// How an opener opened its layer: what decides where a `Trigger::Context` layer hangs. #[derive(Clone, Copy, Debug, Default, PartialEq)] pub(crate) enum Opening { diff --git a/crates/ui-contract/src/contract/overlay/popups.rs b/crates/ui-contract/src/contract/overlay/popups.rs index 7418396e5..2d69204e4 100644 --- a/crates/ui-contract/src/contract/overlay/popups.rs +++ b/crates/ui-contract/src/contract/overlay/popups.rs @@ -1,6 +1,6 @@ //! Free-standing popups: their open slots and the pass's registry, handles, the `Overlays` trait and hover dwell. -use super::opener::{Opening, Trigger}; +use super::opener::{Opening, Trigger, secondary_release_in}; use crate::contract::cx::CxCore; use crate::contract::{BindMut, Component, Cx}; use crate::runtime::access::PopupShown; @@ -194,7 +194,10 @@ impl PopupHandle { let resting = core.response.hovered() || core.focus.visible; let toggled = match trigger { Trigger::Click => core.activation.is_some(), - Trigger::Press => matches!(core.press.phase, craft_behavior::PressPhase::Down { .. }), + Trigger::Press => { + matches!(core.press.phase, craft_behavior::PressPhase::Down { start, .. } if start.button == craft_behavior::PointerButton::Primary) + || (core.response.enabled() && secondary_release_in(&core.response)) + } Trigger::Context => core.intents.take(Intent::ContextMenu), Trigger::Hover => self.dwell(&ctx, core.memory, "dwell", resting, crate::contract::tooltip::DELAY), Trigger::Hold => self.dwell(&ctx, core.memory, "dwell", core.press.phase.is_press(), HOLD), diff --git a/crates/ui-widgets/tests/it/menu/openers.rs b/crates/ui-widgets/tests/it/menu/openers.rs index e693d52e7..996537da0 100644 --- a/crates/ui-widgets/tests/it/menu/openers.rs +++ b/crates/ui-widgets/tests/it/menu/openers.rs @@ -1,7 +1,7 @@ //! A menu with several openers: a context menu over a popover. use craft_ui::buttons::Button; -use craft_ui::contract::{Bind, Role, Show as _, ShowContainer as _}; +use craft_ui::contract::{Bind, Role, Show as _, ShowContainer as _, Trigger}; use craft_ui::menu::{Menu, MenuItem}; use craft_ui::popover::Popover; use craft_ui::testing::{Fixture, pointer, trial}; @@ -50,3 +50,50 @@ fn a_menu_opened_from_a_popover_takes_a_click_on_its_row() { assert!(menu_rect(&fixture).is_none(), "the menu closed"); assert!(fixture.by_label("Reset").is_some(), "the popover stays open"); } + +/// Right-clicks at `at`: move, secondary press, secondary release. +fn right_click(fixture: &mut Fixture, at: egui::Pos2) { + fixture.frame(vec![pointer::moved(at)]); + fixture.frame(vec![pointer::secondary(at, true)]); + fixture.frame(vec![pointer::secondary(at, false)]); + fixture.settle(); +} + +/// A secondary click on a press menu's button opens its menu, as a primary press does. +#[trial] +fn a_secondary_click_on_a_press_menu_button_opens_its_menu() { + let mut fixture = Fixture::new(|ui| { + let opener = Button::new("Layers").show(ui); + Menu::new("Layers menu").opened_by(&opener, Trigger::Press).show(ui, |ui, _| { + MenuItem::new("New layer").show(ui); + }); + }); + fixture.settle(); + let at = fixture.by_label("Layers").expect("the button").center(); + right_click(&mut fixture, at); + assert!(fixture.by_label("New layer").is_some(), "the secondary click opened the menu"); + let menu = menu_rect(&fixture).expect("the menu is open"); + let button = fixture.by_label("Layers").expect("the button").rect; + assert!(menu.min.y >= button.max.y - 1.0 || menu.max.y <= button.min.y + 1.0, "the menu hangs from the button, not the pointer"); +} + +/// A right-click elsewhere on the widget while its context menu is open moves the menu to the new point. +#[trial] +fn a_new_right_click_moves_an_open_context_menu_to_the_pointer() { + let mut fixture = Fixture::new(|ui| { + let target = Button::new("A wide target to right-click at either end").show(ui); + Menu::context("Target menu", &target).show(ui, |ui, _| { + MenuItem::new("Copy").show(ui); + MenuItem::new("Paste").show(ui); + }); + }); + fixture.settle(); + let target = fixture.by_label("A wide target to right-click at either end").expect("the target").rect; + let (first, second) = (target.left_center() + egui::vec2(8.0, 0.0), target.right_center() - egui::vec2(8.0, 0.0)); + right_click(&mut fixture, first); + let before = menu_rect(&fixture).expect("the first right-click opens the menu"); + assert!((before.min.x - first.x).abs() < 8.0, "the menu opens at the pointer"); + right_click(&mut fixture, second); + let after = menu_rect(&fixture).expect("the menu is open after the second right-click"); + assert!(after.min.x > before.min.x + 40.0, "the menu moved to the second point: {before:?} then {after:?}"); +}