diff --git a/crates/ui-egui/src/panel_docking.rs b/crates/ui-egui/src/panel_docking.rs index 0fd8936a45..1a47d5a7d2 100644 --- a/crates/ui-egui/src/panel_docking.rs +++ b/crates/ui-egui/src/panel_docking.rs @@ -301,6 +301,28 @@ pub(crate) fn column_of<'a>(layout: &'a Layout, panel: &str) -> Option<( Some((edge, column)) } +/// Where the dock last drew the docked column holding `panel`; none until the dock has drawn it, and while `panel` +/// floats or is closed. +pub(crate) fn column_rect(app: &VectorcraftApp, panel: &str) -> Option { + let (Some(_), column) = column_of(layout_of(&app.ui), panel)? else { return None }; + app.dock_rt.columns.iter().find(|(first, _)| column.panels().any(|p| p == first)).map(|(_, rect)| *rect) +} + +/// The top left a new floating window of `panel` takes beside the docked column holding `beside`: level with the +/// column's top, the cascade inset away from it on the side toward the middle of the float space. None until the dock +/// has drawn that column. +pub(crate) fn float_origin_beside(app: &VectorcraftApp, panel: &str, beside: &str) -> Option { + let column = column_rect(app, beside)?; + let space = command_space(app); + let x = if column.center().x <= space.area.center().x { + column.right() + space.inset + } else { + let width = space.size_of(&DragUnit::Panel(panel.to_owned()), None).x; + column.left() - space.inset - width + }; + Some(egui::pos2(x, column.top())) +} + /// Is the dock's outer column of panels (Properties · Layers · Libraries) collapsed to icons? pub(crate) fn dock_collapsed(ui: &UiState) -> bool { dock_columns(layout_of(ui)).first().is_some_and(|c| c.iconic) diff --git a/crates/ui-egui/src/toolbar.rs b/crates/ui-egui/src/toolbar.rs index 34a7829ba9..49da6d5a44 100644 --- a/crates/ui-egui/src/toolbar.rs +++ b/crates/ui-egui/src/toolbar.rs @@ -294,11 +294,17 @@ pub(crate) fn is_floating(app: &VectorcraftApp, toolbar: Option<&str>, key: &str strips(app, toolbar).iter().any(|g| g.first() == Some(&key)) } -/// Float the group `first` of `owner` as a strip at `at` (none: the canvas's corner). +/// Float the group `first` of `owner` as a strip at `at`. Without `at` the strip opens at the canvas's corner, or, +/// before a view has been laid out, beside `owner`'s docked rail, else beside the Tools panel's. fn place_strip(app: &mut VectorcraftApp, owner: &Owner, first: &str, at: Option) -> Result<(), String> { - let at = at.unwrap_or_else(|| app.canvas_view().map_or(Pos2::ZERO, |v| v.rect.min)); + let id = strip_id(owner, first); + let at = at + .or_else(|| app.canvas_view().map(|v| v.rect.min)) + .or_else(|| crate::panel_docking::float_origin_beside(app, &id, &owner.panel())) + .or_else(|| crate::panel_docking::float_origin_beside(app, &id, TOOLS)) + .unwrap_or(Pos2::ZERO); // A width of 0 leaves the window's width to the panel's grid. - crate::panel_docking::place_floating(app, &strip_id(owner, first), at, 0.0) + crate::panel_docking::place_floating(app, &id, at, 0.0) } /// Float the toolbar group (of the current layout) holding `tool` as a strip of tool buttons, put diff --git a/crates/ui-egui/src/toolbar/tests.rs b/crates/ui-egui/src/toolbar/tests.rs index 91becce66b..2abb2f3cc1 100644 --- a/crates/ui-egui/src/toolbar/tests.rs +++ b/crates/ui-egui/src/toolbar/tests.rs @@ -483,6 +483,30 @@ fn window_float_tools_floats_and_puts_back_a_group() { assert!(strip_rect(&app, "rectangle").is_none() && !is_floating(&app, None, "pen")); } +/// `window.floatTools` run before a view has been laid out (right after a document opens, or with none open) floats the +/// strip beside the docked Tools rail, not over it, and the strip stays clear of the rail once the frames are drawn. +#[test] +fn a_strip_floated_before_a_view_is_laid_out_opens_beside_the_tools_rail() { + for (case, document) in [("document just opened", true), ("no document", false)] { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + let ctx = crate::testing::ui_context(); + settled(&mut app, &ctx, 0.0, 900.0); + if document { + app.run("file.new", json!({"width": 300, "height": 300})).unwrap(); + } + assert!(app.canvas_view().is_none(), "{case}: no view has been laid out yet"); + let rail = crate::panel_docking::column_rect(&app, TOOLS).unwrap_or_else(|| panic!("{case}: the Tools rail is docked")); + app.run("window.floatTools", json!({"tool": "pen"})).unwrap(); + let strip = strip_rect(&app, "pen").unwrap_or_else(|| panic!("{case}: the Pen strip floats")); + assert!(strip.left() >= rail.right(), "{case}: the strip {strip:?} opens right of the rail {rail:?}"); + assert!((strip.top() - rail.top()).abs() <= f32::EPSILON, "{case}: the strip {strip:?} is level with the rail {rail:?}"); + settled(&mut app, &ctx, 1.0, 900.0); + let rail = crate::panel_docking::column_rect(&app, TOOLS).unwrap_or_else(|| panic!("{case}: the Tools rail stays docked")); + let strip = strip_rect(&app, "pen").unwrap_or_else(|| panic!("{case}: the Pen strip still floats")); + assert!(!strip.intersects(rail), "{case}: the strip {strip:?} is clear of the rail {rail:?}"); + } +} + /// A floated group is a panel of the workspace's layout: a saved workspace brings it back, the built-in ones have none. #[test] fn workspaces_keep_floating_flyouts_and_reset_puts_them_back() {