Fix spurious drawing when mouse gets focus inside touch/stylus events
This commit is contained in:
parent
a2cf104697
commit
0fdc73b9e4
|
|
@ -19,7 +19,7 @@ use crate::canvas::{Canvas, CanvasElement};
|
||||||
use crate::history::VersionedCanvas;
|
use crate::history::VersionedCanvas;
|
||||||
|
|
||||||
use druid::kurbo::BezPath;
|
use druid::kurbo::BezPath;
|
||||||
use druid::{Data, Env, Event, EventCtx, PaintCtx};
|
use druid::{Data, Env, Event, EventCtx, MouseButton, MouseEvent, PaintCtx};
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
pub enum CanvasToolState {
|
pub enum CanvasToolState {
|
||||||
|
|
@ -46,6 +46,11 @@ impl CanvasToolParams {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pressed(mouse_event: &MouseEvent) -> bool {
|
||||||
|
mouse_event.buttons.contains(MouseButton::Left)
|
||||||
|
|| mouse_event.button == MouseButton::Left
|
||||||
|
}
|
||||||
|
|
||||||
impl CanvasToolCtx {
|
impl CanvasToolCtx {
|
||||||
pub fn new(params: CanvasToolParams) -> Self {
|
pub fn new(params: CanvasToolParams) -> Self {
|
||||||
CanvasToolCtx {
|
CanvasToolCtx {
|
||||||
|
|
@ -75,10 +80,10 @@ impl CanvasToolCtx {
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
match (&mut self.state, event) {
|
match (&mut self.state, event) {
|
||||||
(CanvasToolState::Idle, Event::MouseDown(_)) => {
|
(CanvasToolState::Idle, Event::MouseDown(mouse_event)) if pressed(mouse_event) => {
|
||||||
self.state = CanvasToolState::Erasing;
|
self.state = CanvasToolState::Erasing;
|
||||||
}
|
}
|
||||||
(CanvasToolState::Erasing, Event::MouseMove(mouse_event)) => {
|
(CanvasToolState::Erasing, Event::MouseMove(mouse_event)) if pressed(mouse_event) => {
|
||||||
let eraser_rect = druid::Rect::from_center_size(mouse_event.pos, (5.0, 5.0));
|
let eraser_rect = druid::Rect::from_center_size(mouse_event.pos, (5.0, 5.0));
|
||||||
let old_elements = vcanvas.get().elements();
|
let old_elements = vcanvas.get().elements();
|
||||||
let mut new_elements = old_elements.clone();
|
let mut new_elements = old_elements.clone();
|
||||||
|
|
@ -97,7 +102,7 @@ impl CanvasToolCtx {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(CanvasToolState::Erasing, Event::MouseUp(_)) => {
|
(CanvasToolState::Erasing, Event::MouseUp(mouse_event)) if pressed(mouse_event) => {
|
||||||
self.state = CanvasToolState::Idle;
|
self.state = CanvasToolState::Idle;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
@ -112,7 +117,7 @@ impl CanvasToolCtx {
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
match (&mut self.state, event) {
|
match (&mut self.state, event) {
|
||||||
(CanvasToolState::Idle, Event::MouseDown(mouse_event)) => {
|
(CanvasToolState::Idle, Event::MouseDown(mouse_event)) if pressed(mouse_event) => {
|
||||||
let mut kurbo_path = BezPath::new();
|
let mut kurbo_path = BezPath::new();
|
||||||
kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y));
|
kurbo_path.move_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||||
if let CanvasToolParams::Pen { thickness, color } = &self.initial_params {
|
if let CanvasToolParams::Pen { thickness, color } = &self.initial_params {
|
||||||
|
|
@ -132,13 +137,13 @@ impl CanvasToolCtx {
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
Event::MouseMove(mouse_event),
|
Event::MouseMove(mouse_event),
|
||||||
) => {
|
) => if pressed(mouse_event) {
|
||||||
if let CanvasElement::Freehand { ref mut path, .. } = current_path {
|
if let CanvasElement::Freehand { ref mut path, .. } = current_path {
|
||||||
path.kurbo_path
|
path.kurbo_path
|
||||||
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(_)) => {
|
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(mouse_event)) if pressed(mouse_event) => {
|
||||||
vcanvas.update(move |canvas: &mut Canvas| {
|
vcanvas.update(move |canvas: &mut Canvas| {
|
||||||
let current_state = std::mem::replace(&mut self.state, CanvasToolState::Idle);
|
let current_state = std::mem::replace(&mut self.state, CanvasToolState::Idle);
|
||||||
if let CanvasToolState::DrawingFreehand { current_path, .. } = current_state {
|
if let CanvasToolState::DrawingFreehand { current_path, .. } = current_state {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue