Working finally
This commit is contained in:
parent
2d961a6e9c
commit
481db6b96f
|
|
@ -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<dyn Widget<CanvasToolState>>;
|
||||
#[derive(Clone, PartialEq)]
|
||||
enum CanvasToolType {
|
||||
Pen,
|
||||
Eraser,
|
||||
}
|
||||
|
||||
struct PenBuilder;
|
||||
struct EraserBuilder;
|
||||
impl CanvasToolState {
|
||||
fn tool_type(&self) -> CanvasToolType {
|
||||
match self {
|
||||
CanvasToolState::Pen { .. } => CanvasToolType::Pen,
|
||||
CanvasToolState::Eraser => CanvasToolType::Eraser,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CanvasToolBuilder for PenBuilder {
|
||||
fn default_state() -> CanvasToolState {
|
||||
CanvasToolState::Pen {
|
||||
thickness: 2.0
|
||||
}
|
||||
}
|
||||
fn build_widget() -> Box<dyn Widget<CanvasToolState>> {
|
||||
Box::new(SizedBox::new(
|
||||
Label::new(|item: &CanvasToolState, _env: &_| format!("pen"))
|
||||
fn build_simple_tool_widget(tool_state: &CanvasToolState) -> Box<dyn Widget<CanvasToolState>> {
|
||||
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<dyn Widget<CanvasToolState>> {
|
||||
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<AppState> {
|
|||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||
.with_flex_child(
|
||||
List::new(|| {
|
||||
PenBuilder::build_widget()
|
||||
ViewSwitcher::<CanvasToolState, CanvasToolType>::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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue