Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

A dragged command window keeps its place (#5357) (#5444)

raykholoa9fd4b1b0503
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 3ae6349d26..b84ba89455 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -2463,9 +2463,6 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
app.viewport.dialog_rect = None;
}
let Some(mut d) = app.dialog.take() else {
- if app.tree.redefine.is_none() && app.params.is_none() {
- crate::frame::reset_dock_offset(ctx);
- }
return;
};
// Edit Text lasts as long as its text: other design changes leave it open.
@@ -2517,9 +2514,10 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
app.dialog = Some(d);
return;
}
- // Dragged by its title band, the dialog keeps that offset from its docked place while it
- // is open (#1335), kept inside the viewport.
- let moved = crate::frame::dock_offset(ctx, title(&d.kind));
+ // Command dialogs share the last dragged offset on this side until the application
+ // exits, kept inside the viewport.
+ let drag_key = side.dialog_offset_key();
+ let moved = crate::frame::dock_offset(ctx, drag_key);
let docked = egui::pos2(side.anchor_x(vp), top);
let drag_bounds = crate::shell::tool_drag_rect(app, vp);
let anchor = crate::frame::offset_anchor(docked, moved, drag_bounds, side);
@@ -2601,14 +2599,7 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) {
let icon = spec.as_ref().map_or("", |s| s.icon);
let label = spec.as_ref().map_or(heading.as_str(), |s| tr(s.label));
let label = if d.editing.is_some() { heading.as_str() } else { label };
- let heading = crate::ui_kit::FeatureOptions {
- icon,
- title: label,
- drag_key: title(&d.kind),
- moved,
- docked,
- apply_label: repeatable.then_some("Apply"),
- };
+ let heading = crate::ui_kit::FeatureOptions { icon, title: label, drag_key, moved, docked, apply_label: repeatable.then_some("Apply") };
let applying = app.applying.is_some();
let mut collapsed = d.collapsed;
let manager = matches!(
diff --git a/crates/ui-egui/src/frame.rs b/crates/ui-egui/src/frame.rs
index 644521b85f..3035271a5a 100644
--- a/crates/ui-egui/src/frame.rs
+++ b/crates/ui-egui/src/frame.rs
@@ -14,6 +14,14 @@ pub enum Side {
}
impl Side {
+ /// Command dialogs share a position on each side of the viewport.
+ pub fn dialog_offset_key(self) -> &'static str {
+ match self {
+ Side::Right => "sc_command_dialog_right",
+ Side::Left => "sc_command_dialog_left",
+ }
+ }
+
/// The x of the viewport edge this side docks against.
pub fn anchor_x(self, viewport: egui::Rect) -> f32 {
match self {
@@ -23,18 +31,9 @@ impl Side {
}
}
-/// How far the open docked dialog titled `title` was dragged from its docked place (#1335);
-/// zero for a dialog that was not dragged, or once another dialog opens.
-pub fn dock_offset(ctx: &egui::Context, title: &str) -> egui::Vec2 {
- ctx.data(|d| d.get_temp::<(String, egui::Vec2)>(egui::Id::new("sc_dock_offset")))
- .filter(|(t, _)| t == title)
- .map(|(_, v)| v)
- .unwrap_or(egui::Vec2::ZERO)
-}
-
-/// Forget a dragged dialog's place (it closed): the next one opens docked.
-pub fn reset_dock_offset(ctx: &egui::Context) {
- ctx.data_mut(|d| d.remove::<(String, egui::Vec2)>(egui::Id::new("sc_dock_offset")));
+/// How far a docked panel with this key was dragged.
+pub fn dock_offset(ctx: &egui::Context, key: &str) -> egui::Vec2 {
+ ctx.data(|d| d.get_temp::<egui::Vec2>(egui::Id::new(("sc_dock_offset", key)))).unwrap_or(egui::Vec2::ZERO)
}
/// A title drag owns the pointer even when it outruns the dialog's previous position.
@@ -61,7 +60,7 @@ pub fn offset_anchor(docked: Pos2, offset: egui::Vec2, vp: egui::Rect, side: Sid
/// Let `band` (a docked dialog's title band) drag the dialog: the drag adds to its offset
/// `moved` (as last frame left it; `applied` of it is where the dialog is now, after keeping
/// it on screen). Called before the band's buttons are added, so they keep their clicks.
-pub fn drag_dock(ui: &Ui, band: egui::Rect, title: &str, moved: egui::Vec2, applied: egui::Vec2) {
+pub fn drag_dock(ui: &Ui, band: egui::Rect, key: &str, moved: egui::Vec2, applied: egui::Vec2) {
crate::scenario::publish_handle("dialog:title", band.center());
let response = ui.interact(band, ui.id().with("sc_dock_drag"), egui::Sense::drag());
if response.dragged() {
@@ -83,12 +82,12 @@ pub fn drag_dock(ui: &Ui, band: egui::Rect, title: &str, moved: egui::Vec2, appl
(Some((start, from)), Some(now)) => start + (now - from),
_ => applied + response.drag_delta(),
};
- ui.ctx().data_mut(|d| d.insert_temp(egui::Id::new("sc_dock_offset"), (title.to_string(), to)));
+ ui.ctx().data_mut(|d| d.insert_temp(egui::Id::new(("sc_dock_offset", key)), to));
} else if response.hovered() {
ui.ctx().set_cursor_icon(egui::CursorIcon::Grab);
} else if moved != applied {
// Kept on screen: remember where it is, not where it was dragged off to.
- ui.ctx().data_mut(|d| d.insert_temp(egui::Id::new("sc_dock_offset"), (title.to_string(), applied)));
+ ui.ctx().data_mut(|d| d.insert_temp(egui::Id::new(("sc_dock_offset", key)), applied));
}
}
@@ -206,7 +205,7 @@ mod tests {
}
/// #1335: a dialog keeps its size while its Select chip is hovered, and its title band
- /// drags it; it opens docked again after it closes.
+ /// drags it; reopening keeps its position.
#[test]
fn a_docked_dialog_keeps_its_size_under_the_pointer_and_moves_by_its_title() {
for mode in [crate::shell::LayoutMode::Floating, crate::shell::LayoutMode::Docked] {
@@ -236,12 +235,50 @@ mod tests {
h.frames(4);
let viewport = h.app.viewport.rect.unwrap();
assert!(viewport.contains(h.app.viewport.dialog_rect.unwrap().min));
+ let last = h.app.viewport.dialog_rect.unwrap();
h.step(&json!({"key":"Escape"})).unwrap();
h.frames(2);
assert!(h.app.dialog.is_none());
h.step(&json!({"start":"sketch.create"})).unwrap();
h.frames(6);
- assert!((h.app.viewport.dialog_rect.unwrap().min - rest.min).length() < 0.01, "a new dialog opens docked");
+ assert!((h.app.viewport.dialog_rect.unwrap().min - last.min).length() < 0.01, "reopening keeps the dragged position");
+ }
+ }
+ #[test]
+ fn command_dialogs_keep_the_dragged_position_after_closing() {
+ use crate::scenario::Harness;
+ use serde_json::json;
+ for mode in [crate::shell::LayoutMode::Floating, crate::shell::LayoutMode::Docked] {
+ let mut h = Harness::new();
+ h.call("ui.set", json!({"layout":mode}));
+ h.call("ui.resize", json!({"width":1400.0,"height":900.0}));
+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
+ h.app.run("sketch.rectangle.two_point", json!({"p0":[0,0],"p1":[10,10]})).unwrap();
+ h.app.run("sketch.finish", json!({})).unwrap();
+ h.app.run("solid.extrude", json!({"distance":10})).unwrap();
+ h.step(&json!({"start":"solid.extrude"})).unwrap();
+ h.frames(6);
+ let rest = h.app.viewport.dialog_rect.unwrap();
+ let title = crate::scenario::handle_at("dialog:title").unwrap();
+ h.call("ui.drag", json!({"x0":title.x,"y0":title.y,"x1":title.x-300.0,"y1":title.y+60.0,"steps":8}));
+ h.frames(4);
+ let moved = h.app.viewport.dialog_rect.unwrap();
+ assert!((moved.min - rest.min - egui::vec2(-300.0, 60.0)).length() < 2.0);
+ h.step(&json!({"key":"Escape"})).unwrap();
+ h.frames(4);
+ assert!(h.app.dialog.is_none());
+ h.step(&json!({"start":"solid.fillet"})).unwrap();
+ h.frames(6);
+ let fillet = h.app.viewport.dialog_rect.unwrap();
+ assert!((fillet.right_top() - moved.right_top()).length() < 2.0, "Extrude: {moved:?}, Fillet: {fillet:?}");
+ // A smaller window keeps the kept place on screen.
+ h.step(&json!({"key":"Escape"})).unwrap();
+ h.frames(4);
+ h.call("ui.resize", json!({"width":700.0,"height":500.0}));
+ h.step(&json!({"start":"solid.fillet"})).unwrap();
+ h.frames(6);
+ let viewport = h.app.viewport.rect.unwrap();
+ assert!(viewport.contains(h.app.viewport.dialog_rect.unwrap().min), "{viewport:?}");
}
}
}