Prevent video and audio clips from overlapping

This commit is contained in:
Skyler Lehmkuhl
2025-12-03 02:28:23 -05:00
parent ccb29a9e04
commit ef1956e8e3
18 changed files with 880 additions and 229 deletions
@@ -59,50 +59,81 @@ impl AddClipInstanceAction {
}
impl Action for AddClipInstanceAction {
fn execute(&mut self, document: &mut Document) {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
match layer {
AnyLayer::Vector(vector_layer) => {
vector_layer.clip_instances.push(self.clip_instance.clone());
}
AnyLayer::Audio(audio_layer) => {
audio_layer.clip_instances.push(self.clip_instance.clone());
}
AnyLayer::Video(video_layer) => {
video_layer.clip_instances.push(self.clip_instance.clone());
}
}
self.executed = true;
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
// Calculate the clip's effective duration
let clip_duration = document.get_clip_duration(&self.clip_instance.clip_id)
.ok_or_else(|| format!("Clip {} not found", self.clip_instance.clip_id))?;
let trim_start = self.clip_instance.trim_start;
let trim_end = self.clip_instance.trim_end.unwrap_or(clip_duration);
let effective_duration = trim_end - trim_start;
// Auto-adjust position for audio/video layers to avoid overlaps
let adjusted_start = document.find_nearest_valid_position(
&self.layer_id,
self.clip_instance.timeline_start,
effective_duration,
None, // Not excluding any instance
);
if let Some(valid_start) = adjusted_start {
// Update instance to use the valid position
self.clip_instance.timeline_start = valid_start;
} else {
// No valid position found - reject the operation
return Err("Cannot add clip: no valid position found on layer (layer is full)".to_string());
}
// Add the clip instance with adjusted position
let layer = document.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
match layer {
AnyLayer::Vector(vector_layer) => {
vector_layer.clip_instances.push(self.clip_instance.clone());
}
AnyLayer::Audio(audio_layer) => {
audio_layer.clip_instances.push(self.clip_instance.clone());
}
AnyLayer::Video(video_layer) => {
video_layer.clip_instances.push(self.clip_instance.clone());
}
}
self.executed = true;
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
if !self.executed {
return;
return Ok(());
}
let instance_id = self.clip_instance.id;
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
match layer {
AnyLayer::Vector(vector_layer) => {
vector_layer
.clip_instances
.retain(|ci| ci.id != instance_id);
}
AnyLayer::Audio(audio_layer) => {
audio_layer
.clip_instances
.retain(|ci| ci.id != instance_id);
}
AnyLayer::Video(video_layer) => {
video_layer
.clip_instances
.retain(|ci| ci.id != instance_id);
}
let layer = document.get_layer_mut(&self.layer_id)
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
match layer {
AnyLayer::Vector(vector_layer) => {
vector_layer
.clip_instances
.retain(|ci| ci.id != instance_id);
}
AnyLayer::Audio(audio_layer) => {
audio_layer
.clip_instances
.retain(|ci| ci.id != instance_id);
}
AnyLayer::Video(video_layer) => {
video_layer
.clip_instances
.retain(|ci| ci.id != instance_id);
}
self.executed = false;
}
self.executed = false;
Ok(())
}
fn description(&self) -> String {
@@ -261,7 +292,7 @@ mod tests {
// Execute action
let mut action = AddClipInstanceAction::new(layer_id, clip_instance);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify clip instance was added
if let Some(AnyLayer::Vector(vector_layer)) = document.get_layer(&layer_id) {
@@ -272,7 +303,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify clip instance was removed
if let Some(AnyLayer::Vector(vector_layer)) = document.get_layer(&layer_id) {
@@ -49,15 +49,17 @@ impl AddLayerAction {
}
impl Action for AddLayerAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
// Add layer to the document's root
let layer_id = document.root_mut().add_child(self.layer.clone());
// Store the ID for rollback
self.created_layer_id = Some(layer_id);
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
// Remove the created layer if it exists
if let Some(layer_id) = self.created_layer_id {
document.root_mut().remove_child(&layer_id);
@@ -65,6 +67,8 @@ impl Action for AddLayerAction {
// Clear the stored ID
self.created_layer_id = None;
}
Ok(())
}
fn description(&self) -> String {
@@ -88,7 +92,7 @@ mod tests {
// Create and execute action
let mut action = AddLayerAction::new_vector("New Layer");
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify layer was added
assert_eq!(document.root.children.len(), 1);
@@ -97,7 +101,7 @@ mod tests {
assert!(matches!(layer, AnyLayer::Vector(_)));
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify layer was removed
assert_eq!(document.root.children.len(), 0);
@@ -116,8 +120,8 @@ mod tests {
let mut action1 = AddLayerAction::new_vector("Layer 1");
let mut action2 = AddLayerAction::new_vector("Layer 2");
action1.execute(&mut document);
action2.execute(&mut document);
action1.execute(&mut document).unwrap();
action2.execute(&mut document).unwrap();
assert_eq!(document.root.children.len(), 2);
assert_eq!(document.root.children[0].layer().name, "Layer 1");
@@ -50,10 +50,10 @@ impl AddShapeAction {
}
impl Action for AddShapeAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return,
None => return Ok(()),
};
if let AnyLayer::Vector(vector_layer) = layer {
@@ -65,14 +65,15 @@ impl Action for AddShapeAction {
self.created_shape_id = Some(shape_id);
self.created_object_id = Some(object_id);
}
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
// Remove the created shape and object if they exist
if let (Some(shape_id), Some(object_id)) = (self.created_shape_id, self.created_object_id) {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return,
None => return Ok(()),
};
if let AnyLayer::Vector(vector_layer) = layer {
@@ -85,6 +86,7 @@ impl Action for AddShapeAction {
self.created_shape_id = None;
self.created_object_id = None;
}
Ok(())
}
fn description(&self) -> String {
@@ -114,7 +116,7 @@ mod tests {
// Create and execute action
let mut action = AddShapeAction::new(layer_id, shape, object);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify shape and object were added
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
@@ -129,7 +131,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify shape and object were removed
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
@@ -157,7 +159,7 @@ mod tests {
assert_eq!(action.description(), "Add shape");
// Execute
action.execute(&mut document);
action.execute(&mut document).unwrap();
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
assert_eq!(layer.shapes.len(), 1);
@@ -180,8 +182,8 @@ mod tests {
// Execute twice - shapes are stored in HashMap (keyed by ID, so same shape overwrites)
// while shape_instances are stored in Vec (so duplicates accumulate)
action.execute(&mut document);
action.execute(&mut document);
action.execute(&mut document).unwrap();
action.execute(&mut document).unwrap();
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
// Shapes use HashMap keyed by shape.id, so same shape overwrites = 1
@@ -209,8 +211,8 @@ mod tests {
let mut action1 = AddShapeAction::new(layer_id, shape1, object1);
let mut action2 = AddShapeAction::new(layer_id, shape2, object2);
action1.execute(&mut document);
action2.execute(&mut document);
action1.execute(&mut document).unwrap();
action2.execute(&mut document).unwrap();
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
// Two different shapes = 2 entries in HashMap
@@ -26,7 +26,7 @@ impl MoveClipInstancesAction {
}
impl Action for MoveClipInstancesAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
// Expand moves to include grouped instances
let mut expanded_moves = self.layer_moves.clone();
let mut already_processed = std::collections::HashSet::new();
@@ -71,15 +71,67 @@ impl Action for MoveClipInstancesAction {
}
}
// Store expanded moves for rollback
self.layer_moves = expanded_moves.clone();
// Auto-adjust moves to avoid overlaps
let mut adjusted_moves: HashMap<Uuid, Vec<(Uuid, f64, f64)>> = HashMap::new();
// Apply all moves (including expanded)
for (layer_id, moves) in &expanded_moves {
let layer = match document.get_layer_mut(layer_id) {
Some(l) => l,
None => continue,
};
let layer = document.get_layer(layer_id)
.ok_or_else(|| format!("Layer {} not found", layer_id))?;
// Vector layers don't need adjustment
if matches!(layer, AnyLayer::Vector(_)) {
adjusted_moves.insert(*layer_id, moves.clone());
continue;
}
let mut adjusted_layer_moves = Vec::new();
for (instance_id, old_start, new_start) in moves {
// Get the instance to calculate its duration
let clip_instances = match layer {
AnyLayer::Audio(al) => &al.clip_instances,
AnyLayer::Video(vl) => &vl.clip_instances,
AnyLayer::Vector(vl) => &vl.clip_instances,
};
let instance = clip_instances.iter()
.find(|ci| &ci.id == instance_id)
.ok_or_else(|| format!("Instance {} not found", instance_id))?;
let clip_duration = document.get_clip_duration(&instance.clip_id)
.ok_or_else(|| format!("Clip {} not found", instance.clip_id))?;
let trim_start = instance.trim_start;
let trim_end = instance.trim_end.unwrap_or(clip_duration);
let effective_duration = trim_end - trim_start;
// Find nearest valid position, excluding this instance from overlap checks
let adjusted_start = document.find_nearest_valid_position(
layer_id,
*new_start,
effective_duration,
Some(instance_id),
);
if let Some(valid_start) = adjusted_start {
adjusted_layer_moves.push((*instance_id, *old_start, valid_start));
} else {
return Err(format!(
"Cannot move clip: no valid position found on layer"
));
}
}
adjusted_moves.insert(*layer_id, adjusted_layer_moves);
}
// Store adjusted moves for rollback
self.layer_moves = adjusted_moves.clone();
// Apply all adjusted moves
for (layer_id, moves) in &adjusted_moves {
let layer = document.get_layer_mut(layer_id)
.ok_or_else(|| format!("Layer {} not found", layer_id))?;
// Get mutable reference to clip_instances for this layer type
let clip_instances = match layer {
@@ -96,14 +148,14 @@ impl Action for MoveClipInstancesAction {
}
}
}
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
for (layer_id, moves) in &self.layer_moves {
let layer = match document.get_layer_mut(layer_id) {
Some(l) => l,
None => continue,
};
let layer = document.get_layer_mut(layer_id)
.ok_or_else(|| format!("Layer {} not found", layer_id))?;
// Get mutable reference to clip_instances for this layer type
let clip_instances = match layer {
@@ -120,6 +172,8 @@ impl Action for MoveClipInstancesAction {
}
}
}
Ok(())
}
fn description(&self) -> String {
@@ -296,7 +350,7 @@ mod tests {
let mut action = MoveClipInstancesAction::new(layer_moves);
// Execute
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify position changed
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
@@ -309,7 +363,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify position restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
@@ -34,10 +34,10 @@ impl MoveShapeInstancesAction {
}
impl Action for MoveShapeInstancesAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return,
None => return Ok(()),
};
if let AnyLayer::Vector(vector_layer) = layer {
@@ -48,12 +48,13 @@ impl Action for MoveShapeInstancesAction {
});
}
}
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return,
None => return Ok(()),
};
if let AnyLayer::Vector(vector_layer) = layer {
@@ -64,6 +65,7 @@ impl Action for MoveShapeInstancesAction {
});
}
}
Ok(())
}
fn description(&self) -> String {
@@ -109,7 +111,7 @@ mod tests {
let mut action = MoveShapeInstancesAction::new(layer_id, positions);
// Execute
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify position changed
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
@@ -119,7 +121,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify position restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
@@ -68,7 +68,7 @@ impl PaintBucketAction {
}
impl Action for PaintBucketAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
println!("=== PaintBucketAction::execute ===");
// Optimization: Check if we're clicking on an existing shape first
@@ -116,7 +116,7 @@ impl Action for PaintBucketAction {
println!("Updated shape fill color");
}
return; // Done! No need to create a new shape
return Ok(()); // Done! No need to create a new shape
}
}
}
@@ -131,7 +131,7 @@ impl Action for PaintBucketAction {
if all_curves.is_empty() {
println!("No curves found, returning");
return;
return Ok(());
}
// Step 2: Build planar graph
@@ -176,14 +176,15 @@ impl Action for PaintBucketAction {
}
println!("=== Paint Bucket Complete: Face filled with curves ===");
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
// Remove the created shape and object if they exist
if let (Some(shape_id), Some(object_id)) = (self.created_shape_id, self.created_shape_instance_id) {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return,
None => return Ok(()),
};
if let AnyLayer::Vector(vector_layer) = layer {
@@ -194,6 +195,7 @@ impl Action for PaintBucketAction {
self.created_shape_id = None;
self.created_shape_instance_id = None;
}
Ok(())
}
fn description(&self) -> String {
@@ -331,7 +333,7 @@ mod tests {
GapHandlingMode::BridgeSegment,
);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify a filled shape was created
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
@@ -343,7 +345,7 @@ mod tests {
}
// Test rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
// Should only have original shape
@@ -88,7 +88,7 @@ impl SetDocumentPropertiesAction {
}
impl Action for SetDocumentPropertiesAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
// Store old value if not already stored
if self.old_value.is_none() {
self.old_value = Some(self.get_current_value(document));
@@ -97,12 +97,14 @@ impl Action for SetDocumentPropertiesAction {
// Apply new value
let new_value = self.property.value();
self.apply_value(document, new_value);
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(old_value) = self.old_value {
self.apply_value(document, old_value);
}
Ok(())
}
fn description(&self) -> String {
@@ -126,10 +128,10 @@ mod tests {
document.width = 1920.0;
let mut action = SetDocumentPropertiesAction::set_width(1280.0);
action.execute(&mut document);
action.execute(&mut document).unwrap();
assert_eq!(document.width, 1280.0);
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
assert_eq!(document.width, 1920.0);
}
@@ -139,10 +141,10 @@ mod tests {
document.height = 1080.0;
let mut action = SetDocumentPropertiesAction::set_height(720.0);
action.execute(&mut document);
action.execute(&mut document).unwrap();
assert_eq!(document.height, 720.0);
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
assert_eq!(document.height, 1080.0);
}
@@ -152,10 +154,10 @@ mod tests {
document.duration = 10.0;
let mut action = SetDocumentPropertiesAction::set_duration(30.0);
action.execute(&mut document);
action.execute(&mut document).unwrap();
assert_eq!(document.duration, 30.0);
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
assert_eq!(document.duration, 10.0);
}
@@ -165,10 +167,10 @@ mod tests {
document.framerate = 30.0;
let mut action = SetDocumentPropertiesAction::set_framerate(60.0);
action.execute(&mut document);
action.execute(&mut document).unwrap();
assert_eq!(document.framerate, 60.0);
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
assert_eq!(document.framerate, 30.0);
}
@@ -109,7 +109,7 @@ impl SetInstancePropertiesAction {
}
impl Action for SetInstancePropertiesAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let new_value = self.property.value();
let layer_id = self.layer_id;
@@ -140,14 +140,16 @@ impl Action for SetInstancePropertiesAction {
for (instance_id, _) in &self.instance_changes {
self.apply_to_instance(document, instance_id, new_value);
}
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
for (instance_id, old_value) in &self.instance_changes {
if let Some(value) = old_value {
self.apply_to_instance(document, instance_id, *value);
}
}
Ok(())
}
fn description(&self) -> String {
@@ -195,7 +197,7 @@ mod tests {
instance_id,
InstancePropertyChange::X(50.0),
);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify position changed
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -205,7 +207,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -233,7 +235,7 @@ mod tests {
instance_id,
InstancePropertyChange::Rotation(45.0),
);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify rotation changed
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -242,7 +244,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -270,7 +272,7 @@ mod tests {
instance_id,
InstancePropertyChange::Opacity(0.5),
);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify opacity changed
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -279,7 +281,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -314,7 +316,7 @@ mod tests {
vec![instance1_id, instance2_id],
InstancePropertyChange::ScaleX(2.0),
);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify both changed
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -323,7 +325,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify both restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -74,7 +74,7 @@ impl SetLayerPropertiesAction {
}
impl Action for SetLayerPropertiesAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
for (i, &layer_id) in self.layer_ids.iter().enumerate() {
// Find the layer in the document
if let Some(layer) = document.root_mut().get_child_mut(&layer_id) {
@@ -101,9 +101,10 @@ impl Action for SetLayerPropertiesAction {
}
}
}
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
for (i, &layer_id) in self.layer_ids.iter().enumerate() {
// Find the layer in the document
if let Some(layer) = document.root_mut().get_child_mut(&layer_id) {
@@ -120,6 +121,7 @@ impl Action for SetLayerPropertiesAction {
}
}
}
Ok(())
}
fn description(&self) -> String {
@@ -157,14 +159,14 @@ mod tests {
// Create and execute action
let mut action = SetLayerPropertiesAction::new(layer_id, LayerProperty::Volume(0.5));
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify volume changed
let layer_ref = document.root.get_child(&layer_id).unwrap();
assert_eq!(layer_ref.volume(), 0.5);
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify volume restored
let layer_ref = document.root.get_child(&layer_id).unwrap();
@@ -183,13 +185,13 @@ mod tests {
// Mute
let mut action = SetLayerPropertiesAction::new(layer_id, LayerProperty::Muted(true));
action.execute(&mut document);
action.execute(&mut document).unwrap();
let layer_ref = document.root.get_child(&layer_id).unwrap();
assert_eq!(layer_ref.muted(), true);
// Unmute via rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
let layer_ref = document.root.get_child(&layer_id).unwrap();
assert_eq!(layer_ref.muted(), false);
@@ -208,14 +210,14 @@ mod tests {
vec![id1, id2],
LayerProperty::Soloed(true),
);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify both soloed
assert_eq!(document.root.get_child(&id1).unwrap().soloed(), true);
assert_eq!(document.root.get_child(&id2).unwrap().soloed(), true);
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify both unsoloed
assert_eq!(document.root.get_child(&id1).unwrap().soloed(), false);
@@ -234,13 +236,13 @@ mod tests {
// Lock
let mut action = SetLayerPropertiesAction::new(layer_id, LayerProperty::Locked(true));
action.execute(&mut document);
action.execute(&mut document).unwrap();
let layer_ref = document.root.get_child(&layer_id).unwrap();
assert_eq!(layer_ref.locked(), true);
// Unlock via rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
let layer_ref = document.root.get_child(&layer_id).unwrap();
assert_eq!(layer_ref.locked(), false);
@@ -258,13 +260,13 @@ mod tests {
// Set opacity to 0.5
let mut action = SetLayerPropertiesAction::new(layer_id, LayerProperty::Opacity(0.5));
action.execute(&mut document);
action.execute(&mut document).unwrap();
let layer_ref = document.root.get_child(&layer_id).unwrap();
assert_eq!(layer_ref.opacity(), 0.5);
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
let layer_ref = document.root.get_child(&layer_id).unwrap();
assert_eq!(layer_ref.opacity(), 1.0);
@@ -282,13 +284,13 @@ mod tests {
// Hide
let mut action = SetLayerPropertiesAction::new(layer_id, LayerProperty::Visible(false));
action.execute(&mut document);
action.execute(&mut document).unwrap();
let layer_ref = document.root.get_child(&layer_id).unwrap();
assert_eq!(layer_ref.visible(), false);
// Show via rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
let layer_ref = document.root.get_child(&layer_id).unwrap();
assert_eq!(layer_ref.visible(), true);
@@ -307,14 +309,14 @@ mod tests {
vec![id1, id2],
LayerProperty::Locked(true),
);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify both locked
assert_eq!(document.root.get_child(&id1).unwrap().locked(), true);
assert_eq!(document.root.get_child(&id2).unwrap().locked(), true);
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify both unlocked
assert_eq!(document.root.get_child(&id1).unwrap().locked(), false);
@@ -334,14 +336,14 @@ mod tests {
vec![id1, id2],
LayerProperty::Opacity(0.25),
);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify both have reduced opacity
assert_eq!(document.root.get_child(&id1).unwrap().opacity(), 0.25);
assert_eq!(document.root.get_child(&id2).unwrap().opacity(), 0.25);
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify both restored to 1.0
assert_eq!(document.root.get_child(&id1).unwrap().opacity(), 1.0);
@@ -386,7 +388,7 @@ mod tests {
let mut action = SetLayerPropertiesAction::new(fake_id, LayerProperty::Locked(true));
// Should not panic
action.execute(&mut document);
action.rollback(&mut document);
action.execute(&mut document).unwrap();
action.rollback(&mut document).unwrap();
}
}
@@ -60,7 +60,7 @@ impl SetShapePropertiesAction {
}
impl Action for SetShapePropertiesAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
if let Some(shape) = vector_layer.shapes.get_mut(&self.shape_id) {
@@ -107,9 +107,10 @@ impl Action for SetShapePropertiesAction {
}
}
}
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(old_value) = &self.old_value {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
@@ -131,6 +132,7 @@ impl Action for SetShapePropertiesAction {
}
}
}
Ok(())
}
fn description(&self) -> String {
@@ -187,7 +189,7 @@ mod tests {
// Create and execute action
let new_color = Some(ShapeColor::rgb(0, 255, 0));
let mut action = SetShapePropertiesAction::set_fill_color(layer_id, shape_id, new_color);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify color changed
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -196,7 +198,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -224,7 +226,7 @@ mod tests {
// Create and execute action
let mut action = SetShapePropertiesAction::set_stroke_width(layer_id, shape_id, 5.0);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify width changed
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -233,7 +235,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -29,10 +29,10 @@ impl TransformClipInstancesAction {
}
impl Action for TransformClipInstancesAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return,
None => return Ok(()),
};
// Get mutable reference to clip_instances for this layer type
@@ -48,12 +48,13 @@ impl Action for TransformClipInstancesAction {
clip_instance.transform = new.clone();
}
}
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
let layer = match document.get_layer_mut(&self.layer_id) {
Some(l) => l,
None => return,
None => return Ok(()),
};
// Get mutable reference to clip_instances for this layer type
@@ -69,6 +70,7 @@ impl Action for TransformClipInstancesAction {
clip_instance.transform = old.clone();
}
}
Ok(())
}
fn description(&self) -> String {
@@ -108,7 +110,7 @@ mod tests {
let mut action = TransformClipInstancesAction::new(layer_id, transforms);
// Execute action
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify transform changed
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -120,7 +122,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify transform restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -153,7 +155,7 @@ mod tests {
transforms.insert(instance_id, (old_transform, new_transform));
let mut action = TransformClipInstancesAction::new(layer_id, transforms);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify
if let Some(AnyLayer::Audio(al)) = document.get_layer_mut(&layer_id) {
@@ -194,7 +196,7 @@ mod tests {
transforms.insert(instance_id, (old_transform, new_transform));
let mut action = TransformClipInstancesAction::new(layer_id, transforms);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify rotation and scale
if let Some(AnyLayer::Video(vl)) = document.get_layer_mut(&layer_id) {
@@ -240,7 +242,7 @@ mod tests {
);
let mut action = TransformClipInstancesAction::new(layer_id, transforms);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify both transformed
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -256,7 +258,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify both restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -287,8 +289,8 @@ mod tests {
let mut action = TransformClipInstancesAction::new(fake_layer_id, transforms);
// Should not panic, just return early
action.execute(&mut document);
action.rollback(&mut document);
action.execute(&mut document).unwrap();
action.rollback(&mut document).unwrap();
}
#[test]
@@ -30,7 +30,7 @@ impl TransformShapeInstancesAction {
}
impl Action for TransformShapeInstancesAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
for (shape_instance_id, (_old, new)) in &self.shape_instance_transforms {
@@ -40,9 +40,10 @@ impl Action for TransformShapeInstancesAction {
}
}
}
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
if let Some(layer) = document.get_layer_mut(&self.layer_id) {
if let AnyLayer::Vector(vector_layer) = layer {
for (shape_instance_id, (old, _new)) in &self.shape_instance_transforms {
@@ -52,6 +53,7 @@ impl Action for TransformShapeInstancesAction {
}
}
}
Ok(())
}
fn description(&self) -> String {
@@ -89,7 +91,7 @@ mod tests {
let mut action = TransformShapeInstancesAction::new(layer_id, transforms);
// Execute
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify transform changed
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -101,7 +103,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -140,7 +142,7 @@ mod tests {
transforms.insert(instance_id, (old_transform, new_transform));
let mut action = TransformShapeInstancesAction::new(layer_id, transforms);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -187,7 +189,7 @@ mod tests {
);
let mut action = TransformShapeInstancesAction::new(layer_id, transforms);
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify both transformed
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -203,7 +205,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify both restored
if let Some(AnyLayer::Vector(vl)) = document.get_layer_mut(&layer_id) {
@@ -234,8 +236,8 @@ mod tests {
let mut action = TransformShapeInstancesAction::new(fake_layer_id, transforms);
// Should not panic
action.execute(&mut document);
action.rollback(&mut document);
action.execute(&mut document).unwrap();
action.rollback(&mut document).unwrap();
}
#[test]
@@ -254,8 +256,8 @@ mod tests {
let mut action = TransformShapeInstancesAction::new(layer_id, transforms);
// Should not panic - just silently skip nonexistent instance
action.execute(&mut document);
action.rollback(&mut document);
action.execute(&mut document).unwrap();
action.rollback(&mut document).unwrap();
}
#[test]
@@ -276,8 +278,8 @@ mod tests {
let mut action = TransformShapeInstancesAction::new(layer_id, transforms);
// Should not panic - action only operates on vector layers
action.execute(&mut document);
action.rollback(&mut document);
action.execute(&mut document).unwrap();
action.rollback(&mut document).unwrap();
}
#[test]
@@ -62,7 +62,7 @@ impl TrimClipInstancesAction {
}
impl Action for TrimClipInstancesAction {
fn execute(&mut self, document: &mut Document) {
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
// Expand trims to include grouped instances
let mut expanded_trims = self.layer_trims.clone();
let mut already_processed = std::collections::HashSet::new();
@@ -155,11 +155,103 @@ impl Action for TrimClipInstancesAction {
}
}
// Store expanded trims for rollback
self.layer_trims = expanded_trims.clone();
// Auto-clamp trims to avoid overlaps when extending clips
let mut clamped_trims: HashMap<Uuid, Vec<(Uuid, TrimType, TrimData, TrimData)>> = HashMap::new();
// Apply all trims (including expanded)
for (layer_id, trims) in &expanded_trims {
let layer = document.get_layer(layer_id)
.ok_or_else(|| format!("Layer {} not found", layer_id))?;
// Only validate for audio/video layers
let should_validate = matches!(layer, AnyLayer::Audio(_) | AnyLayer::Video(_));
let mut clamped_layer_trims = Vec::new();
for (instance_id, trim_type, old, new) in trims {
let clip_instances = match layer {
AnyLayer::Audio(al) => &al.clip_instances,
AnyLayer::Video(vl) => &vl.clip_instances,
AnyLayer::Vector(vl) => &vl.clip_instances,
};
let instance = clip_instances.iter()
.find(|ci| &ci.id == instance_id)
.ok_or_else(|| format!("Instance {} not found", instance_id))?;
let clip_duration = document.get_clip_duration(&instance.clip_id)
.ok_or_else(|| format!("Clip {} not found", instance.clip_id))?;
let mut clamped_new = new.clone();
match trim_type {
TrimType::TrimLeft => {
if let (Some(old_trim), Some(new_trim), Some(old_timeline), Some(new_timeline)) =
(old.trim_value, new.trim_value, old.timeline_start, new.timeline_start)
{
// If extending to the left (new_trim < old_trim)
if should_validate && new_trim < old_trim {
// Find the maximum we can extend left
let max_extend = document.find_max_trim_extend_left(
layer_id,
instance_id,
instance.timeline_start,
);
// Calculate how much we want to extend
let desired_extend = old_trim - new_trim;
// Clamp to max allowed
let actual_extend = desired_extend.min(max_extend);
let clamped_trim_start = old_trim - actual_extend;
let clamped_timeline_start = old_timeline - actual_extend;
clamped_new = TrimData::left(clamped_trim_start, clamped_timeline_start);
}
}
}
TrimType::TrimRight => {
let old_trim_end = old.trim_value.unwrap_or(clip_duration);
let new_trim_end = new.trim_value.unwrap_or(clip_duration);
// If extending to the right (new_trim_end > old_trim_end)
if should_validate && new_trim_end > old_trim_end {
// Calculate current effective duration
let current_effective_duration = old_trim_end - instance.trim_start;
// Find the maximum we can extend right
let max_extend = document.find_max_trim_extend_right(
layer_id,
instance_id,
instance.timeline_start,
current_effective_duration,
);
// Calculate how much we want to extend
let desired_extend = new_trim_end - old_trim_end;
// Clamp to max allowed
let actual_extend = desired_extend.min(max_extend);
let clamped_trim_end = old_trim_end + actual_extend;
// Don't exceed clip duration
let final_trim_end = clamped_trim_end.min(clip_duration);
clamped_new = TrimData::right(Some(final_trim_end));
}
}
}
clamped_layer_trims.push((*instance_id, *trim_type, old.clone(), clamped_new));
}
clamped_trims.insert(*layer_id, clamped_layer_trims);
}
// Store clamped trims for rollback
self.layer_trims = clamped_trims.clone();
// Apply all clamped trims
for (layer_id, trims) in &clamped_trims {
let layer = match document.get_layer_mut(layer_id) {
Some(l) => l,
None => continue,
@@ -192,9 +284,10 @@ impl Action for TrimClipInstancesAction {
}
}
}
Ok(())
}
fn rollback(&mut self, document: &mut Document) {
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
for (layer_id, trims) in &self.layer_trims {
let layer = match document.get_layer_mut(layer_id) {
Some(l) => l,
@@ -228,6 +321,7 @@ impl Action for TrimClipInstancesAction {
}
}
}
Ok(())
}
fn description(&self) -> String {
@@ -427,7 +521,7 @@ mod tests {
let mut action = TrimClipInstancesAction::new(layer_trims);
// Execute
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify trim applied
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
@@ -441,7 +535,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
@@ -486,7 +580,7 @@ mod tests {
let mut action = TrimClipInstancesAction::new(layer_trims);
// Execute
action.execute(&mut document);
action.execute(&mut document).unwrap();
// Verify trim applied
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {
@@ -499,7 +593,7 @@ mod tests {
}
// Rollback
action.rollback(&mut document);
action.rollback(&mut document).unwrap();
// Verify restored
if let Some(AnyLayer::Vector(layer)) = document.get_layer(&layer_id) {