Working finally
This commit is contained in:
parent
2d961a6e9c
commit
481db6b96f
|
|
@ -18,61 +18,49 @@
|
||||||
|
|
||||||
use druid::im::{vector, Vector};
|
use druid::im::{vector, Vector};
|
||||||
use druid::widget::prelude::*;
|
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};
|
use druid::{AppLauncher, Color, Data, LocalizedString, WindowDesc, Lens, UnitPoint};
|
||||||
|
|
||||||
#[derive(Clone, Data)]
|
#[derive(Clone, Data)]
|
||||||
enum CanvasToolState {
|
enum CanvasToolState {
|
||||||
Pen { thickness: f64 },
|
Pen { thickness: f64, color: Color },
|
||||||
Eraser,
|
Eraser,
|
||||||
}
|
}
|
||||||
|
|
||||||
trait CanvasToolBuilder {
|
#[derive(Clone, PartialEq)]
|
||||||
fn default_state() -> CanvasToolState;
|
enum CanvasToolType {
|
||||||
fn build_widget() -> Box<dyn Widget<CanvasToolState>>;
|
Pen,
|
||||||
|
Eraser,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct PenBuilder;
|
impl CanvasToolState {
|
||||||
struct EraserBuilder;
|
fn tool_type(&self) -> CanvasToolType {
|
||||||
|
match self {
|
||||||
|
CanvasToolState::Pen { .. } => CanvasToolType::Pen,
|
||||||
|
CanvasToolState::Eraser => CanvasToolType::Eraser,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl CanvasToolBuilder for PenBuilder {
|
fn build_simple_tool_widget(tool_state: &CanvasToolState) -> Box<dyn Widget<CanvasToolState>> {
|
||||||
fn default_state() -> CanvasToolState {
|
let (label_text, bg_color) = match tool_state {
|
||||||
CanvasToolState::Pen {
|
CanvasToolState::Pen { thickness, color } =>
|
||||||
thickness: 2.0
|
(format!("pen {}", thickness), color.clone()),
|
||||||
}
|
CanvasToolState::Eraser =>
|
||||||
}
|
(String::from("eraser"), Color::rgb(200, 200, 200))
|
||||||
fn build_widget() -> Box<dyn Widget<CanvasToolState>> {
|
};
|
||||||
Box::new(SizedBox::new(
|
|
||||||
Label::new(|item: &CanvasToolState, _env: &_| format!("pen"))
|
Box::new(
|
||||||
|
SizedBox::new(
|
||||||
|
Label::new(label_text)
|
||||||
.align_horizontal(UnitPoint::CENTER)
|
.align_horizontal(UnitPoint::CENTER)
|
||||||
.background(Color::rgb(19.0, 0.5, 0.5))
|
.background(bg_color)
|
||||||
.fix_width(100.0)
|
.fix_width(100.0)
|
||||||
.fix_height(100.0)
|
.fix_height(100.0)
|
||||||
)
|
)
|
||||||
.padding((10.0, 0.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)]
|
#[derive(Clone, Data, Lens)]
|
||||||
struct AppState {
|
struct AppState {
|
||||||
|
|
@ -89,7 +77,12 @@ fn build_ui() -> impl Widget<AppState> {
|
||||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||||
.with_flex_child(
|
.with_flex_child(
|
||||||
List::new(|| {
|
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)
|
.grow(ListGrowDirection::Right)
|
||||||
.lens(AppState::tools)
|
.lens(AppState::tools)
|
||||||
|
|
@ -119,7 +112,7 @@ pub fn main() {
|
||||||
LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Tools"),
|
LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Tools"),
|
||||||
);
|
);
|
||||||
let app_state = AppState {
|
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)
|
AppLauncher::with_window(window)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue