Merge branch 'area/app3' into area/app4

This commit is contained in:
bflatastic
2026-11-24 01:01:19 +07:00
6 changed files with 126 additions and 44 deletions
+29 -13
View File
@@ -32,12 +32,12 @@ fn frame(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec<egui::Event>
/// 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::Event>) -> 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::ViewportIdMap<egui::ViewportInfo>>(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<egui::Rect> {
let nodes: std::sync::Arc<Vec<Named>> = ctx.data(|d| d.get_temp(egui::Id::unique(NAMED))).unwrap_or_default();
@@ -434,13 +448,15 @@ fn native_host() -> std::rc::Rc<std::cell::RefCell<Host>> {
..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::<egui::ViewportIdMap<egui::ViewportInfo>>(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();
});
+4 -1
View File
@@ -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());
}
+36 -7
View File
@@ -670,6 +670,19 @@ fn mates(layout: &Layout<String>, panel: &str) -> Vec<String> {
}
}
/// The icon column among `columns`: the iconic column holding the most panels of the icon groups.
fn icon_column(columns: &[Column<String>]) -> Option<usize> {
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<String>, to: Target<String>) -> Action<String> {
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<String>, rect: egui::Rect) -> Option<Action<String>> {
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.
@@ -21,6 +21,8 @@ struct Window {
/// Desktop rect of its content.
rect: Rect,
events: Vec<Event>,
/// The OS files held over it, as the OS sends them before a drop.
hovered: Vec<egui::HoveredFile>,
dropped: Vec<egui::DroppedFileHandle>,
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#"<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40"><rect width="10" height="10"/></svg>"#;
// 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())));
}
+29 -20
View File
@@ -142,6 +142,20 @@ fn end_columns(layout: &Layout<String>) -> &[Column<String>] {
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<String>) -> Option<&mut Vec<Column<String>>> {
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<String>, panel: &String) -> Vec<String> {
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<String>, 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<String>]) -> Option<usize> {
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");
+12 -2
View File
@@ -851,7 +851,6 @@ pub(crate) fn os_drop(app: &mut VectorcraftApp, files: Vec<OsFile>, 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<OsFile>, 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);