Skip to content

Commit

Permalink
[osx] Fix floating window disappears when another app gains focus (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Gasparoken committed Dec 13, 2024
1 parent 3a16b2d commit 3b0c950
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
10 changes: 7 additions & 3 deletions os/osx/app_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
#include <string>

@interface NSApplicationOSX : NSApplication
- (void)sendEvent:(NSEvent *)event;
- (void)sendEvent:(NSEvent*)event;
@end

@interface AppDelegateOSX : NSObject<NSApplicationDelegate> {
@interface AppDelegateOSX : NSObject <NSApplicationDelegate> {
// Files that were already processed in the CLI, so we don't need to
// generate a DropFiles event.
std::set<std::string> m_cliFiles;
bool m_isHidden;
NSMutableArray<NSWindow*>* m_floatingWindows;
}

- (id)init;
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender;
- (NSApplicationTerminateReply)applicationShouldTerminate:
(NSApplication*)sender;
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)app;
- (void)applicationWillTerminate:(NSNotification*)notification;
- (void)applicationWillResignActive:(NSNotification*)notification;
Expand All @@ -39,6 +42,7 @@
- (void)markCliFileAsProcessed:(const std::string&)fn;
- (void)resetCliFiles;
- (BOOL)isHidden;
- (NSMutableArray<NSWindow*>*)floatingWindows;
@end

#endif
12 changes: 12 additions & 0 deletions os/osx/app_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ - (id)init
{
if (self = [super init]) {
m_isHidden = false;
m_floatingWindows = [NSMutableArray array];
}
return self;
}
Expand All @@ -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];
Expand All @@ -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
Expand Down Expand Up @@ -170,4 +177,9 @@ - (BOOL)isHidden
return m_isHidden;
}

- (NSMutableArray<NSWindow*>*) floatingWindows
{
return m_floatingWindows;
}

@end
13 changes: 12 additions & 1 deletion os/osx/window.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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];
}

if (spec->modal())
Expand All @@ -140,6 +147,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];
Expand Down

0 comments on commit 3b0c950

Please sign in to comment.