Skip to content

Commit

Permalink
another failed attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
Doublonmousse committed Jun 20, 2024
1 parent 19a6f16 commit 32763c8
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 13 deletions.
16 changes: 16 additions & 0 deletions crates/rnote-engine/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions crates/rnote-engine/src/pens/penholder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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, ..
Expand Down
1 change: 0 additions & 1 deletion crates/rnote-engine/src/pens/penmode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::CloneConfig;
use std::cell::RefCell;

// Imports
use super::PenStyle;
Expand Down
34 changes: 33 additions & 1 deletion crates/rnote-ui/src/appwindow/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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 |_, _| {
Expand Down
28 changes: 17 additions & 11 deletions crates/rnote-ui/src/settingspanel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 32763c8

Please sign in to comment.