Skip to content

Fix the get owner DockingManagerWindow and Update drag and drop #394

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

Merged
merged 5 commits into from
Nov 4, 2022
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
51 changes: 40 additions & 11 deletions source/Components/AvalonDock/Controls/DragService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ This program is provided to you under the terms of the Microsoft Public
************************************************************************/

using AvalonDock.Layout;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Interop;

namespace AvalonDock.Controls
{
Expand Down Expand Up @@ -55,6 +57,7 @@ internal class DragService
private IOverlayWindow _currentWindow;
private List<IDropArea> _currentWindowAreas = new List<IDropArea>();
private IDropTarget _currentDropTarget;
private bool _isDrag;

#endregion fields

Expand All @@ -69,8 +72,6 @@ public DragService(LayoutFloatingWindowControl floatingWindow)
{
_floatingWindow = floatingWindow;
_manager = floatingWindow.Model.Root.Manager;

GetOverlayWindowHosts();
}

#endregion Constructors
Expand All @@ -87,6 +88,12 @@ internal void UpdateMouseLocation(Point dragPosition)
////var floatingWindowModel = _floatingWindow.Model as LayoutFloatingWindow;
// TODO - pass in without DPI adjustment, screen co-ords, adjust inside the target window

if (!_isDrag)
{
GetOverlayWindowHosts();
_isDrag = true;
}

var newHost = _overlayWindowHosts.FirstOrDefault(oh => oh.HitTestScreen(dragPosition));

if (_currentHost != null || _currentHost != newHost)
Expand All @@ -110,7 +117,11 @@ internal void UpdateMouseLocation(Point dragPosition)
if (_currentWindow != null)
_currentWindow.DragLeave(_floatingWindow);
if (_currentHost != null)
{
_currentHost.HideOverlayWindow();
GetOverlayWindowHosts();
}

_currentHost = null;
}

Expand All @@ -119,6 +130,25 @@ internal void UpdateMouseLocation(Point dragPosition)
_currentHost = newHost;
_currentWindow = _currentHost.ShowOverlayWindow(_floatingWindow);
_currentWindow.DragEnter(_floatingWindow);

// Set the target window to topmost
if (_currentHost is LayoutFloatingWindowControl fwc &&
(fwc.OwnedByDockingManagerWindow == _floatingWindow.OwnedByDockingManagerWindow || fwc.OwnedByDockingManagerWindow))
{
BringWindowToTop2(fwc);
}
else if (_currentHost is DockingManager dockingManager)
{
BringWindowToTop2(Window.GetWindow(dockingManager));
}

GetOverlayWindowHosts();

BringWindowToTop2(_floatingWindow);
if (_currentWindow is Window overlayWindow)
{
BringWindowToTop2(overlayWindow);
}
}
}

