Skip to content

Commit f891a43

Browse files
authored
Merge pull request #22629 from ajpinedam/fix/macOS.external.trackpad.handle
fix(macOS): external trackpad handle
2 parents a60a9a2 + f150689 commit f891a43

File tree

7 files changed

+105
-13
lines changed

7 files changed

+105
-13
lines changed

src/SamplesApp/LinkedFiles/WebContent/js/site.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@ function sendWebMessage(backgroundColor) {
1818

1919
window.onload = function() {
2020
document.querySelector('#second').style.backgroundColor = 'blue';
21+
document.querySelector('#second').addEventListener('click', function() {
22+
document.querySelector('#second').style.backgroundColor = 'green';
23+
});
2124
sendWebMessage(getComputedStyle(document.querySelector("#first")).backgroundColor);
2225
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Page
2+
x:Class="UITests.Microsoft_UI_Xaml_Controls.WebView2Tests.WebView2_MouseEvents"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:local="using:UITests.Microsoft_UI_Xaml_Controls.WebView2Tests"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
9+
mc:Ignorable="d"
10+
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
11+
12+
<Grid>
13+
<Button x:Name="LocalButton" Content="1" Click="OnButton_Clicked" Width="200" />
14+
<controls:WebView2 x:Name="Inline" Width="500" Height="500" />
15+
</Grid>
16+
</Page>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Microsoft.Web.WebView2.Core;
2+
using System;
3+
using Microsoft.UI.Xaml.Controls;
4+
5+
namespace UITests.Microsoft_UI_Xaml_Controls.WebView2Tests;
6+
7+
/// <summary>
8+
/// An empty page that can be used on its own or navigated to within a Frame.
9+
/// </summary>
10+
[Uno.UI.Samples.Controls.Sample("WebView",
11+
IsManualTest = true,
12+
IgnoreInSnapshotTests = true,
13+
Description = "Tests that mouse events are properly propagated to the WebView2 control and that events are also handled by the app itself.")]
14+
public sealed partial class WebView2_MouseEvents : Page
15+
{
16+
private int counter;
17+
public WebView2_MouseEvents()
18+
{
19+
this.InitializeComponent();
20+
this.Loaded += WebView2_MouseEvents_Loaded;
21+
}
22+
23+
private void OnButton_Clicked(object o, object s)
24+
{
25+
counter++;
26+
LocalButton.Content = $"{counter}";
27+
}
28+
29+
private async void WebView2_MouseEvents_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
30+
{
31+
await Inline.EnsureCoreWebView2Async();
32+
Inline.CoreWebView2?.SetVirtualHostNameToFolderMapping(
33+
"UnoNativeAssets",
34+
"WebContent",
35+
CoreWebView2HostResourceAccessKind.Allow);
36+
Inline.CoreWebView2?.Navigate("http://UnoNativeAssets/index.html");
37+
}
38+
}

src/SamplesApp/UITests.Shared/UITests.Shared.projitems

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4428,6 +4428,10 @@
44284428
<SubType>Designer</SubType>
44294429
<Generator>MSBuild:Compile</Generator>
44304430
</Page>
4431+
<Page Include="$(MSBuildThisFileDirectory)Microsoft_UI_Xaml_Controls\WebView2Tests\WebView2_MouseEvents.xaml">
4432+
<SubType>Designer</SubType>
4433+
<Generator>MSBuild:Compile</Generator>
4434+
</Page>
44314435
<Page Include="$(MSBuildThisFileDirectory)Microsoft_UI_Xaml_Controls\WebView2Tests\WebView2_NavigationProperties.xaml">
44324436
<SubType>Designer</SubType>
44334437
<Generator>MSBuild:Compile</Generator>
@@ -9868,6 +9872,9 @@
98689872
<Compile Include="$(MSBuildThisFileDirectory)Microsoft_UI_Xaml_Controls\WebView2Tests\WebView2_Folder.xaml.cs">
98699873
<DependentUpon>WebView2_Folder.xaml</DependentUpon>
98709874
</Compile>
9875+
<Compile Include="$(MSBuildThisFileDirectory)Microsoft_UI_Xaml_Controls\WebView2Tests\WebView2_MouseEvents.xaml.cs">
9876+
<DependentUpon>WebView2_MouseEvents.xaml</DependentUpon>
9877+
</Compile>
98719878
<Compile Include="$(MSBuildThisFileDirectory)Microsoft_UI_Xaml_Controls\WebView2Tests\WebView2_NavigationProperties.xaml.cs">
98729879
<DependentUpon>WebView2_NavigationProperties.xaml</DependentUpon>
98739880
</Compile>

src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/MouseButtons.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ NS_ASSUME_NONNULL_BEGIN
1717
/// Current pressed-buttons bitmask (0=none). Built from tracked events; falls back to AppKit/Quartz if needed.
1818
+ (NSInteger)mask;
1919

20+
/// Updates tracking state with the given event, then returns the current button mask.
21+
+ (NSInteger)buttonMask:(NSEvent *)e;
22+
2023
@end
2124

2225
NS_ASSUME_NONNULL_END

src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/MouseButtons.m

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,12 @@ + (NSInteger)mask
5353
}
5454
}
5555

