Add Viewport up/down scroll
This commit is contained in:
parent
022377cb48
commit
edc9b3b2c1
25
src/main.rs
25
src/main.rs
|
|
@ -24,13 +24,15 @@ use druid::widget::prelude::*;
|
||||||
use druid::widget::{Align, Button, CrossAxisAlignment, Flex, List, SizedBox, WidgetExt};
|
use druid::widget::{Align, Button, CrossAxisAlignment, Flex, List, SizedBox, WidgetExt};
|
||||||
use druid::{
|
use druid::{
|
||||||
AppDelegate, AppLauncher, Color, Command, Data, DelegateCtx, Env, FileDialogOptions, FileSpec,
|
AppDelegate, AppLauncher, Color, Command, Data, DelegateCtx, Env, FileDialogOptions, FileSpec,
|
||||||
Handled, Lens, Target, WindowDesc,
|
Handled, Lens, Target, WidgetId, WindowDesc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use im::Vector;
|
use im::Vector;
|
||||||
|
|
||||||
use stiletto::tool::{CanvasToolCtx, CanvasToolParams};
|
use stiletto::tool::{CanvasToolCtx, CanvasToolParams};
|
||||||
use stiletto::widget::{build_simple_tool_widget, CanvasState, CanvasToolIconState, CanvasWidget};
|
use stiletto::widget::{
|
||||||
|
build_simple_tool_widget, CanvasState, CanvasToolIconState, CanvasWidget, SCROLL,
|
||||||
|
};
|
||||||
use stiletto::DocumentSnapshot;
|
use stiletto::DocumentSnapshot;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
|
@ -89,6 +91,8 @@ struct StilettoState {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_ui() -> impl Widget<StilettoState> {
|
fn build_ui() -> impl Widget<StilettoState> {
|
||||||
|
let canvas_id = WidgetId::next();
|
||||||
|
|
||||||
let history_buttons = Flex::row()
|
let history_buttons = Flex::row()
|
||||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
.with_child(Button::new("Undo").on_click(
|
.with_child(Button::new("Undo").on_click(
|
||||||
|
|
@ -107,6 +111,16 @@ fn build_ui() -> impl Widget<StilettoState> {
|
||||||
|
|
||||||
let save_buttons = Flex::row()
|
let save_buttons = Flex::row()
|
||||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
|
.with_child(Button::new("Up").on_click(
|
||||||
|
move |ctx: &mut EventCtx, _data: &mut StilettoState, _env: &Env| {
|
||||||
|
ctx.submit_command(Command::new(SCROLL, -30.0, canvas_id));
|
||||||
|
},
|
||||||
|
))
|
||||||
|
.with_child(Button::new("Down").on_click(
|
||||||
|
move |ctx: &mut EventCtx, _data: &mut StilettoState, _env: &Env| {
|
||||||
|
ctx.submit_command(Command::new(SCROLL, 30.0, canvas_id));
|
||||||
|
},
|
||||||
|
))
|
||||||
.with_child(Button::new("Save").on_click(move |ctx, data: &mut StilettoState, _| {
|
.with_child(Button::new("Save").on_click(move |ctx, data: &mut StilettoState, _| {
|
||||||
if data.current_file_path.is_some() {
|
if data.current_file_path.is_some() {
|
||||||
ctx.submit_command(Command::new(commands::SAVE_FILE, (), Target::Auto));
|
ctx.submit_command(Command::new(commands::SAVE_FILE, (), Target::Auto));
|
||||||
|
|
@ -163,7 +177,12 @@ fn build_ui() -> impl Widget<StilettoState> {
|
||||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
.must_fill_main_axis(true)
|
.must_fill_main_axis(true)
|
||||||
.with_child(SizedBox::new(Align::left(toolbar)).height(50.0))
|
.with_child(SizedBox::new(Align::left(toolbar)).height(50.0))
|
||||||
.with_flex_child((CanvasWidget {}).lens(StilettoState::canvas), 1.0)
|
.with_flex_child(
|
||||||
|
CanvasWidget::new()
|
||||||
|
.lens(StilettoState::canvas)
|
||||||
|
.with_id(canvas_id),
|
||||||
|
1.0,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Delegate;
|
struct Delegate;
|
||||||
|
|
|
||||||
26
src/tool.rs
26
src/tool.rs
|
|
@ -15,7 +15,7 @@
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
use druid::kurbo::BezPath;
|
use druid::kurbo::BezPath;
|
||||||
use druid::{Color, Data, Env, Event, EventCtx, PaintCtx};
|
use druid::{Affine, Color, Data, Env, Event, EventCtx, PaintCtx};
|
||||||
|
|
||||||
use crate::canvas;
|
use crate::canvas;
|
||||||
use crate::canvas::{Canvas, CanvasElement};
|
use crate::canvas::{Canvas, CanvasElement};
|
||||||
|
|
@ -68,31 +68,34 @@ 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,
|
||||||
env: &Env,
|
env: &Env,
|
||||||
) {
|
) {
|
||||||
match self.initial_params.tool_type() {
|
match self.initial_params.tool_type() {
|
||||||
CanvasToolType::Pen => self.handle_pen_event(&ctx, &event, &mut vcanvas, &env),
|
CanvasToolType::Pen => self.handle_pen_event(ctx, event, vcanvas, transform, env),
|
||||||
CanvasToolType::Eraser => self.handle_erase_event(&ctx, &event, &mut vcanvas, &env),
|
CanvasToolType::Eraser => self.handle_erase_event(ctx, event, 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,
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
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 =
|
||||||
druid::Rect::from_center_size(mouse_event.pos, (5.0, 5.0));
|
druid::Rect::from_center_size(transform * 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();
|
||||||
new_elements.retain(|elem| {
|
new_elements.retain(|elem| {
|
||||||
|
|
@ -102,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();
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -119,9 +124,10 @@ 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,
|
||||||
_env: &Env,
|
_env: &Env,
|
||||||
) {
|
) {
|
||||||
match (&mut self.state, event) {
|
match (&mut self.state, event) {
|
||||||
|
|
@ -138,6 +144,7 @@ impl CanvasToolCtx {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
(
|
(
|
||||||
CanvasToolState::DrawingFreehand {
|
CanvasToolState::DrawingFreehand {
|
||||||
|
|
@ -150,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| {
|
||||||
|
|
@ -161,6 +169,7 @@ impl CanvasToolCtx {
|
||||||
stroke_color,
|
stroke_color,
|
||||||
} = current_path
|
} = current_path
|
||||||
{
|
{
|
||||||
|
path.kurbo_path.apply_affine(transform);
|
||||||
canvas.add_element(CanvasElement::Freehand {
|
canvas.add_element(CanvasElement::Freehand {
|
||||||
path,
|
path,
|
||||||
thickness,
|
thickness,
|
||||||
|
|
@ -169,6 +178,7 @@ impl CanvasToolCtx {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
ctx.set_handled();
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,12 @@ use crate::history::VersionedCanvas;
|
||||||
use crate::tool::CanvasToolCtx;
|
use crate::tool::CanvasToolCtx;
|
||||||
use crate::DocumentSnapshot;
|
use crate::DocumentSnapshot;
|
||||||
|
|
||||||
|
use druid::scroll_component::ScrollComponent;
|
||||||
use druid::widget::prelude::*;
|
use druid::widget::prelude::*;
|
||||||
use druid::{Color, Data, Env, Event};
|
use druid::widget::Viewport;
|
||||||
|
use druid::{Affine, Color, Data, Env, Event, Selector};
|
||||||
|
|
||||||
|
pub const SCROLL: Selector<f64> = Selector::new("scroll_canvas");
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
pub struct CanvasState {
|
pub struct CanvasState {
|
||||||
|
|
@ -74,21 +78,76 @@ impl CanvasState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CanvasWidget;
|
pub struct CanvasWidget {
|
||||||
|
viewport: Viewport,
|
||||||
|
scroll_component: ScrollComponent,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CanvasWidget {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
CanvasWidget {
|
||||||
|
viewport: Viewport {
|
||||||
|
content_size: Size::new(0.0, 0.0),
|
||||||
|
rect: druid::Rect::new(0.0, 0.0, 0.0, 0.0),
|
||||||
|
},
|
||||||
|
scroll_component: ScrollComponent::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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) {
|
||||||
data.tool_ctx
|
ctx.request_focus();
|
||||||
.handle_event(ctx, event, &mut data.versioned_canvas, env);
|
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) {
|
||||||
|
scroll_amount = *value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Event::KeyDown(key_event) => {
|
||||||
|
if key_event.code == druid::Code::ArrowDown {
|
||||||
|
scroll_amount = 30.0;
|
||||||
|
} else if key_event.code == druid::Code::ArrowUp {
|
||||||
|
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,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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(
|
fn lifecycle(
|
||||||
&mut self,
|
&mut self,
|
||||||
_ctx: &mut LifeCycleCtx,
|
ctx: &mut LifeCycleCtx,
|
||||||
_event: &LifeCycle,
|
event: &LifeCycle,
|
||||||
_data: &CanvasState,
|
_data: &CanvasState,
|
||||||
_env: &Env,
|
env: &Env,
|
||||||
) {
|
) {
|
||||||
|
self.scroll_component.lifecycle(ctx, event, env);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(
|
||||||
|
|
@ -107,6 +166,7 @@ impl Widget<CanvasState> for CanvasWidget {
|
||||||
// ctx.request_paint_rect(e.bounding_box());
|
// ctx.request_paint_rect(e.bounding_box());
|
||||||
// }
|
// }
|
||||||
//} else {
|
//} else {
|
||||||
|
|
||||||
ctx.request_paint();
|
ctx.request_paint();
|
||||||
//}
|
//}
|
||||||
}
|
}
|
||||||
|
|
@ -136,11 +196,26 @@ impl Widget<CanvasState> for CanvasWidget {
|
||||||
let rect = size.to_rect();
|
let rect = size.to_rect();
|
||||||
|
|
||||||
ctx.fill(rect, &Color::WHITE);
|
ctx.fill(rect, &Color::WHITE);
|
||||||
|
|
||||||
|
let page_content_size = data.versioned_canvas.get().content_size();
|
||||||
|
self.viewport.rect = self.viewport.rect.with_size(size);
|
||||||
|
self.viewport.content_size = druid::Size::new(
|
||||||
|
self.viewport.rect.x1.max(page_content_size.width),
|
||||||
|
self.viewport.rect.y1.max(page_content_size.height),
|
||||||
|
);
|
||||||
|
|
||||||
|
ctx.save().unwrap();
|
||||||
|
ctx.transform(Affine::translate((
|
||||||
|
-self.viewport.rect.x0,
|
||||||
|
-self.viewport.rect.y0,
|
||||||
|
)));
|
||||||
for element in data.versioned_canvas.get().elements().iter() {
|
for element in data.versioned_canvas.get().elements().iter() {
|
||||||
element.draw(ctx);
|
element.draw(ctx);
|
||||||
}
|
}
|
||||||
|
ctx.restore().unwrap();
|
||||||
if data.tool_ctx.needs_repaint() {
|
if data.tool_ctx.needs_repaint() {
|
||||||
data.tool_ctx.paint(ctx, env);
|
data.tool_ctx.paint(ctx, env);
|
||||||
}
|
}
|
||||||
|
self.scroll_component.draw_bars(ctx, &self.viewport, env);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue