Skip to content

Commit 19c78cc

Browse files
Ahamed-AliPureWeen
authored andcommitted
[Windows] Test inclusion for the PointerGestureRecognizer issue in MultiWindow. (#31143)
* Included the test for the PointerGesture Issue on windows * Used Windows API for minimizing the window * Minimize the window using GetWindowHandle * prevent unwanted issues
1 parent f3c0771 commit 19c78cc

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
#if WINDOWS
2+
using System.Runtime.InteropServices;
3+
#endif
4+
5+
namespace Maui.Controls.Sample.Issues;
6+
7+
[Issue(IssueTracker.Github, 30536, "[Windows] PointerGestureRecognizer behaves incorrectly when multiple windows are open", PlatformAffected.UWP)]
8+
public class Issue30536 : ContentPage
9+
{
10+
Button newWindowButton;
11+
Button closeNewWindowButton;
12+
Button minimizeSecondWindowButton;
13+
Label pointerEnterCountLabel;
14+
Label pointerExitCountLabel;
15+
Border theBorder;
16+
int enterCount = 0;
17+
int exitCount = 0;
18+
Window secondWindow;
19+
public Issue30536()
20+
{
21+
InitializeUI();
22+
}
23+
24+
private void InitializeUI()
25+
{
26+
// Create the VerticalStackLayout
27+
var stackLayout = new VerticalStackLayout
28+
{
29+
Spacing = 5
30+
};
31+
32+
// Create New Window button
33+
newWindowButton = new Button
34+
{
35+
Text = "New Window",
36+
AutomationId = "NewWindowButton"
37+
};
38+
newWindowButton.Clicked += OnNewWindowButton_Clicked;
39+
40+
closeNewWindowButton = new Button
41+
{
42+
Text = "Close New Window",
43+
AutomationId = "CloseNewWindowButton"
44+
};
45+
closeNewWindowButton.Clicked += (sender, e) =>
46+
{
47+
if (secondWindow != null)
48+
{
49+
Application.Current.CloseWindow(secondWindow);
50+
secondWindow = null;
51+
}
52+
};
53+
54+
minimizeSecondWindowButton = new Button
55+
{
56+
Text = "Minimize Second Window",
57+
AutomationId = "MinimizeSecondWindowButton"
58+
};
59+
minimizeSecondWindowButton.Clicked += (sender, e) =>
60+
{
61+
if (secondWindow != null)
62+
{
63+
MinimizeWindow(secondWindow);
64+
}
65+
};
66+
67+
// Create pointer enter count label
68+
pointerEnterCountLabel = new Label
69+
{
70+
Text = $"Pointer Enter Count: {enterCount}",
71+
AutomationId = "PointerEnterCountLabel"
72+
73+
};
74+
75+
pointerExitCountLabel = new Label
76+
{
77+
Text = $"Pointer Exit Count: {exitCount}",
78+
AutomationId = "PointerExitCountLabel"
79+
80+
};
81+
82+
// Create the border with red background
83+
theBorder = new Border
84+
{
85+
WidthRequest = 500,
86+
HeightRequest = 500,
87+
BackgroundColor = Colors.Red,
88+
Content = new Button
89+
{
90+
Text = "Tap Me",
91+
AutomationId = "BorderButton"
92+
}
93+
};
94+
95+
// Create and add PointerGestureRecognizer
96+
var pointerGestureRecognizer = new PointerGestureRecognizer();
97+
pointerGestureRecognizer.PointerEntered += OnPointerGestureRecognizer_PointerEntered;
98+
pointerGestureRecognizer.PointerExited += OnPointerGestureRecognizer_PointerExited;
99+
theBorder.GestureRecognizers.Add(pointerGestureRecognizer);
100+
101+
// Add all elements to the stack layout
102+
stackLayout.Children.Add(minimizeSecondWindowButton);
103+
stackLayout.Children.Add(newWindowButton);
104+
stackLayout.Children.Add(closeNewWindowButton);
105+
stackLayout.Children.Add(pointerEnterCountLabel);
106+
stackLayout.Children.Add(pointerExitCountLabel);
107+
stackLayout.Children.Add(theBorder);
108+
109+
// Set the content
110+
Content = stackLayout;
111+
}
112+
113+
#if WINDOWS
114+
// Windows API declarations
115+
[DllImport("user32.dll")]
116+
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
117+
118+
// ShowWindow constants
119+
private const int SW_MINIMIZE = 6;
120+
#endif
121+
122+
private void OnNewWindowButton_Clicked(object sender, EventArgs e)
123+
{
124+
secondWindow = new Window(new ContentPage());
125+
Application.Current.OpenWindow(secondWindow);
126+
}
127+
128+
private void MinimizeWindow(Window window)
129+
{
130+
#if WINDOWS
131+
if (window.Handler?.PlatformView is Microsoft.UI.Xaml.Window win)
132+
{
133+
// Get window handle directly from MAUI's platform view
134+
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(win);
135+
if (hwnd != IntPtr.Zero)
136+
{
137+
ShowWindow(hwnd, SW_MINIMIZE);
138+
}
139+
}
140+
#endif
141+
}
142+
private void OnPointerGestureRecognizer_PointerEntered(object sender, PointerEventArgs e)
143+
{
144+
theBorder.BackgroundColor = Colors.Lime;
145+
enterCount++;
146+
pointerEnterCountLabel.Text = $"Pointer Enter Count: {enterCount}";
147+
}
148+
private void OnPointerGestureRecognizer_PointerExited(object sender, PointerEventArgs e)
149+
{
150+
theBorder.BackgroundColor = Colors.Red;
151+
exitCount++;
152+
pointerExitCountLabel.Text = $"Pointer Exit Count: {exitCount}";
153+
}
154+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#if WINDOWS
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
public class Issue30536 : _IssuesUITest
8+
{
9+
public Issue30536(TestDevice device) : base(device) { }
10+
11+
public override string Issue => "[Windows] PointerGestureRecognizer behaves incorrectly when multiple windows are open";
12+
13+
[Test]
14+
[Category(UITestCategories.Gestures)]
15+
public void PointerGesturesShouldWorkProperlyOnMultiWindows()
16+
{
17+
App.WaitForElement("NewWindowButton");
18+
App.Tap("NewWindowButton");
19+
App.WaitForElement("MinimizeSecondWindowButton");
20+
App.Tap("MinimizeSecondWindowButton");
21+
App.WaitForElement("BorderButton");
22+
App.Tap("BorderButton");
23+
var pointerEnterCount = App.FindElement("PointerEnterCountLabel");
24+
Assert.That(pointerEnterCount.GetText(), Is.EqualTo("Pointer Enter Count: 1"));
25+
26+
var pointerExitCount = App.FindElement("PointerExitCountLabel");
27+
Assert.That(pointerExitCount.GetText(), Is.EqualTo("Pointer Exit Count: 0"));
28+
//once verified that pointer enter and exit counts are correct, close the newly created window
29+
App.WaitForElement("CloseNewWindowButton");
30+
App.Tap("CloseNewWindowButton");
31+
}
32+
}
33+
#endif

0 commit comments

Comments
 (0)