Skip to content

Feature: Improved behavior when dragging items from an unfocused Files window #16508

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 10 commits into from
Dec 3, 2024
46 changes: 46 additions & 0 deletions src/Files.App.CsWin32/Windows.Win32.Extras.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,51 @@ namespace UI.WindowsAndMessaging
{
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate LRESULT WNDPROC(HWND hWnd, uint msg, WPARAM wParam, LPARAM lParam);

/// <summary>Contains information about the size and position of a window.</summary>
/// <remarks>
/// <para><see href="https://learn.microsoft.com/windows/win32/api/winuser/ns-winuser-windowpos">Learn more about this API from docs.microsoft.com</see>.</para>
/// </remarks>
public partial struct WINDOWPOS
{
/// <summary>
/// <para>Type: <b>HWND</b> A handle to the window.</para>
/// <para><see href="https://learn.microsoft.com/windows/win32/api/winuser/ns-winuser-windowpos#members">Read more on docs.microsoft.com</see>.</para>
/// </summary>
internal HWND hwnd;

/// <summary>
/// <para>Type: <b>HWND</b> The position of the window in Z order (front-to-back position). This member can be a handle to the window behind which this window is placed, or can be one of the special values listed with the <a href="https://docs.microsoft.com/windows/desktop/api/winuser/nf-winuser-setwindowpos">SetWindowPos</a> function.</para>
/// <para><see href="https://learn.microsoft.com/windows/win32/api/winuser/ns-winuser-windowpos#members">Read more on docs.microsoft.com</see>.</para>
/// </summary>
internal HWND hwndInsertAfter;

/// <summary>
/// <para>Type: <b>int</b> The position of the left edge of the window.</para>
/// <para><see href="https://learn.microsoft.com/windows/win32/api/winuser/ns-winuser-windowpos#members">Read more on docs.microsoft.com</see>.</para>
/// </summary>
internal int x;

/// <summary>
/// <para>Type: <b>int</b> The position of the top edge of the window.</para>
/// <para><see href="https://learn.microsoft.com/windows/win32/api/winuser/ns-winuser-windowpos#members">Read more on docs.microsoft.com</see>.</para>
/// </summary>
internal int y;

/// <summary>
/// <para>Type: <b>int</b> The window width, in pixels.</para>
/// <para><see href="https://learn.microsoft.com/windows/win32/api/winuser/ns-winuser-windowpos#members">Read more on docs.microsoft.com</see>.</para>
/// </summary>
internal int cx;

/// <summary>
/// <para>Type: <b>int</b> The window height, in pixels.</para>
/// <para><see href="https://learn.microsoft.com/windows/win32/api/winuser/ns-winuser-windowpos#members">Read more on docs.microsoft.com</see>.</para>
/// </summary>
internal int cy;

/// <summary>Type: <b>UINT</b></summary>
public SET_WINDOW_POS_FLAGS flags;
}
}
}
12 changes: 12 additions & 0 deletions src/Files.App/Helpers/Win32/Win32Helper.WindowManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.UI.Input;
using Microsoft.UI.Xaml;
using System.Reflection;
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.UI.WindowsAndMessaging;

Expand Down Expand Up @@ -58,5 +59,16 @@ public static void ChangeCursor(this UIElement uiElement, InputCursor cursor)
[cursor]
);
}

/// <summary>
/// Force window to stay at bottom of other upper windows.
/// </summary>
/// <param name="lParam">The lParam of the message.</param>
public static void ForceWindowPosition(nint lParam)
{
var windowPos = Marshal.PtrToStructure<WINDOWPOS>(lParam);
windowPos.flags |= SET_WINDOW_POS_FLAGS.SWP_NOZORDER;
Marshal.StructureToPtr(windowPos, lParam, false);
}
}
}
27 changes: 27 additions & 0 deletions src/Files.App/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public sealed partial class MainWindow : WinUIEx.WindowEx
public static MainWindow Instance => _Instance ??= new();

