diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index e338c8541..72ca87363 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -316,6 +316,17 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj // picked bodies instead. let triad = crate::dialogs_move::triad(app, &d); let at = triad.map(|t| t.center).or_else(|| anchor(app, &d).map(|(base, _)| base)); + // Tab and Shift+Tab in the box show the next or previous value (Y after X; #4776), + // keeping the keyboard in the box. + // egui moves the keyboard on with Tab before this runs: the box had it last frame. + let id = egui::Id::new("sc_canvas_value"); + let boxed = ui.ctx().memory(|m| m.has_focus(id) || m.had_focus_last_frame(id)); + let back = boxed && ui.input_mut(|i| i.consume_key(egui::Modifiers::SHIFT, egui::Key::Tab)); + let tab = boxed && !back && ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Tab)); + if tab || back { + crate::dialogs_move::step_value_field(&mut d, if back { -1 } else { 1 }); + d.focus = true; + } let focus = std::mem::take(&mut d.focus); if let Some(bs) = at.and_then(|base| proj.to_screen(base)) && let Some((label, _, value)) = d.primary() @@ -323,7 +334,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj // Clear of the triad: its arrows reach LEN_PX and its rings RING of that, so a box // beside the centre would cover the handles the user is reaching for. let off = if triad.is_some() { vec2(crate::gizmo::LEN_PX as f32 + 16.0, -12.0) } else { vec2(16.0, -30.0) }; - value_box(ui, label, value, bs + off, focus || dragging); + value_box_with(ui, label, value, bs + off, focus || dragging, true); } app.dialog = Some(d); return; @@ -567,6 +578,12 @@ fn draw(app: &SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj: &Proj, /// text (e.g. "4 mm" -> "15 mm") would leave the stale, shorter selection from the pick in place, /// covering only part of the new text. fn value_box(ui: &mut egui::Ui, label: &str, value: &mut String, at: Pos2, select_all: bool) { + value_box_with(ui, label, value, at, select_all, false); +} + +/// [`value_box`]; with `keep_tab` Tab stays in the box for the caller (the Move box steps +/// through its values with it). +fn value_box_with(ui: &mut egui::Ui, label: &str, value: &mut String, at: Pos2, select_all: bool, keep_tab: bool) { let t = Tokens::get(); let id = egui::Id::new("sc_canvas_value"); Container::new(ui.ctx(), Surface::Canvas).area(egui::Id::new("sc_canvas_area")).fixed_pos(at).order(egui::Order::Foreground).show( @@ -575,7 +592,7 @@ fn value_box(ui: &mut egui::Ui, label: &str, value: &mut String, at: Pos2, selec Container::popup_frame(ui.style()).inner_margin(egui::Margin::symmetric(6, 3)).show(ui, |ui| { ui.horizontal(|ui| { ui.label(egui::RichText::new(crate::i18n::tr_label(label)).color(t.text_dim).size(crate::theme::Tokens::get().font_caption)); - let r = crate::ui_kit::canvas_value_field(ui, id, value); + let r = crate::ui_kit::canvas_value_field(ui, id, value, keep_tab); crate::params_dialog::complete(ui, &r, value); if select_all { r.request_focus(); diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 22bea0b6f..6e3f44e49 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -973,6 +973,7 @@ impl Dialog { /// The main value (shown on the canvas too): label, kind and the expression. pub fn primary(&mut self) -> Option<(&'static str, ValueKind, &mut String)> { + let move_field = crate::dialogs_move::value_field(self); Some(match &mut self.kind { Kind::Extrude { to_offset, .. } if to_object_on(&self.inputs) => ("To Offset", ValueKind::Length, to_offset), Kind::Extrude { distance, .. } => ("Distance", ValueKind::Length, distance), @@ -995,12 +996,20 @@ impl Dialog { } ("Diameter", ValueKind::Length, diameter) } - // A move's main value follows its type: a distance along X for the ones that - // translate, the angle for Rotate. Point to Point is two picks with nothing to type. - Kind::Move { mode, x, angle, .. } => match *mode { + // A move's main value follows its type: a distance for the ones that translate + // (along the axis dragged or tabbed to last, X at first; #4776), the angle for + // Rotate. Point to Point is two picks with nothing to type. + Kind::Move { mode, x, y, z, rx, ry, rz, angle } => match *mode { crate::dialogs_move::ROTATE => ("Angle", ValueKind::Angle, angle), crate::dialogs_move::POINT_TO_POINT => return None, - _ => ("X", ValueKind::Length, x), + _ => match move_field { + 1 => ("Y", ValueKind::Length, y), + 2 => ("Z", ValueKind::Length, z), + 3 => ("X Angle", ValueKind::Angle, rx), + 4 => ("Y Angle", ValueKind::Angle, ry), + 5 => ("Z Angle", ValueKind::Angle, rz), + _ => ("X", ValueKind::Length, x), + }, }, Kind::PatternRect { spacing, .. } => ("Spacing", ValueKind::Length, spacing), Kind::PatternCirc { angle, .. } => ("Angle", ValueKind::Angle, angle), diff --git a/crates/ui-egui/src/dialogs_move.rs b/crates/ui-egui/src/dialogs_move.rs index a4762e3f0..d5bfde810 100644 --- a/crates/ui-egui/src/dialogs_move.rs +++ b/crates/ui-egui/src/dialogs_move.rs @@ -249,7 +249,46 @@ pub fn triad(app: &SolveApp, d: &Dialog) -> Option { } /// Apply a triad drag to the dialog's values (added to the values when the drag began). +/// Which value the canvas box shows and types into, for the types that translate: X, Y or Z +/// (0 to 2), or, in Free Move, the angle about X, Y or Z (3 to 5). Kept in `extra` as +/// `value_field`; X at first. +pub fn value_field(d: &Dialog) -> usize { + let Kind::Move { mode, .. } = &d.kind else { return 0 }; + let n = fields(*mode); + d.extra.get("value_field").and_then(Value::as_u64).map_or(0, |f| f as usize).min(n.saturating_sub(1)) +} + +/// How many values the box can show for a move type (see [`value_field`]). +fn fields(mode: usize) -> usize { + match mode { + FREE => 6, + TRANSLATE | POINT_TO_POSITION => 3, + _ => 1, + } +} + +/// Show the next (`step` 1) or previous (`step` -1) value in the box, round the type's values. +pub fn step_value_field(d: &mut Dialog, step: i64) { + let Kind::Move { mode, .. } = &d.kind else { return }; + let n = fields(*mode) as i64; + let next = (value_field(d) as i64 + step).rem_euclid(n.max(1)); + d.extra.insert("value_field".into(), json!(next)); +} + pub fn drag(app: &SolveApp, d: &mut Dialog, g: Drag) { + // The box shows the value the handle drives: the distance along a dragged arrow, the angle + // of a dragged ring (#4776). A plane square or the ball moves along two or three axes and + // leaves it as it is. + let field = match g.handle { + crate::gizmo::Handle::Axis(i) => Some(i.min(2)), + crate::gizmo::Handle::Ring(i) => Some(3 + i.min(2)), + _ => None, + }; + if let (Some(f), Kind::Move { mode, .. }) = (field, &d.kind) + && f < fields(*mode) + { + d.extra.insert("value_field".into(), json!(f)); + } let Kind::Move { x, y, z, rx, ry, rz, .. } = &d.kind else { return }; if g.started || !d.extra.contains_key("drag0") { let v = [len(app, x), len(app, y), len(app, z), ang(app, rx), ang(app, ry), ang(app, rz)].map(|r| r.unwrap_or(0.0)); diff --git a/crates/ui-egui/src/dialogs_move_tests.rs b/crates/ui-egui/src/dialogs_move_tests.rs index f213a5980..33a6b0dc3 100644 --- a/crates/ui-egui/src/dialogs_move_tests.rs +++ b/crates/ui-egui/src/dialogs_move_tests.rs @@ -99,3 +99,52 @@ fn point_to_point_takes_the_origin_point() { let p = params(&h.app, &d).unwrap(); assert_eq!(p["translate"], json!([mm(-hi.x), mm(-hi.y), mm(-hi.z)]), "the corner goes onto the origin"); } + +/// #4776: the box shows the value of the handle dragged: the distance along the Y arrow after +/// dragging it, the angle of a ring after turning it; typing then edits that value. +#[test] +fn the_box_shows_the_dragged_axis() { + let mut s = Session::default(); + s.execute("solid.box", &json!({"length": 20, "width": 20, "height": 20})).unwrap(); + let app = SolveApp::new(s, Services::default()); + let mut d = move_dialog(FREE); + let g = |handle, change| crate::gizmo::Drag { handle, change, started: true, done: true }; + drag(&app, &mut d, g(crate::gizmo::Handle::Axis(1), Change::Translate(Vec3::new(0.0, 5.0, 0.0)))); + let (label, kind, value) = d.primary().unwrap(); + assert_eq!((label, kind, value.as_str()), ("Y", ValueKind::Length, "5 mm")); + *value = "7 mm".into(); + let Kind::Move { x, y, .. } = &d.kind else { panic!("not a move") }; + assert_eq!((x.as_str(), y.as_str()), ("0 mm", "7 mm"), "typing edits Y"); + drag(&app, &mut d, g(crate::gizmo::Handle::Ring(2), Change::Rotate { axis: 2, angle: 0.5 })); + assert_eq!(d.primary().map(|p| (p.0, p.1)), Some(("Z Angle", ValueKind::Angle))); + // Translate has no rings: a ring index past its values leaves the box on a distance. + let mut t = move_dialog(TRANSLATE); + drag(&app, &mut t, g(crate::gizmo::Handle::Axis(2), Change::Translate(Vec3::new(0.0, 0.0, 3.0)))); + assert_eq!(t.primary().map(|p| p.0), Some("Z")); +} + +/// #4776: Tab in the box goes to the next value (X, Y, Z, then the angles in Free Move) and +/// Shift+Tab back, the keyboard staying in the box; Enter moves the body by what was typed. +#[test] +fn tab_in_the_box_goes_to_the_next_axis() { + let mut h = crate::scenario::Harness::new(); + h.app.run("solid.box", json!({"length": 20, "width": 20, "height": 20})).unwrap(); + h.settle(); + h.app.run("select.set", json!({"items": [Sel::Body { name: "Body1".into() }]})).unwrap(); + h.app.start("solid.move"); + h.frames(3); + assert_eq!(h.app.dialog.as_mut().and_then(|d| d.primary()).map(|p| p.0), Some("X")); + h.step(&json!({"key": "Tab"})).unwrap(); + h.frames(2); + assert_eq!(h.app.dialog.as_mut().and_then(|d| d.primary()).map(|p| p.0), Some("Y")); + h.step(&json!({"key": "Tab"})).unwrap(); + h.step(&json!({"key": "Tab", "shift": true})).unwrap(); + h.frames(2); + assert_eq!(h.app.dialog.as_mut().and_then(|d| d.primary()).map(|p| p.0), Some("Y"), "Shift+Tab goes back"); + h.step(&json!({"text": "12"})).unwrap(); + h.frames(1); + h.step(&json!({"key": "Enter"})).unwrap(); + h.settle(); + let b = h.app.session.model.state().body("Body1").unwrap().mesh().bounds(); + assert!((b.min.y - 12.0).abs() < 1e-6 && b.min.x.abs() < 1e-6, "moved along Y: {b:?}"); +} diff --git a/crates/ui-egui/src/ui_kit/fields.rs b/crates/ui-egui/src/ui_kit/fields.rs index 760961c5a..3132fc17f 100644 --- a/crates/ui-egui/src/ui_kit/fields.rs +++ b/crates/ui-egui/src/ui_kit/fields.rs @@ -488,9 +488,15 @@ fn length_drag<'a>(value: &'a mut f64, units: &str, decimals: u8) -> egui::DragV /// Small model-anchored value editor. Retains its focus ID and native hit height so it does /// not intercept nearby canvas picks; the owner keeps completion and selection handling. -pub(crate) fn canvas_value_field(ui: &mut Ui, id: egui::Id, value: &mut String) -> Response { +/// With `keep_tab` Tab stays with the field (the caller acts on it) instead of moving the +/// keyboard to the next widget. +pub(crate) fn canvas_value_field(ui: &mut Ui, id: egui::Id, value: &mut String, keep_tab: bool) -> Response { let t = Tokens::get(); - ui.add(egui::TextEdit::singleline(value).id(id).desired_width(t.space_24 * 3.0)) + let mut edit = egui::TextEdit::singleline(value).id(id).desired_width(t.space_24 * 3.0); + if keep_tab { + edit = edit.event_filter(egui::EventFilter { tab: true, horizontal_arrows: true, ..Default::default() }); + } + ui.add(edit) } /// Placement editor whose owner routes Enter and Tab (including rejected expressions). diff --git a/crates/ui-egui/src/ui_kit/gallery.rs b/crates/ui-egui/src/ui_kit/gallery.rs index fde92bbe3..3c4573896 100644 --- a/crates/ui-egui/src/ui_kit/gallery.rs +++ b/crates/ui-egui/src/ui_kit/gallery.rs @@ -622,7 +622,7 @@ const ELEMENTS: &[Element] = &[ slider(ui, &mut 12.0, 0.0..=100.0, Slider { show_value: false, integer: true, ..Default::default() }); }), element!("canvas_value_field", |ui, s| { - canvas_value_field(ui, ui.id().with("canvas_value"), &mut s.value); + canvas_value_field(ui, ui.id().with("canvas_value"), &mut s.value, false); }), element!("placement_field", |ui, s| { placement_field(ui, ui.id().with("placement"), &mut s.value, "Distance");