Skip to content

Commit a05a5e5

Browse files
gumbarrosyaira2
andauthored
Code Quality: Migrated GetCursorPos to CsWin32 (#15174)
Co-authored-by: Yair <[email protected]>
1 parent 578529a commit a05a5e5

File tree

4 files changed

+11
-23
lines changed

4 files changed

+11
-23
lines changed

src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs

-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ public static extern bool SetPropW(
5555
IntPtr hData
5656
);
5757

58-
[DllImport("user32.dll")]
59-
[return: MarshalAs(UnmanagedType.Bool)]
60-
public static extern bool GetCursorPos(
61-
out POINT point
62-
);
63-
6458
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
6559
public static extern IntPtr CreateEvent(
6660
IntPtr lpEventAttributes,

src/Files.App/Helpers/Win32/Win32PInvoke.Structs.cs

-9
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,6 @@ public struct RM_PROCESS_INFO
3232
public bool bRestartable;
3333
}
3434

35-
[StructLayout(LayoutKind.Sequential)]
36-
public struct POINT
37-
{
38-
public int X;
39-
public int Y;
40-
41-
public POINT(int x, int y) => (X, Y) = (x, y);
42-
}
43-
4435
[StructLayout(LayoutKind.Sequential)]
4536
public struct BROWSEINFO
4637
{

src/Files.App/UserControls/TabBar/TabBar.xaml.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Microsoft.UI.Xaml.Shapes;
99
using Windows.ApplicationModel.DataTransfer;
1010
using Windows.Storage;
11+
using Windows.Win32;
12+
using Windows.Win32.Foundation;
1113

1214
namespace Files.App.UserControls.TabBar
1315
{
@@ -58,7 +60,7 @@ public Rectangle DragArea
5860
=> DragAreaRectangle;
5961

6062
/// <summary> Starting position when dragging a tab.</summary>
61-
private Win32PInvoke.POINT dragStartPoint;
63+
private POINT dragStartPoint;
6264

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

152154
// Get cursor position & time to track how far the tab was dragged.
153-
Win32PInvoke.GetCursorPos(out dragStartPoint);
155+
PInvoke.GetCursorPos(out dragStartPoint);
154156
dragStartTime = DateTimeOffset.UtcNow;
155157

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

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

249251
if (sender.TabItems.Count == 1 ||
250252
(dragTime.TotalSeconds < 1 &&

src/Files.App/Utils/Storage/Helpers/FilePropertiesHelpers.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Collections.Concurrent;
1212
using Windows.ApplicationModel;
1313
using Windows.Graphics;
14+
using Windows.Win32;
1415

1516
namespace Files.App.Utils.Storage
1617
{
@@ -136,14 +137,14 @@ public static void OpenPropertiesWindow(object item, IShellPage associatedInstan
136137
new SuppressNavigationTransitionInfo());
137138

138139
// WINUI3: Move window to cursor position
139-
Win32PInvoke.GetCursorPos(out var pointerPosition);
140-
var displayArea = DisplayArea.GetFromPoint(new PointInt32(pointerPosition.X, pointerPosition.Y), DisplayAreaFallback.Nearest);
140+
PInvoke.GetCursorPos(out var pointerPosition);
141+
var displayArea = DisplayArea.GetFromPoint(new PointInt32(pointerPosition.x, pointerPosition.y), DisplayAreaFallback.Nearest);
141142
var appWindowPos = new PointInt32
142143
{
143144
X = displayArea.WorkArea.X
144-
+ Math.Max(0, Math.Min(displayArea.WorkArea.Width - appWindow.Size.Width, pointerPosition.X - displayArea.WorkArea.X)),
145+
+ Math.Max(0, Math.Min(displayArea.WorkArea.Width - appWindow.Size.Width, pointerPosition.x - displayArea.WorkArea.X)),
145146
Y = displayArea.WorkArea.Y
146-
+ Math.Max(0, Math.Min(displayArea.WorkArea.Height - appWindow.Size.Height, pointerPosition.Y - displayArea.WorkArea.Y)),
147+
+ Math.Max(0, Math.Min(displayArea.WorkArea.Height - appWindow.Size.Height, pointerPosition.y - displayArea.WorkArea.Y)),
147148
};
148149

149150
if (App.AppModel.IncrementPropertiesWindowCount() == 1)

0 commit comments

Comments
 (0)