Update to druid 0.7.0

Closes #15
This commit is contained in:
Enrico Lumetti 2021-02-24 18:00:21 +01:00
parent 827d8d6a1c
commit 56ed3b7c01
4 changed files with 574 additions and 357 deletions

858
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,23 +6,24 @@ edition = "2018"
[dependencies] [dependencies]
log = "0.4" log = "0.4"
druid = { version = "0.6.0", features = ["im", "svg"] } druid = { version = "0.7.0", features = ["im", "svg"] }
im = { version = "*" } im = { version = "*" }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
#serde_bare = "0.3.0" #serde_bare = "0.3.0"
serde_json = "1.0" serde_json = "1.0"
[patch.crates-io] [patch.crates-io]
druid = { git = "https://github.com/doppioandante/druid", branch = "v0.6.0_stiletto", features = ["im", "svg"] } druid = { git = "https://github.com/doppioandante/druid", branch = "v0.7.0_stiletto", features = ["im", "svg"] }
#druid = { path = "../druid/druid/", features = ["im", "svg"] }
[dependencies.gtk] [dependencies.gtk]
version = "0.8.1" version = "0.9.2"
features = ["v3_22"] features = ["v3_22"]
[dependencies.gio] [dependencies.gio]
version = "0.8.1" version = "0.9.1"
features = ["v2_56"] features = ["v2_56"]
[dependencies.gdk] [dependencies.gdk]
version = "0.12.1" version = "0.13.2"
features = ["v3_22"] features = ["v3_22"]

View File

@ -19,12 +19,10 @@ use log::{info, warn};
use druid::commands; use druid::commands;
use druid::im::vector; use druid::im::vector;
use druid::widget::prelude::*; use druid::widget::prelude::*;
use druid::widget::{ use druid::widget::{Align, Button, CrossAxisAlignment, Flex, List, SizedBox, WidgetExt};
Align, Button, CrossAxisAlignment, Flex, List, ListGrowDirection, SizedBox, WidgetExt,
};
use druid::{ use druid::{
AppDelegate, AppLauncher, Color, Command, Data, DelegateCtx, Env, FileDialogOptions, FileSpec, AppDelegate, AppLauncher, Color, Command, Data, DelegateCtx, Env, FileDialogOptions, FileSpec,
Lens, LocalizedString, Target, WindowDesc, Handled, Lens, LocalizedString, Target, WindowDesc,
}; };
use im::Vector; use im::Vector;
@ -44,11 +42,11 @@ pub fn main() {
let default_pen_params = CanvasToolParams::Pen { let default_pen_params = CanvasToolParams::Pen {
thickness: 2.0, thickness: 2.0,
color: Color::rgb(0, 0, 0), color: Color::rgb(0.0, 0.0, 0.0),
}; };
let default_pen_params_2 = CanvasToolParams::Pen { let default_pen_params_2 = CanvasToolParams::Pen {
thickness: 2.0, thickness: 2.0,
color: Color::rgb(255, 0, 0), color: Color::rgb(255.0, 0.0, 0.0),
}; };
let canvas_data = StilettoState { let canvas_data = StilettoState {
canvas: CanvasState { canvas: CanvasState {
@ -106,22 +104,18 @@ fn build_ui() -> impl Widget<StilettoState> {
let save_buttons = Flex::row() let save_buttons = Flex::row()
.cross_axis_alignment(CrossAxisAlignment::Center) .cross_axis_alignment(CrossAxisAlignment::Center)
.with_child(Button::new("Open").on_click(move |ctx, _, _| { .with_child(Button::new("Open").on_click(move |ctx, _, _| {
ctx.submit_command( ctx.submit_command(Command::new(
Command::new( druid::commands::SHOW_OPEN_PANEL,
druid::commands::SHOW_OPEN_PANEL, open_dialog_options.clone(),
open_dialog_options.clone(), Target::Auto,
), ))
None,
)
})) }))
.with_child(Button::new("Save").on_click(move |ctx, _, _| { .with_child(Button::new("Save").on_click(move |ctx, _, _| {
ctx.submit_command( ctx.submit_command(Command::new(
Command::new( druid::commands::SHOW_SAVE_PANEL,
druid::commands::SHOW_SAVE_PANEL, save_dialog_options.clone(),
save_dialog_options.clone(), Target::Auto,
), ))
None,
)
})); }));
let tool_buttons = Flex::row() let tool_buttons = Flex::row()
@ -132,10 +126,10 @@ fn build_ui() -> impl Widget<StilettoState> {
.padding((8.0, 0.0)) .padding((8.0, 0.0))
.on_click(|ctx, tool_state, _| { .on_click(|ctx, tool_state, _| {
tool_state.selected = true; tool_state.selected = true;
ctx.submit_command(stiletto::commands::UPDATE_TOOL, None); ctx.submit_command(stiletto::commands::UPDATE_TOOL);
}) })
}) })
.grow(ListGrowDirection::Right) .horizontal()
.lens(StilettoState::tool_icons), .lens(StilettoState::tool_icons),
1.0, 1.0,
); );
@ -167,10 +161,10 @@ impl AppDelegate<StilettoState> for Delegate {
cmd: &Command, cmd: &Command,
data: &mut StilettoState, data: &mut StilettoState,
_env: &Env, _env: &Env,
) -> bool { ) -> Handled {
use std::fs::File; use std::fs::File;
if let Some(Some(file_info)) = cmd.get(commands::SAVE_FILE) { if let Some(file_info) = cmd.get(commands::SAVE_FILE_AS) {
let res_file = File::create(file_info.path()); let res_file = File::create(file_info.path());
if let Ok(f) = res_file { if let Ok(f) = res_file {
let write_res = let write_res =
@ -183,7 +177,7 @@ impl AppDelegate<StilettoState> for Delegate {
} else { } else {
warn!("Cannot create file: {:?}", res_file.err()); warn!("Cannot create file: {:?}", res_file.err());
} }
return true; return Handled::Yes;
} }
if let Some(file_info) = cmd.get(commands::OPEN_FILE) { if let Some(file_info) = cmd.get(commands::OPEN_FILE) {
if let Ok(f) = File::open(file_info.path()) { if let Ok(f) = File::open(file_info.path()) {
@ -196,7 +190,7 @@ impl AppDelegate<StilettoState> for Delegate {
warn!("didn't work: {:?}", res_snapshot.err()); warn!("didn't work: {:?}", res_snapshot.err());
} }
} }
return true; return Handled::Yes;
} }
if cmd.get(stiletto::commands::UPDATE_TOOL).is_some() { if cmd.get(stiletto::commands::UPDATE_TOOL).is_some() {
let old_tool = data.current_tool; let old_tool = data.current_tool;
@ -214,6 +208,6 @@ impl AppDelegate<StilettoState> for Delegate {
)); ));
} }
} }
false Handled::No
} }
} }

