Release implicit mouse capture on focus lost (#20574)

* macOS: Release implicit mouse capture on focus lost

* Added native modal test to EmbeddingPage
This commit is contained in:
Julien Lebosquain
2026-02-04 10:50:15 +00:00
committed by GitHub
parent 6d45ee8cd7
commit 934ca7c9f5
5 changed files with 91 additions and 2 deletions
@@ -16,5 +16,7 @@
</Popup>
</StackPanel>
<Button Name="Reset" Click="Reset_Click">Reset</Button>
<Button Name="RunNativeModalSession" Click="RunNativeModalSession_OnClick">Open Native Modal</Button>
<TextBox Name="ModalResultTextBox" />
</StackPanel>
</UserControl>
@@ -1,10 +1,19 @@
using System;
using Avalonia.Automation;
using Avalonia.Controls;
using Avalonia.Controls.Embedding;
using Avalonia.Interactivity;
using IntegrationTestApp.Embedding;
using MonoMac.AppKit;
using MonoMac.CoreGraphics;
using MonoMac.ObjCRuntime;
namespace IntegrationTestApp;
public partial class EmbeddingPage : UserControl
{
private const long NSModalResponseContinue = -1002;
public EmbeddingPage()
{
InitializeComponent();
@@ -19,5 +28,65 @@ public partial class EmbeddingPage : UserControl
private void Reset_Click(object? sender, RoutedEventArgs e)
{
ResetText();
ModalResultTextBox.Text = "";
}
private void RunNativeModalSession_OnClick(object? sender, RoutedEventArgs e)
{
MacHelper.EnsureInitialized();
var app = NSApplication.SharedApplication;
var modalWindow = CreateNativeWindow();
var session = app.BeginModalSession(modalWindow);
while (true)
{
if (app.RunModalSession(session) != NSModalResponseContinue)
break;
}
app.EndModalSession(session);
}
private NSWindow CreateNativeWindow()
{
var button = new Button
{
Name = "ButtonInModal",
Content = "Button"
};
AutomationProperties.SetAutomationId(button, "ButtonInModal");
var root = new EmbeddableControlRoot
{
Width = 200,
Height = 200,
Content = button
};
root.Prepare();
var window = new NSWindow(
new CGRect(0, 0, root.Width, root.Height),
NSWindowStyle.Titled | NSWindowStyle.Closable,
NSBackingStore.Buffered,
false);
window.Identifier = "ModalNativeWindow";
window.WillClose += (_, _) => NSApplication.SharedApplication.StopModal();
button.Click += (_, _) =>
{
ModalResultTextBox.Text = "Clicked";
window.Close();
};
if (root.TryGetPlatformHandle() is not { } handle)
throw new InvalidOperationException("Could not get platform handle");
window.ContentView = (NSView)Runtime.GetNSObject(handle.Handle)!;
root.StartRendering();
return window;
}
}