Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Paint: keep stylus pressure on quick taps (#1967)

Nigel Ihza Firdausycc4a13b58d09
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 1b4ccbbca..a5b7ebec0 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -2626,6 +2626,12 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
crate::magnetic_lasso_ui::flush(app);
}
if buttons.stopped {
+ // The live painting press already created the stroke and this release commits it.
+ // egui may also report clicked on the same release; replaying that click paints a
+ // second full-pressure dab over the real (possibly light-pressure) pen dab.
+ if press_stroke {
+ buttons.clicked = false;
+ }
let p = response.interact_pointer_pos().map(|p| xf.to_doc(p)).or_else(|| app.drag.as_ref().and_then(|d| d.points.last().map(|q| [q[0], q[1]])));
if let Some(d) = p {
tool_event(app, ToolEvent::Up { x: d[0], y: d[1] }, mods);
@@ -2669,7 +2675,9 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
if tool == Tool::Move && app.ui.transform.is_none() {
begin_transform_controls_at(app, &ctx, &xf, p);
}
- tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: 1.0 }, click_mods);
+ // If this tool was not handled on pointer-down, fall back to a
+ // click-sized gesture while preserving pen pressure (mouse stays at 1).
+ tool_event(app, ToolEvent::Down { x: d[0], y: d[1], pressure: app.stylus.pressure() }, click_mods);
tool_event(app, ToolEvent::Up { x: d[0], y: d[1] }, click_mods);
}
}
@@ -4054,6 +4062,19 @@ pub fn paint_target(app: &PhotocraftApp) -> serde_json::Value {
json!(if (app.ui.mask_target || viewing) && has_mask { "mask" } else { "pixels" })
}
+#[cfg(test)]
+mod tap_pressure_tests {
+ #[test]
+ fn fallback_click_pressure_matches_pen_or_mouse() {
+ let mut s = crate::stylus::Stylus::default();
+ assert_eq!(s.pressure(), 1.0, "mouse clicks paint at full pressure");
+ s.feed.set(Some(crate::stylus::PenSample { pressure: 0.18, ..Default::default() }));
+ assert_eq!(s.pressure(), 0.18, "a tap without a canvas drag still has pen pressure");
+ s.use_pressure = false;
+ assert_eq!(s.pressure(), 1.0, "Use Tablet Pressure off stays full pressure");
+ }
+}
+
#[cfg(test)]
mod tabs_tests;