b671265faa
* Viewport: show the running tool's icon beside the pointer
While an interactive tool or a command's dialog runs, its toolbar icon
rides above and right of the pointer in the viewport, so the next click's
command is visible without looking up at the ribbon. It sits clear of the
system arrow and of the sketch tools' coordinate readout below the
pointer, moves left at the right edge and below the readout at the top.
It shows only where a click reaches the model: not over panels, dialogs,
menus or the navigation bar, and not while the camera moves. The icon is
the command's own (the one the ribbon and the dialog header show), so
there is no new artwork. On by default; "Show tool at cursor" in View
settings (`ui.set {"toolCursor": false}`) turns it off.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Viewport: the tool at the cursor is off by default
As asked in review: the badge starts off, and "Show tool at cursor" in
View settings (or `ui.set {"toolCursor": true}`) turns it on. The test
checks the default and turns it on through the View settings checkbox.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
173 lines
8.3 KiB
Rust
173 lines
8.3 KiB
Rust
//! The tool at the cursor: while an interactive tool or a command's dialog is running, its
|
|
//! toolbar icon rides above and right of the pointer in the viewport (clear of the sketch tools'
|
|
//! coordinate readout below it), so the user sees which command the
|
|
//! next click goes to without looking up at the ribbon. It shows only while the pointer is over
|
|
//! the model itself (not over a panel, a dialog or a menu) and hides while the camera moves.
|
|
//! Off by default; View settings ("Show tool at cursor") or `ui.set {"toolCursor": true}` turn
|
|
//! it on.
|
|
//! It is UI only: drawing it runs no command and changes nothing in the design.
|
|
|
|
use egui::{Pos2, Rect, Stroke, Vec2, pos2, vec2};
|
|
|
|
use crate::SolveApp;
|
|
use crate::theme::Tokens;
|
|
|
|
/// The badge's side and the icon inside it, in points.
|
|
const BADGE: f32 = 26.0;
|
|
const ICON: f32 = 20.0;
|
|
/// The badge sits above and right of the pointer's hot spot: the system arrow hangs below and
|
|
/// right of it, and so does the sketch tools' coordinate readout (`tools::preview`, drawn from
|
|
/// [`READOUT`] below right). From the hot spot to the badge's bottom-left corner:
|
|
const OFFSET: Vec2 = vec2(12.0, -8.0);
|
|
/// Where the sketch tools' coordinate readout starts, down and right of the hot spot.
|
|
const READOUT: f32 = 10.0;
|
|
/// The gap to the hot spot when the badge flips to the left of the pointer.
|
|
const FLIPPED_GAP: f32 = 6.0;
|
|
|
|
/// The command whose tool or dialog is running: the one a click in the viewport goes to.
|
|
pub(crate) fn command(app: &SolveApp) -> Option<&'static str> {
|
|
match (&app.tool, &app.dialog) {
|
|
(Some(tool), _) => Some(tool.cmd),
|
|
(None, Some(dialog)) => dialog.command(),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// The icon the badge shows: None when it is turned off, nothing runs, or the command has no icon.
|
|
pub(crate) fn icon(app: &SolveApp) -> Option<&'static str> {
|
|
if !app.ui.tool_cursor {
|
|
return None;
|
|
}
|
|
command(app).and_then(solidcraft_engine::find_command).map(|spec| spec.icon).filter(|icon| !icon.is_empty())
|
|
}
|
|
|
|
/// The badge for a pointer at `at`: above and right of it, so it never covers the coordinate
|
|
/// readout below. Near the right edge of `bounds` it moves to the left of the pointer; with no
|
|
/// room above, it drops below the readout's line (`readout_height` high), never onto it.
|
|
fn badge_rect(at: Pos2, bounds: Rect, readout_height: f32) -> Rect {
|
|
let mut min = pos2(at.x + OFFSET.x, at.y + OFFSET.y - BADGE);
|
|
if min.x + BADGE > bounds.right() {
|
|
min.x = at.x - FLIPPED_GAP - BADGE;
|
|
}
|
|
if min.y < bounds.top() {
|
|
min.y = at.y + READOUT + readout_height + FLIPPED_GAP;
|
|
}
|
|
Rect::from_min_size(min, vec2(BADGE, BADGE))
|
|
}
|
|
|
|
/// Draw the badge for a pointer at `at` (None: the pointer is not over the model).
|
|
pub(crate) fn show(app: &SolveApp, painter: &egui::Painter, at: Option<Pos2>) {
|
|
let (Some(at), Some(icon)) = (at, icon(app)) else { return };
|
|
let t = Tokens::get();
|
|
// The readout's line height, as `tools::preview` lays it out.
|
|
let readout = painter.layout_no_wrap("0".into(), egui::FontId::proportional(t.font_small), t.text_dim).size().y;
|
|
let r = badge_rect(at, painter.clip_rect(), readout);
|
|
painter.rect(r, t.radius_sm, t.panel.gamma_multiply(0.92), Stroke::new(1.0, t.border), egui::StrokeKind::Inside);
|
|
crate::icons::paint_icon(painter, Rect::from_center_size(r.center(), vec2(ICON, ICON)), icon, 1.0);
|
|
crate::scenario::publish_rect("viewport:tool_cursor", r);
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::scenario::{Harness, rect_of};
|
|
use serde_json::json;
|
|
|
|
/// Rest the pointer on the middle of the viewport; the badge drawn there, if any.
|
|
fn hover_middle(h: &mut Harness) -> (Pos2, Option<Rect>) {
|
|
let c = h.app.viewport.rect.map(|r| r.center()).unwrap();
|
|
h.step(&json!({"move": [c.x, c.y]})).unwrap();
|
|
h.frames(2);
|
|
(c, rect_of("viewport:tool_cursor"))
|
|
}
|
|
|
|
fn icon_of(id: &str) -> &'static str {
|
|
solidcraft_engine::find_command(id).unwrap().icon
|
|
}
|
|
|
|
#[test]
|
|
fn badge_shows_the_running_tool_beside_the_pointer() {
|
|
let mut h = Harness::new();
|
|
h.settle();
|
|
// Off by default: a running tool shows no badge until View settings turn it on.
|
|
assert!(!h.app.ui.tool_cursor, "the badge is on by default");
|
|
h.step(&json!({"start": "sketch.create"})).unwrap();
|
|
let (_, badge) = hover_middle(&mut h);
|
|
assert!(badge.is_none(), "off by default, yet a badge is drawn at {badge:?}");
|
|
h.step(&json!({"key": "Escape"})).unwrap();
|
|
h.settle();
|
|
// Turned on with the checkbox in the navigation bar's View settings.
|
|
h.step(&json!({"click": {"handle": "nav:view"}})).unwrap();
|
|
h.frames(2);
|
|
h.step(&json!({"click": {"handle": "view:tool_cursor"}})).unwrap();
|
|
h.step(&json!({"click": {"handle": "nav:view"}})).unwrap();
|
|
h.frames(2);
|
|
assert!(h.app.ui.tool_cursor, "the View settings checkbox did not turn the badge on");
|
|
let (_, badge) = hover_middle(&mut h);
|
|
assert!(badge.is_none(), "no tool runs, yet a badge is drawn at {badge:?}");
|
|
|
|
// A dialog: Create Sketch waits for a plane.
|
|
h.step(&json!({"start": "sketch.create"})).unwrap();
|
|
let (at, badge) = hover_middle(&mut h);
|
|
assert_eq!(icon(&h.app), Some(icon_of("sketch.create")));
|
|
let badge = badge.unwrap();
|
|
assert_eq!(badge.left_bottom(), at + OFFSET, "the badge sits above and right of the pointer");
|
|
|
|
// An interactive sketch tool.
|
|
h.step(&json!({"click": {"plane": "XY"}})).unwrap();
|
|
h.settle();
|
|
h.step(&json!({"start": "sketch.circle.center"})).unwrap();
|
|
let (at, badge) = hover_middle(&mut h);
|
|
assert_eq!(icon(&h.app), Some(icon_of("sketch.circle.center")));
|
|
let badge = badge.unwrap();
|
|
// The coordinate readout starts READOUT below and right of the pointer: no overlap.
|
|
assert!(badge.bottom() < at.y + READOUT, "the badge {badge:?} covers the readout under {at:?}");
|
|
|
|
// Turned off in View settings.
|
|
h.app.ui.tool_cursor = false;
|
|
let (_, badge) = hover_middle(&mut h);
|
|
assert!(badge.is_none(), "turned off, yet a badge is drawn at {badge:?}");
|
|
h.app.ui.tool_cursor = true;
|
|
|
|
// Over the window's top edge, outside the viewport: nothing.
|
|
h.step(&json!({"move": [4.0, 4.0]})).unwrap();
|
|
h.frames(2);
|
|
assert!(rect_of("viewport:tool_cursor").is_none(), "a badge is drawn outside the viewport");
|
|
|
|
// Escape ends the tool.
|
|
h.step(&json!({"key": "Escape"})).unwrap();
|
|
h.settle();
|
|
assert!(h.app.tool.is_none() && h.app.dialog.is_none(), "Escape left {:?} running", command(&h.app));
|
|
let (_, badge) = hover_middle(&mut h);
|
|
assert!(badge.is_none(), "the tool ended, yet a badge is drawn at {badge:?}");
|
|
}
|
|
|
|
/// Wherever the pointer is, the badge stays inside the viewport and off the coordinate
|
|
/// readout (the rectangle from READOUT below right of the pointer, one line high).
|
|
#[test]
|
|
fn badge_stays_inside_the_viewport_and_off_the_readout() {
|
|
let bounds = Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0));
|
|
let line = 14.0;
|
|
let middle = Pos2::new(400.0, 300.0);
|
|
assert_eq!(badge_rect(middle, bounds, line).left_bottom(), middle + OFFSET);
|
|
for at in [middle, pos2(795.0, 300.0), pos2(400.0, 5.0), pos2(795.0, 5.0), pos2(5.0, 595.0), pos2(795.0, 595.0)] {
|
|
let badge = badge_rect(at, bounds, line);
|
|
assert!(bounds.contains_rect(badge), "at {at:?}: {badge:?} leaves {bounds:?}");
|
|
let readout = Rect::from_min_size(at + vec2(READOUT, READOUT), vec2(120.0, line));
|
|
assert!(!badge.intersects(readout), "at {at:?}: {badge:?} covers the readout {readout:?}");
|
|
assert!(!badge.contains(at), "at {at:?}: {badge:?} covers the hot spot");
|
|
}
|
|
}
|
|
|
|
/// Every interactive tool's command has an icon to show.
|
|
#[test]
|
|
fn every_interactive_tool_has_a_badge_icon() {
|
|
let missing: Vec<&str> = solidcraft_engine::command_specs()
|
|
.iter()
|
|
.filter_map(|spec| crate::tools::Tool::for_command(spec.id).map(|tool| tool.cmd))
|
|
.filter(|cmd| solidcraft_engine::find_command(cmd).is_none_or(|spec| spec.icon.is_empty()))
|
|
.collect();
|
|
assert!(missing.is_empty(), "tools without a badge icon: {missing:?}");
|
|
}
|
|
}
|