56-
// 2) Fall back to AppKit snapshot
57-
NSInteger appKitMask = [NSEvent pressedMouseButtons];
58-
5956
if (m != 0) {
60-
// If we think buttons are down, but AppKit says NONE are down, we likely missed a MouseUp.
61-
// We trust AppKit and reset our counters.
62-
if (appKitMask == 0) {
63-
for (int i = 0; i < 8; i++) {
64-
sDepth[i] = 0;
65-
}
66-
return 0;
67-
}
6857
return m;
6958
}
7059

60+
// 2) AppKit fallback (may be inaccurate; doesn't track taps)
61+
NSInteger appKitMask = [NSEvent pressedMouseButtons];
7162
if (appKitMask != 0) return appKitMask;
7263

7364
// 3) Quartz fallback (polls physical state; taps may already be up)
@@ -79,4 +70,15 @@ + (NSInteger)mask
7970
return m;
8071
}
8172

73+
+ (NSInteger)buttonMask:(NSEvent *)e
74+
{
75+
// reset the mask in cases where we believe we missed a MouseUp
76+
for (int i = 0; i < 8; i++) {
77+
sDepth[i] = 0;
78+
}
79+
80+
[self track:e];
81+
return [self mask];
82+
}
83+
8284
@end

src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/UNOWindow.m

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,9 +972,32 @@ - (void)sendEvent:(NSEvent *)event {
972972
data.pointerDeviceType = pdt;
973973

974974
// mouse
975-
// FIXME: NSEvent.pressedMouseButtons is returning a wrong value in Sequoia when using an extenal trackpad
975+
// FIXME: NSEvent.pressedMouseButtons is returning a wrong value in Sequoia when using an external trackpad
976976
if (_osVersion.majorVersion >= 15) {
977-
data.mouseButtons = (uint32)[MouseButtons mask];
977+
NSInteger mask = [MouseButtons mask];
978+
979+
// If we think buttons are down, but AppKit says NONE are down, we likely missed a MouseUp.
980+
// We trust AppKit and reset our counters, but only when handling a mouse button event.
981+
if (mask != 0 && ((uint32)NSEvent.pressedMouseButtons) == 0) {
982+
NSEventType type = event.type;
983+
BOOL isMouseButtonEvent =
984+
type == NSEventTypeLeftMouseDown ||
985+
type == NSEventTypeLeftMouseUp ||
986+
type == NSEventTypeRightMouseDown ||
987+
type == NSEventTypeRightMouseUp ||
988+
type == NSEventTypeOtherMouseDown ||
989+
type == NSEventTypeOtherMouseUp ||
990+
type == NSEventTypeLeftMouseDragged ||
991+
type == NSEventTypeRightMouseDragged||
992+
type == NSEventTypeOtherMouseDragged;
993+
994+
if (isMouseButtonEvent) {
995+
mask = [MouseButtons buttonMask:event];
996+
}
997+
}
998+
999+
data.mouseButtons = (uint32)mask;
1000+
9781001
} else {
9791002
data.mouseButtons = (uint32)NSEvent.pressedMouseButtons;
9801003
}

0 commit comments

Comments
 (0)