Skip to main content

text_document_frontend/commands/
undo_redo_commands.rs

1// Generated by Qleany v1.4.8 from frontend_undo_redo_commands.tera
2//! Undo/Redo commands
3#![allow(unused_imports, dead_code)]
4
5use crate::app_context::AppContext;
6use anyhow::{Context, Result};
7
8/// Undoes the most recent command on the specified stack.
9pub fn undo(ctx: &AppContext, stack_id: Option<u64>) -> Result<()> {
10    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
11    // Inject event hub before operation
12    undo_redo_manager.set_event_hub(&ctx.event_hub);
13
14    undo_redo_manager.undo(stack_id).context("undo")
15}
16
17/// Redoes the most recently undone command on the specified stack.
18pub fn redo(ctx: &AppContext, stack_id: Option<u64>) -> Result<()> {
19    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
20    undo_redo_manager.set_event_hub(&ctx.event_hub);
21
22    undo_redo_manager.redo(stack_id).context("redo")
23}
24
25/// Checks if there are commands that can be undone on the specified stack.
26pub fn can_undo(ctx: &AppContext, stack_id: Option<u64>) -> bool {
27    let undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
28    undo_redo_manager.can_undo(stack_id)
29}
30
31/// Checks if there are commands that can be redone on the specified stack.
32pub fn can_redo(ctx: &AppContext, stack_id: Option<u64>) -> bool {
33    let undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
34    undo_redo_manager.can_redo(stack_id)
35}
36
37/// Begins a composite command group.
38pub fn begin_composite(ctx: &AppContext, stack_id: Option<u64>) {
39    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
40    undo_redo_manager.set_event_hub(&ctx.event_hub);
41    let _ = undo_redo_manager.begin_composite(stack_id);
42}
43
44/// Ends the current composite command group and adds it to the specified stack.
45pub fn end_composite(ctx: &AppContext) {
46    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
47    undo_redo_manager.set_event_hub(&ctx.event_hub);
48    undo_redo_manager.end_composite();
49}
50
51/// Cancels the current composite command group, before it is saved to the stack.
52pub fn cancel_composite(ctx: &AppContext) {
53    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
54    undo_redo_manager.set_event_hub(&ctx.event_hub);
55    undo_redo_manager.cancel_composite();
56}
57
58/// Clears the undo and redo stacks for a specific stack ID.
59pub fn clear_stack(ctx: &AppContext, stack_id: u64) {
60    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
61    undo_redo_manager.clear_stack(stack_id);
62}
63
64/// Clears the undo and redo stacks.
65pub fn clear_all_stacks(ctx: &AppContext) {
66    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
67    undo_redo_manager.clear_all_stacks();
68}
69
70/// Gets the size of the undo stack.
71pub fn get_stack_size(ctx: &AppContext, stack_id: u64) -> usize {
72    let undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
73    undo_redo_manager.get_stack_size(stack_id)
74}
75
76/// Creates a new undo/redo stack and returns its ID.
77pub fn create_new_stack(ctx: &AppContext) -> u64 {
78    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
79    undo_redo_manager.create_new_stack()
80}
81
82/// Deletes an undo/redo stack by its ID.
83pub fn delete_stack(ctx: &AppContext, stack_id: u64) -> Result<()> {
84    let mut undo_redo_manager = ctx.undo_redo_manager.lock().unwrap();
85    undo_redo_manager
86        .delete_stack(stack_id)
87        .context("deleting undo/redo stack")
88}