Navigation bar: the view commands (#6585) (#6586)

* claim: #6585

* Navigation bar: the view commands (#6585)
This commit is contained in:
Nathan Dane
2026-10-26 17:46:15 +00:00
committed by GitHub
parent 97cce94a1c
commit fbcd4852c1
13 changed files with 491 additions and 53 deletions
+123
View File
@@ -7,6 +7,22 @@ use crate::params::{bad, num, req_vec2, req_vec3, vec3};
use crate::{Result, Sel, Session};
pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("view.fit", "Fit", fit)
.icon("fit").key("F6").noundo().doc_only().params("no parameters → frame the active canvas or selection; display only"),
CommandSpec::new("view.home", "Home view", home)
.icon("home").noundo().doc_only().params("no parameters → home orientation framing the active canvas; display only"),
CommandSpec::new("view.tool", "Navigation tool", tool)
.icon("orbit").noundo().doc_only().params("tool: orbit|pan|zoom|none → tool; latch a camera tool, none releases it"),
CommandSpec::new("view.spin", "Continuous orbit", spin)
.icon("spin").noundo().doc_only().params("on?: bool (omitted: toggle) → on?; continuous orbit until the next click or key"),
CommandSpec::new("view.visual_style", "Visual style", visual_style)
.icon("style").noundo().doc_only().params("style: shaded_with_edges|shaded|wireframe|shaded_with_hidden_edges → style, index (0..3 in that order)"),
CommandSpec::new("view.grid", "Grid", grid)
.icon("style").noundo().doc_only().params("on?: bool (omitted: toggle) → on?; show the viewport grid"),
CommandSpec::new("view.ground_shadow", "Ground shadow", ground_shadow)
.icon("style").noundo().doc_only().params("on?: bool (omitted: toggle) → on?; show the ground shadow"),
CommandSpec::new("view.navigation_bar", "Navigation bar", navigation_bar)
.icon("pan").noundo().doc_only().params("visible?: bool (omitted: toggle) → visible?; show the navigation bar (window setting)"),
CommandSpec::new("view.layout", "Workspace layout", layout)
.icon("settings").noundo().doc_only().params("mode: floating|compact → validated workspace layout; display only, never opens a dialog"),
CommandSpec::new("view.history_layout", "History layout", history_layout)
@@ -123,6 +139,71 @@ fn normal_to(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"camera": to}))
}
// UI-owned settings follow the layout command: validate here, apply in the UI adapter.
fn view_params(p: &Value, cmd: &str, allowed: &[&str]) -> Result<()> {
let object = p.as_object().ok_or_else(|| bad(cmd, "parameters must be an object"))?;
if let Some(key) = object.keys().find(|key| !allowed.contains(&key.as_str())) {
return Err(bad(cmd, format!("unknown parameter: {key}")));
}
Ok(())
}
fn fit(_s: &mut Session, p: &Value) -> Result<Value> {
view_params(p, "view.fit", &[])?;
Ok(json!({}))
}
fn home(_s: &mut Session, p: &Value) -> Result<Value> {
view_params(p, "view.home", &[])?;
Ok(json!({}))
}
fn tool(_s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "view.tool";
view_params(p, cmd, &["tool"])?;
let tool = p
.get("tool")
.and_then(Value::as_str)
.filter(|v| matches!(*v, "orbit" | "pan" | "zoom" | "none"))
.ok_or_else(|| bad(cmd, "tool must be orbit, pan, zoom or none"))?;
Ok(json!({"tool": tool}))
}
fn visual_style(_s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "view.visual_style";
view_params(p, cmd, &["style"])?;
let style = p.get("style").and_then(Value::as_str);
let index = match style {
Some("shaded_with_edges") => 0,
Some("shaded") => 1,
Some("wireframe") => 2,
Some("shaded_with_hidden_edges") => 3,
_ => return Err(bad(cmd, "style must be shaded_with_edges, shaded, wireframe or shaded_with_hidden_edges")),
};
Ok(json!({"style": style, "index": index}))
}
fn view_toggle(p: &Value, cmd: &str, key: &str) -> Result<Value> {
view_params(p, cmd, &[key])?;
match p.get(key) {
Some(value) => value.as_bool().map(|on| json!({key: on})).ok_or_else(|| bad(cmd, format!("{key} must be a boolean"))),
None => Ok(json!({})),
}
}
fn spin(_s: &mut Session, p: &Value) -> Result<Value> {
view_toggle(p, "view.spin", "on")
}
fn grid(_s: &mut Session, p: &Value) -> Result<Value> {
view_toggle(p, "view.grid", "on")
}
fn ground_shadow(_s: &mut Session, p: &Value) -> Result<Value> {
view_toggle(p, "view.ground_shadow", "on")
}
fn navigation_bar(_s: &mut Session, p: &Value) -> Result<Value> {
view_toggle(p, "view.navigation_bar", "visible")
}
fn layout(_s: &mut Session, p: &Value) -> Result<Value> {
let mode = p
.get("mode")
@@ -291,6 +372,48 @@ mod tests {
use solidcraft_geom::Vec3;
use solidcraft_render::UpAxis;
#[test]
fn view_bar_commands_validate_json_without_editing_design_or_history() {
let mut s = Session::default();
let before = s.doc.to_json();
for id in ["view.fit", "view.home"] {
assert_eq!(s.execute(id, &json!({})).unwrap(), json!({}));
assert_eq!(s.execute(id, &Value::Null).unwrap(), json!({}));
assert!(s.execute(id, &json!({"unexpected": true})).is_err());
}
for tool in ["orbit", "pan", "zoom", "none"] {
assert_eq!(s.execute("view.tool", &json!({"tool":tool})).unwrap(), json!({"tool":tool}));
}
for (index, style) in ["shaded_with_edges", "shaded", "wireframe", "shaded_with_hidden_edges"].into_iter().enumerate() {
assert_eq!(s.execute("view.visual_style", &json!({"style":style})).unwrap(), json!({"style":style,"index":index}));
}
for (id, key) in [("view.tool", "tool"), ("view.visual_style", "style")] {
assert!(s.execute(id, &json!({})).is_err());
assert!(s.execute(id, &Value::Null).is_err());
for value in [json!("unknown"), json!(""), json!(0), json!(true), Value::Null, json!([]), json!({})] {
assert!(s.execute(id, &json!({key:value})).is_err(), "{id}: {value}");
}
}
for (id, key) in [("view.spin", "on"), ("view.grid", "on"), ("view.ground_shadow", "on"), ("view.navigation_bar", "visible")] {
assert_eq!(s.execute(id, &json!({})).unwrap(), json!({}));
assert_eq!(s.execute(id, &Value::Null).unwrap(), json!({}));
for on in [false, true] {
assert_eq!(s.execute(id, &json!({key:on})).unwrap(), json!({key:on}));
}
for value in [Value::Null, json!(0), json!("true"), json!([]), json!({})] {
assert!(s.execute(id, &json!({key:value})).is_err(), "{id}: {value}");
}
}
for id in ["view.fit", "view.home", "view.tool", "view.spin", "view.visual_style", "view.grid", "view.ground_shadow", "view.navigation_bar"] {
for params in [json!(true), json!(1), json!("orbit"), json!([]), json!({"unexpected":true})] {
assert!(s.execute(id, &params).is_err(), "{id}: {params}");
}
}
assert_eq!(s.doc.to_json(), before);
assert!(s.undo.is_empty());
assert!(s.camera.is_none());
}
#[test]
fn look_at_planes_uses_the_first_supported_selection_and_flips_on_repeat() {
let original = Camera { yaw: 0.3, pitch: 0.4, roll: 0.7, fov: 0.6, distance: 123.0, ..Default::default() };