From 5bae5b236aa76989fcb109231b71e4b7b91135d0 Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Wed, 30 Oct 2024 15:26:48 -0400 Subject: [PATCH] fix(macos): Bring window to top when activated (cherry picked from commit 6f1da0e34e858a9637dc8a1d6b454a9b4f760a18) --- src/Uno.UI.Runtime.Skia.MacOS/MacOSWindowWrapper.cs | 12 ++++++++++++ src/Uno.UI.Runtime.Skia.MacOS/NativeUno.cs | 3 +++ .../UnoNativeMac/UnoNativeMac/UNOWindow.h | 1 + .../UnoNativeMac/UnoNativeMac/UNOWindow.m | 8 ++++++++ 4 files changed, 24 insertions(+) diff --git a/src/Uno.UI.Runtime.Skia.MacOS/MacOSWindowWrapper.cs b/src/Uno.UI.Runtime.Skia.MacOS/MacOSWindowWrapper.cs index 462d6d56a164..59e3eb33f4a3 100644 --- a/src/Uno.UI.Runtime.Skia.MacOS/MacOSWindowWrapper.cs +++ b/src/Uno.UI.Runtime.Skia.MacOS/MacOSWindowWrapper.cs @@ -37,6 +37,18 @@ public override string Title set => NativeUno.uno_window_set_title(_window.Handle, value); } + public override void Activate() + { + NativeUno.uno_window_activate(_window.Handle); + } + + protected override void ShowCore() + { + // the first call to `Window.Activate` does not reach the above `Activate` method + // https://github.com/unoplatform/uno/blob/fc8e58d77f8cf31d651135c22ea3105099c26fb7/src/Uno.UI/UI/Xaml/Window/Implementations/BaseWindowImplementation.cs#L81-L98 + NativeUno.uno_window_activate(_window.Handle); + } + private void OnHostSizeChanged(Size size) { Bounds = new Rect(default, size); diff --git a/src/Uno.UI.Runtime.Skia.MacOS/NativeUno.cs b/src/Uno.UI.Runtime.Skia.MacOS/NativeUno.cs index a3a4835f4308..27544b404875 100644 --- a/src/Uno.UI.Runtime.Skia.MacOS/NativeUno.cs +++ b/src/Uno.UI.Runtime.Skia.MacOS/NativeUno.cs @@ -201,6 +201,9 @@ internal static unsafe partial void uno_set_window_events_callbacks( [LibraryImport("libUnoNativeMac.dylib")] internal static partial nint uno_window_create(double width, double height); + [LibraryImport("libUnoNativeMac.dylib")] + internal static partial void uno_window_activate(nint window); + [LibraryImport("libUnoNativeMac.dylib")] internal static partial void uno_window_invalidate(nint window); diff --git a/src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/UNOWindow.h b/src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/UNOWindow.h index acbbbb759f1c..6db2db98c5a4 100644 --- a/src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/UNOWindow.h +++ b/src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/UNOWindow.h @@ -40,6 +40,7 @@ void uno_set_resize_callback(resize_fn_ptr p); NSWindow* uno_app_get_main_window(void); NSWindow* uno_window_create(double width, double height); +void uno_window_activate(NSWindow *window); void uno_window_invalidate(NSWindow *window); bool uno_window_resize(NSWindow *window, double width, double height); diff --git a/src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/UNOWindow.m b/src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/UNOWindow.m index a29aa0ff8f36..77454c72a282 100644 --- a/src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/UNOWindow.m +++ b/src/Uno.UI.Runtime.Skia.MacOS/UnoNativeMac/UnoNativeMac/UNOWindow.m @@ -126,6 +126,14 @@ - (void) applicationDidChangeScreenParametersNotification:(NSNotification*) note return window; } +void uno_window_activate(NSWindow *window) +{ +#if DEBUG + NSLog(@"uno_window_activate %@", window); +#endif + [window orderFront:nil]; +} + void uno_window_notify_screen_change(NSWindow *window) { assert(windowDidChangeScreen);