mirror of
https://github.com/storytold/egui.git
synced 2026-11-20 17:42:29 +01:00
Fix popups immediately closing on mouse button release (#7624)
<!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/main/CONTRIBUTING.md) before opening a Pull Request! * Keep your PR:s small and focused. * The PR title is what ends up in the changelog, so make it descriptive! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> * [x] I have followed the instructions in the PR template The PR makes it so a popup is closed on a press of a button but not click (i.e. release) of a button - this fixes the issue of a popup immediately closing after releasing a button. I can reproduce the issue with the following code in `bevy_egui`: ```rust let popup = egui::Popup::new( egui::Id::new("A unique popup"), ctx.clone(), egui::PopupAnchor::PointerFixed, egui::LayerId::new(egui::Order::Foreground, egui::Id::new("popup layer")), ); let mut open_command = None; for message in mouse_button_input_reader.read() { if message.state.is_pressed() && message.button == MouseButton::Right { open_command = Some(SetOpenCommand::Bool(true)); } } popup.open_memory(open_command).show(|ui| { ui.label("This is a popup"); }); ``` When a popup is open due to a "press" event, it will get closed on the next "release" event (as they aren't likely to happen at the same frame). One could argue that the intended usage here is to open a popup only on a release of a button, but for users that have it open on a button press the current `egui` behavior could be counterintuitive. I haven't tested this extensively, but if you find that this fix breaks any other behavior or some intended design where we do expect a popup close only on a button release, feel free to close this. --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
1 parent
7a911a1e1c
commit
30639ace4d
2 files changed
+191
-3
No files matched your search
@@ -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::<bool>(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::<bool>(press_started_while_open_id));
|
||||
}
|
||||
|
||||
match open_kind {
|
||||
|
||||
@@ -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<State>() -> egui_kittest::HarnessBuilder<State> {
|
||||
// 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 <https://github.com/emilk/egui/pull/7624>
|
||||
#[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"
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user