View angles: an optional readout under the orientation gizmo and a command to set them (#6032) (#6249)

* claim: #6032

* View angles: an optional readout under the orientation gizmo and a command to set them (#6032)

View > View angles (also Preferences > Graphics, or view.angle_readout) shows the
view's pitch, yaw and roll in degrees and Ortho/Persp under the axis view or cube,
updated live; off by default. Clicking the readout opens a small card to type
exact angles, which runs view.apply.

The angles are the camera's own (Camera::view_angles / with_view_angles), about
the up axis: yaw is the heading of the line of sight (Front 0, Left 90, Right -90,
Back 180), pitch the eye's elevation (Top 90, Bottom -90), and a positive roll
turns the model clockwise. view.apply documents this convention and now refuses a
pitch past +/-pi/2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HiroKenEz
2026-10-28 14:49:47 +00:00
committed by GitHub
parent 31d6873f7d
commit 4fa051c9fc
19 changed files with 666 additions and 4 deletions
+42 -1
View File
@@ -39,7 +39,7 @@ pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("view.delete", "Delete Named View", view_delete).icon("delete").params("view: name"),
CommandSpec::new("view.list", "List Named Views", view_list).icon("list").noundo().doc_only(),
CommandSpec::new("view.apply", "Restore Named View", view_apply).icon("camera").noundo().params(
"view?: front|back|top|bottom|left|right|iso|home (frames the model) | a named view's name; or camera?: {target: [x,y,z], yaw, pitch, distance, fov?, roll?} (radians, mm; roll defaults to 0, must be finite and is clamped to ±π; up is refused: the up axis comes from the design settings) → camera. The viewport (and the MCP screenshot) moves there. View state: not saved, not an undo step",
"view?: front|back|top|bottom|left|right|iso|home (frames the model) | a named view's name; or camera?: {target: [x,y,z], yaw, pitch, distance, fov?, roll?} (radians, mm, about the up axis; yaw: heading of the line of sight, 0 along +Y (Front), π/2 along +X (Left), π (Back), −π/2 (Right); pitch: the eye's elevation, within ±π/2, π/2 = Top; roll defaults to 0, turns the picture clockwise, must be finite and is clamped to ±π; the View angles readout shows these in degrees; up is refused: the up axis comes from the design settings) → camera. The viewport (and the MCP screenshot) moves there. View state: not saved, not an undo step",
),
CommandSpec::new("browser.order", "Order Browser Items", order).icon("reorder").params("folder, component?, order: [keys] (the folder's items in display order)"),
CommandSpec::new("browser.visibility", "Show/Hide", visibility).icon("eye").params(
@@ -751,6 +751,10 @@ fn view_apply(s: &mut Session, p: &Value) -> Result<Value> {
None => 0.0,
};
let v: solidcraft_doc::NamedView = serde_json::from_value(c.clone()).map_err(|e| bad(cmd, format!("bad camera: {e}")))?;
// Past a pole the same view has other angles; the orbit would snap it back (#6032).
if !(v.pitch.abs() <= std::f64::consts::FRAC_PI_2 + 1e-9) {
return Err(bad(cmd, "camera `pitch` must be within ±π/2 radians (±90°)"));
}
crate::render::Camera { roll: roll.clamp(-std::f64::consts::PI, std::f64::consts::PI), ..camera_of(&v) }
}
// Standard view names come first, as in the browser's Named Views folder.
@@ -1581,6 +1585,43 @@ mod tests {
}
}
/// The View angles popup (#6032) sets the view through `view.apply`: the readout's degrees are
/// the camera's radians, named views read round numbers, and hostile angles never leave a
/// camera past a pole or a NaN behind.
#[test]
fn applying_a_camera_by_view_angles_reads_back_and_refuses_hostile_pitch() {
use crate::render::ViewAngles;
let mut s = Session::default();
for (view, (yaw, pitch)) in
[("front", (0.0, 0.0)), ("left", (90.0, 0.0)), ("right", (-90.0, 0.0)), ("back", (180.0, 0.0)), ("top", (0.0, 90.0))]
{
run(&mut s, "view.apply", json!({"view": view}));
let a = s.camera.expect("a camera for the standard view").view_angles();
assert!((a.yaw - yaw).abs() < 1e-3 && (a.pitch - pitch).abs() < 1e-3 && a.roll == 0.0, "{view}: {a:?}");
}
let typed = ViewAngles { yaw: -30.0, pitch: 15.0, roll: 5.0 };
let cam = crate::render::Camera::default().with_view_angles(typed).expect("finite angles");
let p = json!({"camera": {"target": [0, 0, 0], "yaw": cam.yaw, "pitch": cam.pitch, "roll": cam.roll, "distance": 80}});
run(&mut s, "view.apply", p);
let a = s.camera.expect("the typed camera").view_angles();
assert!((a.yaw - typed.yaw).abs() < 1e-9 && (a.pitch - typed.pitch).abs() < 1e-9 && (a.roll - typed.roll).abs() < 1e-9, "{a:?}");
// Straight down is allowed (90° in radians may round a hair past π/2).
run(&mut s, "view.apply", json!({"camera": {"target": [0, 0, 0], "yaw": 0, "pitch": 90.0_f64.to_radians(), "distance": 80}}));
// Many turns of yaw are the same heading; the readout brings it into range.
run(&mut s, "view.apply", json!({"camera": {"target": [0, 0, 0], "yaw": 1e300, "pitch": 0, "distance": 80}}));
let a = s.camera.expect("a camera").view_angles();
assert!(a.yaw.is_finite() && (-180.0..=180.0).contains(&a.yaw), "{a:?}");
let before = serde_json::to_value(s.camera).unwrap();
for pitch in [json!(2.0), json!(-1.6), json!(1e300), json!(-f64::MAX), json!(f64::NAN), json!("90"), json!(null)] {
let err = s.execute("view.apply", &json!({"camera": {"target": [0, 0, 0], "yaw": 0, "pitch": pitch, "distance": 80}})).unwrap_err();
assert!(err.to_string().contains("camera"), "{pitch}: {err}");
assert_eq!(serde_json::to_value(s.camera).unwrap(), before, "a refused camera leaves the view alone");
}
for yaw in [json!(f64::NAN), json!(f64::INFINITY), json!("0")] {
assert!(s.execute("view.apply", &json!({"camera": {"target": [0, 0, 0], "yaw": yaw, "pitch": 0, "distance": 80}})).is_err(), "{yaw}");
}
}
#[test]
fn named_views_are_saved_renamed_and_deleted() {
let mut s = Session::default();
+26
View File
@@ -79,6 +79,8 @@ CommandSpec::new("view.ambient_occlusion", "Ambient Occlusion", ambient_occlusio
.icon("settings").noundo().doc_only().params("enabled: bool → enabled; apply soft contact shading in the GPU viewport"),
CommandSpec::new("view.perspective", "Perspective view", perspective)
.icon("perspective").noundo().doc_only().params("enabled: bool → enabled; draw the view in perspective, or orthographic when false; a sketch being edited is always shown orthographic"),
CommandSpec::new("view.angle_readout", "View Angles", crate::checked!("view.angle_readout", angle_readout))
.icon("camera").noundo().doc_only().contract(&ANGLE_READOUT_CONTRACT).output(crate::schema::output_schema_for::<crate::schema::view_navigation::AngleReadoutResult>).params("enabled: bool → enabled; show the view's pitch, yaw and roll (degrees, as view.apply's camera in radians) and Ortho or Persp under the orientation gizmo; display only, never opens a dialog"),
CommandSpec::new("view.navigate", "Navigate View", navigate)
.icon("orbit")
.noundo()
@@ -871,9 +873,33 @@ fn perspective(_s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({"enabled": enabled}))
}
const ANGLE_READOUT_CONTRACT: crate::schema::TypedContract<crate::schema::view_navigation::AngleReadoutParams> = crate::schema::TypedContract::new();
/// The View angles readout under the orientation gizmo (#6032): a display setting the UI keeps.
fn angle_readout(_s: &mut Session, p: &Value) -> Result<Value> {
let enabled = p.get("enabled").and_then(Value::as_bool).ok_or_else(|| bad("view.angle_readout", "`enabled` must be a boolean"))?;
let reply = crate::schema::view_navigation::AngleReadoutResult { enabled, param_warnings: Vec::new() };
serde_json::to_value(reply).map_err(|e| crate::EngineError::Other(e.to_string()))
}
#[cfg(test)]
mod ambient_occlusion_tests {
use super::*;
#[test]
fn angle_readout_validates_display_setting_without_editing_design_or_history() {
let mut s = Session::default();
let before = s.doc.to_json();
for enabled in [true, false] {
assert_eq!(s.execute("view.angle_readout", &json!({"enabled": enabled})).unwrap(), json!({"enabled": enabled}));
}
for p in [json!({}), json!({"enabled": 1}), json!({"enabled": "true"}), json!({"enabled": null}), json!({"enabled": f64::NAN})] {
assert!(s.execute("view.angle_readout", &p).is_err(), "{p}");
}
assert_eq!(s.doc.to_json(), before);
assert!(s.undo.is_empty());
assert!(s.camera.is_none(), "a display setting leaves the view where it is");
}
#[test]
fn validates_display_setting_without_editing_design_or_history() {
let mut s = Session::default();
@@ -1,6 +1,7 @@
//! Render shell requests. The UI applies validated echoes without design edits.
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[serde(deny_unknown_fields)]
@@ -36,3 +37,37 @@ pub struct RenderBrowser {
/// Open the floating Browser card in a narrow Render window.
pub open: bool,
}
/// Show or hide the View angles readout under the orientation gizmo (#6032): the view's pitch, yaw
/// and roll in degrees (the angles of `view.apply`'s camera, which takes them in radians) and Ortho
/// or Persp for the projection. A display setting the app keeps: not saved with the design, not an
/// undo step, and it never opens a dialog.
#[derive(Debug, Deserialize, JsonSchema)]
#[schemars(deny_unknown_fields)]
pub struct AngleReadoutParams {
/// True shows the readout, false hides it.
#[serde(deserialize_with = "enabled_flag")]
pub enabled: bool,
}
/// A boolean, with the `view.angle_readout` handler's message for anything else.
fn enabled_flag<'de, D: serde::Deserializer<'de>>(d: D) -> Result<bool, D::Error> {
match Value::deserialize(d)? {
Value::Bool(b) => Ok(b),
_ => Err(serde::de::Error::custom("must be a boolean")),
}
}
/// What `view.angle_readout` returns.
#[derive(Debug, Serialize, JsonSchema)]
pub struct AngleReadoutResult {
/// Whether the readout is shown now.
pub enabled: bool,
/// In the warn parameter-check mode: what the call's parameters got wrong.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub param_warnings: Vec<String>,
}
#[cfg(test)]
#[path = "view_navigation_tests.rs"]
mod tests;
@@ -0,0 +1,35 @@
use serde_json::{Value, json};
use super::AngleReadoutParams;
use crate::Session;
use crate::schema::tests::{assert_keys, assert_strict_keeps_todays_errors, conforms};
use crate::schema::{ParamCheck, input_schema_for, output_schema_for};
#[test]
fn schemas_and_the_key_check_agree() {
assert_keys(input_schema_for::<AngleReadoutParams>(), &["enabled"]);
}
#[test]
fn strict_keeps_todays_errors() {
assert_strict_keeps_todays_errors(&[
("view.angle_readout", json!({"enabled": 1})),
("view.angle_readout", json!({"enabled": "true"})),
("view.angle_readout", json!({"enabled": null})),
]);
}
#[test]
fn angle_readout_replies_match_the_output_schema() {
let schema = output_schema_for::<super::AngleReadoutResult>();
let schema = schema.as_value();
let mut s = Session { param_check: ParamCheck::Warn, ..Session::default() };
for enabled in [true, false] {
let r = s.execute("view.angle_readout", &json!({"enabled": enabled})).unwrap_or_else(|e| panic!("{e}"));
assert!(conforms(schema, schema, &r), "{r} doesn't match {schema}");
assert_eq!(r.get("param_warnings"), None, "{r}");
assert_eq!(r["enabled"], Value::Bool(enabled));
}
assert!(!conforms(schema, schema, &json!({"enabled": "yes"})));
assert!(!conforms(schema, schema, &json!({})));
}