Kaynağa Gözat

Fixed 2 bugs

flabbet 4 yıl önce
ebeveyn
işleme
9bbe35e864

+ 12 - 2
PixiEditor/Models/Layers/StructuredLayerTree.cs

@@ -97,8 +97,18 @@ namespace PixiEditor.Models.Layers
 
         private int CalculateTopIndex(int displayIndex, GuidStructureItem structureData, ObservableCollection<Layer> layers)
         {
-            int originalTopIndex = layers.IndexOf(layers.First(x => x.LayerGuid == structureData.EndLayerGuid));
-            int originalBottomIndex = layers.IndexOf(layers.First(x => x.LayerGuid == structureData.StartLayerGuid));
+            var endLayer = layers.FirstOrDefault(x => x.LayerGuid == structureData.EndLayerGuid);
+            var bottomLayer = layers.FirstOrDefault(x => x.LayerGuid == structureData.StartLayerGuid);
+            int originalTopIndex = 0;
+            int originalBottomIndex = 0;
+            if (endLayer != null)
+            {
+                originalTopIndex = layers.IndexOf(endLayer);
+            }
+            if (bottomLayer != null)
+            {
+                originalBottomIndex = layers.IndexOf(bottomLayer);
+            }
 
             return displayIndex + (originalTopIndex - originalBottomIndex);
         }

+ 34 - 3
PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -150,7 +150,12 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
         public void NewLayer(object parameter)
         {
-            bool isCollapsed = parameter is LayerGroupControl control && !control.GroupData.IsExpanded;
+            GuidStructureItem control = GetGroupFromParameter(parameter);
+            bool isCollapsed = false;
+            if (control != null)
+            {
+                isCollapsed = !control.IsExpanded;
+            }
             var doc = Owner.BitmapManager.ActiveDocument;
             var activeLayerParent = doc.LayerStructure.GetGroupByLayer(doc.ActiveLayerGuid);
             Guid lastActiveLayerGuid = doc.ActiveLayerGuid;
@@ -162,12 +167,16 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
                 doc.MoveLayerInStructure(doc.Layers[^1].LayerGuid, lastActiveLayerGuid, true);
                 if (isCollapsed)
                 {
-                    LayerGroupControl groupControl = parameter as LayerGroupControl;
-                    doc.LayerStructure.AssignParent(doc.ActiveLayerGuid, groupControl.GroupData.Parent?.GroupGuid);
+                    doc.LayerStructure.AssignParent(doc.ActiveLayerGuid, control.Parent?.GroupGuid);
                 }
 
                 doc.UndoManager.UndoStack.Pop();
             }
+            if (control != null)
+            {
+                control.IsExpanded = !isCollapsed;
+                doc.RaisePropertyChange(nameof(doc.LayerStructure));
+            }
         }
 
         public bool CanCreateNewLayer(object parameter)
@@ -317,6 +326,28 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             return Owner.DocumentIsNotNull(null) && index != 0 && Owner.BitmapManager.ActiveDocument.Layers.Count(x => x.IsActive) == 1;
         }
 
+        private GuidStructureItem GetGroupFromParameter(object parameter)
+        {
+            if (parameter is LayerGroupControl)
+            {
+                return ((LayerGroupControl)parameter).GroupData;
+            }
+            else if (parameter is Layer layer)
+            {
+                var group = Owner.BitmapManager.ActiveDocument.LayerStructure.GetGroupByLayer(layer.LayerGuid);
+                if (group != null)
+                {
+                    while (group.IsExpanded && group.Parent != null)
+                    {
+                        group = group.Parent;
+                    }
+                }
+                return group;
+            }
+
+            return null;
+        }
+
         private void BitmapManager_DocumentChanged(object sender, Models.Events.DocumentChangedEventArgs e)
         {
             if (e.OldDocument != null)