diff --git a/crates/ui-widgets/src/buttons/split_button.rs b/crates/ui-widgets/src/buttons/split_button.rs index 59f212c0b..0e88ffb3a 100644 --- a/crates/ui-widgets/src/buttons/split_button.rs +++ b/crates/ui-widgets/src/buttons/split_button.rs @@ -2,7 +2,7 @@ use super::face::{self, Arrangement, FaceLook, FaceSpec}; use crate::contract::{ - Access, BodyCx, Common, Component, Cx, Entry, Focus as _, Hover, MeasureCx, Name, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx, + Access, Bind, BodyCx, Common, Component, Cx, Entry, Focus as _, Hover, MeasureCx, Name, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx, Pointer as _, Props, Role, ShowContainer as _, Styled as _, WordSetCx as _, }; use crate::menu::{self, Menu}; @@ -231,16 +231,24 @@ impl Component for SplitButton<'_, T> { /// The menu: one `MenuItem` row per entry, a rule for a separator, or the caller's body. Running an entry's row /// reports its value and closes the menu; a disabled entry takes neither focus-run nor a pick. A body's rows run - /// their own commands, which close the layer as any row's run does. + /// their own commands, which close the layer as any row's run does; the body's `request_close` (a palette's pick) + /// closes the layer too. fn overlay(&mut self, ui: &mut OverlayUi<'_>, cx: &mut Cx<'_, Self>) { let popup = cx.popup("menu"); let name = cx.label(); let entries = match &mut self.menu { SplitMenu::Entries(entries) => *entries, SplitMenu::Body(body) => { - ui.show(popup, menu::layer(false), |ui, _| { - let _ = Menu::inline(name.clone()).show(ui, |ui, cx| body(ui, cx)); + // The body's `request_close` (a swatch pick) and a row's run close the inline menu; it is open while + // the popup is, so its close is the popup's. Its own open slot would stay closed and leave the layer + // open around an empty body. Its focus scope ignores Escape, so the binding takes no Escape from the + // layer's own rule. + let closed = ui.show(popup, menu::layer(false), |ui, _| { + Menu::inline(name.clone()).open(Bind::given(true)).show(ui, |ui, cx| body(ui, cx)).output.closed }); + if closed.flatten().is_some() { + popup.close(cx); + } return; } }; diff --git a/crates/ui-widgets/tests/it/buttons.rs b/crates/ui-widgets/tests/it/buttons.rs index 3f5b256aa..8ac469537 100644 --- a/crates/ui-widgets/tests/it/buttons.rs +++ b/crates/ui-widgets/tests/it/buttons.rs @@ -540,3 +540,36 @@ fn a_side_menus_selected_row_shows_its_hover_band() { assert_ne!(on.rest, fill.off.rest, "the shown page's row is marked"); assert!(plain.expect("a ghost button has a surface").fill.on.is_none(), "a selected ghost button elsewhere keeps its look"); } + +/// A split button's body that closes itself (a colour palette's swatch pick) closes the split's menu layer: before, +/// the close reached only the inline menu inside the layer, which stayed open around an empty body. Opened again, the +/// body shows its rows. +#[trial] +fn a_split_bodys_request_close_closes_the_menu_layer_and_it_opens_again_with_its_body() { + let picked = Rc::new(Cell::new(0)); + let count = Rc::clone(&picked); + let mut fixture = Fixture::new(move |ui| { + let count = Rc::clone(&count); + let _ = SplitButton::::body("Shading", move |ui, cx| { + if craft_ui::contract::clicked(&Button::new("Red").show(ui).response) { + count.set(count.get() + 1); + cx.request_close(); + } + }) + .arrow_label("Shading colours") + .show(ui); + }); + fixture.settle(); + let open_menu = |fixture: &mut Fixture| { + let arrow = fixture.by_label("Shading colours").expect("the arrow part"); + fixture.click(&arrow).settle(); + assert!(!fixture.by_role(Role::MENU).is_empty(), "the arrow opened the menu"); + }; + open_menu(&mut fixture); + let swatch = fixture.by_label("Red").expect("the body's swatch is shown"); + fixture.click(&swatch).settle(); + assert_eq!(picked.get(), 1, "the swatch ran once"); + assert!(fixture.by_role(Role::MENU).is_empty(), "the body's close closed the menu layer"); + open_menu(&mut fixture); + assert!(fixture.by_label("Red").is_some(), "opened again, the menu shows its body"); +}