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(
|
pub fn handle_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
ctx: &EventCtx,
|
mut ctx: &mut EventCtx,
|
||||||
event: &Event,
|
event: &Event,
|
||||||
mut vcanvas: &mut VersionedCanvas,
|
mut vcanvas: &mut VersionedCanvas,
|
||||||
transform: Affine,
|
transform: Affine,
|
||||||
env: &Env,
|
env: &Env,
|
||||||
) {
|
) {
|
||||||
match self.initial_params.tool_type() {
|
match self.initial_params.tool_type() {
|
||||||
CanvasToolType::Pen => {
|
CanvasToolType::Pen => self.handle_pen_event(ctx, event, vcanvas, transform, env),
|
||||||
self.handle_pen_event(&ctx, &event, &mut vcanvas, transform, &env)
|
CanvasToolType::Eraser => self.handle_erase_event(ctx, event, vcanvas, transform, env),
|
||||||
}
|
|
||||||
CanvasToolType::Eraser => {
|
|
||||||
self.handle_erase_event(&ctx, &event, &mut vcanvas, transform, &env)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_erase_event(
|
pub fn handle_erase_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
_ctx: &EventCtx,
|
ctx: &mut EventCtx,
|
||||||
event: &Event,
|
event: &Event,
|
||||||
vcanvas: &mut VersionedCanvas,
|
vcanvas: &mut VersionedCanvas,
|
||||||
transform: Affine,
|
transform: Affine,
|
||||||
|
|
@ -95,6 +91,7 @@ impl CanvasToolCtx {
|
||||||
match (&mut self.state, event) {
|
match (&mut self.state, event) {
|
||||||
(CanvasToolState::Idle, Event::MouseDown(_)) => {
|
(CanvasToolState::Idle, Event::MouseDown(_)) => {
|
||||||
self.state = CanvasToolState::Erasing;
|
self.state = CanvasToolState::Erasing;
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
(CanvasToolState::Erasing, Event::MouseMove(mouse_event)) => {
|
(CanvasToolState::Erasing, Event::MouseMove(mouse_event)) => {
|
||||||
let eraser_rect =
|
let eraser_rect =
|
||||||
|
|
@ -108,16 +105,18 @@ impl CanvasToolCtx {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
true
|
||||||
});
|
});
|
||||||
if new_elements.len() != old_elements.len() {
|
if new_elements.len() != old_elements.len() {
|
||||||
vcanvas.update(|canvas: &mut Canvas| {
|
vcanvas.update(|canvas: &mut Canvas| {
|
||||||
*canvas = Canvas::new_with_elements(new_elements);
|
*canvas = Canvas::new_with_elements(new_elements);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
(CanvasToolState::Erasing, Event::MouseUp(_)) => {
|
(CanvasToolState::Erasing, Event::MouseUp(_)) => {
|
||||||
self.state = CanvasToolState::Idle;
|
self.state = CanvasToolState::Idle;
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -125,7 +124,7 @@ impl CanvasToolCtx {
|
||||||
|
|
||||||
pub fn handle_pen_event(
|
pub fn handle_pen_event(
|
||||||
&mut self,
|
&mut self,
|
||||||
_ctx: &EventCtx,
|
ctx: &mut EventCtx,
|
||||||
event: &Event,
|
event: &Event,
|
||||||
vcanvas: &mut VersionedCanvas,
|
vcanvas: &mut VersionedCanvas,
|
||||||
transform: Affine,
|
transform: Affine,
|
||||||
|
|
@ -145,6 +144,7 @@ impl CanvasToolCtx {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
(
|
(
|
||||||
CanvasToolState::DrawingFreehand {
|
CanvasToolState::DrawingFreehand {
|
||||||
|
|
@ -157,6 +157,7 @@ impl CanvasToolCtx {
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(_)) => {
|
(CanvasToolState::DrawingFreehand { .. }, Event::MouseUp(_)) => {
|
||||||
vcanvas.update(move |canvas: &mut Canvas| {
|
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::scroll_component::ScrollComponent;
|
||||||
use druid::widget::prelude::*;
|
use druid::widget::prelude::*;
|
||||||
use druid::widget::Viewport;
|
use druid::widget::Viewport;
|
||||||
use druid::{Affine, Color, Data, Env, Event};
|
use druid::{Affine, Color, Data, Env, Event, Selector};
|
||||||
use druid::{Selector};
|
|
||||||
|
|
||||||
pub const SCROLL: Selector<f64> = Selector::new("scroll_canvas");
|
pub const SCROLL: Selector<f64> = Selector::new("scroll_canvas");
|
||||||
|
|
||||||
|
|
@ -99,33 +98,47 @@ impl CanvasWidget {
|
||||||
impl Widget<CanvasState> for CanvasWidget {
|
impl Widget<CanvasState> for CanvasWidget {
|
||||||
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut CanvasState, env: &Env) {
|
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut CanvasState, env: &Env) {
|
||||||
ctx.request_focus();
|
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 {
|
match event {
|
||||||
Event::Command(cmd) => {
|
Event::Command(cmd) => {
|
||||||
if let Some(value) = cmd.get(SCROLL) {
|
if let Some(value) = cmd.get(SCROLL) {
|
||||||
self.viewport.rect.y0 += value;
|
scroll_amount = *value;
|
||||||
needs_update = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::KeyDown(key_event) => {
|
Event::KeyDown(key_event) => {
|
||||||
if key_event.code == druid::Code::ArrowDown {
|
if key_event.code == druid::Code::ArrowDown {
|
||||||
self.viewport.rect.y0 += 30.0;
|
scroll_amount = 30.0;
|
||||||
needs_update = true;
|
|
||||||
} else if key_event.code == druid::Code::ArrowUp {
|
} else if key_event.code == druid::Code::ArrowUp {
|
||||||
self.viewport.rect.y0 = 0.0_f64.max(self.viewport.rect.y0 - 30.0);
|
scroll_amount = -30.0;
|
||||||
needs_update = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let transform = Affine::translate((self.viewport.rect.x0, self.viewport.rect.y0));
|
let transform =
|
||||||
data.tool_ctx
|
Affine::translate((self.viewport.rect.x0, self.viewport.rect.y0));
|
||||||
.handle_event(ctx, event, &mut data.versioned_canvas, transform, env);
|
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.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(
|
fn lifecycle(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue