From 32763c80cffe7f9c312b11b9815677b0c22f0ddc Mon Sep 17 00:00:00 2001 From: Doublonmousse <115779707+Doublonmousse@users.noreply.github.com> Date: Thu, 20 Jun 2024 12:43:17 +0200 Subject: [PATCH] another failed attempt --- crates/rnote-engine/src/engine/mod.rs | 16 ++++++++++ crates/rnote-engine/src/pens/penholder.rs | 38 +++++++++++++++++++++++ crates/rnote-engine/src/pens/penmode.rs | 1 - crates/rnote-ui/src/appwindow/actions.rs | 34 +++++++++++++++++++- crates/rnote-ui/src/settingspanel/mod.rs | 28 ++++++++++------- 5 files changed, 104 insertions(+), 13 deletions(-) diff --git a/crates/rnote-engine/src/engine/mod.rs b/crates/rnote-engine/src/engine/mod.rs index 0d4e4ff653..c0cca51d9b 100644 --- a/crates/rnote-engine/src/engine/mod.rs +++ b/crates/rnote-engine/src/engine/mod.rs @@ -525,6 +525,22 @@ impl Engine { ) } + /// Change the pen or earser style. + pub fn change_pen_eraser_style(&mut self, mode: PenMode, new_style: PenStyle) -> WidgetFlags { + self.penholder.change_style_single_mode( + mode, + new_style, + &mut EngineViewMut { + tasks_tx: self.engine_tasks_tx(), + pens_config: &mut self.pens_config, + document: &mut self.document, + store: &mut self.store, + camera: &mut self.camera, + audioplayer: &mut self.audioplayer, + }, + ) + } + /// Change the pen style (temporary) override. pub fn change_pen_style_override( &mut self, diff --git a/crates/rnote-engine/src/pens/penholder.rs b/crates/rnote-engine/src/pens/penholder.rs index ed0cccaa27..38603fffbd 100644 --- a/crates/rnote-engine/src/pens/penholder.rs +++ b/crates/rnote-engine/src/pens/penholder.rs @@ -167,6 +167,21 @@ impl PenHolder { widget_flags } + /// Change the pen or eraser style. + pub fn change_style_single_mode( + &mut self, + mode: PenMode, + new_style: PenStyle, + engine_view: &mut EngineViewMut, + ) -> WidgetFlags { + let widget_flags = self.change_style_pen_eraser_int(new_style, mode, engine_view); + // When the style is changed externally, the toggle mode / internal states are reset + self.toggle_pen_style = None; + self.prev_shortcut_key = None; + + widget_flags + } + /// Change the style override. pub fn change_style_override( &mut self, @@ -357,6 +372,29 @@ impl PenHolder { widget_flags } + /// Internal method for changing the pen/eraser style, without some of the side effects that are happening in the public + /// method. + fn change_style_pen_eraser_int( + &mut self, + new_style: PenStyle, + mode: PenMode, + engine_view: &mut EngineViewMut, + ) -> WidgetFlags { + let mut widget_flags = WidgetFlags::default(); + + if self.pen_mode_state.get_style(mode) != new_style { + // Deselecting when changing the style + let all_strokes = engine_view.store.selection_keys_as_rendered(); + engine_view.store.set_selected_keys(&all_strokes, false); + + self.pen_mode_state.set_style_single_mode(mode, new_style); + widget_flags |= self.reinstall_pen_current_style(engine_view); + widget_flags.refresh_ui = true; + } + + widget_flags + } + /// Handles the pen event in the global scope if the current pen has not handled it. /// /// Used to implement things like nudging the view, react to pressed buttons that weren't handled by th pen, .. diff --git a/crates/rnote-engine/src/pens/penmode.rs b/crates/rnote-engine/src/pens/penmode.rs index de195c3688..5e0a549f2b 100644 --- a/crates/rnote-engine/src/pens/penmode.rs +++ b/crates/rnote-engine/src/pens/penmode.rs @@ -1,5 +1,4 @@ use crate::CloneConfig; -use std::cell::RefCell; // Imports use super::PenStyle; diff --git a/crates/rnote-ui/src/appwindow/actions.rs b/crates/rnote-ui/src/appwindow/actions.rs index 97a032c766..3f0180958b 100644 --- a/crates/rnote-ui/src/appwindow/actions.rs +++ b/crates/rnote-ui/src/appwindow/actions.rs @@ -10,6 +10,7 @@ use p2d::bounding_volume::BoundingVolume; use rnote_compose::penevent::ShortcutKey; use rnote_compose::SplitOrder; use rnote_engine::engine::StrokeContent; +use rnote_engine::pens::PenMode; use rnote_engine::pens::PenStyle; use rnote_engine::strokes::resize::{ImageSizeOption, Resize}; use rnote_engine::{Camera, Engine}; @@ -91,6 +92,11 @@ impl RnAppWindow { &String::from("brush").to_variant(), ); self.add_action(&action_pen_style); + let action_change_pen_styles = gio::SimpleAction::new( + "pen-styles", + Some(&<(String, String)>::static_variant_type()), + ); + self.add_action(&action_change_pen_styles); let action_undo_stroke = gio::SimpleAction::new("undo", None); self.add_action(&action_undo_stroke); let action_redo_stroke = gio::SimpleAction::new("redo", None); @@ -340,7 +346,7 @@ impl RnAppWindow { // display a popup that can unlock the pen tool appwindow.overlays().dispatch_toast_w_button_singleton( &gettext("Tool Locked"), - &gettext("Unlock"), //padlock symbol ? + &gettext("Unlock"), clone!(@weak canvas, @weak appwindow => move |_reload_toast | { canvas.engine_mut().penholder.pen_mode_state_mut().unlock_pen(active_pen); appwindow.sidebar().settings_panel().set_lock_state(active_pen,false); @@ -360,6 +366,32 @@ impl RnAppWindow { }}), ); + // action to change pen or eraser style (activated from the settings panel) + action_change_pen_styles.connect_activate( + clone!(@weak self as appwindow => move |_action,target| { + let (pen_type_str,tool_str) = target.unwrap().get::<(String,String)>().unwrap(); + + let pen_mode = match pen_type_str.as_str() { + "pen" => PenMode::Pen, + "eraser" => PenMode::Eraser, + _ => PenMode::Pen + }; + + let pen_style = match PenStyle::from_str(tool_str.as_str()) { + Ok(s) => s, + Err(e) => { + tracing::error!("Activated pen-style action with invalid target, Err: {e:}"); + return; + } + }; + let canvas = appwindow.active_tab_wrapper().canvas(); + + let mut widget_flags = canvas.engine_mut().change_pen_eraser_style(pen_mode,pen_style); + widget_flags |= canvas.engine_mut().change_pen_style_override(None); + appwindow.handle_widget_flags(widget_flags, &canvas); + }), + ); + // Tab actions action_active_tab_move_left.connect_activate( clone!(@weak self as appwindow => move |_, _| { diff --git a/crates/rnote-ui/src/settingspanel/mod.rs b/crates/rnote-ui/src/settingspanel/mod.rs index 7d565b7bee..b4c0055e56 100644 --- a/crates/rnote-ui/src/settingspanel/mod.rs +++ b/crates/rnote-ui/src/settingspanel/mod.rs @@ -909,18 +909,24 @@ impl RnSettingsPanel { let lock_pen = imp.lock_pen_mode.get(); let lock_eraser = imp.lock_eraser_mode.get(); - imp.lock_pen_mode.connect_local("action-changed",false, clone!(@weak lock_pen, @weak appwindow => @default-return None, move |_values| { - let action = lock_pen.action(); - // BorrowMut with this ... - appwindow.active_tab_wrapper().canvas().engine_mut().penholder.pen_mode_state_mut().set_style_single_mode(PenMode::Pen, action); - None - })); + imp.lock_pen_mode.connect_local( + "action-changed", + false, + clone!(@weak lock_pen, @weak appwindow => @default-return None, move |_values| { + let action = lock_pen.action(); + adw::prelude::ActionGroupExt::activate_action(&appwindow, "pen-styles", Some(&("pen",action.to_string()).to_variant())); + None + }), + ); - imp.lock_eraser_mode.connect_local("action-changed",false, clone!(@weak lock_pen, @weak appwindow => @default-return None, move |_values| { - let action = lock_eraser.action(); - appwindow.active_tab_wrapper().canvas().engine_mut().penholder.pen_mode_state_mut().set_style_single_mode(PenMode::Eraser, action); - None - })); + // imp.lock_eraser_mode.connect_local( + // "action-changed", + // false, + // clone!(@weak lock_eraser, @weak appwindow => @default-return None, move |_values| { + // let action = lock_eraser.action(); + // adw::prelude::ActionGroupExt::activate_action(&appwindow, "pen-styles", Some(&("eraser",action.to_string()).to_variant())); + // None + // })); } fn revert_format(&self, appwindow: &RnAppWindow) {