Skip to content

Commit

Permalink
Merge pull request #436 from fredriks123/Issue_434
Browse files Browse the repository at this point in the history
Changed how the next active document is picked on document close.
  • Loading branch information
Dirkster99 authored Aug 16, 2023
2 parents 2ce0033 + 0cc7cb5 commit 04f358a
Showing 1 changed file with 45 additions and 77 deletions.
122 changes: 45 additions & 77 deletions source/Components/AvalonDock/DockingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LayoutContent>().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<LayoutContent>().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)
Expand All @@ -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;

Expand All @@ -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<LayoutDocument> siblingDocuments = parentContainer?.Children.OfType<LayoutDocument>() ?? Enumerable.Empty<LayoutDocument>();

int index = 0;

foreach (LayoutDocument layoutDocument in this.Layout.Descendents().OfType<LayoutDocument>())
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<LayoutDocument>())
foreach (LayoutDocument document in this.Layout.Descendents().OfType<LayoutDocument>())
{
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<LayoutContent>().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<LayoutContent>().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)
Expand Down

0 comments on commit 04f358a

Please sign in to comment.