Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions crates/ui-egui/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<(u64, egui::Rect)>>(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::<Vec<(u64, egui::Rect)>>(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::<bool>(id)).unwrap_or(false)
} else {
ctx.data_mut(|d| d.remove::<bool>(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.
Expand Down
38 changes: 38 additions & 0 deletions crates/ui-egui/src/exprcomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 5 additions & 2 deletions crates/ui-egui/src/ribbon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
jgregoriorincon marked this conversation as resolved.
Resolved
})
.labelled_by(input_label.id);
if ui.button(tl!("Set Filter")).clicked() {
let w = app.forms.expression.trim().to_string();
let _ =
Expand Down
5 changes: 5 additions & 0 deletions crates/ui-egui/src/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
59 changes: 59 additions & 0 deletions crates/ui-egui/tests/menu_checkboxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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})");
}
}
9 changes: 5 additions & 4 deletions docs/user-guide/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading