Compare commits
1 Commits
cf6a96c1e7
...
efdd2bc88a
| Author | SHA1 | Date |
|---|---|---|
|
|
efdd2bc88a |
22
src/tool.rs
22
src/tool.rs
|
|
@ -68,25 +68,21 @@ impl CanvasToolCtx {
|
|||
|
||||
pub fn handle_event(
|
||||
&mut self,
|
||||
ctx: &EventCtx,
|
||||
mut ctx: &mut EventCtx,
|
||||
event: &Event,
|
||||
mut vcanvas: &mut VersionedCanvas,
|
||||
transform: Affine,
|
||||
env: &Env,
|
||||
) {
|
||||
match self.initial_params.tool_type() {
|
||||
CanvasToolType::Pen => {
|
||||
self.handle_pen_event(&ctx, &event, &mut vcanvas, transform, &env)
|
||||
}
|
||||
CanvasToolType::Eraser => {
|
||||
self.handle_erase_event(&ctx, &event, &mut vcanvas, transform, &env)
|
||||
}
|
||||
CanvasToolType::Pen => self.handle_pen_event(ctx, event, vcanvas, transform, env),
|
||||
CanvasToolType::Eraser => self.handle_erase_event(ctx, event, vcanvas, transform, env),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_erase_event(
|
||||
&mut self,
|
||||
_ctx: &EventCtx,
|
||||
ctx: &mut EventCtx,
|
||||
event: &Event,
|
||||
vcanvas: &mut VersionedCanvas,
|
||||
transform: Affine,
|
||||
|
|
@ -95,6 +91,7 @@ impl CanvasToolCtx {
|
|||
match (&mut self.state, event) {
|
||||
(CanvasToolState::Idle, Event::MouseDown(_)) => {
|
||||
self.state = CanvasToolState::Erasing;
|
||||
ctx.set_handled();
|
||||
}
|
||||
(CanvasToolState::Erasing, Event::MouseMove(mouse_event)) => {
|
||||
let eraser_rect =
|
||||
|
|
@ -108,16 +105,18 @@ impl CanvasToolCtx {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
true
|
||||
});
|
||||
if new_elements.len() != old_elements.len() {
|
||||
vcanvas.update(|canvas: &mut Canvas| {
|
||||
*canvas = Canvas::new_with_elements(new_elements);
|
||||
});
|
||||
}
|
||||
ctx.set_handled();
|
||||
}
|
||||
(CanvasToolState::Erasing, Event::MouseUp(_)) => {
|
||||
self.state = CanvasToolState::Idle;
|
||||
ctx.set_handled();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
@ -125,7 +124,7 @@ impl CanvasToolCtx {
|
|||
|
||||
pub fn handle_pen_event(
|
||||
&mut self,
|
||||
_ctx: &EventCtx,
|
||||
ctx: &mut EventCtx,
|
||||
event: &Event,
|
||||
vcanvas: &mut VersionedCanvas,
|
||||
transform: Affine,
|
||||
|
|
@ -145,6 +144,7 @@ impl CanvasToolCtx {
|
|||
},
|
||||
};
|
||||
}
|
||||
ctx.set_handled();
|
||||
}
|
||||
(
|
||||
CanvasToolState::DrawingFreehand {
|
||||
|
|
@ -157,6 +157,7 @@ impl CanvasToolCtx {
|
|||
path.kurbo_path
|
||||
.line_to((mouse_event.pos.x, mouse_event.pos.y));
|
||||
}
|
||||
ctx.set_handled();
|
||||
}
|
||||
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(_)) => {
|
||||
vcanvas.update(move |canvas: &mut Canvas| {
|
||||
|
|
@ -177,6 +178,7 @@ impl CanvasToolCtx {
|
|||
}
|
||||
}
|
||||
});
|
||||
ctx.set_handled();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,8 +24,7 @@ use crate::DocumentSnapshot;
|
|||
use druid::scroll_component::ScrollComponent;
|
||||
use druid::widget::prelude::*;
|
||||
use druid::widget::Viewport;
|
||||
use druid::{Affine, Color, Data, Env, Event};
|
||||
use druid::{Selector};
|
||||
use druid::{Affine, Color, Data, Env, Event, Selector};
|
||||
|
||||
pub const SCROLL: Selector<f64> = Selector::new("scroll_canvas");
|
||||
|
||||
|
|
@ -99,33 +98,47 @@ impl CanvasWidget {
|
|||
impl Widget<CanvasState> for CanvasWidget {
|
||||
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut CanvasState, env: &Env) {
|
||||
ctx.request_focus();
|
||||
let mut needs_update = false;
|
||||
self.scroll_component
|
||||
.event(&mut self.viewport, ctx, event, env);
|
||||
if !ctx.is_handled() {
|
||||
let mut scroll_amount = 0.0;
|
||||
match event {
|
||||
Event::Command(cmd) => {
|
||||
if let Some(value) = cmd.get(SCROLL) {
|
||||
self.viewport.rect.y0 += value;
|
||||
needs_update = true;
|
||||
scroll_amount = *value;
|
||||
}
|
||||
}
|
||||
Event::KeyDown(key_event) => {
|
||||
if key_event.code == druid::Code::ArrowDown {
|
||||
self.viewport.rect.y0 += 30.0;
|
||||
needs_update = true;
|
||||
scroll_amount = 30.0;
|
||||
} else if key_event.code == druid::Code::ArrowUp {
|
||||
self.viewport.rect.y0 = 0.0_f64.max(self.viewport.rect.y0 - 30.0);
|
||||
needs_update = true;
|
||||
scroll_amount = -30.0;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let transform = Affine::translate((self.viewport.rect.x0, self.viewport.rect.y0));
|
||||
data.tool_ctx
|
||||
.handle_event(ctx, event, &mut data.versioned_canvas, transform, env);
|
||||
let transform =
|
||||
Affine::translate((self.viewport.rect.x0, self.viewport.rect.y0));
|
||||
data.tool_ctx.handle_event(
|
||||
ctx,
|
||||
event,
|
||||
&mut data.versioned_canvas,
|
||||
transform,
|
||||
env,
|
||||
);
|
||||
}
|
||||
}
|
||||
if needs_update {
|
||||
if scroll_amount != 0.0 {
|
||||
self.viewport.rect.y0 = 0.0_f64.max(self.viewport.rect.y0 + scroll_amount);
|
||||
self.scroll_component
|
||||
.reset_scrollbar_fade(|d| ctx.request_timer(d), env);
|
||||
ctx.request_paint();
|
||||
ctx.set_handled();
|
||||
}
|
||||
}
|
||||
// TODO: replace this by own handling of Wheel event (must be able to extend content size)
|
||||
self.scroll_component
|
||||
.handle_scroll(&mut self.viewport, ctx, event, env);
|
||||
}
|
||||
|
||||
fn lifecycle(
|
||||
&mut self,
|
||||
|
|
|
|||
Loading…
Reference in New Issue