diff --git a/crates/ui-egui/locales/en.toml b/crates/ui-egui/locales/en.toml index 215cef140..7d2bce160 100644 --- a/crates/ui-egui/locales/en.toml +++ b/crates/ui-egui/locales/en.toml @@ -902,6 +902,7 @@ name = "English" "Nested instance" = "Nested instance" "New board" = "New board" "New design file" = "New design file" +"New from selection" = "New from selection" "Newest first" = "Newest first" "Next or previous control in the focused region" = "Next or previous control in the focused region" "Next frame" = "Next frame" diff --git a/crates/ui-egui/src/frame_new_from_selection_tests.rs b/crates/ui-egui/src/frame_new_from_selection_tests.rs new file mode 100644 index 000000000..85cfe75b3 --- /dev/null +++ b/crates/ui-egui/src/frame_new_from_selection_tests.rs @@ -0,0 +1,66 @@ +//! The Frame tool's panel starts with New from selection (#1683): with layers selected it wraps +//! them in a frame, as Object ▸ Frame selection does, in one undo step, and returns to the Move +//! tool; with nothing selected it does nothing. + +use egui::accesskit::Role; +use egui::{Pos2, pos2}; +use figcraft_doc::{NodeId, NodeKind}; +use figcraft_engine::{Session, ToolId}; +use serde_json::json; + +use crate::layers_drag_tests::Ui; + +/// Two rectangles, both selected when `select`, with the Frame tool active. +fn scene(select: bool) -> (Ui, [NodeId; 2]) { + let mut s = Session::new(); + let mut rect = + |x: i32| NodeId(s.execute("shape.rectangle", &json!({"x": x, "y": 0, "width": 50, "height": 50})).unwrap()["id"].as_u64().unwrap()); + let ids = [rect(0), rect(80)]; + let selected: &[NodeId] = if select { &ids } else { &[] }; + s.execute("select.set", &json!({"ids": selected})).unwrap(); + s.execute("tool.set", &json!({"tool": "frame"})).unwrap(); + (Ui::new(s), ids) +} + +/// The middle of the button named `label`, found in the AccessKit tree. +fn named_button(u: &mut Ui, label: &str) -> Pos2 { + u.ctx.enable_accesskit(); + u.frame(vec![]); + let out = u.frame_output(vec![]); + let nodes = out.platform_output.accesskit_update.expect("an AccessKit update").nodes; + let n = nodes.iter().map(|(_, n)| n).find(|n| n.role() == Role::Button && n.label() == Some(label)).expect(label); + let b = n.bounds().expect("bounds"); + pos2(((b.x0 + b.x1) / 2.0) as f32, ((b.y0 + b.y1) / 2.0) as f32) +} + +fn click(u: &mut Ui, p: Pos2) { + u.button(p, true); + u.button(p, false); + u.frame(vec![]); +} + +#[test] +fn new_from_selection_frames_the_selected_layers_and_returns_to_move() { + let (mut u, [a, b]) = scene(true); + let history = u.app.session.undo_depth().0; + let at = named_button(&mut u, "New from selection"); + click(&mut u, at); + let doc = u.app.session.doc().unwrap(); + let parent = doc.node(a).unwrap().parent.expect("a parent"); + assert_eq!(doc.node(b).unwrap().parent, Some(parent), "both layers in the same frame"); + assert!(matches!(doc.node(parent).unwrap().kind, NodeKind::Frame), "the parent is a new frame"); + assert_eq!(u.app.session.undo_depth().0, history + 1, "one undo step"); + assert_eq!(u.app.session.tool.tool, ToolId::Move); +} + +#[test] +fn new_from_selection_does_nothing_without_a_selection() { + let (mut u, [a, _]) = scene(false); + let page = u.app.session.doc().unwrap().node(a).unwrap().parent; + let history = u.app.session.undo_depth().0; + let at = named_button(&mut u, "New from selection"); + click(&mut u, at); + assert_eq!(u.app.session.doc().unwrap().node(a).unwrap().parent, page, "nothing was framed"); + assert_eq!(u.app.session.undo_depth().0, history); + assert_eq!(u.app.session.tool.tool, ToolId::Frame, "the Frame tool stays"); +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 3464de0b3..28c7ef602 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -110,6 +110,8 @@ mod font_menu_regrow_tests; #[cfg(test)] mod frame_label_tests; #[cfg(test)] +mod frame_new_from_selection_tests; +#[cfg(test)] mod gap_handle_tests; #[cfg(test)] mod gradient_shorthand_tests; diff --git a/crates/ui-egui/src/panels/frame_presets.rs b/crates/ui-egui/src/panels/frame_presets.rs index 9eb281622..612ef90f8 100644 --- a/crates/ui-egui/src/panels/frame_presets.rs +++ b/crates/ui-egui/src/panels/frame_presets.rs @@ -1,5 +1,5 @@ -//! The Frame tool's panel: frame presets by category. Clicking one adds a frame of that size next -//! to the page's frames and returns to the Move tool. +//! The Frame tool's panel: New from selection, then frame presets by category. Clicking a preset +//! adds a frame of that size next to the page's frames; either returns to the Move tool. use egui::{Align2, CornerRadius, Id, Sense, Ui, pos2, vec2}; use serde_json::json; @@ -10,11 +10,24 @@ use crate::icons::{self, Icon}; use crate::theme::{Tokens, regular, semibold}; use crate::widgets::{self, PAD}; -/// Draw the expandable frame-preset categories; choosing a preset runs `frame.create`, -/// switches to the Move tool and zooms to the new selection. +/// Draw New from selection and the expandable frame-preset categories. New from selection wraps +/// the selected layers in a frame (`object.frameSelection`, #1683); choosing a preset runs +/// `frame.create` and zooms to the new selection. Both switch to the Move tool. pub fn panel(app: &mut FigApp, ui: &mut Ui) { let t = Tokens::get(ui.ctx()); widgets::section_header(ui, tr("Frame"), &[], false); + let selected = app.session.ds().is_ok_and(|ds| !ds.selection.is_empty()); + let mut from_selection = false; + ui.horizontal(|ui| { + ui.add_space(PAD); + from_selection = widgets::button(ui, tr("New from selection"), false, selected).clicked(); + }); + ui.add_space(4.0); + if from_selection { + app.run("object.frameSelection", json!({})); + app.run("tool.set", json!({"tool": "move"})); + return; + } let w = ui.available_width(); let mut cats: Vec<&str> = Vec::new(); for (c, ..) in figcraft_engine::cmd::create::FRAME_PRESETS {