From ada0caaf162251f3f80858a9f3a882a705cc4654 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 00:59:32 +0700 Subject: [PATCH 1/2] Docking and drops as production: the area's edits of the pass are kept unless a command changed the workspace in it (the revision read before the pass), so a native window's OS placement is recorded; a float command that moves an OS window moves its recorded placement with it; Dock column n and Dock icon count from the icon column (the iconic column holding the most icon-group panels, production's icon_column), and a column docked out of the icon column opens expanded; a dropped file with no name opens or is placed by its sniffed format (an SVG opens, a raster image is placed) --- crates/ui-egui/src/panel_docking.rs | 43 ++++++++++++++++++++++++----- crates/ui-egui/src/place.rs | 14 ++++++++-- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/panel_docking.rs b/crates/ui-egui/src/panel_docking.rs index 4263cde3b6..5d2cec0b8a 100644 --- a/crates/ui-egui/src/panel_docking.rs +++ b/crates/ui-egui/src/panel_docking.rs @@ -670,6 +670,19 @@ fn mates(layout: &Layout, panel: &str) -> Vec { } } +/// The icon column among `columns`: the iconic column holding the most panels of the icon groups. +fn icon_column(columns: &[Column]) -> Option { + let in_rows = |p: &&String| ICON_PANEL_GROUPS.iter().any(|row| row.contains(&p.as_str())); + columns + .iter() + .enumerate() + .filter(|(_, c)| c.iconic) + .map(|(k, c)| (c.groups.iter().flat_map(|g| &g.panels).filter(in_rows).count(), k)) + .filter(|(count, _)| *count > 0) + .max() + .map(|(_, k)| k) +} + /// The action that moves `unit` to `to`. fn unit_action(unit: &DragUnit, to: Target) -> Action { match unit { @@ -705,7 +718,7 @@ impl Legacy { Self::Icons => { let columns = edge_columns(layout, DockEdge::End); let moving = if group { mates } else { vec![id.clone()] }; - if let Some(column) = columns.iter().rev().find(|c| c.iconic) { + if let Some(column) = icon_column(columns).and_then(|k| columns.get(k)) { if column.panels().any(|p| p == panel) { return Ok(Vec::new()); } @@ -736,7 +749,7 @@ impl Legacy { let staying: Vec<(usize, &String)> = columns.iter().enumerate().filter_map(|(k, c)| c.panels().find(|p| !moving.contains(p)).map(|p| (k, p))).collect(); // Place 1 is next to the icon column, inwards; without one, next to the outer column. - let base = match columns.iter().rposition(|c| c.iconic) { + let base = match icon_column(columns) { Some(icons) => staying.iter().filter(|(k, _)| *k <= icons).count(), None => staying.len().min(1), }; @@ -756,12 +769,14 @@ impl Legacy { Target::at((*anchor).clone(), DockPlacement::column(true)) } }; + // Taken out of an icon column, the new column keeps that column's icons: it opens expanded. + let expand = layout.opens_in_flyout(&id).then(|| Action::set_iconic(id.clone(), false)); let unit = match window { Some(_) if whole => DragUnit::Window(id), _ if whole => DragUnit::Group(id), _ => DragUnit::Panel(id), }; - Ok(vec![unit_action(&unit, to)]) + Ok(std::iter::once(unit_action(&unit, to)).chain(expand).collect()) } } } @@ -992,7 +1007,20 @@ fn float_actions( (None, DragUnit::Panel(_)) => Action::float(panel.clone(), rect), (None, _) => unit_action(unit, Target::float(rect)), }; - Ok(std::iter::once(float).chain(expand).collect()) + let native = window.filter(|_| kept.is_some()).and_then(|w| native_moved(ctx, w, rect)); + Ok(std::iter::once(float).chain(native).chain(expand).collect()) +} + +/// The record of where the OS window of `window` (on `ctx`) is, moved to `rect` in the main window's points: an open OS +/// window stays where the dock recorded it, so a command that moves the window moves the record with it. +fn native_moved(ctx: Option<&egui::Context>, window: &Floating, rect: egui::Rect) -> Option> { + let mut native = window.native.clone()?; + let main = craft::ui::runtime::windows::WindowHost::of(ctx?).facts(egui::ViewportId::ROOT)?; + let at = rect.min + main.inner.min.to_vec2(); + native.normal.pos = at.into(); + native.normal.size = rect.size().into(); + native.position_px = Some([(at.x * native.scale).round() as i32, (at.y * native.scale).round() as i32]); + Some(Action::set_native(window.panels().next()?.clone(), Some(native))) } /// Where floating `window` is, in the main window's points: where the OS put its own window when it has one (on `ctx`), @@ -1264,6 +1292,7 @@ pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui, keeps_tools: boo // The area shares the workspace's `Arc`, so panel bodies that read the layout in the pass // (`visible`, `showing`) see it; their changes go through `apply`. let mut workspace = Arc::clone(&app.ui.dock_workspace); + let revision = workspace.revision(); let flyout_before = app.ui.open_panel.clone(); let presenting = app.ui.screen_mode >= 3; // Shift+Tab, from its key, a menu or automation, closes a panel popped out of the icon column. @@ -1293,9 +1322,9 @@ pub(crate) fn show(app: &mut VectorcraftApp, ui: &mut egui::Ui, keeps_tools: boo }; app.dock_rt.specs = specs; app.dock_rt.rails = rails; - // A workspace a command switched to in this pass (`Workspace::replace`) stays; the pass's edits - // were to the one it replaced. - if app.ui.dock_workspace.revision() == workspace.revision() { + // A workspace a command switched to or changed in this pass (`Workspace::replace`) stays; the + // pass's edits were to the one it replaced. + if app.ui.dock_workspace.revision() == revision { app.ui.dock_workspace = workspace; } // A command of this pass that opened or closed a flyout wins over the area's own change. diff --git a/crates/ui-egui/src/place.rs b/crates/ui-egui/src/place.rs index dd869b2008..13b7398cb6 100644 --- a/crates/ui-egui/src/place.rs +++ b/crates/ui-egui/src/place.rs @@ -851,7 +851,6 @@ pub(crate) fn os_drop(app: &mut VectorcraftApp, files: Vec, spot: DropSp let files = files .into_iter() .filter_map(|f| { - let target = app.drop_target(&f.name, spot, shift); let path = f.path.map(|p| p.to_string_lossy().into_owned()); // A file the platform gave neither a path nor bytes for (the web reads them after the drop) has nothing to // open or place. @@ -860,7 +859,18 @@ pub(crate) fn os_drop(app: &mut VectorcraftApp, files: Vec, spot: DropSp (None, None) => return None, (Some(_), _) => vec![], }; - Some((target, (f.name, path, bytes))) + // Bytes with no name, as a backend may give them: their format decides, so an SVG opens as a document and + // a raster image is placed. + if f.name.is_empty() { + let name = "dropped".to_owned(); + let target = if fileio::detect(&name, &bytes).is_none_or(|format| !format.raster) { + DropTarget::Open + } else { + app.drop_target(&name, spot, shift) + }; + return Some((target, (name, path, bytes))); + } + Some((app.drop_target(&f.name, spot, shift), (f.name, path, bytes))) }) .collect(); drop_files(app, files); From e2365ead0b66a08675538355137dba01e5c2a949 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Tue, 24 Nov 2026 00:59:33 +0700 Subject: [PATCH 2/2] =?UTF-8?q?Tests:=20the=20OS-window=20hosts=20list=20t?= =?UTF-8?q?he=20main=20window=20and=20every=20OS=20window=20in=20each=20pa?= =?UTF-8?q?ss,=20as=20egui-winit=20does;=20the=20native=20harness=20holds?= =?UTF-8?q?=20files=20over=20a=20window=20before=20it=20drops=20them,=20as?= =?UTF-8?q?=20the=20OS=20does;=20F7's=20text-field=20guard=20clicks=20the?= =?UTF-8?q?=20Layers=20panel's=20search=20field=20once=20the=20dialog=20ha?= =?UTF-8?q?s=20gone=20and=20releases=20it=20with=20a=20canvas=20click;=20t?= =?UTF-8?q?he=20column-places=20case=20finds=20the=20end=20dock=20by=20its?= =?UTF-8?q?=20path;=20the=20Tools=20panel's=20=E2=80=A2=E2=80=A2=E2=80=A2?= =?UTF-8?q?=20button=20is=20looked=20up=20outside=20the=20drawer,=20whose?= =?UTF-8?q?=20title=20has=20the=20same=20name;=20icon=5Fcolumn=20moves=20f?= =?UTF-8?q?rom=20the=20tests=20to=20the=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/ui-egui/src/doc_windows/tests.rs | 42 +++++++++++----- crates/ui-egui/src/edit_toolbar/tests.rs | 5 +- .../ui-egui/src/panel_docking/native_tests.rs | 17 ++++++- crates/ui-egui/src/panel_docking/tests.rs | 49 +++++++++++-------- 4 files changed, 78 insertions(+), 35 deletions(-) diff --git a/crates/ui-egui/src/doc_windows/tests.rs b/crates/ui-egui/src/doc_windows/tests.rs index 97620d7bb1..c0a4374cce 100644 --- a/crates/ui-egui/src/doc_windows/tests.rs +++ b/crates/ui-egui/src/doc_windows/tests.rs @@ -32,12 +32,12 @@ fn frame(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec /// One headless frame of the canvas panel's body and the floating document windows on a 1200 × /// 800 window. The windows' access nodes, by name, are kept for [`window_of`]. fn run_frame(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec) -> egui::FullOutput { - let raw = egui::RawInput { - screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, vec2(1200.0, 800.0))), - max_texture_side: Some(8192), - events, - ..Default::default() - }; + let screen = egui::Rect::from_min_size(Pos2::ZERO, vec2(1200.0, 800.0)); + // The main window and the OS windows [`native_host`] ran last frame, as egui-winit lists them: + // a window missing from the list is a closed one. + let mut viewports = ctx.data_mut(|d| d.remove_temp::>(egui::Id::unique(OS_WINDOWS))).unwrap_or_default(); + viewports.insert(egui::ViewportId::ROOT, main_window(screen)); + let raw = egui::RawInput { screen_rect: Some(screen), max_texture_side: Some(8192), events, viewports, ..Default::default() }; ctx.enable_accesskit(); let out = crate::testing::part_frame(app, ctx, raw, |app, ui| { app.canvas_area(ui); @@ -62,6 +62,20 @@ type Named = (egui::accesskit::Role, String, egui::Rect); /// Where [`run_frame`] keeps the last frame's [`Named`] nodes. const NAMED: &str = "doc-window-tests-named"; +/// Where [`native_host`] keeps the OS windows it ran, for [`run_frame`]'s next frame. +const OS_WINDOWS: &str = "doc-window-tests-os-windows"; + +/// The main window, with the keyboard, its top-left corner at (0, 0) on the screen. +fn main_window(screen: egui::Rect) -> egui::ViewportInfo { + egui::ViewportInfo { + inner_rect: Some(screen), + outer_rect: Some(screen), + focused: Some(true), + native_pixels_per_point: Some(1.0), + ..Default::default() + } +} + /// The last frame's [`Named`] nodes of `role` whose name `matches`. fn named(ctx: &egui::Context, role: egui::accesskit::Role, matches: impl Fn(&str) -> bool) -> Option { let nodes: std::sync::Arc> = ctx.data(|d| d.get_temp(egui::Id::unique(NAMED))).unwrap_or_default(); @@ -434,13 +448,15 @@ fn native_host() -> std::rc::Rc> { ..Default::default() }; let events = std::mem::take(&mut shared.borrow_mut().events); - let input = egui::RawInput { - viewport_id: id, - viewports: std::iter::once((id, info)).collect(), - screen_rect: Some(rect), - events, - ..Default::default() - }; + let os_windows = egui::Id::unique(OS_WINDOWS); + let mut viewports = ctx.data_mut(|d| { + let windows = d.get_temp_mut_or_default::>(os_windows); + windows.insert(id, egui::ViewportInfo { events: vec![], ..info.clone() }); + windows.clone() + }); + viewports.insert(egui::ViewportId::ROOT, main_window(egui::Rect::from_min_size(Pos2::ZERO, vec2(1200.0, 800.0)))); + viewports.insert(id, info); + let input = egui::RawInput { viewport_id: id, viewports, screen_rect: Some(rect), events, ..Default::default() }; let mut full = ctx.run_ui(input, |ui| (viewport.viewport_ui_cb)(ui)); full.textures_delta.clear(); }); diff --git a/crates/ui-egui/src/edit_toolbar/tests.rs b/crates/ui-egui/src/edit_toolbar/tests.rs index 7f0233d3e4..4c6cc77da5 100644 --- a/crates/ui-egui/src/edit_toolbar/tests.rs +++ b/crates/ui-egui/src/edit_toolbar/tests.rs @@ -315,7 +315,10 @@ fn a_new_toolbar_fills_from_the_drawer_beside_it() { // The Tools panel's ••• button takes the drawer back beside its toolbar. let nodes = settle(&mut app, &ctx, 11.0); let bar = one(&nodes, "Mine"); - let main_edit = named(&nodes, EDIT).into_iter().find(|r| !bar.contains(r.center())).expect("the Tools panel's ••• button"); + // The drawer and its title are named like the ••• buttons: the button is in neither Mine nor the drawer. + let open = drawer(&nodes); + let main_edit = + named(&nodes, EDIT).into_iter().find(|r| !bar.contains(r.center()) && !open.contains(r.center())).expect("the Tools panel's ••• button"); click(&mut app, &ctx, 12.0, main_edit.center()); assert!(app.ui.tools_drawer && crate::custom_toolbars::drawer_target(&app.ui).is_none()); } diff --git a/crates/ui-egui/src/panel_docking/native_tests.rs b/crates/ui-egui/src/panel_docking/native_tests.rs index 36cfd51931..b21b94244e 100644 --- a/crates/ui-egui/src/panel_docking/native_tests.rs +++ b/crates/ui-egui/src/panel_docking/native_tests.rs @@ -21,6 +21,8 @@ struct Window { /// Desktop rect of its content. rect: Rect, events: Vec, + /// The OS files held over it, as the OS sends them before a drop. + hovered: Vec, dropped: Vec, frames: usize, /// The texts its last frame drew, with their rects in the window. @@ -68,7 +70,14 @@ impl Host { let mut host = host.borrow_mut(); let builder = &viewport.builder; let rect = Rect::from_min_size(builder.position.unwrap_or_default(), builder.inner_size.unwrap_or(vec2(300.0, 200.0))); - host.windows.entry(id).or_insert_with(|| Window { rect, events: Vec::new(), dropped: Vec::new(), frames: 0, texts: Vec::new() }); + host.windows.entry(id).or_insert_with(|| Window { + rect, + events: Vec::new(), + hovered: Vec::new(), + dropped: Vec::new(), + frames: 0, + texts: Vec::new(), + }); let viewports = host.infos(); let (time, focused) = (host.time, host.focus == id); let Some(window) = host.windows.get_mut(&id) else { return }; @@ -78,6 +87,7 @@ impl Host { viewports, screen_rect: Some(Rect::from_min_size(Pos2::ZERO, window.rect.size())), events: std::mem::take(&mut window.events), + hovered_files: std::mem::take(&mut window.hovered), dropped_files: std::mem::take(&mut window.dropped), focused, time: Some(time), @@ -286,6 +296,11 @@ fn a_file_dropped_on_a_native_window_opens() { let viewport = h.float("layers"); let documents = h.app.session.documents().len(); let svg = br#""#; + // The OS holds the file over the window first, then drops it. + if let Some(window) = h.host.borrow_mut().windows.get_mut(&viewport) { + window.hovered.push(egui::HoveredFile::default()); + } + h.frame(vec![]); if let Some(window) = h.host.borrow_mut().windows.get_mut(&viewport) { window.dropped.push(Arc::new(MockDrop(svg.to_vec()))); } diff --git a/crates/ui-egui/src/panel_docking/tests.rs b/crates/ui-egui/src/panel_docking/tests.rs index 59506fca4b..2544e8756a 100644 --- a/crates/ui-egui/src/panel_docking/tests.rs +++ b/crates/ui-egui/src/panel_docking/tests.rs @@ -142,6 +142,20 @@ fn end_columns(layout: &Layout) -> &[Column] { edge_columns(layout, DockEdge::End) } +/// The columns of the end dock, to set a layout no command makes. +fn end_dock_mut(layout: &mut Layout) -> Option<&mut Vec>> { + let path = layout.dock_path(DockEdge::End)?; + let mut node = layout.root.as_mut()?; + for second_child in path { + let Node::Split { first, second, .. } = node else { return None }; + node = if second_child { second } else { first }; + } + match node { + Node::Dock { columns, .. } => Some(columns), + _ => None, + } +} + /// The panels of the group holding `panel`, docked or floating. fn group_of(layout: &Layout, panel: &String) -> Vec { layout.place(panel).and_then(|place| layout.group(&place)).map(|group| group.panels.clone()).unwrap_or_default() @@ -152,19 +166,6 @@ fn in_iconic_column(layout: &Layout, panel: &str) -> bool { layout.opens_in_flyout(&panel.to_owned()) } -/// The icon column among `columns`: the iconic column holding the most panels of the icon groups. -fn icon_column(columns: &[Column]) -> Option { - let in_rows = |p: &&String| ICON_PANEL_GROUPS.iter().any(|row| row.contains(&p.as_str())); - columns - .iter() - .enumerate() - .filter(|(_, c)| c.iconic) - .map(|(k, c)| (c.groups.iter().flat_map(|g| &g.panels).filter(in_rows).count(), k)) - .filter(|(count, _)| *count > 0) - .max() - .map(|(_, k)| k) -} - /// Answers the topmost open dialog with OK or Cancel, then runs the frame's dialog pump. A held OK is answered /// through the pump. fn answer(app: &mut VectorcraftApp, ok: bool) { @@ -363,9 +364,8 @@ fn panel_commands_give_the_placement_the_old_tests_checked() { // A layout converted from an old dock split side by side, with the dock collapsed: two // collapsed tree columns before the icon column. No command makes it, so it is set directly. let mut layout = h.layout(); - if let Some(Node::Split { second, .. }) = layout.root.as_mut() - && let Node::Dock { columns, .. } = second.as_mut() { + let columns = end_dock_mut(&mut layout).expect("the layout has an end dock"); let libraries = columns[0].groups[0].panels.pop().unwrap(); assert_eq!(libraries, "libraries"); columns.insert(1, Column::new(vec![Group::new(vec![libraries], 0)]).with_iconic(true)); @@ -383,9 +383,8 @@ fn panel_commands_give_the_placement_the_old_tests_checked() { // No icon column: `icon` makes one. let mut layout = h.layout(); - if let Some(Node::Split { second, .. }) = layout.root.as_mut() - && let Node::Dock { columns, .. } = second.as_mut() { + let columns = end_dock_mut(&mut layout).expect("the layout has an end dock"); columns.remove(2); } h.set_layout(layout); @@ -812,11 +811,21 @@ fn other_function_keys_and_keyboard_guards_keep_their_routes() { assert!(showing(&h.app.ui, "layers"), "F7 does nothing while a dialog is open"); answer(&mut h.app, false); - let field = egui::Id::unique("panel-shortcut-text-focus"); - h.ctx.memory_mut(|m| m.request_focus(field)); + // The Layers panel's search field, clicked once the dialog has gone: it has the keys until a click on the canvas + // takes them. + h.frames(2); + let field = h + .nodes + .iter() + .filter(|(_, n)| n.role() == egui::accesskit::Role::TextInput && n.label() == Some("Search layers")) + .find_map(|(_, n)| n.bounds()) + .map(|b| pos2(((b.x0 + b.x1) / 2.0) as f32, ((b.y0 + b.y1) / 2.0) as f32)) + .expect("the Layers panel's search field"); + h.click(field); h.f7(); assert!(showing(&h.app.ui, "layers"), "F7 does nothing while a text field has focus"); - h.ctx.memory_mut(|m| m.surrender_focus(field)); + let canvas = h.canvas(); + h.click(canvas.right_bottom() - vec2(20.0, 20.0)); h.f7(); assert!(!showing(&h.app.ui, "layers"), "F7 toggles when the field releases focus");