Skip to content

Commit

Permalink
Code Quality: Migrated GetCursorPos to CsWin32 (files-community#15174)
Browse files Browse the repository at this point in the history
Co-authored-by: Yair <39923744+yaira2@users.noreply.github.com>
  • Loading branch information
gumbarros and yaira2 authored Apr 14, 2024
1 parent 578529a commit a05a5e5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 23 deletions.
6 changes: 0 additions & 6 deletions src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ public static extern bool SetPropW(
IntPtr hData
);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(
out POINT point
);

[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr CreateEvent(
IntPtr lpEventAttributes,
Expand Down
9 changes: 0 additions & 9 deletions src/Files.App/Helpers/Win32/Win32PInvoke.Structs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ public struct RM_PROCESS_INFO
public bool bRestartable;
}

[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;

public POINT(int x, int y) => (X, Y) = (x, y);
}

[StructLayout(LayoutKind.Sequential)]
public struct BROWSEINFO
{
Expand Down
10 changes: 6 additions & 4 deletions src/Files.App/UserControls/TabBar/TabBar.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.UI.Xaml.Shapes;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.Win32;
using Windows.Win32.Foundation;

namespace Files.App.UserControls.TabBar
{
Expand Down Expand Up @@ -58,7 +60,7 @@ public Rectangle DragArea
=> DragAreaRectangle;

/// <summary> Starting position when dragging a tab.</summary>
private Win32PInvoke.POINT dragStartPoint;
private POINT dragStartPoint;

/// <summary> Starting time when dragging a tab. </summary>
private DateTimeOffset dragStartTime;
Expand Down Expand Up @@ -150,7 +152,7 @@ private void TabView_TabDragStarting(TabView sender, TabViewTabDragStartingEvent
args.Data.RequestedOperation = DataPackageOperation.Move;

// Get cursor position & time to track how far the tab was dragged.
Win32PInvoke.GetCursorPos(out dragStartPoint);
PInvoke.GetCursorPos(out dragStartPoint);
dragStartTime = DateTimeOffset.UtcNow;

// Focus the UI Element, without this the focus sometimes changes
Expand Down Expand Up @@ -241,10 +243,10 @@ private async void TabView_TabDroppedOutside(TabView sender, TabViewTabDroppedOu
if (isCancelingDragOperation)
return;

Win32PInvoke.GetCursorPos(out var droppedPoint);
PInvoke.GetCursorPos(out var droppedPoint);
var droppedTime = DateTimeOffset.UtcNow;
var dragTime = droppedTime - dragStartTime;
var dragDistance = Math.Sqrt(Math.Pow((dragStartPoint.X - droppedPoint.X), 2) + Math.Pow((dragStartPoint.Y - droppedPoint.Y), 2));
var dragDistance = Math.Sqrt(Math.Pow(dragStartPoint.x - droppedPoint.x, 2) + Math.Pow(dragStartPoint.y - droppedPoint.y, 2));

if (sender.TabItems.Count == 1 ||
(dragTime.TotalSeconds < 1 &&
Expand Down
9 changes: 5 additions & 4 deletions src/Files.App/Utils/Storage/Helpers/FilePropertiesHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Collections.Concurrent;
using Windows.ApplicationModel;
using Windows.Graphics;
using Windows.Win32;

namespace Files.App.Utils.Storage
{
Expand Down Expand Up @@ -136,14 +137,14 @@ public static void OpenPropertiesWindow(object item, IShellPage associatedInstan
new SuppressNavigationTransitionInfo());

// WINUI3: Move window to cursor position
Win32PInvoke.GetCursorPos(out var pointerPosition);
var displayArea = DisplayArea.GetFromPoint(new PointInt32(pointerPosition.X, pointerPosition.Y), DisplayAreaFallback.Nearest);
PInvoke.GetCursorPos(out var pointerPosition);
var displayArea = DisplayArea.GetFromPoint(new PointInt32(pointerPosition.x, pointerPosition.y), DisplayAreaFallback.Nearest);
var appWindowPos = new PointInt32
{
X = displayArea.WorkArea.X
+ Math.Max(0, Math.Min(displayArea.WorkArea.Width - appWindow.Size.Width, pointerPosition.X - displayArea.WorkArea.X)),
+ Math.Max(0, Math.Min(displayArea.WorkArea.Width - appWindow.Size.Width, pointerPosition.x - displayArea.WorkArea.X)),
Y = displayArea.WorkArea.Y
+ Math.Max(0, Math.Min(displayArea.WorkArea.Height - appWindow.Size.Height, pointerPosition.Y - displayArea.WorkArea.Y)),
+ Math.Max(0, Math.Min(displayArea.WorkArea.Height - appWindow.Size.Height, pointerPosition.y - displayArea.WorkArea.Y)),
};

if (App.AppModel.IncrementPropertiesWindowCount() == 1)
Expand Down

0 comments on commit a05a5e5

Please sign in to comment.