public nint WindowHandle { get; }
private bool CanWindowToFront { get; set; } = true;
private readonly object _canWindowToFrontLock = new();

public MainWindow()
{
Expand All @@ -35,6 +37,8 @@ public MainWindow()
AppWindow.TitleBar.ButtonPressedBackgroundColor = Colors.Transparent;
AppWindow.TitleBar.ButtonHoverBackgroundColor = Colors.Transparent;
AppWindow.SetIcon(AppLifecycleHelper.AppIconPath);

WinUIEx.WindowManager.Get(this).WindowMessageReceived += WindowManager_WindowMessageReceived;
}

public void ShowSplashScreen()
Expand Down Expand Up @@ -339,5 +343,28 @@ x.tabItem.NavigationParameter.NavigationParameter is PaneNavigationArguments pan
}
}
}

public bool SetCanWindowToFront(bool canWindowToFront)
{
lock (_canWindowToFrontLock)
{
if (CanWindowToFront != canWindowToFront)
{
CanWindowToFront = canWindowToFront;
return true;
}
return false;
}
}

private const int WM_WINDOWPOSCHANGING = 0x0046;
private void WindowManager_WindowMessageReceived(object? sender, WinUIEx.Messaging.WindowMessageEventArgs e)
{
if ((!CanWindowToFront) && e.Message.MessageId == WM_WINDOWPOSCHANGING)
{
Win32Helper.ForceWindowPosition(e.Message.LParam);
e.Handled = true;
}
}
}
}
56 changes: 51 additions & 5 deletions src/Files.App/Views/Layouts/BaseLayoutPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public abstract class BaseLayoutPage : Page, IBaseLayoutPage, INotifyPropertyCha
private CancellationTokenSource? groupingCancellationToken;

private bool shiftPressed;
private bool itemDragging;

private ListedItem? dragOverItem = null;
private ListedItem? hoveredItem = null;
Expand Down Expand Up @@ -1012,13 +1013,23 @@ protected virtual void FileList_DragItemsStarting(object sender, DragItemsStarti
var storageItemList = orderedItems.Where(x => !(x.IsHiddenItem && x.IsLinkItem && x.IsRecycleBinItem && x.IsShortcut)).Select(x => VirtualStorageItem.FromListedItem(x));
e.Data.SetStorageItems(storageItemList, false);
}

MainWindow.Instance.SetCanWindowToFront(false);
itemDragging = true;
}
catch (Exception)
{
e.Cancel = true;
}
}

protected virtual void FileList_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
{
itemDragging = false;
MainWindow.Instance.SetCanWindowToFront(true);
// No need to bring the window to the front
}

