From 10201e1de7b476d049d9518439c560ca51248eea Mon Sep 17 00:00:00 2001 From: mavrick Date: Wed, 2 Dec 2026 19:06:01 -0600 Subject: [PATCH] fix(ui): a menu that scrolls shows its scroll bar, and a click on it keeps the menu open #3627 made a menu taller than the window scroll, but hid its scroll bar so a click on it wouldn't close the menu: nothing then said the menu had more below. The bar is back, beside the rows. Such a menu closes on a click outside it and closes itself on a click pressed on its rows (kept as the press happens: egui forgets it by the release), not while a submenu is open, so the bar scrolls it and nothing else. --- crates/ui-egui/src/widgets.rs | 60 ++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index ac1baf8f47..c3167af229 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -713,10 +713,9 @@ fn small_menu_with( } /// Shows the menu `opener` opens, closed as `close` says. Where the menu was taller than the -/// window has room for under `opener`, its body scrolls, so it opens there with its last rows in -/// reach in a short window or a long language; a menu that fits is laid out as it always was. It -/// scrolls by the wheel, a drag and Tab, without a scroll bar: a click on a bar inside the menu -/// would be a click that closes it. +/// window has room for under `opener`, its body scrolls, with a scroll bar beside its rows, so it +/// opens there with its last rows in reach in a short window or a long language; a menu that fits +/// is laid out as it always was. pub(crate) fn show_menu(opener: &Response, close: egui::PopupCloseBehavior, add: impl FnOnce(&mut Ui) -> R) -> Option { let ctx = opener.ctx.clone(); // Less the menu's frame, above and below, and a gap from the window's edge. @@ -724,14 +723,32 @@ pub(crate) fn show_menu(opener: &Response, close: egui::PopupCloseBehavior, a // The menu's height last frame. let tall = opener.id.with("menu_body_height"); let scrolls = ctx.data(|d| d.get_temp::(tall)).is_some_and(|h| h > room); - let shown = egui::Popup::menu(opener).close_behavior(close).show(|ui| { + // egui closes a menu on any click in it, its scroll bar's included. A menu that scrolls closes + // on a click outside it instead, and closes itself on a click that was pressed on its rows. + let rows_close = scrolls && close == egui::PopupCloseBehavior::CloseOnClick; + let behavior = if rows_close { egui::PopupCloseBehavior::CloseOnClickOutside } else { close }; + // Whether the press of the click under way was on the rows: egui forgets where a press was by + // the time it reports the click. + let pressed_on_rows = opener.id.with("menu_pressed_on_rows"); + let shown = egui::Popup::menu(opener).close_behavior(behavior).show(|ui| { ui.set_min_width(MENU_MIN_W); if scrolls { - let scroll = egui::ScrollArea::vertical() - .max_height(room) - .scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysHidden) - .show(ui, add); + // Beside the rows, not floating over them, so a press on it is told from one on a row. + ui.spacing_mut().scroll = egui::style::ScrollStyle::solid(); + let scroll = egui::ScrollArea::vertical().max_height(room).show(ui, add); ctx.data_mut(|d| d.insert_temp(tall, scroll.content_size.y)); + if rows_close { + let (pressed, clicked, at) = ui.input(|i| (i.pointer.any_pressed(), i.pointer.any_click(), i.pointer.interact_pos())); + if pressed { + let on_rows = at.is_some_and(|p| scroll.inner_rect.contains(p)); + ctx.data_mut(|d| d.insert_temp(pressed_on_rows, on_rows)); + } + // As egui's own close on a click: not while a submenu a row opened is open. + let deepest = egui::containers::menu::MenuState::is_deepest_open_sub_menu(&ctx, egui::Popup::default_response_id(opener)); + if clicked && deepest && ctx.data(|d| d.get_temp::(pressed_on_rows)).unwrap_or(false) { + ui.close(); + } + } return scroll.inner; } let top = ui.cursor().min.y; @@ -740,6 +757,10 @@ pub(crate) fn show_menu(opener: &Response, close: egui::PopupCloseBehavior, a ctx.data_mut(|d| d.insert_temp(tall, height)); inner }); + // A press from before the menu opened, or from its last opening, isn't one on its rows. + if shown.is_none() || ctx.input(|i| i.pointer.any_click()) { + ctx.data_mut(|d| d.remove::(pressed_on_rows)); + } shown.map(|r| r.inner) } @@ -1135,8 +1156,9 @@ pub fn picked_colour(hsva: egui::ecolor::Hsva, before: egui::ecolor::Hsva, old: mod tests { use egui_kittest::kittest::Queryable; - /// A menu taller than the room under its button scrolls, and still closes as a menu does: not as - /// it scrolls, but on a click in a row that doesn't close it itself, and on a click outside. + /// A menu taller than the room under its button scrolls, with a scroll bar, and still closes as a + /// menu does: not as it scrolls or on its scroll bar, but on a click in a row that doesn't close + /// it itself, and on a click outside. #[test] fn a_menu_that_scrolls_closes_as_a_menu_does() { let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(300.0, 300.0)).build_ui(|ui| { @@ -1165,6 +1187,22 @@ mod tests { }); h.run_steps(3); assert!(h.query_by_label("Row 1").is_some_and(|row| row.rect().min.y < top), "it scrolls and stays open"); + // Its scroll bar, beside the rows: a click there keeps it open. + let row = h.get_by_label("Row 4").rect(); + let bar = egui::pos2(row.max.x + 7.0, row.center().y); + h.event(egui::Event::PointerMoved(bar)); + h.run(); + for pressed in [true, false] { + h.event(egui::Event::PointerButton { + pos: bar, + button: egui::PointerButton::Primary, + pressed, + modifiers: egui::Modifiers::NONE, + }); + h.run(); + } + h.run_steps(2); + assert!(h.query_by_label("Row 4").is_some(), "a click on its scroll bar keeps it open"); h.get_by_label("Row 3").click(); h.run_steps(3); assert!(h.query_by_label("Row 3").is_none(), "a click in a row closes it");