-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an actions history implementation (#34)
* add an initial feature outline using the `Command` pattern * add shortcuts system * move palette command to a separate module * add a basic history implementation for the stitch creation * test `AddStitchCommand` * fix drawing * don't overwright history on pattern loading * add a history implementation for the stitch removing * move app setup function to the `lib.rs` and split the state to several instances * rename commands to actions * extract the history to a separate module * update documentation * test history * use `cargo-nextest` for running tests
- Loading branch information
Showing
42 changed files
with
1,013 additions
and
524 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use tauri::WebviewWindow; | ||
|
||
use crate::{ | ||
error::CommandResult, | ||
state::{HistoryState, PatternKey, PatternsState}, | ||
}; | ||
|
||
#[tauri::command] | ||
pub fn undo<R: tauri::Runtime>( | ||
pattern_key: PatternKey, | ||
window: WebviewWindow<R>, | ||
history: tauri::State<HistoryState<R>>, | ||
patterns: tauri::State<PatternsState>, | ||
) -> CommandResult<()> { | ||
let mut history = history.write().unwrap(); | ||
let mut patterns = patterns.write().unwrap(); | ||
if let Some(action) = history.get_mut(&pattern_key).undo() { | ||
action.perform(&window, patterns.get_mut(&pattern_key).unwrap())?; | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[tauri::command] | ||
pub fn redo<R: tauri::Runtime>( | ||
pattern_key: PatternKey, | ||
window: WebviewWindow<R>, | ||
history: tauri::State<HistoryState<R>>, | ||
patterns: tauri::State<PatternsState>, | ||
) -> CommandResult<()> { | ||
let mut history = history.write().unwrap(); | ||
let mut patterns = patterns.write().unwrap(); | ||
if let Some(action) = history.get_mut(&pattern_key).redo() { | ||
action.perform(&window, patterns.get_mut(&pattern_key).unwrap())?; | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
pub mod history; | ||
pub mod palette; | ||
pub mod path; | ||
pub mod pattern; | ||
pub mod stitches; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use crate::{ | ||
core::pattern::PaletteItem, | ||
state::{PatternKey, PatternsState}, | ||
}; | ||
|
||
#[tauri::command] | ||
pub fn add_palette_item(pattern_key: PatternKey, palette_item: PaletteItem, patterns: tauri::State<PatternsState>) { | ||
let mut patterns = patterns.write().unwrap(); | ||
let patproj = patterns.get_mut(&pattern_key).unwrap(); | ||
patproj.pattern.palette.push(palette_item); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::{ | ||
core::{ | ||
actions::{Action, AddStitchAction, RemoveStitchAction}, | ||
pattern::Stitch, | ||
}, | ||
error::CommandResult, | ||
state::{HistoryState, PatternKey, PatternsState}, | ||
}; | ||
|
||
#[tauri::command] | ||
pub fn add_stitch<R: tauri::Runtime>( | ||
pattern_key: PatternKey, | ||
stitch: Stitch, | ||
window: tauri::WebviewWindow<R>, | ||
history: tauri::State<HistoryState<R>>, | ||
patterns: tauri::State<PatternsState>, | ||
) -> CommandResult<()> { | ||
let mut history = history.write().unwrap(); | ||
let mut patterns = patterns.write().unwrap(); | ||
let action = AddStitchAction::new(stitch); | ||
action.perform(&window, patterns.get_mut(&pattern_key).unwrap())?; | ||
history.get_mut(&pattern_key).push(Box::new(action)); | ||
Ok(()) | ||
} | ||
|
||
#[tauri::command] | ||
pub fn remove_stitch<R: tauri::Runtime>( | ||
pattern_key: PatternKey, | ||
stitch: Stitch, | ||
window: tauri::WebviewWindow<R>, | ||
history: tauri::State<HistoryState<R>>, | ||
patterns: tauri::State<PatternsState>, | ||
) -> CommandResult<()> { | ||
let mut history = history.write().unwrap(); | ||
let mut patterns = patterns.write().unwrap(); | ||
let action = RemoveStitchAction::new(stitch); | ||
action.perform(&window, patterns.get_mut(&pattern_key).unwrap())?; | ||
history.get_mut(&pattern_key).push(Box::new(action)); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//! This module contains the definition of actions that can be performed on a pattern project. | ||
//! These actions include operations like adding or removing stitches or palette items, updating pattern information, etc. | ||
//! | ||
//! Actually, the actions implements the `Command` pattern. | ||
//! Hovewer we named it `Action` to avoid confusion with the `commands` from Tauri. | ||
//! | ||
//! Each method of the `Action` accepts a reference to the `WebviewWindow` and a mutable reference to the `PatternProject`. | ||
//! The `WebviewWindow` is used to emit events to the frontend. | ||
//! The reason for this is that the `Action` can affects many aspects of the `PatternProject` so it is easier to emit an event for each change. | ||
|
||
use anyhow::Result; | ||
use tauri::WebviewWindow; | ||
|
||
use super::pattern::PatternProject; | ||
|
||
mod stitches; | ||
pub use stitches::*; | ||
|
||
/// An action that can be executed and revoked. | ||
pub trait Action<R: tauri::Runtime>: Send + Sync + dyn_clone::DynClone { | ||
/// Perform the action. | ||
fn perform(&self, window: &WebviewWindow<R>, patproj: &mut PatternProject) -> Result<()>; | ||
|
||
/// Revoke (undo) the action. | ||
fn revoke(&self, window: &WebviewWindow<R>, patproj: &mut PatternProject) -> Result<()>; | ||
} | ||
|
||
dyn_clone::clone_trait_object!(<R: tauri::Runtime> Action<R>); | ||
|
||
#[cfg(debug_assertions)] | ||
#[derive(Clone)] | ||
pub struct MockAction; | ||
|
||
impl<R: tauri::Runtime> Action<R> for MockAction { | ||
fn perform(&self, _window: &WebviewWindow<R>, _patproj: &mut PatternProject) -> Result<()> { | ||
Ok(()) | ||
} | ||
|
||
fn revoke(&self, _window: &WebviewWindow<R>, _patproj: &mut PatternProject) -> Result<()> { | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.