Wake Bristle tip preview when a stationary pen turns

This commit is contained in:
dotCooCoo
2026-11-17 17:44:15 -08:00
parent 375b2a23e1
commit e3672e958b
4 changed files with 63 additions and 6 deletions
+2
View File
@@ -16,6 +16,8 @@ pub(super) struct HoverContext {
pub(super) center: Point,
pub(super) zoom: f64,
pub(super) rotation: f64,
/// A pen turning in place changes a Bristle tip even when its pointer stays still.
pub(super) pen: Option<PenPose>,
/// The active document's id and revision (which moves with every edit, undo and selection
/// change).
pub(super) doc: Option<(u64, u64)>,
+13 -4
View File
@@ -12,6 +12,9 @@ use super::{Color32, Drag, Mods, Point, PointerEvent, PointerKind, Pos2, Shape,
/// Dispatch canvas pointer, keyboard and drop events through the active document view.
pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Response, rect: egui::Rect) {
if let Some(feed) = app.services.stylus.as_ref() {
feed.bind_repaint(ui.ctx());
}
// A modal dialog takes the canvas's presses, finger taps, long touches and view navigation, from the frame it opens.
let modal = crate::panel_docking::modal_open(app, ui.ctx());
let (pointer, m, space, hover) = wheel_view(app, ui, resp, rect, modal);
@@ -53,7 +56,7 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo
});
// What a hover means besides where the pointer is on screen: the tool, the canvas, the view
// (scrolled, zoomed or turned by the wheel, a gesture, keys or a command) and the document and
// selection as they are (another tab, an edit, an undo). Under a still pointer, a tool switched
// selection as they are (another tab, an edit, an undo), and the pen's orientation. Under a still pointer, a tool switched
// to is hovered at once, and when the rest changes, a tool that asks for it
// ([`Session::tool_wants_rehover`]) is hovered again where the pointer is.
let context = HoverContext {
@@ -62,13 +65,14 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo
center: v.center,
zoom: v.zoom,
rotation: v.rotation,
pen: pen_pose(app, &xf),
doc: app.session.active().map(|st| (st.uid, st.revision)),
};
let (tool_switched, context_changed) = ui.data_mut(|d| {
let (tool_switched, context_changed, pen_changed) = ui.data_mut(|d| {
let id = egui::Id::unique("canvas-hover-context");
let prev = d.get_temp::<HoverContext>(id);
d.insert_temp(id, context);
prev.map_or((false, false), |prev| (prev.tool != context.tool, prev != context))
prev.map_or((false, false, false), |prev| (prev.tool != context.tool, prev != context, prev.pen != context.pen))
});
if app.ui.screen_mode == 3 && matches!(drag, None | Some(Drag::Present | Drag::Gesture)) {
@@ -137,7 +141,12 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo
} else if let Some(p) = hover_tool
// A press on an artboard's name starts no drag: the moves while it is held are not hover.
&& !(pointer.primary_down() && ui.data(|d| d.get_temp::<(usize, bool)>(name_press_id()).is_some()))
&& (pointer.is_moving() || mods_changed || !was_over || tool_switched || (context_changed && app.session.tool_wants_rehover()))
&& (pointer.is_moving()
|| mods_changed
|| !was_over
|| tool_switched
|| (context_changed && app.session.tool_wants_rehover())
|| (pen_changed && context.tool == "paintbrush"))
{
let ev = PointerEvent {
kind: PointerKind::Move,
+12 -1
View File
@@ -11005,7 +11005,7 @@ fn the_tip_brings_back_the_perspective_grid_tool_without_showing_the_grid() {
}
/// The native pen feed's barrel rotation reaches the Paintbrush during canvas hover, before any
/// stroke begins.
/// stroke begins. Turning the pen or replacing it with a mouse updates a still pointer too.
#[test]
fn bristle_pen_tip_reaches_the_canvas_before_a_press() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
@@ -11031,6 +11031,17 @@ fn bristle_pen_tip_reaches_the_canvas_before_a_press() {
frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]);
let horizontal = tip(&mut app).expect("hovering pen shows its brush tip");
assert!(horizontal.width() > horizontal.height() * 2.0, "{horizontal:?}");
feed.set_sample(Some(crate::stylus::PenSample {
pressure: 0.0,
pose: PenPose { rotation: Some(90.0), ..Default::default() },
..Default::default()
}));
frame(&mut app, &ctx, vec![]);
let vertical = tip(&mut app).expect("turning a still pen turns its tip");
assert!(vertical.height() > vertical.width() * 2.0, "{vertical:?}");
feed.set(None);
frame(&mut app, &ctx, vec![]);
assert!(tip(&mut app).is_none(), "a mouse clears a still pen's tip");
}
/// #764: the pen's tilt and barrel rotation in the host's feed reach the Paintbrush's points,
+36 -1
View File
@@ -24,7 +24,7 @@
//! the tip selects the tool used before.
use std::collections::VecDeque;
use std::sync::{Arc, Condvar, Mutex, MutexGuard};
use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock};
use std::time::Duration;
pub use vectorcraft_tools::PenPose;
@@ -82,6 +82,8 @@ impl Pen {
#[derive(Default)]
struct Shared {
pen: Mutex<Pen>,
/// Wake the canvas for a pen that turns without a new window pointer event.
repaint: OnceLock<egui::Context>,
/// Signaled on every [`StylusFeed::set_sample`] ([`StylusFeed::set`] included).
set: Condvar,
/// Written on the UI thread itself ([`StylusFeed::on_ui_thread`]): a press never waits.
@@ -104,6 +106,10 @@ impl StylusFeed {
self.0.pen.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}
pub(crate) fn bind_repaint(&self, ctx: &egui::Context) {
self.0.repaint.get_or_init(|| ctx.clone());
}
/// A pen turned to its tip moved with `pressure`, or `None`: another pointer moved
/// ([`Self::set_sample`]).
pub fn set(&self, pressure: Option<f32>) {
@@ -119,6 +125,7 @@ impl StylusFeed {
let pressure = sample.map(|s| if s.pressure.is_finite() { s.pressure.clamp(0.0, 1.0) } else { 1.0 });
let eraser = sample.is_some_and(|s| s.eraser);
let mut pen = self.lock();
let before_pose = pen.pose;
if eraser != pen.eraser {
*pen = Pen::default();
}
@@ -138,8 +145,12 @@ impl StylusFeed {
}
}
pen.latest = pressure;
let pose_changed = pen.pose != before_pose;
drop(pen);
self.0.set.notify_all();
if pose_changed && let Some(ctx) = self.0.repaint.get() {
ctx.request_repaint();
}
}
/// Like [`Self::set_sample`], for a pen sample that comes with one pointer move of the window,
@@ -282,6 +293,30 @@ pub(crate) fn sync_eraser_tool(app: &mut VectorcraftApp) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
#[test]
fn changing_a_still_pens_pose_wakes_the_canvas() {
let feed = StylusFeed::default();
let ctx = egui::Context::default();
let repaints = Arc::new(AtomicUsize::new(0));
let count = repaints.clone();
ctx.set_repaint_observer(move |_| {
count.fetch_add(1, Ordering::Relaxed);
});
feed.bind_repaint(&ctx);
let pose = PenPose { rotation: Some(0.0), ..Default::default() };
let sample = PenSample { pressure: 0.0, pose, ..Default::default() };
let writer = feed.clone();
std::thread::spawn(move || writer.set_sample(Some(sample))).join().unwrap();
assert_eq!(repaints.load(Ordering::Relaxed), 1);
feed.set_sample(Some(sample));
assert_eq!(repaints.load(Ordering::Relaxed), 1, "an unchanged pose needs no new frame");
feed.set_sample(Some(PenSample { pose: PenPose { rotation: Some(90.0), ..pose }, ..sample }));
assert_eq!(repaints.load(Ordering::Relaxed), 2);
feed.set_sample(None);
assert_eq!(repaints.load(Ordering::Relaxed), 3, "mouse input clears the tip");
}
#[test]
fn the_feed_keeps_the_last_pressure_within_0_to_1() {