Expand All @@ -135,7 +165,6 @@ internal void UpdateMouseLocation(Point dragPosition)
List<IDropArea> areasToRemove = new List<IDropArea>();
_currentWindowAreas.ForEach(a =>
{

//is mouse still inside this area?
if (!a.DetectionRect.Contains(a.TransformToDeviceDPI(dragPosition)))
{
Expand Down Expand Up @@ -215,6 +244,7 @@ internal void Drop(Point dropLocation, out bool dropHandled)

_currentWindow = null;
_currentHost = null;
_isDrag = false;
}

/// <summary>
Expand Down Expand Up @@ -246,6 +276,12 @@ internal void Abort()

#region Private Methods

private void BringWindowToTop2(Window window)
{
Win32Helper.SetWindowPos(new WindowInteropHelper(window).Handle,
IntPtr.Zero, 0, 0, 0, 0, Win32Helper.SetWindowPosFlags.IgnoreResize | Win32Helper.SetWindowPosFlags.IgnoreMove | Win32Helper.SetWindowPosFlags.DoNotActivate);
}

/// <summary>
/// Adds <see cref="IOverlayWindowHost"/>s into a private collection of possible
/// drop target hosts that can show a drop target button to drop a dragged
Expand All @@ -256,14 +292,7 @@ private void GetOverlayWindowHosts()
{
if (_manager.Layout.RootPanel.CanDock)
{
// Add LayoutFloatingWindowControls as drop target hosts
// 1) Don't drop a floating window on to itself
// 2) Use this Drop target if its visible
_overlayWindowHosts.AddRange(_manager.GetFloatingWindowsByZOrder().OfType<LayoutAnchorableFloatingWindowControl>().Where(fw => fw != _floatingWindow && fw.IsVisible));
_overlayWindowHosts.AddRange(_manager.GetFloatingWindowsByZOrder().OfType<LayoutDocumentFloatingWindowControl>().Where(fw => fw != _floatingWindow && fw.IsVisible));

// Add dockingManager itself as a drop target host
_overlayWindowHosts.Add(_manager);
_manager.GetOverlayWindowHostsByZOrder(ref _overlayWindowHosts, _floatingWindow);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,9 @@ private void CreateOverlayWindow(LayoutFloatingWindowControl draggingWindow)

// Usually, the overlay window is made a child of the main window. However, if the floating
// window being dragged isn't also a child of the main window (because OwnedByDockingManagerWindow
// is set to false to allow the parent window to be minimized independently of floating windows),
// this causes the floating window to be moved behind the main window. Just not setting the parent
// seems to work acceptably here in that case.
// is set to false to allow the parent window to be minimized independently of floating windows)
if (draggingWindow?.OwnedByDockingManagerWindow ?? true)
_overlayWindow.Owner = Window.GetWindow(this);
_overlayWindow.Owner = Window.GetWindow(_model.Root.Manager);
else
_overlayWindow.Owner = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ This program is provided to you under the terms of the Microsoft Public
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Interop;

namespace AvalonDock.Controls
{
Expand Down Expand Up @@ -231,11 +230,9 @@ private void CreateOverlayWindow(LayoutFloatingWindowControl draggingWindow)

// Usually, the overlay window is made a child of the main window. However, if the floating
// window being dragged isn't also a child of the main window (because OwnedByDockingManagerWindow
// is set to false to allow the parent window to be minimized independently of floating windows),
// this causes the floating window to be moved behind the main window. Just not setting the parent
// seems to work acceptably here in that case.
// is set to false to allow the parent window to be minimized independently of floating windows)
if (draggingWindow?.OwnedByDockingManagerWindow ?? true)
_overlayWindow.Owner = Window.GetWindow(this);
_overlayWindow.Owner = Window.GetWindow(_model.Root.Manager);
else
_overlayWindow.Owner = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public abstract class LayoutFloatingWindowControl : Window, ILayoutControl
/// </summary>
/// <see cref="TotalMargin"/>
private bool _isTotalMarginSet = false;

#endregion fields

#region Constructors
Expand Down
46 changes: 41 additions & 5 deletions source/Components/AvalonDock/DockingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1621,7 +1621,7 @@ internal UIElement CreateUIElementForModel(ILayoutElement model)
{
//Owner = Window.GetWindow(this)
};

newFW.UpdateOwnership();

// Fill list before calling Show (issue #254)
Expand Down Expand Up @@ -1665,7 +1665,7 @@ internal UIElement CreateUIElementForModel(ILayoutElement model)
{
//Owner = Window.GetWindow(this)
};

newFW.UpdateOwnership();

// Fill list before calling Show (issue #254)
Expand Down Expand Up @@ -1777,6 +1777,44 @@ internal IEnumerable<LayoutFloatingWindowControl> GetFloatingWindowsByZOrder()
}
}

internal void GetOverlayWindowHostsByZOrder(ref List<IOverlayWindowHost> overlayWindowHosts, LayoutFloatingWindowControl dragFloatingWindow)
{
overlayWindowHosts.Clear();

var topFloatingWindows = new List<IOverlayWindowHost>();
var bottomFloatingWindows = new List<IOverlayWindowHost>();

var parentWindow = Window.GetWindow(this);
var windowParentHandle = parentWindow != null ? new WindowInteropHelper(parentWindow).Handle : Process.GetCurrentProcess().MainWindowHandle;
var b = Win32Helper.GetWindowZOrder(windowParentHandle, out var mainWindow_z);
var currentHandle = Win32Helper.GetWindow(windowParentHandle, (uint)Win32Helper.GetWindow_Cmd.GW_HWNDFIRST);
while (currentHandle != IntPtr.Zero)
{
for (int i = 0; i < _fwList.Count; i++)
{
var fw = _fwList[i];
if (fw is IOverlayWindowHost host && fw != dragFloatingWindow && fw.IsVisible)
{
var fw_hwnd = new WindowInteropHelper(fw).Handle;
if (currentHandle == fw_hwnd && fw.Model.Root != null && fw.Model.Root.Manager == this)
{
if (fw.OwnedByDockingManagerWindow || (Win32Helper.GetWindowZOrder(fw_hwnd, out var fw_z) && fw_z > mainWindow_z))
topFloatingWindows.Add(host);
else
bottomFloatingWindows.Add(host);
break;
}
}
}

currentHandle = Win32Helper.GetWindow(currentHandle, (uint)Win32Helper.GetWindow_Cmd.GW_HWNDNEXT);
}

overlayWindowHosts.AddRange(topFloatingWindows);
overlayWindowHosts.Add(this);
overlayWindowHosts.AddRange(bottomFloatingWindows);
}

internal void RemoveFloatingWindow(LayoutFloatingWindowControl floatingWindow) => _fwList.Remove(floatingWindow);

internal void ExecuteCloseCommand(LayoutDocument document)
Expand Down Expand Up @@ -2104,9 +2142,7 @@ private void CreateOverlayWindow(LayoutFloatingWindowControl draggingWindow = nu

// Usually, the overlay window is made a child of the main window. However, if the floating
// window being dragged isn't also a child of the main window (because OwnedByDockingManagerWindow
// is set to false to allow the parent window to be minimized independently of floating windows),
// this causes the floating window to be moved behind the main window. Just not setting the parent
// seems to work acceptably here in that case.
// is set to false to allow the parent window to be minimized independently of floating windows)
if (draggingWindow?.OwnedByDockingManagerWindow ?? true)
_overlayWindow.Owner = Window.GetWindow(this);
else
Expand Down
25 changes: 22 additions & 3 deletions source/Components/AvalonDock/Win32Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,6 @@ internal static RECT GetWindowRect(IntPtr hWnd)
[DllImport("user32.dll")]
internal static extern IntPtr GetTopWindow(IntPtr hWnd);

internal const uint GW_HWNDNEXT = 2;
internal const uint GW_HWNDPREV = 3;

[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

Expand All @@ -340,6 +337,28 @@ internal enum GetWindow_Cmd : uint
GW_ENABLEDPOPUP = 6
}

public static bool GetWindowZOrder(IntPtr hwnd, out int zOrder)
{
var lowestHwnd = GetWindow(hwnd, (uint)GetWindow_Cmd.GW_HWNDLAST);

var z = 0;
var hwndTmp = lowestHwnd;
while (hwndTmp != IntPtr.Zero)
{
if (hwnd == hwndTmp)
{
zOrder = z;
return true;
}

hwndTmp = GetWindow(hwndTmp, (uint)GetWindow_Cmd.GW_HWNDPREV);
z++;
}

zOrder = int.MinValue;
return false;
}

internal static int MakeLParam(int LoWord, int HiWord)
{
return (int)((HiWord << 16) | (LoWord & 0xffff));
Expand Down