Skip to content
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions src/Dock.Model/FactoryBase.Dockable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public virtual void RemoveDockable(IDockable dockable, bool collapse)
var index = dock.VisibleDockables.IndexOf(dockable);
if (index < 0)
{
if (FindRoot(dockable, x => x.IsFocusableRoot) is { } root &&
ReferenceEquals(root.FocusedDockable, dockable))
{
SetFocusedDockable(dock, dock.ActiveDockable);
}
return;
}

Expand All @@ -69,6 +74,12 @@ public virtual void RemoveDockable(IDockable dockable, bool collapse)
}
}

if (FindRoot(dockable, x => x.IsFocusableRoot) is { } rootDock &&
ReferenceEquals(rootDock.FocusedDockable, dockable))
{
SetFocusedDockable(dock, dock.ActiveDockable);
}

// Clean up orphaned splitters
CleanupOrphanedSplitters(dock);

Expand Down
64 changes: 35 additions & 29 deletions src/Dock.Model/FactoryBase.Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,47 +275,53 @@ private void SetIsActive(IDockable dockable, bool active)
/// <inheritdoc />
public virtual void SetFocusedDockable(IDock dock, IDockable? dockable)
{
if (dock.ActiveDockable is not null && FindRoot(dock.ActiveDockable, x => x.IsFocusableRoot) is { } root)
if (FindRoot(dock, x => x.IsFocusableRoot) is not { } root)
{
if (dockable is not null)
{
var results = Find(x => x is IRootDock);
return;
}

foreach (var result in results)
if (dockable is not null)
{
var results = Find(x => x is IRootDock);

foreach (var result in results)
{
if (result is IRootDock rootDock
&& rootDock.IsFocusableRoot
&& rootDock != root)
{
if (result is IRootDock rootDock
&& rootDock.IsFocusableRoot
&& rootDock != root)
if (rootDock.FocusedDockable?.Owner is not null)
{
if (rootDock.FocusedDockable?.Owner is not null)
{
SetIsActive(rootDock.FocusedDockable.Owner, false);
// Trigger deactivation event for the dockable that lost focus
OnDockableDeactivated(rootDock.FocusedDockable);
}
SetIsActive(rootDock.FocusedDockable.Owner, false);
// Trigger deactivation event for the dockable that lost focus
OnDockableDeactivated(rootDock.FocusedDockable);
}
}
}
}

if (root.FocusedDockable?.Owner is not null)
{
SetIsActive(root.FocusedDockable.Owner, false);
// Trigger deactivation event for the dockable that lost focus
OnDockableDeactivated(root.FocusedDockable);
}
if (root.FocusedDockable?.Owner is not null)
{
SetIsActive(root.FocusedDockable.Owner, false);
// Trigger deactivation event for the dockable that lost focus
OnDockableDeactivated(root.FocusedDockable);
}

if (dockable is not null)
if (dockable is not null)
{
if (root.FocusedDockable != dockable)
{
if (root.FocusedDockable != dockable)
{
root.FocusedDockable = dockable;
}
root.FocusedDockable = dockable;
}
}
else
{
root.FocusedDockable = null;
}

if (root.FocusedDockable?.Owner is not null)
{
SetIsActive(root.FocusedDockable.Owner, true);
}
if (root.FocusedDockable?.Owner is not null)
{
SetIsActive(root.FocusedDockable.Owner, true);
}
}
}
21 changes: 21 additions & 0 deletions tests/Dock.Avalonia.HeadlessTests/FactoryDockableLifecycleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,25 @@ public void CloseDockable_Active_Selects_Neighbour()
Assert.Same(doc2, dock.ActiveDockable);
Assert.Single(dock.VisibleDockables!);
}

[AvaloniaFact]
public void CloseDockable_Active_Clears_FocusedDockable()
{
var factory = new Factory();
var root = new RootDock { VisibleDockables = factory.CreateList<IDockable>() };
root.Factory = factory;

var doc = new Document();
factory.AddDockable(root, doc);

root.ActiveDockable = doc;

Assert.Same(doc, root.FocusedDockable);

factory.CloseDockable(doc);

Assert.Null(root.ActiveDockable);
Assert.Null(root.FocusedDockable);
Assert.Empty(root.VisibleDockables!);
}
}
Loading