Change layer duplication so the duplicate is placed above the original (#1124)

Previously, the duplicated layer would be placed at the top of the stack. It is now inserted one layer above the original.

---------

Co-authored-by: Ollie Dolan <olliedolan10@gmail.com>
Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Christopher Mendoza 2023-04-16 15:34:02 -07:00 committed by Keavon Chambers
parent 4c9ef0fe6c
commit b83f2c24f1
2 changed files with 26 additions and 22 deletions

View File

@ -718,7 +718,11 @@ impl Document {
let layer = self.layer(&path)?.clone(); let layer = self.layer(&path)?.clone();
let (folder_path, _) = split_path(path.as_slice()).unwrap_or((&[], 0)); let (folder_path, _) = split_path(path.as_slice()).unwrap_or((&[], 0));
let folder = self.folder_mut(folder_path)?; let folder = self.folder_mut(folder_path)?;
if let Some(new_layer_id) = folder.add_layer(layer, None, -1) {
let selected_id = path.last().copied().unwrap_or_default();
let insert_index = folder.layer_ids.iter().position(|&id| id == selected_id).unwrap_or(0) as isize + 1;
if let Some(new_layer_id) = folder.add_layer(layer, None, insert_index) {
let new_path = [folder_path, &[new_layer_id]].concat(); let new_path = [folder_path, &[new_layer_id]].concat();
self.mark_as_dirty(folder_path)?; self.mark_as_dirty(folder_path)?;
Some( Some(

View File

@ -70,31 +70,31 @@ impl FolderLayer {
pub fn add_layer(&mut self, layer: Layer, id: Option<LayerId>, insert_index: isize) -> Option<LayerId> { pub fn add_layer(&mut self, layer: Layer, id: Option<LayerId>, insert_index: isize) -> Option<LayerId> {
let mut insert_index = insert_index as i128; let mut insert_index = insert_index as i128;
// Bounds check for the insert index
if insert_index < 0 { if insert_index < 0 {
insert_index = self.layers.len() as i128 + insert_index + 1; insert_index = self.layers.len() as i128 + insert_index + 1;
} }
if insert_index > self.layers.len() as i128 || insert_index < 0 {
if insert_index <= self.layers.len() as i128 && insert_index >= 0 { return None;
if let Some(id) = id {
self.next_assignment_id = id;
}
if self.layer_ids.contains(&self.next_assignment_id) {
return None;
}
let id = self.next_assignment_id;
self.layers.insert(insert_index as usize, layer);
self.layer_ids.insert(insert_index as usize, id);
// Linear probing for collision avoidance
while self.layer_ids.contains(&self.next_assignment_id) {
self.next_assignment_id += 1;
}
Some(id)
} else {
None
} }
if let Some(id) = id {
self.next_assignment_id = id;
}
if self.layer_ids.contains(&self.next_assignment_id) {
return None;
}
let id = self.next_assignment_id;
self.layers.insert(insert_index as usize, layer);
self.layer_ids.insert(insert_index as usize, id);
// Linear probing for collision avoidance
while self.layer_ids.contains(&self.next_assignment_id) {
self.next_assignment_id += 1;
}
Some(id)
} }
/// Remove a layer with a given ID from the folder. /// Remove a layer with a given ID from the folder.