Compare commits

..

3 Commits

Author SHA1 Message Date
Enrico Lumetti 7c9ce7a734 Add Viewport up/down scroll 2021-03-02 00:26:39 +01:00
Enrico Lumetti a1f813631b Make gtk dependencies optional 2021-03-02 00:13:03 +01:00
Enrico Lumetti f330760c1c Add support to stylus eraser 2021-03-02 00:11:39 +01:00
4 changed files with 45 additions and 47 deletions

6
Cargo.lock generated
View File

@ -338,7 +338,7 @@ checksum = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0"
[[package]]
name = "druid"
version = "0.7.0"
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#f0a6fd3f6bece1b1d10e261ce57673d7f6f1969a"
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#2cbb14456fb1813e673d2151626630d2c4db68bf"
dependencies = [
"console_log",
"druid-derive",
@ -361,7 +361,7 @@ dependencies = [
[[package]]
name = "druid-derive"
version = "0.4.0"
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#f0a6fd3f6bece1b1d10e261ce57673d7f6f1969a"
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#2cbb14456fb1813e673d2151626630d2c4db68bf"
dependencies = [
"proc-macro2",
"quote",
@ -371,7 +371,7 @@ dependencies = [
[[package]]
name = "druid-shell"
version = "0.7.0"
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#f0a6fd3f6bece1b1d10e261ce57673d7f6f1969a"
source = "git+https://github.com/doppioandante/druid?branch=v0.7.0_stiletto#2cbb14456fb1813e673d2151626630d2c4db68bf"
dependencies = [
"anyhow",
"bitflags",

View File

@ -16,14 +16,14 @@ serde_json = "1.0"
druid = { git = "https://github.com/doppioandante/druid", branch = "v0.7.0_stiletto", features = ["im", "svg"] }
#druid = { path = "../druid/druid/", features = ["im", "svg"] }
[dependencies.gtk]
[target.'cfg(target_os="linux")'.dependencies.gtk]
version = "0.9.2"
features = ["v3_22"]
[dependencies.gio]
[target.'cfg(target_os="linux")'.dependencies.gio]
version = "0.9.1"
features = ["v2_56"]
[dependencies.gdk]
[target.'cfg(target_os="linux")'.dependencies.gdk]
version = "0.13.2"
features = ["v3_22"]

View File

@ -21,7 +21,9 @@ use log::{info, warn};
use druid::commands;
use druid::im::vector;
use druid::widget::prelude::*;
use druid::widget::{Align, Button, Controller, CrossAxisAlignment, Flex, List, SizedBox, WidgetExt};
use druid::widget::{
Align, Button, Controller, CrossAxisAlignment, Flex, List, SizedBox, WidgetExt,
};
use druid::{
AppDelegate, AppLauncher, Color, Command, Data, DelegateCtx, Env, FileDialogOptions, FileSpec,
Handled, Lens, Target, WidgetId, WindowDesc,
@ -207,27 +209,29 @@ struct ToolSwitcher {
impl ToolSwitcher {
pub fn new() -> Self {
ToolSwitcher {
saved_tool: 0,
}
ToolSwitcher { saved_tool: 0 }
}
}
impl<W: Widget<StilettoState>> Controller<StilettoState, W> for ToolSwitcher {
fn event(&mut self, child: &mut W, ctx: &mut EventCtx, event: &Event, data: &mut StilettoState, env: &Env) {
fn event(
&mut self,
child: &mut W,
ctx: &mut EventCtx,
event: &Event,
data: &mut StilettoState,
env: &Env,
) {
match event {
Event::Notification(cmd) => {
let new_tool = if cmd.get(stiletto::commands::PUSH_ERASER).is_some() {
self.saved_tool = data.current_tool;
Some(data.eraser_tool_id)
}
else if cmd.get(stiletto::commands::POP_ERASER).is_some() {
} else if cmd.get(stiletto::commands::POP_ERASER).is_some() {
Some(self.saved_tool)
}
else if cmd.get(stiletto::commands::UPDATE_TOOL).is_some() {
} else if cmd.get(stiletto::commands::UPDATE_TOOL).is_some() {
let old_tool = data.current_tool;
data
.tool_icons
data.tool_icons
.iter()
.enumerate()
.position(|(pos, x)| pos != old_tool && x.selected)
@ -235,8 +239,7 @@ impl<W: Widget<StilettoState>> Controller<StilettoState, W> for ToolSwitcher {
None
};
if let Some(new_tool) = new_tool
{
if let Some(new_tool) = new_tool {
data.set_tool(new_tool);
ctx.set_handled();
}
@ -261,7 +264,8 @@ impl AppDelegate<StilettoState> for Delegate {
use std::fs::File;
if cmd.get(commands::SAVE_FILE_AS).is_some() || cmd.get(commands::SAVE_FILE).is_some() {
let mut path_buf = cmd.get(commands::SAVE_FILE_AS)
let mut path_buf = cmd
.get(commands::SAVE_FILE_AS)
.map(|file_info| file_info.path().to_path_buf())
.or_else(|| data.current_file_path.as_ref().cloned())
.unwrap();

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use im::{Vector};
use im::Vector;
use crate::canvas::Canvas;
use crate::history::VersionedCanvas;
@ -26,6 +26,7 @@ use druid::widget::prelude::*;
use druid::widget::Viewport;
use druid::{Affine, Color, Data, Env, Event, PointerType, Selector};
#[derive(Clone, Data)]
pub struct CanvasState {
versioned_canvas: VersionedCanvas,
@ -85,13 +86,7 @@ impl CanvasState {
&mut self.tool_ctx
}
pub fn handle_event(
&mut self,
mut ctx: &mut EventCtx,
event: &Event,
transform: Affine,
env: &Env,
) {
pub fn handle_event(&mut self, mut ctx: &mut EventCtx, event: &Event, transform: Affine, env: &Env) {
self.tool_ctx
.handle_event(ctx, event, &mut self.versioned_canvas, transform, env);
}
@ -103,8 +98,6 @@ pub struct CanvasWidget {
}
impl CanvasWidget {
pub const SCROLL: Selector<f64> = Selector::new("scroll_canvas");
pub fn new() -> Self {
CanvasWidget {
viewport: Viewport {
@ -114,11 +107,14 @@ impl CanvasWidget {
scroll_component: ScrollComponent::new(),
}
}
pub const SCROLL: Selector<f64> = Selector::new("scroll_canvas");
}
impl Widget<CanvasState> for CanvasWidget {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut CanvasState, env: &Env) {
ctx.request_focus();
self.scroll_component
.event(&mut self.viewport, ctx, event, env);
if !ctx.is_handled() {
@ -152,6 +148,19 @@ impl Widget<CanvasState> for CanvasWidget {
}
_ => {}
}
// TODO: the first eraser toggle is not handled
if toggle_eraser_event && data.temporary_erasing != enable_temporary_erasing {
if enable_temporary_erasing {
ctx.submit_notification(crate::commands::PUSH_ERASER);
} else {
ctx.submit_notification(crate::commands::POP_ERASER);
}
data.temporary_erasing = enable_temporary_erasing;
} else {
let transform =
Affine::translate((self.viewport.rect.x0, self.viewport.rect.y0));
data.handle_event(ctx, event, transform, env);
}
if scroll_amount != 0.0 {
self.viewport.rect.y0 = 0.0_f64.max(self.viewport.rect.y0 + scroll_amount);
self.scroll_component
@ -159,21 +168,6 @@ impl Widget<CanvasState> for CanvasWidget {
ctx.request_paint();
ctx.set_handled();
}
if !ctx.is_handled() {
// TODO: the first eraser toggle is not handled
if toggle_eraser_event && data.temporary_erasing != enable_temporary_erasing {
if enable_temporary_erasing {
ctx.submit_notification(crate::commands::PUSH_ERASER);
} else {
ctx.submit_notification(crate::commands::POP_ERASER);
}
data.temporary_erasing = enable_temporary_erasing;
} else {
let transform =
Affine::translate((self.viewport.rect.x0, self.viewport.rect.y0));
data.handle_event(ctx, event, transform, env);
}
}
}
// TODO: replace this by own handling of Wheel event (must be able to extend content size)
self.scroll_component
@ -253,8 +247,8 @@ impl Widget<CanvasState> for CanvasWidget {
element.draw(ctx);
}
ctx.restore().unwrap();
if data.get_tool_ctx().needs_repaint() {
data.get_tool_ctx().paint(ctx, env);
if data.tool_ctx.needs_repaint() {
data.tool_ctx.paint(ctx, env);
}
self.scroll_component.draw_bars(ctx, &self.viewport, env);
}