SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch Create menu: family flyouts open beside their row, on hover (#8267) (#8713)
N3MI-DG06dfbc4000f4
diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rsindex 98e8a811b9..efe9839d93 100644--- a/crates/ui-egui/src/context_menu.rs+++ b/crates/ui-egui/src/context_menu.rs@@ -209,7 +209,7 @@ pub struct MenuState {submenu: Option<usize>,submenu_key: Option<usize>,/// When the pointer came to rest on another row while a flyout was open (it closes the- /// flyout after [`SUBMENU_CLOSE_DELAY`]).+ /// flyout after [`crate::ui_kit::SUBMENU_CLOSE_DELAY`]).submenu_leave: Option<f64>,/// The radial ring is shown (viewport menus).pub radial: bool,@@ -271,9 +271,6 @@ const DIRS: usize = 8;/// Half axes of the ring of radial items.const RX: f32 = crate::ui_kit::RADIAL_RADIUS;const RY: f32 = crate::ui_kit::RADIAL_RADIUS;-/// Seconds the pointer rests on another row before an open flyout closes: a path to the flyout-/// that crosses a neighbouring row keeps it open.-const SUBMENU_CLOSE_DELAY: f64 = 0.3;// ---------------------------------------------------------------------------------------------// What the menus offer@@ -2184,7 +2181,7 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {// Appearance keeps its direct editor action as well as hover shortcuts.if item.enabled && row.clicked() && item.id == "ui.appearance" {picked = Some(item.clone());- } else if item.enabled && ((row.hovered() && ctx.input(|i| i.pointer.delta() != Vec2::ZERO)) || row.clicked()) {+ } else if item.enabled && (crate::ui_kit::Menu::flyout_hover(&row) || row.clicked()) {if app.menu.submenu != Some(k) {app.menu.submenu_key = None;}@@ -2204,16 +2201,10 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {// (#6657). A pointer that is still (the flyout opened with the keys) leaves it open.let moved = ctx.input(|i| i.pointer.delta() != Vec2::ZERO);if app.menu.submenu.is_some() && other_row && (moved || app.menu.submenu_leave.is_some()) {- let now = ctx.input(|i| i.time);- let since = *app.menu.submenu_leave.get_or_insert(now);- let left = SUBMENU_CLOSE_DELAY - (now - since);- if left <= 0.0 || !left.is_finite() {+ if crate::ui_kit::Menu::flyout_leaving(ctx, &mut app.menu.submenu_leave) {app.menu.submenu = None;app.menu.submenu_key = None;- app.menu.submenu_leave = None;submenu_row = None;- } else {- ctx.request_repaint_after(std::time::Duration::from_secs_f64(left));}} else {app.menu.submenu_leave = None;diff --git a/crates/ui-egui/src/ui_kit/menu.rs b/crates/ui-egui/src/ui_kit/menu.rsindex c3a24e8ff0..d7479e1068 100644--- a/crates/ui-egui/src/ui_kit/menu.rs+++ b/crates/ui-egui/src/ui_kit/menu.rs@@ -5,6 +5,10 @@ use egui::{Color32, FontId, Rect, Response, Sense, Stroke, Ui, pos2, vec2};pub(crate) struct Menu;+/// Seconds the pointer rests on another row before an open flyout closes: a path to the flyout+/// that crosses a neighbouring row keeps it open.+pub(crate) const SUBMENU_CLOSE_DELAY: f64 = 0.3;+impl Menu {pub(crate) fn context<R>(response: &Response, add: impl FnOnce(&mut Ui) -> R) -> Option<egui::InnerResponse<R>> {Container::new(&response.ctx, Surface::Popup).context_menu(response).frame(Self::frame()).show(add)@@ -25,11 +29,21 @@ impl Menu {Self::button_with(ui, |ui, _| super::text_button(ui, label, true), add)}+ /// A row whose popup opens beside it, on the left only when the right has no room (#8267).pub(crate) fn item_button<R>(ui: &mut Ui, item: &MenuItem<'_>, add: impl FnOnce(&mut Ui) -> R) -> egui::InnerResponse<Option<R>> {- Self::button_with(ui, |ui, _| item.show(ui), add)+ Self::button_at(ui, |ui, _| item.show(ui), add, true)}fn button_with<R>(ui: &mut Ui, button: impl FnOnce(&mut Ui, bool) -> Response, add: impl FnOnce(&mut Ui) -> R) -> egui::InnerResponse<Option<R>> {+ Self::button_at(ui, button, add, false)+ }++ fn button_at<R>(+ ui: &mut Ui,+ button: impl FnOnce(&mut Ui, bool) -> Response,+ add: impl FnOnce(&mut Ui) -> R,+ beside: bool,+ ) -> egui::InnerResponse<Option<R>> {let previous = ui.style().clone();let frame = Self::frame();let style = ui.style_mut();@@ -68,6 +82,13 @@ impl Menu {submenu};submenu.show(ui, &response, content)+ } else if beside {+ let open = Self::flyout_open(ui, &response, popup_id);+ let popup = Container::new(ui.ctx(), Surface::Popup).menu(&response).open(open).frame(frame);+ // Last pass's size; the first pass assumes the flyout fits on the right.+ let size = popup.get_expected_size().unwrap_or(egui::Vec2::ZERO);+ let at = Self::flyout_at(ui.ctx().content_rect(), response.interact_rect, size);+ popup.anchor(at).align(egui::RectAlign::BOTTOM_START).align_alternatives(&[]).gap(0.0).show(content)} else {Container::new(ui.ctx(), Surface::Popup).menu(&response).frame(frame).show(content)};@@ -76,6 +97,78 @@ impl Menu {response}+ /// Hovering a row opens its flyout, like the context menu's. The open flyout stays while the+ /// pointer is on its row or inside it, and closes after resting elsewhere in the parent menu.+ fn flyout_open(ui: &Ui, row: &Response, popup_id: egui::Id) -> bool {+ // (open flyout, when the pointer left it, the pass that last showed it, the pointer has+ // moved since the parent menu opened)+ type State = (Option<egui::Id>, Option<f64>, u64, bool);+ let key = ui.id().with("sc_flyout");+ let ctx = ui.ctx();+ let pass = ctx.cumulative_pass_nr();+ let (mut open, mut leave, seen, mut moved) = ctx.data(|d| d.get_temp::<State>(key)).unwrap_or((None, None, 0, false));+ // A parent menu that closed and opened again starts with no flyout.+ if seen + 1 < pass {+ (open, leave, moved) = (None, None, false);+ }+ // Only a menu that opened under a still pointer waits for a move; after that, any row the+ // pointer rests on shows its flyout, including once another flyout has closed.+ moved |= Self::pointer_moved(ctx);+ if open.is_none() && ((moved && row.hovered()) || row.clicked()) {+ open = Some(popup_id);+ }+ if open == Some(popup_id) {+ let pointer = ctx.pointer_hover_pos();+ let inside = pointer.is_some_and(|p| row.rect.contains(p) || ctx.read_response(popup_id).is_some_and(|r| r.rect.contains(p)));+ let elsewhere = pointer.is_some_and(|p| ui.clip_rect().contains(p)) && !inside;+ if !row.clicked() && elsewhere && (Self::pointer_moved(ctx) || leave.is_some()) {+ if Self::flyout_leaving(ctx, &mut leave) {+ open = None;+ // The row under the pointer may come earlier in this pass; it opens on the next.+ ctx.request_repaint();+ }+ } else {+ leave = None;+ }+ }+ ctx.data_mut(|d| d.insert_temp(key, (open, leave, pass, moved)));+ open == Some(popup_id)+ }++ fn pointer_moved(ctx: &egui::Context) -> bool {+ ctx.input(|i| i.pointer.delta() != egui::Vec2::ZERO)+ }++ /// A row opens its flyout when the pointer moves onto it, so a menu that opens under a still+ /// pointer (or is driven with the keys) opens none.+ pub(crate) fn flyout_hover(row: &Response) -> bool {+ row.hovered() && Self::pointer_moved(&row.ctx)+ }++ /// The pointer rests on another row of the parent menu since `leave`: true once the open+ /// flyout should close after [`SUBMENU_CLOSE_DELAY`] (#6657).+ pub(crate) fn flyout_leaving(ctx: &egui::Context, leave: &mut Option<f64>) -> bool {+ let now = ctx.input(|i| i.time);+ let left = SUBMENU_CLOSE_DELAY - (now - *leave.get_or_insert(now));+ if left <= 0.0 || !left.is_finite() {+ *leave = None;+ return true;+ }+ ctx.request_repaint_after(std::time::Duration::from_secs_f64(left));+ false+ }++ /// Where a flyout of `size` sits beside its `row`: on the right with its first row level with+ /// `row`, on the left only when the right has no room (#8267), and kept on screen.+ pub(crate) fn flyout_at(screen: Rect, row: Rect, size: egui::Vec2) -> egui::Pos2 {+ let margin = Self::frame().total_margin();+ let gap = margin.right + 2.0;+ let right = row.right() + gap;+ let x = if right + size.x <= screen.right() { right } else { (row.left() - gap - size.x).max(screen.left()) };+ let y = (row.top() - margin.top).min(screen.bottom() - size.y).max(screen.top());+ pos2(x, y)+ }+pub(crate) fn frame() -> egui::Frame {super::Container::menu_frame()}@@ -131,12 +224,10 @@ impl Menu {enter && selected.is_some_and(|k| enabled.get(k).copied().unwrap_or(false))}- /// Place a submenu beside its row, flipping at the screen edge.+ /// Place a submenu beside its row with [`Self::flyout_at`].pub(crate) fn submenu<R>(ctx: &egui::Context, id: egui::Id, row: Rect, height: f32, add: impl FnOnce(&mut Ui) -> R) -> egui::InnerResponse<R> {- let screen = ctx.content_rect();let width = crate::context_menu::LIST_W;- let x = if row.right() + width <= screen.right() { row.right() } else { (row.left() - width).max(screen.left()) };- let at = pos2(x, row.top().min((screen.bottom() - height).max(screen.top())));+ let at = Self::flyout_at(ctx.content_rect(), row, vec2(width, height));Container::new(ctx, Surface::Popup).area(id).fixed_pos(at).order(egui::Order::Foreground).show(ctx, |ui| Self::show(ui, width, add))}}@@ -431,6 +522,24 @@ mod tests {assert_eq!(frame(key(egui::Key::Enter)), (Some(2), true));}+ #[test]+ fn flyouts_open_right_of_their_row_and_flip_left_only_without_room() {+ let screen = Rect::from_min_size(pos2(0.0, 0.0), vec2(1000.0, 600.0));+ let size = vec2(200.0, 100.0);+ let top = Menu::frame().total_margin().top;+ let row = Rect::from_min_max(pos2(100.0, 50.0), pos2(400.0, 80.0));+ let at = Menu::flyout_at(screen, row, size);+ assert!(at.x > row.right() && at.x + size.x <= screen.right(), "{at:?}");+ assert_eq!(at.y, row.top() - top);+ // No room on the right: the flyout sits left of the row, still clear of it.+ let edge = row.translate(vec2(500.0, 0.0));+ let at = Menu::flyout_at(screen, edge, size);+ assert!(at.x + size.x < edge.left() && at.x >= screen.left(), "{at:?}");+ // A row near the bottom keeps its flyout on screen.+ let low = row.translate(vec2(0.0, 500.0));+ assert_eq!(Menu::flyout_at(screen, low, size).y, screen.bottom() - size.y);+ }+#[test]fn disabled_menu_rows_are_inert_and_live_rows_accept_clicks() {for enabled in [false, true] {