diff --git a/crates/ui-egui/src/completion.rs b/crates/ui-egui/src/completion.rs index 6241bf6c6e..a5c6fbb858 100644 --- a/crates/ui-egui/src/completion.rs +++ b/crates/ui-egui/src/completion.rs @@ -86,9 +86,57 @@ pub(crate) fn popup(ui: &Ui, id: Id, corner: egui::Pos2, room: f32, content: imp .inner }); let rect = area.response.rect; + // A box inside a menu sits on a foreground layer too, and a click in it brings the menu to + // the top, over the popup. As the menu's sublayer, the popup stays drawn right above it + // (egui clears sublayers each pass, so this is set on every one); and where it's drawn is + // kept for the menu, which would take a click on it past its own edge as one outside it. + let parent = ui.layer_id(); + if parent.order == egui::Order::Foreground { + ui.ctx().set_sublayer(parent, area.response.layer_id); + let pass = ui.ctx().cumulative_pass_nr(); + ui.ctx().data_mut(|d| { + let drawn = d.get_temp_mut_or_default::>(over_menu_id()); + drawn.retain(|(at, _)| at + 1 >= pass); + drawn.push((pass, rect)); + }); + } (rect, rect.size() + egui::vec2(0.0, area.inner)) } +fn over_menu_id() -> Id { + Id::unique("completion_over_menu") +} + +/// Whether the click under way (held, or let go this pass) was pressed on a popup drawn over a +/// menu (a menu's box's list, or its signature): that click is the popup's, not one outside the +/// menu, though the list, taking its item on the press, is gone where it's let go. The popup +/// drawn the last pass counts, as the menu checks its clicks before its box draws it again. +pub(crate) fn pressed_over_menu(ctx: &egui::Context) -> bool { + let id = over_menu_id().with("pressed"); + // Where the button went down this pass, read from the pass's events: a press let go in the + // same pass (a quick tap, a slow frame) has no `press_origin` left by then. + let (pressed, held) = ctx.input(|i| { + let pressed = i.events.iter().find_map(|e| match e { + egui::Event::PointerButton { pos, pressed: true, .. } => Some(*pos), + _ => None, + }); + (pressed, i.pointer.any_down() || i.pointer.any_released()) + }); + if let Some(pos) = pressed { + let pass = ctx.cumulative_pass_nr(); + let on = ctx + .data(|d| d.get_temp::>(over_menu_id())) + .is_some_and(|drawn| drawn.iter().any(|(at, r)| at + 1 >= pass && r.contains(pos))); + ctx.data_mut(|d| d.insert_temp(id, on)); + on + } else if held { + ctx.data(|d| d.get_temp::(id)).unwrap_or(false) + } else { + ctx.data_mut(|d| d.remove::(id)); + false + } +} + /// How a list and its help sit in a screen `width` wide: the help beside the list in the room the /// screen leaves, or under it when that's too narrow to read (a narrow window), so neither is cut /// off at the screen's edge; and the help's width. diff --git a/crates/ui-egui/src/exprcomplete.rs b/crates/ui-egui/src/exprcomplete.rs index 0264dae79a..c72287c951 100644 --- a/crates/ui-egui/src/exprcomplete.rs +++ b/crates/ui-egui/src/exprcomplete.rs @@ -1057,6 +1057,44 @@ mod tests { assert_eq!(h.state(), "round(1)"); } + /// A box inside a menu (the ribbon's Display Filters): a click in the box brings the menu, + /// a foreground area as the list is, to the top. The list stays drawn over it, and a click + /// there still reaches the list. + #[test] + fn the_list_stays_over_a_menu_its_box_is_in() { + use egui_kittest::kittest::Queryable; + let id = Id::unique("expr"); + let menu = Id::unique("menu"); + let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(600.0, 400.0)).build_ui_state( + move |ui, text: &mut String| { + egui::Area::new(menu).order(egui::Order::Foreground).fixed_pos(egui::pos2(20.0, 20.0)).show(ui.ctx(), |ui| { + egui::Frame::popup(ui.style()).show(ui, |ui| { + ui.set_min_size(egui::vec2(400.0, 250.0)); + let label = ui.label("Filter"); + let _ = expr_edit(ui, id, text, Some(&ds()), |e| e).labelled_by(label.id); + }); + }); + }, + String::new(), + ); + h.run(); + h.ctx.memory_mut(|m| m.request_focus(id)); + h.run(); + h.event(egui::Event::Text("po".into())); + h.run(); + let at = h.ctx.read_response(id).expect("the box").rect.center(); + h.event(egui::Event::PointerMoved(at)); + h.event(button(at, true)); + h.step(); + h.event(button(at, false)); + h.run(); + let item = h.get_by_role_and_label(egui::accesskit::Role::Button, "pop").rect().center(); + let menu_layer = egui::LayerId::new(egui::Order::Foreground, menu); + assert_ne!(h.ctx.layer_id_at(item), Some(menu_layer), "the list is drawn over the menu"); + click(&mut h, "pop"); + assert_eq!(h.state(), "pop"); + } + /// A click whose press and release come in one frame (a quick tap, a slow frame) reaches the /// item too, and the box keeps the focus. (kittest's `click()` gives each its own frame.) #[test] diff --git a/crates/ui-egui/src/ribbon.rs b/crates/ui-egui/src/ribbon.rs index 37ad606c78..6ca0fd42b7 100644 --- a/crates/ui-egui/src/ribbon.rs +++ b/crates/ui-egui/src/ribbon.rs @@ -4325,8 +4325,11 @@ fn effects_groups(app: &mut RustGisApp, ui: &mut Ui, layer: &rustgis_engine::map small_form_menu(ui, app, "filter", &label, "Show different features at different scales.", |ui, app| { let input_label = ui.label(egui::RichText::new(tl!("Below 1:5,000,000 show everything; beyond it only what matches:")).small().weak()); - ui.add(egui::TextEdit::singleline(&mut app.forms.expression).hint_text("pop_max > 1000000").desired_width(220.0)) - .labelled_by(input_label.id); + let id = egui::Id::unique("display_filters_where"); + crate::exprcomplete::expr_edit(ui, id, &mut app.forms.expression, Some(&layer.dataset), |e| { + e.hint_text("pop_max > 1000000").desired_width(220.0) + }) + .labelled_by(input_label.id); if ui.button(tl!("Set Filter")).clicked() { let w = app.forms.expression.trim().to_string(); let _ = diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 4e455886fb..c524b57db1 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -588,6 +588,11 @@ fn small_menu_with( ui.painter().text(pos2(r.min.x + 26.0, r.center().y), Align2::LEFT_CENTER, label, regular(11.5), t.text); caret(ui.painter(), pos2(r.max.x - 9.0, r.center().y), t.text); let resp = tooltip(app, resp, label, desc, "ui.menu", &Value::Null); + // A click on the suggestion list of a box in the menu (Display Filters) isn't one outside it, + // even past the menu's edge. + let on_list = crate::completion::pressed_over_menu(ui.ctx()); + let close = + if on_list && close == egui::PopupCloseBehavior::CloseOnClickOutside { egui::PopupCloseBehavior::IgnoreClicks } else { close }; egui::Popup::menu(&resp).close_behavior(close).show(|ui| { ui.set_min_width(MENU_MIN_W); menu(ui, app); diff --git a/crates/ui-egui/tests/menu_checkboxes.rs b/crates/ui-egui/tests/menu_checkboxes.rs index c1cdddf0a0..9159db9eda 100644 --- a/crates/ui-egui/tests/menu_checkboxes.rs +++ b/crates/ui-egui/tests/menu_checkboxes.rs @@ -186,3 +186,62 @@ fn celestial_body_choices_dismiss_the_basemap_gallery() { assert!(!egui::Popup::is_any_open(&h.ctx), "{} must dismiss the gallery", body.name); } } + +/// The primary button `pressed`, or let go, at `pos`. +fn button(pos: egui::Pos2, pressed: bool) -> egui::Event { + egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: Default::default() } +} + +/// A click on the suggestion list of Display Filters' box writes the item and keeps the menu open, +/// on a row past the menu's bottom edge too, where it's no longer over the menu: whether the +/// button is let go frames after the press, as a click usually is, or in the same frame (a quick +/// tap, a slow frame). A click elsewhere still closes the menu, either way (#1240). +#[test] +fn display_filters_keeps_its_menu_open_on_a_click_in_its_list() { + use egui::accesskit::Role; + for held in [true, false] { + let mut app = empty_app(); + let text = r#"{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"pop_est":1},"geometry":{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,0]]]}}]}"#; + let (dataset, _) = read_geojson(text, "Areas").unwrap(); + app.session.map_mut().add_dataset(dataset); + app.run("layer.select", json!({"layer": "Areas"})).unwrap(); + app.ui.tab = "Style".into(); + let mut h = harness(app); + click(&mut h, "Display Filters"); + h.get_by_role_and_label(Role::TextInput, "Below 1:5,000,000 show everything; beyond it only what matches:").click(); + h.run(); + h.event(egui::Event::Text("p".into())); + h.run(); + let menu_bottom = h.get_by_role_and_label(Role::Button, "Clear Filters").rect().max.y; + let row = h.get_by_role_and_label(Role::Button, "perimeter()"); + assert!(row.rect().min.y > menu_bottom + 8.0, "the row is past the menu's edge"); + let at = row.rect().center(); + h.event(egui::Event::PointerMoved(at)); + h.step(); + if held { + h.event(button(at, true)); + h.step(); + h.step(); + h.event(button(at, false)); + } else { + // kittest gives each pointer event a frame of its own: both into one frame by hand. + h.input_mut().events.extend([button(at, true), button(at, false)]); + } + h.run(); + assert!(egui::Popup::is_any_open(&h.ctx), "a click in the list keeps the menu open (held: {held})"); + assert_eq!(h.state().forms.expression, "perimeter()", "held: {held}"); + // A click on the map, outside the menu, still closes it. + let away = egui::pos2(1750.0, 1100.0); + h.event(egui::Event::PointerMoved(away)); + h.step(); + if held { + h.event(button(away, true)); + h.step(); + h.event(button(away, false)); + } else { + h.input_mut().events.extend([button(away, true), button(away, false)]); + } + h.run(); + assert!(!egui::Popup::is_any_open(&h.ctx), "a click outside closes the menu (held: {held})"); + } +} diff --git a/docs/user-guide/expressions.md b/docs/user-guide/expressions.md index b21d521c7e..ddcaa81aba 100644 --- a/docs/user-guide/expressions.md +++ b/docs/user-guide/expressions.md @@ -242,10 +242,11 @@ the cursor leaves that call. The same list, signature and closing brackets help in the other one-line expression boxes: the **By Expression** boxes of a layer's symbol and of a label class, Unique Values' **Expression**, -a report's **Where** and an attribute rule's expression. Apart from the rule's, which **Add** -applies, those boxes apply what's written when they lose focus, and a click in the list keeps the -focus, so a field taken from it isn't applied half written; while the list is open Escape closes -it, not the box. +a report's **Where**, an attribute rule's expression and the ribbon's **Display Filters** box +(Style › Drawing Effects). Apart from the rule's, which **Add** applies, and the display filter's, +which **Set Filter** applies, those boxes apply what's written when they lose focus, and a click +in the list keeps the focus, so a field taken from it isn't applied half written; while the list +is open Escape closes it, not the box nor the Display Filters menu. They help too in the clause boxes of several lines: Select By Attributes' **Where**, the Layer Properties **Query** tab and a label class's SQL query. There the list shows under the cursor's