mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-26 19:36:16 +00:00
Merge pull request #9373 from dotCooCoo/fix/precise-cursors-reach-tools
Cursors: Use Precise Cursors and Caps Lock hide the brush tip outline again
This commit is contained in:
@@ -150,6 +150,7 @@ impl VectorcraftApp {
|
||||
carry_target: None,
|
||||
last_drag: None,
|
||||
escaped_press: false,
|
||||
host_caps_lock: None,
|
||||
committed_preview: false,
|
||||
panes: Default::default(),
|
||||
windows: crate::doc_windows::Windows::new(known),
|
||||
|
||||
@@ -406,7 +406,33 @@ impl VectorcraftApp {
|
||||
shape_widgets: self.ui.view.shape_widgets,
|
||||
screen: self.screen_frame(),
|
||||
artboard: self.view().map_or(0, |v| v.artboard),
|
||||
..ViewInfo::default()
|
||||
precise: self.precise_cursors(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the drawing tools show precise cursors: General › Use Precise Cursors is on, or Caps Lock is
|
||||
/// (`Session::caps_lock`, which the canvas keeps at the host's state and `view.capsLock` sets). The canvas's pointer
|
||||
/// and the tools' brush tips both follow it.
|
||||
pub(crate) fn precise_cursors(&self) -> bool {
|
||||
self.session.prefs.use_precise_cursors || self.session.caps_lock
|
||||
}
|
||||
|
||||
/// Takes the host's Caps Lock state `on` when it changes: the session follows it until the next change, and a
|
||||
/// `view.capsLock` call between changes stays in place.
|
||||
pub(crate) fn follow_caps_lock(&mut self, on: bool) {
|
||||
if self.canvas.host_caps_lock == Some(on) {
|
||||
return;
|
||||
}
|
||||
let first_reading = self.canvas.host_caps_lock.is_none();
|
||||
self.canvas.host_caps_lock = Some(on);
|
||||
// A `view.capsLock` call that ran before the first reading stays in place; that reading is only the baseline.
|
||||
if first_reading && self.session.caps_lock {
|
||||
return;
|
||||
}
|
||||
if self.session.caps_lock != on
|
||||
&& let Err(err) = self.run("view.capsLock", serde_json::json!({ "on": on }))
|
||||
{
|
||||
log::error!("Could not update the Caps Lock state: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,6 +107,9 @@ pub struct CanvasCache {
|
||||
/// Escape cancelled the tool's press while the button is still down: its release still sends the tool its Up,
|
||||
/// which gives back a selection tool Cmd lent.
|
||||
pub escaped_press: bool,
|
||||
/// The host's Caps Lock state at the last canvas pass, which `VectorcraftApp::follow_caps_lock` compares with the
|
||||
/// next reading; `None` before the first pass.
|
||||
pub host_caps_lock: Option<bool>,
|
||||
/// A press on the canvas committed a panel field's running preview before the tool took the press, as the field's
|
||||
/// commit came first in production: the field's own commit later in the pass has nothing left to run.
|
||||
pub committed_preview: bool,
|
||||
|
||||
@@ -194,6 +194,11 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin
|
||||
};
|
||||
let pasteboard = app.session.documents().get(win.0).map(|st| artboards::pasteboard_color(app, st, app.outline_view()));
|
||||
let layers = move || bounds;
|
||||
let host_caps_lock = craft::ui::runtime::lock_keys::caps_lock(ui.ctx());
|
||||
app.follow_caps_lock(host_caps_lock);
|
||||
// The canvas reverses the state it is given while the host's Caps Lock is on: given this, it shows the precise
|
||||
// cursors the tools are told about.
|
||||
let precise = app.precise_cursors() != host_caps_lock;
|
||||
let prefs = &app.session.prefs;
|
||||
let mut canvas = Canvas::new("Document")
|
||||
.id(canvas_id(win))
|
||||
@@ -205,7 +210,7 @@ pub(crate) fn show_document(app: &mut VectorcraftApp, ui: &mut Ui, win: Win, kin
|
||||
.body_nudges(ARROWS)
|
||||
.rulers(x, y)
|
||||
.with_rulers(strips)
|
||||
.precise_cursors(prefs.use_precise_cursors);
|
||||
.precise_cursors(precise);
|
||||
if let Some(pasteboard) = pasteboard {
|
||||
canvas = canvas.pasteboard(pasteboard);
|
||||
}
|
||||
|
||||
@@ -67,3 +67,88 @@ fn bucket_modifier_only_hover_updates_the_visible_cursor() {
|
||||
assert_ne!(alt, plain, "Alt shows the Eyedropper's cursor");
|
||||
assert_eq!(shown(&mut app, egui::Modifiers::NONE), plain, "and letting go brings the bucket back");
|
||||
}
|
||||
|
||||
/// The Blob Brush's or the Eraser's tip outline at the pointer, as the canvas draws it this pass.
|
||||
fn shows_tip(app: &mut VectorcraftApp) -> bool {
|
||||
let view = app.view_info();
|
||||
app.session.overlays(view).iter().any(|o| matches!(o, vectorcraft_tools::Overlay::Path { .. }))
|
||||
}
|
||||
|
||||
/// Use Precise Cursors or Caps Lock, or both, gives the Blob Brush and the Eraser only the crosshair: the tool draws no
|
||||
/// tip outline around it. With both off, each shows its own pointer and the tip outline.
|
||||
#[test]
|
||||
fn precise_cursors_hide_the_brush_tip_outline() {
|
||||
use craft::ui::runtime::windows::WindowHost;
|
||||
let crosshair = Some(crate::cursors::tool_cursor(vectorcraft_tools::Cursor::Crosshair));
|
||||
for tool in ["blobBrush", "eraser"] {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.run("file.new", serde_json::json!({"width": 400, "height": 300})).unwrap();
|
||||
app.select_tool(tool);
|
||||
let ctx = crate::testing::ui_context();
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
let at = app.canvas_view().unwrap().to_screen(Point::new(200.0, 150.0));
|
||||
for (preference, caps_lock) in [(false, false), (true, false), (false, true), (true, true), (false, false)] {
|
||||
app.run("prefs.set", serde_json::json!({"key": "usePreciseCursors", "value": preference})).unwrap();
|
||||
WindowHost::of(&ctx).set_caps_lock(Some(caps_lock));
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]);
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at + egui::vec2(1.0, 0.0))]);
|
||||
let precise = preference || caps_lock;
|
||||
let case = format!("{tool}, Use Precise Cursors {preference}, Caps Lock {caps_lock}");
|
||||
assert_eq!(app.session.caps_lock, caps_lock, "{case}: the session takes the host's Caps Lock state");
|
||||
assert_eq!(app.view_info().precise, precise, "{case}");
|
||||
assert_eq!(shows_tip(&mut app), !precise, "{case}");
|
||||
assert_eq!(craft::ui::runtime::cursor::current(&ctx) == crosshair, precise, "{case}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `view.capsLock` sets the Caps Lock state the tools and the canvas's pointer follow until the host's state changes.
|
||||
#[test]
|
||||
fn the_caps_lock_command_gives_the_brushes_the_crosshair() {
|
||||
use craft::ui::runtime::windows::WindowHost;
|
||||
let crosshair = Some(crate::cursors::tool_cursor(vectorcraft_tools::Cursor::Crosshair));
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.run("file.new", serde_json::json!({"width": 400, "height": 300})).unwrap();
|
||||
app.select_tool("blobBrush");
|
||||
let ctx = crate::testing::ui_context();
|
||||
WindowHost::of(&ctx).set_caps_lock(Some(false));
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
let at = app.canvas_view().unwrap().to_screen(Point::new(200.0, 150.0));
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]);
|
||||
assert!(shows_tip(&mut app));
|
||||
app.run("view.capsLock", serde_json::json!({"on": true})).unwrap();
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at + egui::vec2(1.0, 0.0))]);
|
||||
assert!(app.session.caps_lock, "the host's unchanged state leaves the command's in place");
|
||||
assert!(!shows_tip(&mut app));
|
||||
assert_eq!(craft::ui::runtime::cursor::current(&ctx), crosshair);
|
||||
app.run("view.capsLock", serde_json::json!({"on": false})).unwrap();
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]);
|
||||
assert!(shows_tip(&mut app));
|
||||
assert_ne!(craft::ui::runtime::cursor::current(&ctx), crosshair);
|
||||
// With the host's Caps Lock on, the command turns it off for the tools and the canvas's pointer alike.
|
||||
WindowHost::of(&ctx).set_caps_lock(Some(true));
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at + egui::vec2(1.0, 0.0))]);
|
||||
assert!(!shows_tip(&mut app));
|
||||
assert_eq!(craft::ui::runtime::cursor::current(&ctx), crosshair);
|
||||
app.run("view.capsLock", serde_json::json!({"on": false})).unwrap();
|
||||
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]);
|
||||
assert!(!app.session.caps_lock);
|
||||
assert!(shows_tip(&mut app));
|
||||
assert_ne!(craft::ui::runtime::cursor::current(&ctx), crosshair);
|
||||
}
|
||||
|
||||
/// A `view.capsLock` call that runs before the first canvas pass stays in place when the host's Caps Lock is off.
|
||||
#[test]
|
||||
fn the_caps_lock_command_before_the_first_frame_stays_in_place() {
|
||||
use craft::ui::runtime::windows::WindowHost;
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.run("file.new", serde_json::json!({"width": 400, "height": 300})).unwrap();
|
||||
app.select_tool("blobBrush");
|
||||
app.run("view.capsLock", serde_json::json!({"on": true})).unwrap();
|
||||
let ctx = crate::testing::ui_context();
|
||||
WindowHost::of(&ctx).set_caps_lock(Some(false));
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
assert!(app.session.caps_lock, "the first host reading is only the baseline");
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
assert!(app.session.caps_lock);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user