From aaa7a08af297985e04c0c4dc22660f7d8d484eaa Mon Sep 17 00:00:00 2001
From: sukamoni <newsukamoni@gmail.com>
Date: Mon, 4 Jan 2021 19:55:27 +0900
Subject: [PATCH] Fixed a bug that freezed when changing DocumentPane
 Orientation

---
 .../Controls/DocumentPaneDropTarget.cs        | 182 ++++++------------
 1 file changed, 60 insertions(+), 122 deletions(-)

diff --git a/source/Components/AvalonDock/Controls/DocumentPaneDropTarget.cs b/source/Components/AvalonDock/Controls/DocumentPaneDropTarget.cs
index b72c7399..b40d17cf 100644
--- a/source/Components/AvalonDock/Controls/DocumentPaneDropTarget.cs
+++ b/source/Components/AvalonDock/Controls/DocumentPaneDropTarget.cs
@@ -83,41 +83,26 @@ protected override void Drop(LayoutDocumentFloatingWindow floatingWindow)
 					#region DropTargetType.DocumentPaneDockBottom
 
 					{
-						var parentModel = targetModel.Parent as ILayoutGroup;
-						var parentModelOrientable = targetModel.Parent as ILayoutOrientableGroup;
-						int insertToIndex = parentModel.IndexOfChild(targetModel);
+						var paneGroupContainer = targetModel.Parent;
+						var paneGroupOrientaion = paneGroupContainer as ILayoutOrientableGroup;
+						var paneGroup = paneGroupContainer as LayoutDocumentPaneGroup;
 
-						if (parentModelOrientable.Orientation != System.Windows.Controls.Orientation.Vertical &&
-							parentModel.ChildrenCount == 1)
-							parentModelOrientable.Orientation = System.Windows.Controls.Orientation.Vertical;
+						if (paneGroupOrientaion.Orientation != System.Windows.Controls.Orientation.Vertical)
+						{
+							paneGroup.Orientation = System.Windows.Controls.Orientation.Vertical;
+						}
 
-						if (parentModelOrientable.Orientation == System.Windows.Controls.Orientation.Vertical)
+						var insertToIndex = paneGroup.IndexOfChild(targetModel);
+						if (insertToIndex == (paneGroup.Children.Count - 1))
 						{
-							var layoutDocumentPaneGroup = floatingWindow.RootPanel as LayoutDocumentPaneGroup;
-							if (layoutDocumentPaneGroup != null &&
-								(layoutDocumentPaneGroup.Children.Count == 1 ||
-									layoutDocumentPaneGroup.Orientation == System.Windows.Controls.Orientation.Vertical))
-							{
-								var documentsToMove = layoutDocumentPaneGroup.Children.ToArray();
-								for (int i = 0; i < documentsToMove.Length; i++)
-									parentModel.InsertChildAt(insertToIndex + 1 + i, documentsToMove[i]);
-							}
-							else
-								parentModel.InsertChildAt(insertToIndex + 1, floatingWindow.RootPanel);
+							insertToIndex = paneGroup.Children.Count;
 						}
-						else
+						var documentsToMove = floatingWindow.Children.ToArray();
+						for (int i = 0; i < documentsToMove.Length; i++)
 						{
-							var targetModelAsPositionableElement = targetModel as ILayoutPositionableElement;
-							var newOrientedPanel = new LayoutDocumentPaneGroup()
-							{
-								Orientation = System.Windows.Controls.Orientation.Vertical,
-								DockWidth = targetModelAsPositionableElement.DockWidth,
-								DockHeight = targetModelAsPositionableElement.DockHeight,
-							};
-
-							parentModel.InsertChildAt(insertToIndex, newOrientedPanel);
-							newOrientedPanel.Children.Add(targetModel);
-							newOrientedPanel.Children.Add(floatingWindow.RootPanel);
+							var floatingChild = documentsToMove[i];
+							paneGroup.InsertChildAt(insertToIndex + i, floatingChild);
+
 						}
 					}
 					break;
