Skip to content

Commit

Permalink
Fixing exceptions in Drag LayoutAnchoreables to DocumentPane
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirkster99 committed Apr 1, 2020
1 parent da2c59b commit f9a226f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion source/Components/AvalonDock/Layout/LayoutAnchorable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public void Show()
{
var previousContainerAsLayoutGroup = PreviousContainer as ILayoutGroup;
if (PreviousContainerIndex < previousContainerAsLayoutGroup.ChildrenCount)
previousContainerAsLayoutGroup.InsertChildAt(PreviousContainerIndex, this);
previousContainerAsLayoutGroup.InsertChildAt((PreviousContainerIndex < 0 ? 0 : PreviousContainerIndex), this);
else
previousContainerAsLayoutGroup.InsertChildAt(previousContainerAsLayoutGroup.ChildrenCount, this);

Expand Down
11 changes: 9 additions & 2 deletions source/Components/AvalonDock/Layout/LayoutContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -693,18 +693,25 @@ internal void CloseInternal()
var root = Root;
var parentAsContainer = Parent;

// Determine if this pane is the only DocumentPane present in the layout of the main window (not floating)
// then it will be skipped for removal in GarbageCollection (even if it is empty)
bool isParentRemoved = Root.Manager.Layout.IsParentRemoved(Parent);

if (PreviousContainer == null)
{
var parentAsGroup = Parent as ILayoutGroup;
PreviousContainer = parentAsContainer;
PreviousContainerIndex = parentAsGroup.IndexOfChild(this);

if (parentAsGroup is ILayoutPaneSerializable layoutPaneSerializable)
if (isParentRemoved == false && parentAsGroup is ILayoutPaneSerializable layoutPaneSerializable)
{
PreviousContainerId = layoutPaneSerializable.Id;
// This parentAsGroup will be removed in the GarbageCollection below

// Check whether this parentAsGroup will be removed in the GarbageCollection below
if (parentAsGroup.Children.Count() == 1 && parentAsGroup.Parent != null && Root.Manager != null)
{
// move LayoutContent up in the layout tree by setting PreviousContainer to it's grandparent
// (a LayoutPanel) by assumption that the parent will be garbage collected below.
Parent = Root.Manager.Layout;
PreviousContainer = parentAsGroup.Parent;
PreviousContainerIndex = -1;
Expand Down
22 changes: 19 additions & 3 deletions source/Components/AvalonDock/Layout/LayoutRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,7 @@ public void CollectGarbage()
}

//...if this pane is the only documentpane present in the layout of the main window (not floating) then skip it
if (emptyPane is LayoutDocumentPane &&
emptyPane.FindParent<LayoutDocumentFloatingWindow>() == null &&
this.Descendents().OfType<LayoutDocumentPane>().Count(c => c != emptyPane && c.FindParent<LayoutDocumentFloatingWindow>() == null) == 0)
if (IsParentRemoved(emptyPane))
continue;

//...if this empty pane is not referenced by anyone, then remove it from its parent container
Expand Down Expand Up @@ -620,6 +618,24 @@ void IXmlSerializable.WriteXml(XmlWriter writer)
}
writer.WriteEndElement();
}

/// <summary>
/// Determine if the parent pane is the only DocumentPane present in the layout of the main window (not floating)
/// then it will be skipped for removal in GarbageCollection (even if it is empty).
///
/// Otherwise, an empty pane will be removed in the LayoutRoot.GarbageCollection() which means that PreviousContainer
/// must be adjusted before items are removed from their parent pane.
/// </summary>
/// <param name="parent"></param>
/// <returns></returns>
internal bool IsParentRemoved(ILayoutContainer parent)
{
if (parent == null) return false;

return (parent is LayoutDocumentPane &&
parent.FindParent<LayoutDocumentFloatingWindow>() == null &&
this.Descendents().OfType<LayoutDocumentPane>().Count(c => c != parent && c.FindParent<LayoutDocumentFloatingWindow>() == null) == 0);
}
#endregion IXmlSerializable interface members

#endregion Public Methods
Expand Down

0 comments on commit f9a226f

Please sign in to comment.