From 481db6b96fcd1f7c4404be45899490347004eea1 Mon Sep 17 00:00:00 2001 From: Enrico Lumetti Date: Sat, 21 Nov 2020 16:30:04 +0100 Subject: [PATCH] Working finally --- src/bin/canvas_tool_list.rs | 71 +++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 39 deletions(-) diff --git a/src/bin/canvas_tool_list.rs b/src/bin/canvas_tool_list.rs index 2170486..b191ce6 100644 --- a/src/bin/canvas_tool_list.rs +++ b/src/bin/canvas_tool_list.rs @@ -18,61 +18,49 @@ use druid::im::{vector, Vector}; use druid::widget::prelude::*; -use druid::widget::{Align, Label, Button, CrossAxisAlignment, Flex, List, ListGrowDirection, WidgetExt, SizedBox, Either}; +use druid::widget::{Align, Label, Button, CrossAxisAlignment, Flex, List, ListGrowDirection, WidgetExt, SizedBox, ViewSwitcher}; use druid::{AppLauncher, Color, Data, LocalizedString, WindowDesc, Lens, UnitPoint}; #[derive(Clone, Data)] enum CanvasToolState { - Pen { thickness: f64 }, + Pen { thickness: f64, color: Color }, Eraser, } -trait CanvasToolBuilder { - fn default_state() -> CanvasToolState; - fn build_widget() -> Box>; +#[derive(Clone, PartialEq)] +enum CanvasToolType { + Pen, + Eraser, } -struct PenBuilder; -struct EraserBuilder; - -impl CanvasToolBuilder for PenBuilder { - fn default_state() -> CanvasToolState { - CanvasToolState::Pen { - thickness: 2.0 +impl CanvasToolState { + fn tool_type(&self) -> CanvasToolType { + match self { + CanvasToolState::Pen { .. } => CanvasToolType::Pen, + CanvasToolState::Eraser => CanvasToolType::Eraser, } } - fn build_widget() -> Box> { - Box::new(SizedBox::new( - Label::new(|item: &CanvasToolState, _env: &_| format!("pen")) +} + +fn build_simple_tool_widget(tool_state: &CanvasToolState) -> Box> { + let (label_text, bg_color) = match tool_state { + CanvasToolState::Pen { thickness, color } => + (format!("pen {}", thickness), color.clone()), + CanvasToolState::Eraser => + (String::from("eraser"), Color::rgb(200, 200, 200)) + }; + + Box::new( + SizedBox::new( + Label::new(label_text) .align_horizontal(UnitPoint::CENTER) - .background(Color::rgb(19.0, 0.5, 0.5)) + .background(bg_color) .fix_width(100.0) .fix_height(100.0) ) .padding((10.0, 0.0))) - } } -impl CanvasToolBuilder for EraserBuilder { - fn default_state() -> CanvasToolState { - CanvasToolState::Eraser {} - } - fn build_widget() -> Box> { - Box::new(SizedBox::new( - Label::new(|item: &CanvasToolState, _env: &_| format!("eraser")) - .align_horizontal(UnitPoint::CENTER) - .background(Color::rgb(0.5, 0.5, 0.5)) - .fix_width(100.0) - .fix_height(100.0) - ) - .padding((10.0, 0.0))) - } -} - -enum CanvasTool { - Pen(PenBuilder), - Eraser(EraserBuilder) -} #[derive(Clone, Data, Lens)] struct AppState { @@ -89,7 +77,12 @@ fn build_ui() -> impl Widget { .cross_axis_alignment(CrossAxisAlignment::Center) .with_flex_child( List::new(|| { - PenBuilder::build_widget() + ViewSwitcher::::new( + |tool_state, _| tool_state.tool_type(), + |_, tool_state, _| { + build_simple_tool_widget(&tool_state) + } + ) }) .grow(ListGrowDirection::Right) .lens(AppState::tools) @@ -119,7 +112,7 @@ pub fn main() { LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Tools"), ); let app_state = AppState { - tools: vector![CanvasToolState::Eraser], + tools: vector![CanvasToolState::Eraser, CanvasToolState::Pen { thickness: 3.3, color: Color::rgb(30.0, 0.4, 0.4) }] }; AppLauncher::with_window(window)