@@ -129,42 +114,26 @@ protected override void Drop(LayoutDocumentFloatingWindow floatingWindow)
 					#region DropTargetType.DocumentPaneDockTop
 
 					{
-						var parentModel = targetModel.Parent as ILayoutGroup;
-						var parentModelOrientable = targetModel.Parent as ILayoutOrientableGroup;
-						int insertToIndex = parentModel.IndexOfChild(targetModel);
+						var paneGroupContainer = targetModel.Parent;
+						var paneGroupOrientaion = paneGroupContainer as ILayoutOrientableGroup;
+						var paneGroup = paneGroupContainer as LayoutDocumentPaneGroup;
 
-						if (parentModelOrientable.Orientation != System.Windows.Controls.Orientation.Vertical &&
-							parentModel.ChildrenCount == 1)
-							parentModelOrientable.Orientation = System.Windows.Controls.Orientation.Vertical;
+						if(paneGroupOrientaion.Orientation != System.Windows.Controls.Orientation.Vertical)
+						{
+							paneGroup.Orientation = System.Windows.Controls.Orientation.Vertical;
+						}
 
-						if (parentModelOrientable.Orientation == System.Windows.Controls.Orientation.Vertical)
+						var insertToIndex = paneGroup.IndexOfChild(targetModel);
+						if(insertToIndex < 0 )
 						{
-							var layoutDocumentPaneGroup = floatingWindow.RootPanel as LayoutDocumentPaneGroup;
-							if (layoutDocumentPaneGroup != null &&
-								(layoutDocumentPaneGroup.Children.Count == 1 ||
-									layoutDocumentPaneGroup.Orientation == System.Windows.Controls.Orientation.Vertical))
-							{
-								var documentsToMove = layoutDocumentPaneGroup.Children.ToArray();
-								for (int i = 0; i < documentsToMove.Length; i++)
-									parentModel.InsertChildAt(insertToIndex + i, documentsToMove[i]);
-							}
-							else
-								parentModel.InsertChildAt(insertToIndex, floatingWindow.RootPanel);
+							insertToIndex = 0;
 						}
-						else
+						var documentsToMove = floatingWindow.Children.ToArray();
+						for (int i = 0; i < documentsToMove.Length; i++)
 						{
-							var targetModelAsPositionableElement = targetModel as ILayoutPositionableElement;
-							var newOrientedPanel = new LayoutDocumentPaneGroup()
-							{
-								Orientation = System.Windows.Controls.Orientation.Vertical,
-								DockWidth = targetModelAsPositionableElement.DockWidth,
-								DockHeight = targetModelAsPositionableElement.DockHeight,
-							};
-
-							parentModel.InsertChildAt(insertToIndex, newOrientedPanel);
-							//the floating window must be added after the target modal as it could be raise a CollectGarbage call
-							newOrientedPanel.Children.Add(targetModel);
-							newOrientedPanel.Children.Insert(0, floatingWindow.RootPanel);
+							var floatingChild = documentsToMove[i];
+							paneGroup.InsertChildAt(insertToIndex + i, floatingChild);
+
 						}
 					}
 					break;
@@ -176,42 +145,26 @@ protected override void Drop(LayoutDocumentFloatingWindow floatingWindow)
 					#region DropTargetType.DocumentPaneDockLeft
 
 					{
-						var parentModel = targetModel.Parent as ILayoutGroup;
-						var parentModelOrientable = targetModel.Parent as ILayoutOrientableGroup;
-						int insertToIndex = parentModel.IndexOfChild(targetModel);
+						var paneGroupContainer = targetModel.Parent;
+						var paneGroupOrientaion = paneGroupContainer as ILayoutOrientableGroup;
+						var paneGroup = paneGroupContainer as LayoutDocumentPaneGroup;
 
-						if (parentModelOrientable.Orientation != System.Windows.Controls.Orientation.Horizontal &&
-							parentModel.ChildrenCount == 1)
-							parentModelOrientable.Orientation = System.Windows.Controls.Orientation.Horizontal;
+						if (paneGroupOrientaion.Orientation != System.Windows.Controls.Orientation.Horizontal)
+						{
+							paneGroup.Orientation = System.Windows.Controls.Orientation.Horizontal;
+						}
 
-						if (parentModelOrientable.Orientation == System.Windows.Controls.Orientation.Horizontal)
+						var insertToIndex = paneGroup.IndexOfChild(targetModel);
+						if (insertToIndex < 0)
 						{
-							var layoutDocumentPaneGroup = floatingWindow.RootPanel as LayoutDocumentPaneGroup;
-							if (layoutDocumentPaneGroup != null &&
-								(layoutDocumentPaneGroup.Children.Count == 1 ||
-									layoutDocumentPaneGroup.Orientation == System.Windows.Controls.Orientation.Horizontal))
-							{
-								var documentsToMove = layoutDocumentPaneGroup.Children.ToArray();
-								for (int i = 0; i < documentsToMove.Length; i++)
-									parentModel.InsertChildAt(insertToIndex + i, documentsToMove[i]);
-							}
-							else
-								parentModel.InsertChildAt(insertToIndex, floatingWindow.RootPanel);
+							insertToIndex = 0;
 						}
-						else
+						var documentsToMove = floatingWindow.Children.ToArray();
+						for (int i = 0; i < documentsToMove.Length; i++)
 						{
-							var targetModelAsPositionableElement = targetModel as ILayoutPositionableElement;
-							var newOrientedPanel = new LayoutDocumentPaneGroup()
-							{
-								Orientation = System.Windows.Controls.Orientation.Horizontal,
-								DockWidth = targetModelAsPositionableElement.DockWidth,
-								DockHeight = targetModelAsPositionableElement.DockHeight,
-							};
-
-							parentModel.InsertChildAt(insertToIndex, newOrientedPanel);
-							//the floating window must be added after the target modal as it could be raise a CollectGarbage call
-							newOrientedPanel.Children.Add(targetModel);
-							newOrientedPanel.Children.Insert(0, floatingWindow.RootPanel);
+							var floatingChild = documentsToMove[i];
+							paneGroup.InsertChildAt(insertToIndex + i, floatingChild);
+
 						}
 					}
 					break;
