M1: New from selection is disabled for sections and keeps the tool on failure (#1683)

Sections never go in frames, so with a section selected the button showed
object.frameSelection's error and still switched to the Move tool. It is
now enabled only for a selection the command frames, and the Move tool
follows only a frame made. Screen readers hear when it is disabled.
This commit is contained in:
saimskywalker committed 2026-10-31 02:50:33 +08:00
1 parent 2605ceb513
commit c7fb4e33df
2 files changed
+53 -9

No files matched your search

@@ -22,15 +22,16 @@ fn scene(select: bool) -> (Ui, [NodeId; 2]) {
(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 {
/// The middle of the button named `label`, found in the AccessKit tree, and whether it says it is
/// disabled.
fn named_button(u: &mut Ui, label: &str) -> (Pos2, bool) {
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)
(pos2(((b.x0 + b.x1) / 2.0) as f32, ((b.y0 + b.y1) / 2.0) as f32), n.is_disabled())
}
fn click(u: &mut Ui, p: Pos2) {
@@ -43,7 +44,8 @@ fn click(u: &mut Ui, p: Pos2) {
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");
let (at, disabled) = named_button(&mut u, "New from selection");
assert!(!disabled);
click(&mut u, at);
let doc = u.app.session.doc().unwrap();
let parent = doc.node(a).unwrap().parent.expect("a parent");
@@ -58,9 +60,39 @@ 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");
let (at, disabled) = named_button(&mut u, "New from selection");
assert!(disabled, "screen readers hear it is disabled");
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");
}
/// Sections never go in frames (`object.frameSelection` refuses them): with a section selected,
/// alone or with other layers, the button is disabled, and clicking it shows no error and keeps
/// the Frame tool.
#[test]
fn new_from_selection_is_disabled_for_a_section() {
let (mut u, [a, b]) = scene(false);
let section = NodeId(u.app.session.execute("object.wrapInSection", &json!({"ids": [a]})).unwrap()["id"].as_u64().unwrap());
for selected in [vec![section], vec![section, b]] {
u.app.session.execute("select.set", &json!({"ids": selected})).unwrap();
u.app.session.execute("tool.set", &json!({"tool": "frame"})).unwrap();
let history = u.app.session.undo_depth().0;
let (at, disabled) = named_button(&mut u, "New from selection");
assert!(disabled, "{selected:?}");
click(&mut u, at);
assert_eq!(u.app.session.undo_depth().0, history, "nothing was framed");
assert!(u.app.ui.toast.is_none(), "no error: {:?}", u.app.ui.toast);
assert_eq!(u.app.session.tool.tool, ToolId::Frame, "the Frame tool stays");
}
// A layer inside the section can be framed (in the section).
u.app.session.execute("select.set", &json!({"ids": [a]})).unwrap();
u.app.session.execute("tool.set", &json!({"tool": "frame"})).unwrap();
let (at, disabled) = named_button(&mut u, "New from selection");
assert!(!disabled);
click(&mut u, at);
let frame = u.app.session.doc().unwrap().node(a).unwrap().parent.unwrap();
assert_eq!(u.app.session.doc().unwrap().node(frame).unwrap().parent, Some(section));
assert_eq!(u.app.session.tool.tool, ToolId::Move);
}
+16 -4
View File
@@ -2,29 +2,41 @@
//! 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 figcraft_doc::{Document, NodeId, NodeKind};
use serde_json::json;
use crate::FigApp;
use crate::a11y;
use crate::i18n::tr;
use crate::icons::{self, Icon};
use crate::theme::{Tokens, regular, semibold};
use crate::widgets::{self, PAD};
/// Whether `object.frameSelection` would frame `selection`: layers on a page, none of them a
/// section (sections never go in frames, #1683).
fn can_frame(doc: &Document, selection: &[NodeId]) -> bool {
!selection.is_empty() && selection.iter().all(|i| doc.node(*i).is_some_and(|n| n.parent.is_some() && !matches!(n.kind, NodeKind::Section { .. })))
}
/// 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 framable = app.session.ds().is_ok_and(|ds| can_frame(&ds.doc, &ds.selection));
let mut from_selection = false;
ui.horizontal(|ui| {
ui.add_space(PAD);
from_selection = widgets::button(ui, tr("New from selection"), false, selected).clicked();
let resp = widgets::button(ui, tr("New from selection"), false, framable);
if !framable {
a11y::disabled(&resp);
}
from_selection = resp.clicked();
});
ui.add_space(4.0);
if from_selection {
app.run("object.frameSelection", json!({}));
// The Frame tool stays when framing failed (its error is shown).
if from_selection && app.run("object.frameSelection", json!({})).is_some() {
app.run("tool.set", json!({"tool": "move"}));
return;
}