View File

@ -17,7 +17,7 @@
use druid::widget::{Container, SizedBox, Svg, SvgData, ViewSwitcher, WidgetExt}; use druid::widget::{Container, SizedBox, Svg, SvgData, ViewSwitcher, WidgetExt};
use druid::{Color, Data, UnitPoint}; use druid::{Color, Data, UnitPoint};
use crate::tool::{CanvasToolParams, CanvasToolType}; use crate::tool::CanvasToolParams;
#[derive(Clone, Data)] #[derive(Clone, Data)]
pub struct CanvasToolIconState { pub struct CanvasToolIconState {
@ -26,10 +26,6 @@ pub struct CanvasToolIconState {
} }
impl CanvasToolIconState { impl CanvasToolIconState {
fn tool_type(&self) -> CanvasToolType {
self.tool_params.tool_type()
}
fn svg_data(&self) -> SvgData { fn svg_data(&self) -> SvgData {
match self.tool_params { match self.tool_params {
CanvasToolParams::Pen { .. } => include_str!("../../icons/pen.svg") CanvasToolParams::Pen { .. } => include_str!("../../icons/pen.svg")
@ -46,9 +42,9 @@ pub fn build_simple_tool_widget(
width: f64, width: f64,
height: f64, height: f64,
padding: f64, padding: f64,
) -> ViewSwitcher<CanvasToolIconState, (CanvasToolType, bool)> { ) -> ViewSwitcher<CanvasToolIconState, CanvasToolIconState> {
ViewSwitcher::<CanvasToolIconState, (CanvasToolType, bool)>::new( ViewSwitcher::<CanvasToolIconState, CanvasToolIconState>::new(
|tool_state, _| (tool_state.tool_type(), tool_state.selected), |tool_state, _| tool_state.clone(),
move |_, tool_state, _| { move |_, tool_state, _| {
Box::new( Box::new(
Container::new( Container::new(
@ -62,7 +58,7 @@ pub fn build_simple_tool_widget(
) )
.border( .border(
if tool_state.selected { if tool_state.selected {
Color::rgb(10, 10, 10) Color::rgb(10.0, 10.0, 10.0)
} else { } else {
Color::BLACK Color::BLACK
}, },