@@ -223,41 +176,26 @@ protected override void Drop(LayoutDocumentFloatingWindow floatingWindow)
 					#region DropTargetType.DocumentPaneDockRight
 
 					{
-						var parentModel = targetModel.Parent as ILayoutGroup;
-						var parentModelOrientable = targetModel.Parent as ILayoutOrientableGroup;
-						int insertToIndex = parentModel.IndexOfChild(targetModel);
+						var paneGroupContainer = targetModel.Parent;
+						var paneGroupOrientaion = paneGroupContainer as ILayoutOrientableGroup;
+						var paneGroup = paneGroupContainer as LayoutDocumentPaneGroup;
 
-						if (parentModelOrientable.Orientation != System.Windows.Controls.Orientation.Horizontal &&
-							parentModel.ChildrenCount == 1)
-							parentModelOrientable.Orientation = System.Windows.Controls.Orientation.Horizontal;
+						if (paneGroupOrientaion.Orientation != System.Windows.Controls.Orientation.Horizontal)
+						{
+							paneGroup.Orientation = System.Windows.Controls.Orientation.Horizontal;
+						}
 
-						if (parentModelOrientable.Orientation == System.Windows.Controls.Orientation.Horizontal)
+						var insertToIndex = paneGroup.IndexOfChild(targetModel);
+						if (insertToIndex == (paneGroup.Children.Count - 1))
 						{
-							var layoutDocumentPaneGroup = floatingWindow.RootPanel as LayoutDocumentPaneGroup;
-							if (layoutDocumentPaneGroup != null &&
-								(layoutDocumentPaneGroup.Children.Count == 1 ||
-									layoutDocumentPaneGroup.Orientation == System.Windows.Controls.Orientation.Horizontal))
-							{
-								var documentToMove = layoutDocumentPaneGroup.Children.ToArray();
-								for (int i = 0; i < documentToMove.Length; i++)
-									parentModel.InsertChildAt(insertToIndex + 1 + i, documentToMove[i]);
-							}
-							else
-								parentModel.InsertChildAt(insertToIndex + 1, floatingWindow.RootPanel);
+							insertToIndex = paneGroup.Children.Count;
 						}
-						else
+						var documentsToMove = floatingWindow.Children.ToArray();
+						for (int i = 0; i < documentsToMove.Length; i++)
 						{
-							var targetModelAsPositionableElement = targetModel as ILayoutPositionableElement;
-							var newOrientedPanel = new LayoutDocumentPaneGroup()
-							{
-								Orientation = System.Windows.Controls.Orientation.Horizontal,
-								DockWidth = targetModelAsPositionableElement.DockWidth,
-								DockHeight = targetModelAsPositionableElement.DockHeight,
-							};
-
-							parentModel.InsertChildAt(insertToIndex, newOrientedPanel);
-							newOrientedPanel.Children.Add(targetModel);
-							newOrientedPanel.Children.Add(floatingWindow.RootPanel);
+							var floatingChild = documentsToMove[i];
+							paneGroup.InsertChildAt(insertToIndex + i, floatingChild);
+
 						}
 					}
 					break;