diff --git a/source/Components/AvalonDock/DockingManager.cs b/source/Components/AvalonDock/DockingManager.cs index aaeffa0e..8c7ca9d0 100644 --- a/source/Components/AvalonDock/DockingManager.cs +++ b/source/Components/AvalonDock/DockingManager.cs @@ -1870,6 +1870,36 @@ internal void RemoveFloatingWindow(LayoutFloatingWindowControl floatingWindow) LayoutFloatingWindowControlClosed?.Invoke(this, new LayoutFloatingWindowControlClosedEventArgs(floatingWindow)); } + + + internal void ExecuteCloseAllButThisCommand(LayoutContent contentSelected) + { + foreach (var contentToClose in Layout.Descendents().OfType().Where(d => d != contentSelected && (d.Parent is LayoutDocumentPane || d.Parent is LayoutDocumentFloatingWindow)).ToArray()) + Close(contentToClose); + } + + internal void ExecuteCloseAllCommand(LayoutContent contentSelected) + { + foreach (var contentToClose in Layout.Descendents().OfType().Where(d => (d.Parent is LayoutDocumentPane || d.Parent is LayoutDocumentFloatingWindow)).ToArray()) + Close(contentToClose); + } + + internal void ExecuteCloseCommand(LayoutAnchorable anchorable) + { + if (!(anchorable is LayoutAnchorable model)) return; + + AnchorableClosingEventArgs closingArgs = null; + AnchorableClosing?.Invoke(this, closingArgs = new AnchorableClosingEventArgs(model)); + if (closingArgs?.Cancel == true) + return; + + if (model.CloseAnchorable()) + { + RemoveViewFromLogicalChild(model); + AnchorableClosed?.Invoke(this, new AnchorableClosedEventArgs(model)); + } + } + internal void ExecuteCloseCommand(LayoutDocument document) { if (DocumentClosing != null) @@ -1879,10 +1909,8 @@ internal void ExecuteCloseCommand(LayoutDocument document) if (argsClosing.Cancel) return; } - // - // Determine the index of the document that will be removed. - // - int indexOfDocumentToRemove = GetIndexOfDocument(document); + // Get the document to activate after the close. + LayoutDocument documentToActivate = GetDocumentToActivate(document); if (!document.CloseDocument()) return; @@ -1894,98 +1922,38 @@ internal void ExecuteCloseCommand(LayoutDocument document) //get rid of the closed document content document.Content = null; - int indexOfDocumentToSelect = indexOfDocumentToRemove - 1; - - if (indexOfDocumentToSelect < 0) - { - indexOfDocumentToSelect = 0; - } - // - // Determine the new active document and activate it. + // Activate the document determined to be the next active document. // This doesn't only update the layout, but also all related (dependency) properties. // - LayoutDocument layoutDocument = GetDocumentOnIndex(indexOfDocumentToSelect); - - if (layoutDocument != null) + if (documentToActivate != null) { - layoutDocument.IsActive = true; + documentToActivate.IsActive = true; } } - private LayoutDocument GetDocumentOnIndex(int indexToFind) + private LayoutDocument GetDocumentToActivate(LayoutDocument previousDocument) { - if (indexToFind < 0) - { - throw new ArgumentOutOfRangeException(nameof(indexToFind)); - } + ILayoutContainer parentContainer = previousDocument.Parent; + IEnumerable siblingDocuments = parentContainer?.Children.OfType() ?? Enumerable.Empty(); - int index = 0; - - foreach (LayoutDocument layoutDocument in this.Layout.Descendents().OfType()) + foreach (var childPair in siblingDocuments.Zip(siblingDocuments.Skip(1), Tuple.Create)) { - if (index == indexToFind) + if (childPair.Item2 == previousDocument) { - return layoutDocument; + return childPair.Item1; } - - index++; } - return null; - } - - private int GetIndexOfDocument(LayoutDocument documentToFind) - { - if (documentToFind == null) - { - throw new ArgumentNullException(nameof(documentToFind)); - } - - int index = 0; - - foreach (LayoutDocument layoutDocument in this.Layout.Descendents().OfType()) + foreach (LayoutDocument document in this.Layout.Descendents().OfType()) { - if (layoutDocument == documentToFind) + if (document.IsSelected) { - return index; + return document; } - - index++; } - // - // Not found. - // - return -1; - } - - internal void ExecuteCloseAllButThisCommand(LayoutContent contentSelected) - { - foreach (var contentToClose in Layout.Descendents().OfType().Where(d => d != contentSelected && (d.Parent is LayoutDocumentPane || d.Parent is LayoutDocumentFloatingWindow)).ToArray()) - Close(contentToClose); - } - - internal void ExecuteCloseAllCommand(LayoutContent contentSelected) - { - foreach (var contentToClose in Layout.Descendents().OfType().Where(d => (d.Parent is LayoutDocumentPane || d.Parent is LayoutDocumentFloatingWindow)).ToArray()) - Close(contentToClose); - } - - internal void ExecuteCloseCommand(LayoutAnchorable anchorable) - { - if (!(anchorable is LayoutAnchorable model)) return; - - AnchorableClosingEventArgs closingArgs = null; - AnchorableClosing?.Invoke(this, closingArgs = new AnchorableClosingEventArgs(model)); - if (closingArgs?.Cancel == true) - return; - - if (model.CloseAnchorable()) - { - RemoveViewFromLogicalChild(model); - AnchorableClosed?.Invoke(this, new AnchorableClosedEventArgs(model)); - } + return null; } internal void ExecuteHideCommand(LayoutAnchorable anchorable)