summaryrefslogtreecommitdiff
path: root/rust/crates/commands/src/batch_command.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/crates/commands/src/batch_command.rs')
-rw-r--r--rust/crates/commands/src/batch_command.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/rust/crates/commands/src/batch_command.rs b/rust/crates/commands/src/batch_command.rs
new file mode 100644
index 00000000..bd928d33
--- /dev/null
+++ b/rust/crates/commands/src/batch_command.rs
@@ -0,0 +1,61 @@
+use scene::Project;
+
+use crate::command::{Command, CommandError, CommandResult};
+
+pub struct BatchCommand {
+ commands: Vec<Box<dyn Command>>,
+}
+
+impl BatchCommand {
+ pub fn new(commands: Vec<Box<dyn Command>>) -> Self {
+ Self { commands }
+ }
+
+ pub fn len(&self) -> usize {
+ self.commands.len()
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.commands.is_empty()
+ }
+}
+
+impl Command for BatchCommand {
+ fn execute(
+ &mut self,
+ project: &mut Project,
+ ) -> Result<Option<CommandResult>, CommandError> {
+ let mut latest_selection_result = None;
+
+ for command in &mut self.commands {
+ let result = command.execute(project)?;
+ if result.as_ref().and_then(|r| r.selection.as_ref()).is_some() {
+ latest_selection_result = result;
+ }
+ }
+
+ Ok(latest_selection_result)
+ }
+
+ fn undo(&mut self, project: &mut Project) {
+ for command in self.commands.iter_mut().rev() {
+ command.undo(project);
+ }
+ }
+
+ fn redo(
+ &mut self,
+ project: &mut Project,
+ ) -> Result<Option<CommandResult>, CommandError> {
+ let mut latest_selection_result = None;
+
+ for command in &mut self.commands {
+ let result = command.redo(project)?;
+ if result.as_ref().and_then(|r| r.selection.as_ref()).is_some() {
+ latest_selection_result = result;
+ }
+ }
+
+ Ok(latest_selection_result)
+ }
+}