Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed how the next active document is picked on document close. #436

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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