diff --git a/crates/egui/src/containers/popup.rs b/crates/egui/src/containers/popup.rs index ce87d135..3cdc76c4 100644 --- a/crates/egui/src/containers/popup.rs +++ b/crates/egui/src/containers/popup.rs @@ -652,8 +652,24 @@ impl<'a> Popup<'a> { frame.show(ui, content).inner }); - // If the popup was just opened with a click, we don't want to immediately close it again. - let close_click = was_open_last_frame && ctx.input(|i| i.pointer.any_click()); + // Only a click whose press started while the popup was already open may close it. + // Otherwise a popup opened on a pointer *press* (e.g. a right-press context menu) + // would immediately close again on the following *release*. + // This also covers the case where the popup was just opened with a click. + let press_started_while_open_id = id.with("press_started_while_open"); + let (any_pressed, any_click) = + ctx.input(|i| (i.pointer.any_pressed(), i.pointer.any_click())); + let press_started_while_open = if !was_open_last_frame { + ctx.data_mut(|d| d.remove::(press_started_while_open_id)); + false + } else if any_pressed { + ctx.data_mut(|d| d.insert_temp(press_started_while_open_id, true)); + true + } else { + ctx.data(|d| d.get_temp(press_started_while_open_id)) + .unwrap_or(false) + }; + let close_click = press_started_while_open && any_click; let closed_by_click = match close_behavior { PopupCloseBehavior::CloseOnClick => close_click, @@ -675,6 +691,7 @@ impl<'a> Popup<'a> { if should_close { response.response.set_close(); + ctx.data_mut(|d| d.remove::(press_started_while_open_id)); } match open_kind { diff --git a/crates/egui_kittest/tests/popup.rs b/crates/egui_kittest/tests/popup.rs index 9d9dba65..7452ed5a 100644 --- a/crates/egui_kittest/tests/popup.rs +++ b/crates/egui_kittest/tests/popup.rs @@ -1,4 +1,7 @@ -use egui::{Align, Layout, Popup}; +use egui::{ + Align, Layout, Modifiers, PointerButton, Popup, PopupCloseBehavior, Pos2, Sense, + SetOpenCommand, Vec2, +}; use egui_kittest::Harness; use kittest::Queryable as _; @@ -175,3 +178,171 @@ fn test_interactive_tooltip() { assert!(harness.state().link_clicked); } + +fn pointer_button( + harness: &Harness<'_, impl Sized>, + pos: Pos2, + button: PointerButton, + pressed: bool, +) { + harness.event(egui::Event::PointerMoved(pos)); + harness.event(egui::Event::PointerButton { + pos, + button, + pressed, + modifiers: Modifiers::NONE, + }); +} + +/// Press, hold for a few frames, then release, like a real user would. +/// +/// [`kittest::Node::click`] sends press and release in the same frame, +/// which hides bugs where press and release are handled differently. +fn slow_click(harness: &mut Harness<'_, impl Sized>, pos: Pos2, button: PointerButton) { + pointer_button(harness, pos, button, true); + harness.run_steps(3); + pointer_button(harness, pos, button, false); + harness.run(); +} + +fn harness_builder() -> egui_kittest::HarnessBuilder { + // Short frames, so that press-hold-release is still a click: + Harness::builder() + .with_size(Vec2::new(500.0, 300.0)) + .with_step_dt(1.0 / 60.0) +} + +/// A popup opened on a pointer *press* should not close on the following *release*. +/// +/// See +#[test] +fn popup_opened_on_press_stays_open_after_release() { + const TARGET: &str = "Right-press me"; + const POPUP_CONTENT: &str = "Popup content"; + const OUTSIDE: Pos2 = Pos2::new(450.0, 250.0); + + for close_behavior in [ + PopupCloseBehavior::CloseOnClick, + PopupCloseBehavior::CloseOnClickOutside, + ] { + let mut harness = harness_builder().build_ui(|ui| { + let response = ui.add(egui::Label::new(TARGET).sense(Sense::click())); + let pressed = response.hovered() + && ui.input(|i| i.pointer.button_pressed(PointerButton::Secondary)); + Popup::from_response(&response) + .open_memory(pressed.then_some(SetOpenCommand::Bool(true))) + .close_behavior(close_behavior) + .show(|ui| { + ui.label(POPUP_CONTENT); + }); + }); + harness.run(); + + let target = harness.get_by_label(TARGET).rect().center(); + + pointer_button(&harness, target, PointerButton::Secondary, true); + harness.run_steps(3); + assert!( + harness.query_by_label(POPUP_CONTENT).is_some(), + "{close_behavior:?}: popup should open on press" + ); + + pointer_button(&harness, target, PointerButton::Secondary, false); + harness.run(); + assert!( + harness.query_by_label(POPUP_CONTENT).is_some(), + "{close_behavior:?}: popup should stay open after the release that opened it" + ); + + slow_click(&mut harness, OUTSIDE, PointerButton::Primary); + assert!( + harness.query_by_label(POPUP_CONTENT).is_none(), + "{close_behavior:?}: clicking outside should close the popup" + ); + } +} + +/// Clicking an item inside a [`PopupCloseBehavior::CloseOnClick`] popup should register the click +/// *and* close the popup, also when press and release happen in different frames. +#[test] +fn popup_item_click_registers_and_closes() { + const BUTTON: &str = "Open menu"; + const ITEM: &str = "Item"; + + #[derive(Default)] + struct State { + item_clicked: bool, + } + + let mut harness = harness_builder().build_ui_state( + |ui, state: &mut State| { + let response = ui.button(BUTTON); + Popup::menu(&response).show(|ui| { + if ui.button(ITEM).clicked() { + state.item_clicked = true; + } + }); + }, + State::default(), + ); + harness.run(); + + let button = harness.get_by_label(BUTTON).rect().center(); + slow_click(&mut harness, button, PointerButton::Primary); + assert!(harness.query_by_label(ITEM).is_some(), "menu should open"); + + let item = harness.get_by_label(ITEM).rect().center(); + slow_click(&mut harness, item, PointerButton::Primary); + assert!(harness.state().item_clicked, "item click should register"); + assert!( + harness.query_by_label(ITEM).is_none(), + "menu should close after clicking an item" + ); + + // Clicking the menu button should toggle the menu open, then closed: + slow_click(&mut harness, button, PointerButton::Primary); + assert!(harness.query_by_label(ITEM).is_some(), "menu should reopen"); + slow_click(&mut harness, button, PointerButton::Primary); + assert!( + harness.query_by_label(ITEM).is_none(), + "clicking the menu button should close the menu" + ); +} + +/// Starting a drag outside a popup (e.g. on a slider) should not close it, +/// since that is not a click. +#[test] +fn popup_stays_open_when_dragging_outside() { + const BUTTON: &str = "Open popup"; + const POPUP_CONTENT: &str = "Popup content"; + + let mut harness = harness_builder().build_ui_state( + |ui, value: &mut f32| { + ui.add(egui::Slider::new(value, 0.0..=100.0).text("Value")); + let response = ui.button(BUTTON); + Popup::menu(&response).show(|ui| { + ui.label(POPUP_CONTENT); + }); + }, + 0.0, + ); + harness.run(); + + let button = harness.get_by_label(BUTTON).rect().center(); + slow_click(&mut harness, button, PointerButton::Primary); + assert!(harness.query_by_label(POPUP_CONTENT).is_some()); + + let slider = harness.get_by_role(egui::accesskit::Role::Slider).rect(); + harness.drag_at(slider.left_center()); + harness.run_steps(3); + harness.hover_at(slider.center()); + harness.run_steps(3); + harness.drop_at(slider.center()); + harness.run(); + + assert!(*harness.state() > 0.0, "slider should have been dragged"); + assert!( + harness.query_by_label(POPUP_CONTENT).is_some(), + "dragging outside the popup should not close it" + ); +}