Rename CanvasData to CanvasState

This commit is contained in:
Enrico Lumetti 2020-11-21 21:08:26 +01:00
parent 7925cfb2d8
commit f42db5fb69
2 changed files with 16 additions and 16 deletions

View File

@ -26,7 +26,7 @@ use druid::{
}; };
use stiletto::history::VersionedCanvas; use stiletto::history::VersionedCanvas;
use stiletto::widget::{CanvasData, CanvasWidget}; use stiletto::widget::{CanvasState, CanvasWidget};
use stiletto::DocumentSnapshot; use stiletto::DocumentSnapshot;
pub fn main() { pub fn main() {
@ -35,7 +35,7 @@ pub fn main() {
.title( .title(
LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Stiletto"), LocalizedString::new("custom-widget-demo-window-title").with_placeholder("Stiletto"),
); );
let canvas_data = CanvasData { let canvas_data = CanvasState {
current_element: None, current_element: None,
elements: VersionedCanvas::new(vector![]), elements: VersionedCanvas::new(vector![]),
}; };
@ -46,15 +46,15 @@ pub fn main() {
.expect("launch failed"); .expect("launch failed");
} }
fn build_ui() -> impl Widget<CanvasData> { fn build_ui() -> impl Widget<CanvasState> {
let history_buttons = let history_buttons =
Flex::row() Flex::row()
.cross_axis_alignment(CrossAxisAlignment::Center) .cross_axis_alignment(CrossAxisAlignment::Center)
.with_child(Button::new("Undo").on_click( .with_child(Button::new("Undo").on_click(
|_ctx: &mut EventCtx, data: &mut CanvasData, _env: &Env| data.perform_undo(), |_ctx: &mut EventCtx, data: &mut CanvasState, _env: &Env| data.perform_undo(),
)) ))
.with_child(Button::new("Redo").on_click( .with_child(Button::new("Redo").on_click(
|_ctx: &mut EventCtx, data: &mut CanvasData, _env: &Env| data.perform_redo(), |_ctx: &mut EventCtx, data: &mut CanvasState, _env: &Env| data.perform_redo(),
)); ));
let stlt = FileSpec::new("Stiletto notebook", &["stlt"]); let stlt = FileSpec::new("Stiletto notebook", &["stlt"]);
@ -100,13 +100,13 @@ fn build_ui() -> impl Widget<CanvasData> {
struct Delegate; struct Delegate;
impl AppDelegate<CanvasData> for Delegate { impl AppDelegate<CanvasState> for Delegate {
fn command( fn command(
&mut self, &mut self,
_ctx: &mut DelegateCtx, _ctx: &mut DelegateCtx,
_target: Target, _target: Target,
cmd: &Command, cmd: &Command,
data: &mut CanvasData, data: &mut CanvasState,
_env: &Env, _env: &Env,
) -> bool { ) -> bool {
use std::fs::File; use std::fs::File;

View File

@ -26,12 +26,12 @@ use druid::widget::prelude::*;
use druid::{Color, Data, Env, Event}; use druid::{Color, Data, Env, Event};
#[derive(Clone, Data)] #[derive(Clone, Data)]
pub struct CanvasData { pub struct CanvasState {
pub current_element: Option<CanvasElement>, pub current_element: Option<CanvasElement>,
pub elements: VersionedCanvas, pub elements: VersionedCanvas,
} }
impl CanvasData { impl CanvasState {
pub fn is_drawing(&self) -> bool { pub fn is_drawing(&self) -> bool {
self.current_element.is_some() self.current_element.is_some()
} }
@ -62,8 +62,8 @@ impl CanvasData {
pub struct CanvasWidget; pub struct CanvasWidget;
impl Widget<CanvasData> for CanvasWidget { impl Widget<CanvasState> for CanvasWidget {
fn event(&mut self, _ctx: &mut EventCtx, event: &Event, data: &mut CanvasData, _env: &Env) { fn event(&mut self, _ctx: &mut EventCtx, event: &Event, data: &mut CanvasState, _env: &Env) {
match event { match event {
Event::MouseDown(mouse_event) => { Event::MouseDown(mouse_event) => {
let mut kurbo_path = BezPath::new(); let mut kurbo_path = BezPath::new();
@ -101,7 +101,7 @@ impl Widget<CanvasData> for CanvasWidget {
&mut self, &mut self,
_ctx: &mut LifeCycleCtx, _ctx: &mut LifeCycleCtx,
_event: &LifeCycle, _event: &LifeCycle,
_data: &CanvasData, _data: &CanvasState,
_env: &Env, _env: &Env,
) { ) {
} }
@ -109,8 +109,8 @@ impl Widget<CanvasData> for CanvasWidget {
fn update( fn update(
&mut self, &mut self,
ctx: &mut UpdateCtx, ctx: &mut UpdateCtx,
old_data: &CanvasData, old_data: &CanvasState,
data: &CanvasData, data: &CanvasState,
_env: &Env, _env: &Env,
) { ) {
// the current_element is moved to the elements array, no need to repaint // the current_element is moved to the elements array, no need to repaint
@ -130,7 +130,7 @@ impl Widget<CanvasData> for CanvasWidget {
&mut self, &mut self,
_layout_ctx: &mut LayoutCtx, _layout_ctx: &mut LayoutCtx,
bc: &BoxConstraints, bc: &BoxConstraints,
_data: &CanvasData, _data: &CanvasState,
_env: &Env, _env: &Env,
) -> Size { ) -> Size {
// BoxConstraints are passed by the parent widget. // BoxConstraints are passed by the parent widget.
@ -145,7 +145,7 @@ impl Widget<CanvasData> for CanvasWidget {
// The paint method gets called last, after an event flow. // The paint method gets called last, after an event flow.
// It goes event -> update -> layout -> paint, and each method can influence the next. // It goes event -> update -> layout -> paint, and each method can influence the next.
// Basically, anything that changes the appearance of a widget causes a paint. // Basically, anything that changes the appearance of a widget causes a paint.
fn paint(&mut self, ctx: &mut PaintCtx, data: &CanvasData, _env: &Env) { fn paint(&mut self, ctx: &mut PaintCtx, data: &CanvasState, _env: &Env) {
// (ctx.size() returns the size of the layout rect we're painting in) // (ctx.size() returns the size of the layout rect we're painting in)
let size = ctx.size(); let size = ctx.size();
let rect = size.to_rect(); let rect = size.to_rect();