Skip to content

Commit

Permalink
Merge pull request #346 from BAndysc/sort_windows
Browse files Browse the repository at this point in the history
Sort dock control parent windows by their z-order to correctly determine potential window to dock to
  • Loading branch information
wieslawsoltes authored Jul 23, 2024
2 parents aaf327f + 809fda5 commit 06b445e
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/Dock.Avalonia/Internal/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,35 @@ namespace Dock.Avalonia.Internal;

internal static class Extensions
{
private static int IndexOf(this Window[] windowsArray, Window? windowToFind)
{
if (windowToFind == null)
return -1;

for (var i = 0; i < windowsArray.Length; i++)
{
if (ReferenceEquals(windowsArray[i], windowToFind))
return i;
}

return -1;
}

public static IEnumerable<DockControl> GetZOrderedDockControls(this IList<IDockControl> dockControls)
{
// Note: we should traverse the dock controls in their windows' z-order.
// However there is no way to get the z-order of a window in Avalonia.
// Uncomment once this PR is merged and a new Avalonia version is released
// https://github.com/AvaloniaUI/Avalonia/pull/14909
// return dockControls
// .OfType<DockControl>()
// .Select(dock => (dock, order: (dock.GetVisualRoot() as Window)?.WindowZOrder ?? IntPtr.Zero))
// .OrderByDescending(x => x.order)
// .Select(pair => pair.dock);
var windows = dockControls
.OfType<DockControl>()
.Select(dock => dock.GetVisualRoot() as Window)
.OfType<Window>()
.Distinct()
.ToArray();

Window.SortWindowsByZOrder(windows);

// For now, as a workaround, iterating in the reverse order of the dock controls is better then the regular order,
// because the main window dock control is always at index 0 and all the other windows are always
// on top of the main window.
return dockControls
.OfType<DockControl>()
.Reverse();
.Select(dock => (dock, order: windows.IndexOf(dock.GetVisualRoot() as Window)))
.OrderByDescending(x => x.order)
.Select(pair => pair.dock);
}
}

0 comments on commit 06b445e

Please sign in to comment.