Mirror mirror of the wall, how the fuck do lens work?
This commit is contained in:
parent
993081419d
commit
9369c3113d
|
|
@ -257,7 +257,7 @@ checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0"
|
|||
[[package]]
|
||||
name = "druid"
|
||||
version = "0.6.0"
|
||||
source = "git+https://github.com/doppioandante/druid?branch=stylus_events_0.6.0#b0f9dc0268409a56b5066f569ad245e5df016166"
|
||||
source = "git+https://github.com/doppioandante/druid?branch=v0.6.0_stiletto#f8f8a566b77795cfa10caf7f2861f9240d159e90"
|
||||
dependencies = [
|
||||
"console_log",
|
||||
"druid-derive",
|
||||
|
|
@ -278,7 +278,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "druid-derive"
|
||||
version = "0.3.1"
|
||||
source = "git+https://github.com/doppioandante/druid?branch=stylus_events_0.6.0#b0f9dc0268409a56b5066f569ad245e5df016166"
|
||||
source = "git+https://github.com/doppioandante/druid?branch=v0.6.0_stiletto#f8f8a566b77795cfa10caf7f2861f9240d159e90"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
|
@ -288,7 +288,7 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "druid-shell"
|
||||
version = "0.6.0"
|
||||
source = "git+https://github.com/doppioandante/druid?branch=stylus_events_0.6.0#b0f9dc0268409a56b5066f569ad245e5df016166"
|
||||
source = "git+https://github.com/doppioandante/druid?branch=v0.6.0_stiletto#f8f8a566b77795cfa10caf7f2861f9240d159e90"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"bitflags",
|
||||
|
|
|
|||
|
|
@ -18,17 +18,68 @@
|
|||
|
||||
use druid::im::{vector, Vector};
|
||||
use druid::widget::prelude::*;
|
||||
use druid::{AppLauncher, Color, Data, LocalizedString, WindowDesc, Lens};
|
||||
use druid::widget::{Align, Label, Button, CrossAxisAlignment, Flex, List, ListGrowDirection, WidgetExt, SizedBox};
|
||||
use druid::{AppLauncher, Color, Data, LocalizedString, WindowDesc, Lens, UnitPoint};
|
||||
|
||||
#[derive(Clone, Data)]
|
||||
enum CanvasToolState {
|
||||
Pen { thickness: f64 },
|
||||
Eraser,
|
||||
}
|
||||
|
||||
trait CanvasToolBuilder {
|
||||
fn default_state() -> CanvasToolState;
|
||||
fn build_widget(&self) -> Box<dyn Widget<CanvasToolState>>;
|
||||
}
|
||||
|
||||
struct PenBuilder;
|
||||
struct EraserBuilder;
|
||||
|
||||
impl CanvasToolBuilder for PenBuilder {
|
||||
fn default_state() -> CanvasToolState {
|
||||
CanvasToolState::Pen {
|
||||
thickness: 2.0
|
||||
}
|
||||
}
|
||||
fn build_widget(&self) -> Box<dyn Widget<CanvasToolState>> {
|
||||
Box::new(SizedBox::new(
|
||||
Label::new(|item: &CanvasToolState, _env: &_| format!("pen"))
|
||||
.align_horizontal(UnitPoint::CENTER)
|
||||
.background(Color::rgb(19.0, 0.5, 0.5))
|
||||
.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(&self) -> 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 {
|
||||
numbers: Vector<u32>,
|
||||
tools: Vector<CanvasToolState>,
|
||||
}
|
||||
|
||||
fn build_ui() -> impl Widget<AppState> {
|
||||
use druid::UnitPoint;
|
||||
use druid::widget::{Align, Label, Button, CrossAxisAlignment, Flex, List, ListGrowDirection, WidgetExt, SizedBox};
|
||||
|
||||
let list_buttons = Flex::row()
|
||||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||
.with_child(Button::new("Add"))
|
||||
|
|
@ -38,17 +89,10 @@ fn build_ui() -> impl Widget<AppState> {
|
|||
.cross_axis_alignment(CrossAxisAlignment::Center)
|
||||
.with_flex_child(
|
||||
List::new(|| {
|
||||
SizedBox::new(
|
||||
Label::new(|item: &u32, _env: &_| format!("{}", item))
|
||||
.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))
|
||||
//CanvasToolBuilder::build_widget ???
|
||||
})
|
||||
.grow(ListGrowDirection::Right)
|
||||
.lens(AppState::numbers)
|
||||
.lens(AppState::tools)
|
||||
, 1.0);
|
||||
|
||||
let toolbar = Flex::row()
|
||||
|
|
@ -75,7 +119,7 @@ pub fn main() {
|
|||
LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Tools"),
|
||||
);
|
||||
let app_state = AppState {
|
||||
numbers: vector![1,2],
|
||||
tools: vector![CanvasToolState::Eraser],
|
||||
};
|
||||
|
||||
AppLauncher::with_window(window)
|
||||
|
|
|
|||
Loading…
Reference in New Issue