record::extension
The record::extension module provides functionality for making undo and redo requests from outside the Reactor using events and triggers.
Types
RequestUndo
#![allow(unused)]
fn main() {
enum RequestUndo<Act> {
Once,
IndexTo(usize),
To(Act),
All,
}
}
Represents a request to undo operations. If an undo or redo is already in progress, the request will be ignored.
Once: Corresponds torecord::undo::onceIndexTo(usize): Corresponds torecord::undo::index_toTo(Act): Corresponds torecord::undo::toAll: Corresponds torecord::undo::all
RequestRedo
#![allow(unused)]
fn main() {
enum RequestRedo<Act> {
Once,
IndexTo(usize),
To(Act),
All,
}
}
Represents a request to redo operations. If an undo or redo is already in progress, the request will be ignored.
Once: Corresponds torecord::redo::onceIndexTo(usize): Corresponds torecord::redo::index_toTo(Act): Corresponds torecord::redo::toAll: Corresponds torecord::redo::all
Traits
RecordExtension
#![allow(unused)]
fn main() {
trait RecordExtension {
fn add_record<Act>(&mut self) -> &mut Self
where
Act: Clone + PartialEq + Send + Sync + 'static;
}
}
Allows undo and redo requests to be made using RequestUndo and RequestRedo from outside the Reactor.
Methods
add_record<Act>: Sets upRequestUndoandRequestRedoand their associated systems.
Examples
Using Events
#![allow(unused)]
fn main() {
use bevy::prelude::*;
use bevy_flurx::prelude::*;
#[derive(Resource, Default)]
struct UndoRedoState {
can_undo: bool,
can_redo: bool,
}
fn setup_app(app: &mut App) {
// Add record functionality for your operation type
app.add_record::<MyOperation>();
}
fn update_ui_system(
mut ui_state: ResMut<UndoRedoState>,
record: Res<Record<MyOperation>>,
) {
ui_state.can_undo = !record.tracks.is_empty();
ui_state.can_redo = !record.redo.is_empty();
}
fn handle_input(
keys: Res<Input<KeyCode>>,
mut undo_events: MessageWriter<RequestUndo<MyOperation>>,
mut redo_events: MessageWriter<RequestRedo<MyOperation>>,
) {
// Handle Ctrl+Z for undo
if keys.pressed(KeyCode::ControlLeft) && keys.just_pressed(KeyCode::Z) {
undo_events.send(RequestUndo::Once);
}
// Handle Ctrl+Y for redo
if keys.pressed(KeyCode::ControlLeft) && keys.just_pressed(KeyCode::Y) {
redo_events.send(RequestRedo::Once);
}
}
}
Using Triggers
#![allow(unused)]
fn main() {
use bevy::prelude::*;
use bevy_flurx::prelude::*;
fn handle_button_click(
interaction_query: Query<&Interaction, (Changed<Interaction>, With<UndoButton>)>,
world: &mut World,
) {
for interaction in interaction_query.iter() {
if *interaction == Interaction::Clicked {
// Trigger an undo operation
world.trigger(RequestUndo::<MyOperation>::Once);
}
}
}
}
How It Works
When you call app.add_record::<Act>(), the following happens:
- The
Record<Act>resource is initialized - Event types for
RequestUndo<Act>andRequestRedo<Act>are registered - Systems are added to handle these events and triggers
When a RequestUndo or RequestRedo event is sent or triggered:
- The corresponding system creates a new Reactor
- The Reactor executes the appropriate undo or redo action
- If the action fails (e.g., because an undo or redo is already in progress), the error is ignored
When to Use
Use the extension module when you need to:
- Trigger undo/redo operations from UI elements
- Handle keyboard shortcuts for undo/redo
- Integrate undo/redo functionality with other systems in your application
This module is particularly useful for applications with complex UI that need to provide undo/redo functionality through various means (buttons, keyboard shortcuts, etc.).