diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1ecbea8 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,38 @@ +#[derive(Clone, druid::Data)] +pub struct Path { + pub kurbo_path: druid::kurbo::BezPath, +} + +#[derive(Clone, druid::Data)] +pub enum CanvasElement { + Freehand { + path: Path, + thickness: f64, + } +} + +impl CanvasElement { + pub fn bounding_box(&self) -> druid::Rect { + match self { + CanvasElement::Freehand{ path, thickness } => { + use druid::kurbo::Shape; + path.kurbo_path.bounding_box().inflate(*thickness, *thickness) + } + } + } + pub fn draw(&self, ctx: &mut druid::PaintCtx) { + match self { + CanvasElement::Freehand{ path, thickness } => { + use druid::RenderContext; + let stroke_color = druid::Color::rgb8(0, 128, 0); + ctx.stroke(&path.kurbo_path, &stroke_color, *thickness); + } + } + } + + pub fn get_path_mut(&mut self) -> Option<&mut Path> { + match self { + CanvasElement::Freehand{ path, .. } => Some(path), + } + } +} diff --git a/src/main.rs b/src/main.rs index 0e87c24..8fc3d97 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,50 +19,6 @@ use druid::kurbo::BezPath; use druid::widget::prelude::*; use druid::{AppLauncher, Color, Data, Event, LocalizedString, WindowDesc}; -mod stiletto { - use druid::widget::prelude::*; - - #[derive(Clone, druid::Data)] - pub struct Path { - pub kurbo_path: druid::kurbo::BezPath, - } - - #[derive(Clone, druid::Data)] - pub enum CanvasElement { - Freehand { - path: Path, - thickness: f64 - } - } - - impl CanvasElement { - pub fn bounding_box(&self) -> druid::Rect { - match self { - CanvasElement::Freehand{ path, thickness } => { - use druid::kurbo::Shape; - path.kurbo_path.bounding_box().inflate(*thickness, *thickness) - } - } - } - pub fn draw(&self, ctx: &mut druid::PaintCtx) { - match self { - CanvasElement::Freehand{ path, thickness } => { - let stroke_color = druid::Color::rgb8(0, 128, 0); - ctx.stroke(&path.kurbo_path, &stroke_color, *thickness); - } - } - } - - pub fn get_path_mut(&mut self) -> Option<&mut Path> { - match self { - CanvasElement::Freehand{ path, .. } => Some(path), - } - } - } -} - -struct CanvasWidget; - use stiletto::CanvasElement; #[derive(Clone, Data)] @@ -77,6 +33,8 @@ impl CanvasData { } } +struct CanvasWidget; + impl Widget for CanvasWidget { fn event(&mut self, _ctx: &mut EventCtx, event: &Event, data: &mut CanvasData, _env: &Env) { match event { @@ -177,8 +135,25 @@ impl Widget for CanvasWidget { } } +fn build_ui() -> impl Widget { + use druid::widget::{Align, Button, Flex, CrossAxisAlignment, SizedBox}; + let toolbar = Flex::row() + .cross_axis_alignment(CrossAxisAlignment::Center) + .with_spacer(30.0) + .with_child( + Button::new("Undo")) + .with_child( + Button::new("Redo")); + + Flex::column() + .cross_axis_alignment(CrossAxisAlignment::Center) + .must_fill_main_axis(true) + .with_child(SizedBox::new(Align::left(toolbar)).height(50.0)) + .with_flex_child(CanvasWidget {}, 1.0) +} + pub fn main() { - let window = WindowDesc::new(|| CanvasWidget {}) + let window = WindowDesc::new(build_ui) .window_size((1024.0, 1400.0)) .title( LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Stiletto"),