private void Item_DragLeave(object sender, DragEventArgs e)
{
var item = GetItemFromElement(sender);
Expand Down Expand Up @@ -1147,13 +1158,19 @@ protected void FileList_ContainerContentChanging(ListViewBase sender, ContainerC
{
RefreshContainer(args.ItemContainer, args.InRecycleQueue);
RefreshItem(args.ItemContainer, args.Item, args.InRecycleQueue, args);

itemDragging = false;
MainWindow.Instance.SetCanWindowToFront(true);
// No need to bring the window to the front
}

private void RefreshContainer(SelectorItem container, bool inRecycleQueue)
{
container.PointerPressed -= FileListItem_PointerPressed;
container.PointerEntered -= FileListItem_PointerEntered;
container.PointerExited -= FileListItem_PointerExited;
container.Tapped -= FileListItem_Tapped;
container.DoubleTapped -= FileListItem_DoubleTapped;
container.RightTapped -= FileListItem_RightTapped;

if (inRecycleQueue)
Expand All @@ -1163,12 +1180,11 @@ private void RefreshContainer(SelectorItem container, bool inRecycleQueue)
else
{
container.PointerPressed += FileListItem_PointerPressed;
container.PointerEntered += FileListItem_PointerEntered;
container.PointerExited += FileListItem_PointerExited;
container.Tapped += FileListItem_Tapped;
container.DoubleTapped += FileListItem_DoubleTapped;
container.RightTapped += FileListItem_RightTapped;
if (UserSettingsService.FoldersSettingsService.SelectFilesOnHover)
{
container.PointerEntered += FileListItem_PointerEntered;
container.PointerExited += FileListItem_PointerExited;
}
}
}

Expand Down Expand Up @@ -1200,6 +1216,10 @@ private void RefreshItem(SelectorItem container, object item, bool inRecycleQueu

protected internal void FileListItem_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// Set can window to front and bring the window to the front if necessary
if ((!itemDragging) && MainWindow.Instance.SetCanWindowToFront(true))
Win32Helper.BringToForegroundEx(new(MainWindow.Instance.WindowHandle));

if (sender is not SelectorItem selectorItem)
return;

Expand All @@ -1225,6 +1245,10 @@ protected internal void FileListItem_PointerPressed(object sender, PointerRouted

protected internal void FileListItem_PointerEntered(object sender, PointerRoutedEventArgs e)
{
// Set can window to front before the item is dragged
if (sender is SelectorItem selectorItem && selectorItem.IsSelected)
MainWindow.Instance.SetCanWindowToFront(false);

if (!UserSettingsService.FoldersSettingsService.SelectFilesOnHover)
return;

Expand Down Expand Up @@ -1271,15 +1295,37 @@ selectedItems is not null &&

protected internal void FileListItem_PointerExited(object sender, PointerRoutedEventArgs e)
{
// Set can window to front
if (!itemDragging)
MainWindow.Instance.SetCanWindowToFront(true);

if (!UserSettingsService.FoldersSettingsService.SelectFilesOnHover)
return;

hoverTimer.Stop();
hoveredItem = null;
}

protected void FileListItem_Tapped(object sender, TappedRoutedEventArgs e)
{
// Set can window to front and bring the window to the front if necessary
if ((!itemDragging) && MainWindow.Instance.SetCanWindowToFront(true))
Win32Helper.BringToForegroundEx(new(MainWindow.Instance.WindowHandle));
}

protected void FileListItem_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
// Set can window to front and bring the window to the front if necessary
if ((!itemDragging) && MainWindow.Instance.SetCanWindowToFront(true))
Win32Helper.BringToForegroundEx(new(MainWindow.Instance.WindowHandle));
}

protected void FileListItem_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
// Set can window to front and bring the window to the front if necessary
if ((!itemDragging) && MainWindow.Instance.SetCanWindowToFront(true))
Win32Helper.BringToForegroundEx(new(MainWindow.Instance.WindowHandle));

var rightClickedItem = GetItemFromElement(sender);

if (rightClickedItem is not null && !((SelectorItem)sender).IsSelected)
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Views/Layouts/ColumnLayoutPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
CanDragItems="{x:Bind AllowItemDrag, Mode=OneWay}"
ContainerContentChanging="FileList_ContainerContentChanging"
DoubleTapped="FileList_DoubleTapped"
DragItemsCompleted="FileList_DragItemsCompleted"
DragItemsStarting="FileList_DragItemsStarting"
DragOver="ItemsLayout_DragOver"
Drop="ItemsLayout_Drop"
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Views/Layouts/DetailsLayoutPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
CanDragItems="{x:Bind AllowItemDrag, Mode=OneWay}"
ContainerContentChanging="FileList_ContainerContentChanging"
DoubleTapped="FileList_DoubleTapped"
DragItemsCompleted="FileList_DragItemsCompleted"
DragItemsStarting="FileList_DragItemsStarting"
DragOver="ItemsLayout_DragOver"
Drop="ItemsLayout_Drop"
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Views/Layouts/GridLayoutPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@
ContainerContentChanging="FileList_ContainerContentChanging"
DoubleTapped="FileList_DoubleTapped"
DragEnter="ItemsLayout_DragEnter"
DragItemsCompleted="FileList_DragItemsCompleted"
DragItemsStarting="FileList_DragItemsStarting"
DragLeave="ItemsLayout_DragLeave"
DragOver="ItemsLayout_DragOver"
Expand Down