Add copy and paste
This commit is contained in:
@@ -40,3 +40,6 @@ pathdiff = "0.2"
|
||||
# Audio encoding for embedded files
|
||||
flacenc = "0.4" # For FLAC encoding (lossless)
|
||||
claxon = "0.4" # For FLAC decoding
|
||||
|
||||
# System clipboard
|
||||
arboard = "3"
|
||||
|
||||
@@ -226,12 +226,14 @@ impl Action for AddClipInstanceAction {
|
||||
// For sampled audio, send AddAudioClipSync query
|
||||
use daw_backend::command::{Query, QueryResponse};
|
||||
|
||||
let duration = clip.duration;
|
||||
let internal_start = self.clip_instance.trim_start;
|
||||
let internal_end = self.clip_instance.trim_end.unwrap_or(clip.duration);
|
||||
let effective_duration = self.clip_instance.timeline_duration
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
let start_time = self.clip_instance.timeline_start;
|
||||
let offset = self.clip_instance.trim_start;
|
||||
|
||||
let query =
|
||||
Query::AddAudioClipSync(*backend_track_id, *audio_pool_index, start_time, duration, offset);
|
||||
Query::AddAudioClipSync(*backend_track_id, *audio_pool_index, start_time, effective_duration, internal_start);
|
||||
|
||||
match controller.send_query(query)? {
|
||||
QueryResponse::AudioClipInstanceAdded(Ok(instance_id)) => {
|
||||
|
||||
@@ -25,6 +25,8 @@ pub mod rename_folder;
|
||||
pub mod delete_folder;
|
||||
pub mod move_asset_to_folder;
|
||||
pub mod update_midi_notes;
|
||||
pub mod remove_clip_instances;
|
||||
pub mod remove_shapes;
|
||||
|
||||
pub use add_clip_instance::AddClipInstanceAction;
|
||||
pub use add_effect::AddEffectAction;
|
||||
@@ -48,3 +50,5 @@ pub use rename_folder::RenameFolderAction;
|
||||
pub use delete_folder::{DeleteFolderAction, DeleteStrategy};
|
||||
pub use move_asset_to_folder::MoveAssetToFolderAction;
|
||||
pub use update_midi_notes::UpdateMidiNotesAction;
|
||||
pub use remove_clip_instances::RemoveClipInstancesAction;
|
||||
pub use remove_shapes::RemoveShapesAction;
|
||||
|
||||
@@ -0,0 +1,269 @@
|
||||
//! Remove clip instances action
|
||||
//!
|
||||
//! Handles removing one or more clip instances from layers (for cut/delete).
|
||||
|
||||
use crate::action::{Action, BackendClipInstanceId, BackendContext};
|
||||
use crate::clip::ClipInstance;
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Action that removes clip instances from layers
|
||||
pub struct RemoveClipInstancesAction {
|
||||
/// (layer_id, instance_id) pairs to remove
|
||||
removals: Vec<(Uuid, Uuid)>,
|
||||
/// Saved instances for rollback (layer_id -> ClipInstance)
|
||||
saved: Vec<(Uuid, ClipInstance)>,
|
||||
/// Saved backend mappings for rollback (instance_id -> BackendClipInstanceId)
|
||||
saved_backend_ids: HashMap<Uuid, BackendClipInstanceId>,
|
||||
}
|
||||
|
||||
impl RemoveClipInstancesAction {
|
||||
/// Create a new remove clip instances action
|
||||
pub fn new(removals: Vec<(Uuid, Uuid)>) -> Self {
|
||||
Self {
|
||||
removals,
|
||||
saved: Vec::new(),
|
||||
saved_backend_ids: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for RemoveClipInstancesAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
self.saved.clear();
|
||||
|
||||
for (layer_id, instance_id) in &self.removals {
|
||||
let layer = document
|
||||
.get_layer_mut(layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", layer_id))?;
|
||||
|
||||
let clip_instances = match layer {
|
||||
AnyLayer::Vector(vl) => &mut vl.clip_instances,
|
||||
AnyLayer::Audio(al) => &mut al.clip_instances,
|
||||
AnyLayer::Video(vl) => &mut vl.clip_instances,
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
};
|
||||
|
||||
// Find and remove the instance, saving it for rollback
|
||||
if let Some(pos) = clip_instances.iter().position(|ci| ci.id == *instance_id) {
|
||||
let removed = clip_instances.remove(pos);
|
||||
self.saved.push((*layer_id, removed));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
// Re-insert saved instances
|
||||
for (layer_id, instance) in self.saved.drain(..).rev() {
|
||||
let layer = document
|
||||
.get_layer_mut(&layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", layer_id))?;
|
||||
|
||||
let clip_instances = match layer {
|
||||
AnyLayer::Vector(vl) => &mut vl.clip_instances,
|
||||
AnyLayer::Audio(al) => &mut al.clip_instances,
|
||||
AnyLayer::Video(vl) => &mut vl.clip_instances,
|
||||
AnyLayer::Effect(el) => &mut el.clip_instances,
|
||||
};
|
||||
|
||||
clip_instances.push(instance);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
let count = self.removals.len();
|
||||
if count == 1 {
|
||||
"Delete clip instance".to_string()
|
||||
} else {
|
||||
format!("Delete {} clip instances", count)
|
||||
}
|
||||
}
|
||||
|
||||
fn execute_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
) -> Result<(), String> {
|
||||
let controller = match backend.audio_controller.as_mut() {
|
||||
Some(c) => c,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
for (layer_id, instance_id) in &self.removals {
|
||||
// Only process audio layers
|
||||
let layer = match document.get_layer(layer_id) {
|
||||
Some(l) => l,
|
||||
None => continue,
|
||||
};
|
||||
if !matches!(layer, AnyLayer::Audio(_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let track_id = match backend.layer_to_track_map.get(layer_id) {
|
||||
Some(id) => *id,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
// Remove from backend using stored mapping
|
||||
if let Some(backend_id) = backend.clip_instance_to_backend_map.remove(instance_id) {
|
||||
self.saved_backend_ids.insert(*instance_id, backend_id.clone());
|
||||
match backend_id {
|
||||
BackendClipInstanceId::Midi(midi_id) => {
|
||||
controller.remove_midi_clip(track_id, midi_id);
|
||||
}
|
||||
BackendClipInstanceId::Audio(audio_id) => {
|
||||
controller.remove_audio_clip(track_id, audio_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback_backend(
|
||||
&mut self,
|
||||
backend: &mut BackendContext,
|
||||
document: &Document,
|
||||
) -> Result<(), String> {
|
||||
use crate::clip::AudioClipType;
|
||||
|
||||
let controller = match backend.audio_controller.as_mut() {
|
||||
Some(c) => c,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
// Re-add clips that were removed from backend
|
||||
for (layer_id, instance) in &self.saved {
|
||||
let layer = match document.get_layer(layer_id) {
|
||||
Some(l) => l,
|
||||
None => continue,
|
||||
};
|
||||
if !matches!(layer, AnyLayer::Audio(_)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let track_id = match backend.layer_to_track_map.get(layer_id) {
|
||||
Some(id) => *id,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let clip = match document.get_audio_clip(&instance.clip_id) {
|
||||
Some(c) => c,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
match &clip.clip_type {
|
||||
AudioClipType::Midi { midi_clip_id } => {
|
||||
use daw_backend::command::{Query, QueryResponse};
|
||||
|
||||
let internal_start = instance.trim_start;
|
||||
let internal_end = instance.trim_end.unwrap_or(clip.duration);
|
||||
let external_start = instance.timeline_start;
|
||||
let external_duration = instance
|
||||
.timeline_duration
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
|
||||
let midi_instance = daw_backend::MidiClipInstance::new(
|
||||
0,
|
||||
*midi_clip_id,
|
||||
internal_start,
|
||||
internal_end,
|
||||
external_start,
|
||||
external_duration,
|
||||
);
|
||||
|
||||
let query = Query::AddMidiClipInstanceSync(track_id, midi_instance);
|
||||
if let Ok(QueryResponse::MidiClipInstanceAdded(Ok(new_id))) =
|
||||
controller.send_query(query)
|
||||
{
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
instance.id,
|
||||
BackendClipInstanceId::Midi(new_id),
|
||||
);
|
||||
}
|
||||
}
|
||||
AudioClipType::Sampled { audio_pool_index } => {
|
||||
use daw_backend::command::{Query, QueryResponse};
|
||||
|
||||
let internal_start = instance.trim_start;
|
||||
let internal_end = instance.trim_end.unwrap_or(clip.duration);
|
||||
let effective_duration = instance.timeline_duration
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
let start_time = instance.timeline_start;
|
||||
|
||||
let query = Query::AddAudioClipSync(
|
||||
track_id,
|
||||
*audio_pool_index,
|
||||
start_time,
|
||||
effective_duration,
|
||||
internal_start,
|
||||
);
|
||||
if let Ok(QueryResponse::AudioClipInstanceAdded(Ok(new_id))) =
|
||||
controller.send_query(query)
|
||||
{
|
||||
backend.clip_instance_to_backend_map.insert(
|
||||
instance.id,
|
||||
BackendClipInstanceId::Audio(new_id),
|
||||
);
|
||||
}
|
||||
}
|
||||
AudioClipType::Recording => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear saved backend IDs
|
||||
self.saved_backend_ids.clear();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::layer::VectorLayer;
|
||||
|
||||
#[test]
|
||||
fn test_remove_clip_instances() {
|
||||
let mut document = Document::new("Test");
|
||||
|
||||
let clip_id = Uuid::new_v4();
|
||||
let mut vector_layer = VectorLayer::new("Layer 1");
|
||||
|
||||
let mut ci1 = ClipInstance::new(clip_id);
|
||||
ci1.timeline_start = 0.0;
|
||||
let id1 = ci1.id;
|
||||
|
||||
let mut ci2 = ClipInstance::new(clip_id);
|
||||
ci2.timeline_start = 5.0;
|
||||
let id2 = ci2.id;
|
||||
|
||||
vector_layer.clip_instances.push(ci1);
|
||||
vector_layer.clip_instances.push(ci2);
|
||||
|
||||
let layer_id = document.root_mut().add_child(AnyLayer::Vector(vector_layer));
|
||||
|
||||
// Remove first clip instance
|
||||
let mut action = RemoveClipInstancesAction::new(vec![(layer_id, id1)]);
|
||||
action.execute(&mut document).unwrap();
|
||||
|
||||
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
|
||||
assert_eq!(vl.clip_instances.len(), 1);
|
||||
assert_eq!(vl.clip_instances[0].id, id2);
|
||||
}
|
||||
|
||||
// Rollback
|
||||
action.rollback(&mut document).unwrap();
|
||||
|
||||
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
|
||||
assert_eq!(vl.clip_instances.len(), 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
//! Remove shapes action
|
||||
//!
|
||||
//! Handles removing shapes and shape instances from a vector layer (for cut/delete).
|
||||
|
||||
use crate::action::Action;
|
||||
use crate::document::Document;
|
||||
use crate::layer::AnyLayer;
|
||||
use crate::object::ShapeInstance;
|
||||
use crate::shape::Shape;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Action that removes shapes and their instances from a vector layer
|
||||
pub struct RemoveShapesAction {
|
||||
/// Layer ID containing the shapes
|
||||
layer_id: Uuid,
|
||||
/// Shape IDs to remove
|
||||
shape_ids: Vec<Uuid>,
|
||||
/// Shape instance IDs to remove
|
||||
instance_ids: Vec<Uuid>,
|
||||
/// Saved shapes for rollback
|
||||
saved_shapes: Vec<(Uuid, Shape)>,
|
||||
/// Saved instances for rollback
|
||||
saved_instances: Vec<ShapeInstance>,
|
||||
}
|
||||
|
||||
impl RemoveShapesAction {
|
||||
/// Create a new remove shapes action
|
||||
pub fn new(layer_id: Uuid, shape_ids: Vec<Uuid>, instance_ids: Vec<Uuid>) -> Self {
|
||||
Self {
|
||||
layer_id,
|
||||
shape_ids,
|
||||
instance_ids,
|
||||
saved_shapes: Vec::new(),
|
||||
saved_instances: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Action for RemoveShapesAction {
|
||||
fn execute(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
self.saved_shapes.clear();
|
||||
self.saved_instances.clear();
|
||||
|
||||
let layer = document
|
||||
.get_layer_mut(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
|
||||
|
||||
let vector_layer = match layer {
|
||||
AnyLayer::Vector(vl) => vl,
|
||||
_ => return Err("Not a vector layer".to_string()),
|
||||
};
|
||||
|
||||
// Remove and save shape instances
|
||||
let mut remaining_instances = Vec::new();
|
||||
for inst in vector_layer.shape_instances.drain(..) {
|
||||
if self.instance_ids.contains(&inst.id) {
|
||||
self.saved_instances.push(inst);
|
||||
} else {
|
||||
remaining_instances.push(inst);
|
||||
}
|
||||
}
|
||||
vector_layer.shape_instances = remaining_instances;
|
||||
|
||||
// Remove and save shape definitions
|
||||
for shape_id in &self.shape_ids {
|
||||
if let Some(shape) = vector_layer.shapes.remove(shape_id) {
|
||||
self.saved_shapes.push((*shape_id, shape));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rollback(&mut self, document: &mut Document) -> Result<(), String> {
|
||||
let layer = document
|
||||
.get_layer_mut(&self.layer_id)
|
||||
.ok_or_else(|| format!("Layer {} not found", self.layer_id))?;
|
||||
|
||||
let vector_layer = match layer {
|
||||
AnyLayer::Vector(vl) => vl,
|
||||
_ => return Err("Not a vector layer".to_string()),
|
||||
};
|
||||
|
||||
// Restore shapes
|
||||
for (id, shape) in self.saved_shapes.drain(..) {
|
||||
vector_layer.shapes.insert(id, shape);
|
||||
}
|
||||
|
||||
// Restore instances
|
||||
for inst in self.saved_instances.drain(..) {
|
||||
vector_layer.shape_instances.push(inst);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn description(&self) -> String {
|
||||
let count = self.instance_ids.len();
|
||||
if count == 1 {
|
||||
"Delete shape".to_string()
|
||||
} else {
|
||||
format!("Delete {} shapes", count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::layer::VectorLayer;
|
||||
use crate::object::ShapeInstance;
|
||||
use crate::shape::Shape;
|
||||
use vello::kurbo::BezPath;
|
||||
|
||||
#[test]
|
||||
fn test_remove_shapes() {
|
||||
let mut document = Document::new("Test");
|
||||
|
||||
let mut vector_layer = VectorLayer::new("Layer 1");
|
||||
|
||||
// Add a shape and instance
|
||||
let mut path = BezPath::new();
|
||||
path.move_to((0.0, 0.0));
|
||||
path.line_to((100.0, 100.0));
|
||||
let shape = Shape::new(path);
|
||||
let shape_id = shape.id;
|
||||
let instance = ShapeInstance::new(shape_id);
|
||||
let instance_id = instance.id;
|
||||
|
||||
vector_layer.shapes.insert(shape_id, shape);
|
||||
vector_layer.shape_instances.push(instance);
|
||||
|
||||
let layer_id = document.root_mut().add_child(AnyLayer::Vector(vector_layer));
|
||||
|
||||
// Remove
|
||||
let mut action = RemoveShapesAction::new(layer_id, vec![shape_id], vec![instance_id]);
|
||||
action.execute(&mut document).unwrap();
|
||||
|
||||
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
|
||||
assert!(vl.shapes.is_empty());
|
||||
assert!(vl.shape_instances.is_empty());
|
||||
}
|
||||
|
||||
// Rollback
|
||||
action.rollback(&mut document).unwrap();
|
||||
|
||||
if let Some(AnyLayer::Vector(vl)) = document.get_layer(&layer_id) {
|
||||
assert_eq!(vl.shapes.len(), 1);
|
||||
assert_eq!(vl.shape_instances.len(), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -415,16 +415,18 @@ impl Action for SplitClipInstanceAction {
|
||||
}
|
||||
|
||||
// 2. Add the new (right) instance
|
||||
let duration = clip.duration;
|
||||
let internal_start = new_instance.trim_start;
|
||||
let internal_end = new_instance.trim_end.unwrap_or(clip.duration);
|
||||
let effective_duration = new_instance.timeline_duration
|
||||
.unwrap_or(internal_end - internal_start);
|
||||
let start_time = new_instance.timeline_start;
|
||||
let offset = new_instance.trim_start;
|
||||
|
||||
let query = Query::AddAudioClipSync(
|
||||
*backend_track_id,
|
||||
*audio_pool_index,
|
||||
start_time,
|
||||
duration,
|
||||
offset,
|
||||
effective_duration,
|
||||
internal_start,
|
||||
);
|
||||
|
||||
match controller.send_query(query)? {
|
||||
|
||||
@@ -0,0 +1,283 @@
|
||||
//! Clipboard management for cut/copy/paste operations
|
||||
//!
|
||||
//! Supports multiple content types (clip instances, shapes) with
|
||||
//! cross-platform clipboard integration via arboard.
|
||||
|
||||
use crate::clip::{AudioClip, ClipInstance, ImageAsset, VectorClip, VideoClip};
|
||||
use crate::layer::{AudioLayerType, AnyLayer};
|
||||
use crate::object::ShapeInstance;
|
||||
use crate::shape::Shape;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Layer type tag for clipboard, so paste knows where clips can go
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum ClipboardLayerType {
|
||||
Vector,
|
||||
Video,
|
||||
AudioSampled,
|
||||
AudioMidi,
|
||||
Effect,
|
||||
}
|
||||
|
||||
impl ClipboardLayerType {
|
||||
/// Determine the clipboard layer type from a document layer
|
||||
pub fn from_layer(layer: &AnyLayer) -> Self {
|
||||
match layer {
|
||||
AnyLayer::Vector(_) => ClipboardLayerType::Vector,
|
||||
AnyLayer::Video(_) => ClipboardLayerType::Video,
|
||||
AnyLayer::Audio(al) => match al.audio_layer_type {
|
||||
AudioLayerType::Sampled => ClipboardLayerType::AudioSampled,
|
||||
AudioLayerType::Midi => ClipboardLayerType::AudioMidi,
|
||||
},
|
||||
AnyLayer::Effect(_) => ClipboardLayerType::Effect,
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a layer is compatible with this clipboard layer type
|
||||
pub fn is_compatible(&self, layer: &AnyLayer) -> bool {
|
||||
match (self, layer) {
|
||||
(ClipboardLayerType::Vector, AnyLayer::Vector(_)) => true,
|
||||
(ClipboardLayerType::Video, AnyLayer::Video(_)) => true,
|
||||
(ClipboardLayerType::AudioSampled, AnyLayer::Audio(al)) => {
|
||||
al.audio_layer_type == AudioLayerType::Sampled
|
||||
}
|
||||
(ClipboardLayerType::AudioMidi, AnyLayer::Audio(al)) => {
|
||||
al.audio_layer_type == AudioLayerType::Midi
|
||||
}
|
||||
(ClipboardLayerType::Effect, AnyLayer::Effect(_)) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Content stored in the clipboard
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum ClipboardContent {
|
||||
/// Clip instances with their referenced clip definitions
|
||||
ClipInstances {
|
||||
/// Source layer type (for paste compatibility)
|
||||
layer_type: ClipboardLayerType,
|
||||
/// The clip instances (IDs will be regenerated on paste)
|
||||
instances: Vec<ClipInstance>,
|
||||
/// Referenced audio clip definitions
|
||||
audio_clips: Vec<(Uuid, AudioClip)>,
|
||||
/// Referenced video clip definitions
|
||||
video_clips: Vec<(Uuid, VideoClip)>,
|
||||
/// Referenced vector clip definitions
|
||||
vector_clips: Vec<(Uuid, VectorClip)>,
|
||||
/// Referenced image assets
|
||||
image_assets: Vec<(Uuid, ImageAsset)>,
|
||||
},
|
||||
/// Shapes and shape instances from a vector layer
|
||||
Shapes {
|
||||
/// Shape definitions (id -> shape)
|
||||
shapes: Vec<(Uuid, Shape)>,
|
||||
/// Shape instances referencing the shapes above
|
||||
instances: Vec<ShapeInstance>,
|
||||
},
|
||||
}
|
||||
|
||||
impl ClipboardContent {
|
||||
/// Create a clone of this content with all UUIDs regenerated
|
||||
/// Returns the new content and a mapping from old to new IDs
|
||||
pub fn with_regenerated_ids(&self) -> (Self, HashMap<Uuid, Uuid>) {
|
||||
let mut id_map = HashMap::new();
|
||||
|
||||
match self {
|
||||
ClipboardContent::ClipInstances {
|
||||
layer_type,
|
||||
instances,
|
||||
audio_clips,
|
||||
video_clips,
|
||||
vector_clips,
|
||||
image_assets,
|
||||
} => {
|
||||
// Regenerate clip definition IDs
|
||||
let new_audio_clips: Vec<(Uuid, AudioClip)> = audio_clips
|
||||
.iter()
|
||||
.map(|(old_id, clip)| {
|
||||
let new_id = Uuid::new_v4();
|
||||
id_map.insert(*old_id, new_id);
|
||||
let mut new_clip = clip.clone();
|
||||
new_clip.id = new_id;
|
||||
(new_id, new_clip)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let new_video_clips: Vec<(Uuid, VideoClip)> = video_clips
|
||||
.iter()
|
||||
.map(|(old_id, clip)| {
|
||||
let new_id = Uuid::new_v4();
|
||||
id_map.insert(*old_id, new_id);
|
||||
let mut new_clip = clip.clone();
|
||||
new_clip.id = new_id;
|
||||
(new_id, new_clip)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let new_vector_clips: Vec<(Uuid, VectorClip)> = vector_clips
|
||||
.iter()
|
||||
.map(|(old_id, clip)| {
|
||||
let new_id = Uuid::new_v4();
|
||||
id_map.insert(*old_id, new_id);
|
||||
let mut new_clip = clip.clone();
|
||||
new_clip.id = new_id;
|
||||
(new_id, new_clip)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let new_image_assets: Vec<(Uuid, ImageAsset)> = image_assets
|
||||
.iter()
|
||||
.map(|(old_id, asset)| {
|
||||
let new_id = Uuid::new_v4();
|
||||
id_map.insert(*old_id, new_id);
|
||||
let mut new_asset = asset.clone();
|
||||
new_asset.id = new_id;
|
||||
(new_id, new_asset)
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Regenerate clip instance IDs and remap clip_id references
|
||||
let new_instances: Vec<ClipInstance> = instances
|
||||
.iter()
|
||||
.map(|inst| {
|
||||
let new_instance_id = Uuid::new_v4();
|
||||
id_map.insert(inst.id, new_instance_id);
|
||||
let mut new_inst = inst.clone();
|
||||
new_inst.id = new_instance_id;
|
||||
// Remap clip_id to new definition ID
|
||||
if let Some(new_clip_id) = id_map.get(&inst.clip_id) {
|
||||
new_inst.clip_id = *new_clip_id;
|
||||
}
|
||||
new_inst
|
||||
})
|
||||
.collect();
|
||||
|
||||
(
|
||||
ClipboardContent::ClipInstances {
|
||||
layer_type: layer_type.clone(),
|
||||
instances: new_instances,
|
||||
audio_clips: new_audio_clips,
|
||||
video_clips: new_video_clips,
|
||||
vector_clips: new_vector_clips,
|
||||
image_assets: new_image_assets,
|
||||
},
|
||||
id_map,
|
||||
)
|
||||
}
|
||||
ClipboardContent::Shapes { shapes, instances } => {
|
||||
// Regenerate shape definition IDs
|
||||
let new_shapes: Vec<(Uuid, Shape)> = shapes
|
||||
.iter()
|
||||
.map(|(old_id, shape)| {
|
||||
let new_id = Uuid::new_v4();
|
||||
id_map.insert(*old_id, new_id);
|
||||
let mut new_shape = shape.clone();
|
||||
new_shape.id = new_id;
|
||||
(new_id, new_shape)
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Regenerate instance IDs and remap shape_id references
|
||||
let new_instances: Vec<ShapeInstance> = instances
|
||||
.iter()
|
||||
.map(|inst| {
|
||||
let new_instance_id = Uuid::new_v4();
|
||||
id_map.insert(inst.id, new_instance_id);
|
||||
let mut new_inst = inst.clone();
|
||||
new_inst.id = new_instance_id;
|
||||
// Remap shape_id to new definition ID
|
||||
if let Some(new_shape_id) = id_map.get(&inst.shape_id) {
|
||||
new_inst.shape_id = *new_shape_id;
|
||||
}
|
||||
new_inst
|
||||
})
|
||||
.collect();
|
||||
|
||||
(
|
||||
ClipboardContent::Shapes {
|
||||
shapes: new_shapes,
|
||||
instances: new_instances,
|
||||
},
|
||||
id_map,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// JSON prefix for clipboard text to identify Lightningbeam content
|
||||
const CLIPBOARD_PREFIX: &str = "LIGHTNINGBEAM_CLIPBOARD:";
|
||||
|
||||
/// Manages clipboard operations with internal + system clipboard
|
||||
pub struct ClipboardManager {
|
||||
/// Internal clipboard (preserves rich data without serialization loss)
|
||||
internal: Option<ClipboardContent>,
|
||||
/// System clipboard handle (lazy-initialized)
|
||||
system: Option<arboard::Clipboard>,
|
||||
}
|
||||
|
||||
impl ClipboardManager {
|
||||
/// Create a new clipboard manager
|
||||
pub fn new() -> Self {
|
||||
let system = arboard::Clipboard::new().ok();
|
||||
Self {
|
||||
internal: None,
|
||||
system,
|
||||
}
|
||||
}
|
||||
|
||||
/// Copy content to both internal and system clipboard
|
||||
pub fn copy(&mut self, content: ClipboardContent) {
|
||||
// Serialize to system clipboard as JSON text
|
||||
if let Some(system) = self.system.as_mut() {
|
||||
if let Ok(json) = serde_json::to_string(&content) {
|
||||
let clipboard_text = format!("{}{}", CLIPBOARD_PREFIX, json);
|
||||
let _ = system.set_text(clipboard_text);
|
||||
}
|
||||
}
|
||||
|
||||
// Store internally for rich access
|
||||
self.internal = Some(content);
|
||||
}
|
||||
|
||||
/// Try to paste content
|
||||
/// Returns internal clipboard if available, falls back to system clipboard JSON
|
||||
pub fn paste(&mut self) -> Option<ClipboardContent> {
|
||||
// Try internal clipboard first
|
||||
if let Some(content) = &self.internal {
|
||||
return Some(content.clone());
|
||||
}
|
||||
|
||||
// Fall back to system clipboard
|
||||
if let Some(system) = self.system.as_mut() {
|
||||
if let Ok(text) = system.get_text() {
|
||||
if let Some(json) = text.strip_prefix(CLIPBOARD_PREFIX) {
|
||||
if let Ok(content) = serde_json::from_str::<ClipboardContent>(json) {
|
||||
return Some(content);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
/// Check if there's content available to paste
|
||||
pub fn has_content(&mut self) -> bool {
|
||||
if self.internal.is_some() {
|
||||
return true;
|
||||
}
|
||||
|
||||
if let Some(system) = self.system.as_mut() {
|
||||
if let Ok(text) = system.get_text() {
|
||||
return text.starts_with(CLIPBOARD_PREFIX);
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -40,3 +40,4 @@ pub mod planar_graph;
|
||||
pub mod file_types;
|
||||
pub mod file_io;
|
||||
pub mod export;
|
||||
pub mod clipboard;
|
||||
|
||||
Reference in New Issue
Block a user