From a1498f00240cf6710b13993ac362621c405cb074 Mon Sep 17 00:00:00 2001 From: Gaspar Capello Date: Thu, 21 Nov 2024 11:54:52 -0300 Subject: [PATCH] [osx] Fix floating window disappears when another app gains focus (fix aseprite/aseprite#4265) --- os/osx/app_delegate.h | 3 +++ os/osx/app_delegate.mm | 12 ++++++++++++ os/osx/window.mm | 13 ++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/os/osx/app_delegate.h b/os/osx/app_delegate.h index 0bbaf04a9..3910ffeac 100644 --- a/os/osx/app_delegate.h +++ b/os/osx/app_delegate.h @@ -23,7 +23,9 @@ // generate a DropFiles event. std::set m_cliFiles; bool m_isHidden; + NSMutableArray* m_floatingWindows; } + - (id)init; - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender; - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)app; @@ -39,6 +41,7 @@ - (void)markCliFileAsProcessed:(const std::string&)fn; - (void)resetCliFiles; - (BOOL)isHidden; +- (NSMutableArray*) floatingWindows; @end #endif diff --git a/os/osx/app_delegate.mm b/os/osx/app_delegate.mm index 61ac23601..13f19229f 100644 --- a/os/osx/app_delegate.mm +++ b/os/osx/app_delegate.mm @@ -69,6 +69,7 @@ - (id)init { if (self = [super init]) { m_isHidden = false; + m_floatingWindows = [NSMutableArray array]; } return self; } @@ -92,6 +93,9 @@ - (void)applicationWillTerminate:(NSNotification*)notification - (void)applicationWillResignActive:(NSNotification*)notification { + for (NSWindow* floatingWindow in self.floatingWindows) + floatingWindow.level = NSNormalWindowLevel; + NSEvent* event = [NSApp currentEvent]; if (event != nil) [ViewOSX updateKeyFlags:event]; @@ -102,6 +106,9 @@ - (void)applicationDidBecomeActive:(NSNotification*)notification NSEvent* event = [NSApp currentEvent]; if (event != nil) [ViewOSX updateKeyFlags:event]; + + for (NSWindow* floatingWindow in self.floatingWindows) + floatingWindow.level = NSFloatingWindowLevel; } - (void)applicationDidHide:(NSNotification*)notification @@ -170,4 +177,9 @@ - (BOOL)isHidden return m_isHidden; } +- (NSMutableArray*) floatingWindows +{ + return m_floatingWindows; +} + @end diff --git a/os/osx/window.mm b/os/osx/window.mm index 38148f919..bda8849ce 100644 --- a/os/osx/window.mm +++ b/os/osx/window.mm @@ -12,6 +12,7 @@ #include "os/osx/window.h" #include "base/debug.h" +#include "os/osx/app_delegate.h" #include "os/event.h" #include "os/osx/app.h" #include "os/osx/event_queue.h" @@ -120,7 +121,13 @@ - (WindowOSXObjc*)initWithImpl:(os::WindowOSX*)impl if (spec->floating()) { self.level = NSFloatingWindowLevel; - self.hidesOnDeactivate = true; + self.hidesOnDeactivate = false; + // Needed to keep track all the floating windows pointers + // to handle its 'floatingWindow.level' when the application + // turns active/inactive inside 'app_delegate.mm'. + AppDelegateOSX* delegate = (AppDelegateOSX*)[NSApp delegate]; + if (delegate) + [delegate.floatingWindows addObject:self]; } // Hide the "View > Show Tab Bar" menu item @@ -137,6 +144,10 @@ - (WindowOSXObjc*)initWithImpl:(os::WindowOSX*)impl - (void)removeImpl { + AppDelegateOSX* delegate = (AppDelegateOSX*)[NSApp delegate]; + if (delegate) + [delegate.floatingWindows removeObject:self]; + [m_view removeImpl]; [self setDelegate:nil];