Merge pull request #2148 from TrevorBrown-dev/feat/scale-tool-focus-2146

Scale tool: K focuses the Scale field, so K, 2, Enter scales by two (#2146)
This commit is contained in:
BFlat
2026-11-12 09:15:53 +07:00
committed by GitHub
5 changed files with 257 additions and 7 deletions
+193
View File
@@ -737,6 +737,199 @@ fn scrubbing_the_scale_panel_width_scales_live_in_one_undo_step() {
assert_eq!((w, h, undo_steps(&u)), (60.0, 45.0, steps + 1), "40×30 scaled to a width of 60");
}
/// A 100 x 100 rectangle at the origin, selected, in a Ui with the Move tool.
fn scale_setup() -> (Ui, NodeId) {
let mut s = Session::new();
let r = id(s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 100, "height": 100})).unwrap());
s.execute("select.set", &json!({"ids": [r]})).unwrap();
let mut u = Ui::new(s);
u.frame(vec![]);
u.frame(vec![]);
(u, r)
}
fn scale_field_focused(u: &Ui) -> bool {
u.ctx.memory(|m| m.has_focus(Id::unique("scale_k").with("te")))
}
/// Measured in Figma's web app (2026-11-09): K opens the Scale section with its input focused.
#[test]
fn pressing_k_focuses_the_scale_field_with_its_text_selected() {
let (mut u, _) = scale_setup();
assert!(!scale_field_focused(&u));
key(&mut u, Key::K, Modifiers::NONE);
u.frame(vec![]);
assert_eq!(u.app.session.tool.tool, figcraft_engine::ToolId::Scale);
assert!(scale_field_focused(&u), "the scale field has the keyboard");
let state = egui::TextEdit::load_state(&u.ctx, Id::unique("scale_k").with("te")).expect("the editor exists");
let range = state.cursor.char_range().expect("a selection");
let shown = u.ctx.data(|d| d.get_temp::<String>(Id::unique("scale_k").with("editing"))).expect("the field is being edited");
let (lo, hi) = (range.primary.index.min(range.secondary.index), range.primary.index.max(range.secondary.index));
assert_eq!((lo, hi), (egui::text::CCursor::new(0).index, egui::text::CCursor::new(shown.chars().count()).index), "all of its text is selected");
}
/// K with nothing selected, then a selection: the field doesn't take the keyboard from the canvas.
#[test]
fn a_selection_after_k_does_not_move_the_keyboard_to_the_scale_field() {
let (mut u, r) = scale_setup();
u.app.run("select.set", json!({"ids": []}));
u.frame(vec![]);
key(&mut u, Key::K, Modifiers::NONE);
for _ in 0..3 {
u.frame(vec![]);
}
u.app.run("select.set", json!({"ids": [r]}));
for _ in 0..3 {
u.frame(vec![]);
}
assert_eq!(u.app.session.tool.tool, figcraft_engine::ToolId::Scale);
assert!(!scale_field_focused(&u), "the canvas keeps the keyboard");
}
/// Ctrl+Enter in the factor field is not the tool's exit.
#[test]
fn ctrl_enter_in_the_scale_field_keeps_the_scale_tool() {
let (mut u, _) = scale_setup();
key(&mut u, Key::K, Modifiers::NONE);
u.frame(vec![]);
key(&mut u, Key::Enter, Modifiers::CTRL);
u.frame(vec![]);
assert_eq!(u.app.session.tool.tool, figcraft_engine::ToolId::Scale);
}
/// An Enter on the first frame the panel is back from another tab (the field not yet focused again)
/// doesn't end the tool on a stale "had the keyboard" flag.
#[test]
fn enter_on_the_first_frame_back_from_another_tab_keeps_the_scale_tool() {
let (mut u, _) = scale_setup();
key(&mut u, Key::K, Modifiers::NONE);
u.frame(vec![]);
assert!(scale_field_focused(&u));
u.app.ui.right_tab = crate::state::RightTab::Prototype;
u.frame(vec![]);
u.frame(vec![]);
u.app.ui.right_tab = crate::state::RightTab::Design;
key(&mut u, Key::Enter, Modifiers::NONE);
u.frame(vec![]);
assert_eq!(u.app.session.tool.tool, figcraft_engine::ToolId::Scale);
}
/// The Scale tool chosen again while the Design tab was away (Prototype tab, Move, then K): the
/// field takes the keyboard again when the Design tab comes back.
#[test]
fn the_scale_field_takes_the_keyboard_again_after_another_tab() {
let (mut u, _) = scale_setup();
key(&mut u, Key::K, Modifiers::NONE);
u.frame(vec![]);
assert!(scale_field_focused(&u));
u.app.ui.right_tab = crate::state::RightTab::Prototype;
u.frame(vec![]);
u.app.run("tool.set", json!({"tool": "move"}));
u.frame(vec![]);
u.app.run("tool.set", json!({"tool": "scale"}));
u.frame(vec![]);
u.app.ui.right_tab = crate::state::RightTab::Design;
u.frame(vec![]);
u.frame(vec![]);
assert!(scale_field_focused(&u), "the field has the keyboard again");
}
/// A value typed and left unsettled when the panel went away is dropped: back again, the field
/// shows the fresh value with all of its text selected, so typing replaces it.
#[test]
fn the_scale_field_comes_back_fresh_with_its_text_selected() {
let (mut u, r) = scale_setup();
key(&mut u, Key::K, Modifiers::NONE);
u.frame(vec![]);
u.frame(vec![Event::Text("7".to_string())]);
u.app.ui.right_tab = crate::state::RightTab::Prototype;
u.frame(vec![]);
u.frame(vec![]);
u.app.ui.right_tab = crate::state::RightTab::Design;
u.frame(vec![]);
u.frame(vec![]);
assert!(scale_field_focused(&u));
let shown = u.ctx.data(|d| d.get_temp::<String>(Id::unique("scale_k").with("editing"))).expect("the field is being edited");
assert_ne!(shown, "7", "the leftover edit is gone");
let state = egui::TextEdit::load_state(&u.ctx, Id::unique("scale_k").with("te")).expect("the editor exists");
let range = state.cursor.char_range().expect("a selection");
let (lo, hi) = (range.primary.index.min(range.secondary.index), range.primary.index.max(range.secondary.index));
assert_eq!((lo, hi), (egui::text::CCursor::new(0).index, egui::text::CCursor::new(shown.chars().count()).index));
assert_eq!(geom(&u, r), (0.0, 0.0, 100.0, 100.0));
}
/// Tab moves the keyboard from the factor field to the next one; an Enter there leaves the
/// Scale tool on.
#[test]
fn enter_in_another_field_after_tab_keeps_the_scale_tool() {
let (mut u, _) = scale_setup();
key(&mut u, Key::K, Modifiers::NONE);
u.frame(vec![]);
assert!(scale_field_focused(&u));
// Tab, then Enter on the very next frame (as a quick Tab, Enter or a click and Enter would).
let ev = |key, pressed| Event::Key { key, physical_key: None, pressed, repeat: false, modifiers: Modifiers::NONE };
u.frame(vec![ev(Key::Tab, true), ev(Key::Tab, false)]);
assert!(!scale_field_focused(&u), "Tab moved the keyboard on");
for _ in 0..4 {
u.frame(vec![]);
assert!(!scale_field_focused(&u), "the field doesn't take the keyboard back");
}
u.frame(vec![ev(Key::Enter, true), ev(Key::Enter, false)]);
u.frame(vec![]);
assert_eq!(u.app.session.tool.tool, figcraft_engine::ToolId::Scale);
}
/// K, 2, Enter: 100 x 100 becomes 200 x 200 about the panel's origin, the tool is Move, one undo step.
#[test]
fn k_two_enter_scales_the_selection_by_two() {
let (mut u, r) = scale_setup();
let steps = undo_steps(&u);
key(&mut u, Key::K, Modifiers::NONE);
u.frame(vec![]);
u.frame(vec![Event::Text("2".to_string())]);
key(&mut u, Key::Enter, Modifiers::NONE);
u.frame(vec![]);
// About the panel's origin, FigCraft's top-left default (Figma's grid starts at the centre).
assert_eq!(geom(&u, r), (0.0, 0.0, 200.0, 200.0));
assert!(!scale_field_focused(&u), "the field let go of the keyboard");
assert_eq!(u.app.session.tool.tool, figcraft_engine::ToolId::Move);
assert_eq!(u.app.session.ds().unwrap().selection, vec![r]);
assert_eq!(undo_steps(&u), steps + 1);
u.app.run("edit.undo", json!({}));
assert_eq!(geom(&u, r), (0.0, 0.0, 100.0, 100.0));
}
/// "2x" and "1.5" are factors too.
#[test]
fn the_scale_field_takes_2x_and_1_5() {
for (text, size) in [("2x", 200.0), ("1.5", 150.0)] {
let (mut u, r) = scale_setup();
key(&mut u, Key::K, Modifiers::NONE);
u.frame(vec![]);
u.frame(vec![Event::Text(text.to_string())]);
key(&mut u, Key::Enter, Modifiers::NONE);
u.frame(vec![]);
assert_eq!(geom(&u, r), (0.0, 0.0, size, size), "{text}");
}
}
/// Escape in the scale field closes it and changes nothing.
#[test]
fn escape_in_the_scale_field_closes_it_and_changes_nothing() {
let (mut u, r) = scale_setup();
let steps = undo_steps(&u);
key(&mut u, Key::K, Modifiers::NONE);
u.frame(vec![]);
u.frame(vec![Event::Text("3".to_string())]);
key(&mut u, Key::Escape, Modifiers::NONE);
u.frame(vec![]);
assert_eq!(geom(&u, r), (0.0, 0.0, 100.0, 100.0));
assert!(!scale_field_focused(&u), "the field let go of the keyboard");
assert_eq!(u.app.session.tool.tool, figcraft_engine::ToolId::Move);
assert_eq!(u.app.session.ds().unwrap().selection, vec![r]);
assert_eq!(undo_steps(&u), steps);
}
/// The texts painted in the last frame: what they say, whether they end in an ellipsis, and where
/// they show (within their clip).
fn painted_texts(shapes: &[egui::epaint::ClippedShape]) -> Vec<(String, bool, egui::Rect)> {
+3
View File
@@ -264,6 +264,9 @@ fn design(app: &mut FigApp, ui: &mut Ui) {
let sel = ds.selection.clone();
let vector_edit = ds.vector_edit.clone();
if sel.is_empty() {
if app.session.tool.tool == figcraft_engine::ToolId::Scale {
crate::scale_panel::note_no_selection(ui);
}
page_section(app, ui, &doc);
return;
}
+46 -4
View File
@@ -15,8 +15,36 @@ use crate::widgets::{self, FieldEdit, Lead};
/// The Scale section, shown in place of Position while the Scale tool is active.
pub fn section(app: &mut FigApp, ui: &mut Ui, doc: &Document, sel: &[NodeId]) {
let Some(b) = doc.bounds_of(sel) else { return };
let Some(b) = doc.bounds_of(sel) else {
note_no_selection(ui);
return;
};
widgets::section_header(ui, tr("Scale"), &[], false);
// The factor field takes the keyboard, with its text selected, whenever the section shows
// after a frame without it: the tool was chosen, or the panel came back from another tab or a
// hidden UI.
let k_id = Id::unique("scale_k");
let shown_id = Id::unique("scale_shown_frame");
let frame = ui.ctx().cumulative_frame_nr();
let last = ui.data(|d| d.get_temp::<u64>(shown_id));
ui.data_mut(|d| d.insert_temp(shown_id, frame));
let reappeared = last.is_none_or(|l| l.saturating_add(1) < frame);
if reappeared {
widgets::request_edit(ui, k_id);
}
// Enter or Escape in the factor field ends the tool (back to Move, the selection kept). Whether
// the field had the keyboard is read as it was when the last frame's widgets were done: the
// shortcut layer may let go of it before this frame's panel runs, and a click or Tab that moved
// it to another field during the last frame counts.
let in_k_id = Id::unique("scale_in_k");
// A flag left from before a gap (another tab, a hidden panel) is stale: the field didn't have
// the keyboard on the frame before this one.
let in_k = !reappeared && ui.data(|d| d.get_temp::<bool>(in_k_id)).unwrap_or(false);
// Plain Enter or Escape only: Ctrl+Enter and Alt+Enter are not the tool's exit.
let (enter, esc) = ui.input(|i| {
let plain = !(i.modifiers.ctrl || i.modifiers.alt || i.modifiers.command);
(plain && i.key_pressed(egui::Key::Enter), plain && i.key_pressed(egui::Key::Escape))
});
let col = widgets::col_width(ui.available_width());
// Anchor: column and row (0 start, 1 middle, 2 end) of the point that stays put.
let anchor = app.session.tool.scale_anchor;
@@ -41,9 +69,7 @@ pub fn section(app: &mut FigApp, ui: &mut Ui, doc: &Document, sel: &[NodeId]) {
widgets::sublabels(ui, tr("Scale"), Some(tr("Anchor")));
crate::right::block(ui, 52.0, 2.0, |ui| {
// Relative to the layers as they are: a scrub applies once, when let go.
if let Some(FieldEdit::Set(k) | FieldEdit::Scrub(k, true)) =
widgets::number_field(ui, Id::unique("scale_k"), Lead::None, Some(1.0), "x", col, 0.1)
{
if let Some(FieldEdit::Set(k) | FieldEdit::Scrub(k, true)) = widgets::number_field(ui, k_id, Lead::None, Some(1.0), "x", col, 0.1) {
factor = Some(k);
}
ui.add_space(8.0);
@@ -70,6 +96,22 @@ pub fn section(app: &mut FigApp, ui: &mut Ui, doc: &Document, sel: &[NodeId]) {
}
}
}
let focused_now = ui.memory(|m| m.has_focus(k_id.with("te")));
ui.data_mut(|d| d.insert_temp(in_k_id, focused_now));
if in_k && (enter || esc) {
app.run("tool.set", json!({"tool": "move"}));
}
}
/// The Scale tool is on but nothing is selected, so the section isn't shown: the frame still
/// counts as shown (a selection that appears later is not the tool being chosen, and the field must
/// not take the keyboard from the canvas), and the field is not the one with the keyboard.
pub fn note_no_selection(ui: &Ui) {
let frame = ui.ctx().cumulative_frame_nr();
ui.data_mut(|d| {
d.insert_temp(Id::unique("scale_shown_frame"), frame);
d.insert_temp(Id::unique("scale_in_k"), false);
});
}
/// The factor (clamped to 0.01–100) and pivot for scaling `b` by `k` around `anchor`; None when
+12 -1
View File
@@ -223,6 +223,16 @@ pub fn number_field(ui: &mut Ui, id: Id, lead: Lead, value: Option<f64>, suffix:
number_field_mixed(ui, id, lead, value, suffix, width, step)?.settled()
}
/// Ask number field `id` to open for typing on its next frame, its text selected (a tool that
/// opens with its field focused, such as Scale). An edit left over from when the field was last
/// shown (it went away with the keyboard on it) is dropped first, or the request would be ignored.
pub fn request_edit(ui: &Ui, id: Id) {
ui.data_mut(|d| {
d.remove::<String>(id.with("editing"));
d.insert_temp(id.with("beginedit"), true);
});
}
/// Whether the edit of number field `id` began with a Cmd/Ctrl-click. A padding field edited
/// that way sets every side (Guide to auto layout, Keyboard shortcut guide).
pub fn edit_began_with_command(ui: &Ui, id: Id) -> bool {
@@ -400,7 +410,8 @@ fn number_field_impl(
}
// Typing on a field the keyboard reached (Tab) edits it: the editor takes the typed text.
let typed_now = editing.is_none() && on_field && ui.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Text(_))));
let started = editing.is_none() && ((field_resp.clicked() && !alt_now) || typed_now);
let requested = ui.data_mut(|d| d.remove_temp::<bool>(id.with("beginedit"))).unwrap_or(false);
let started = editing.is_none() && ((field_resp.clicked() && !alt_now) || typed_now || requested);
// The text the field would commit to no effect: what it showed when the edit began, or the
// value the arrows last stepped it to.
let kept_id = id.with("kept");
+3 -2
View File
File diff suppressed because one or more lines are too long