Skip to content

Commit

Permalink
Fix floating window disappears when another app gains focus
Browse files Browse the repository at this point in the history
This is the solution for issue aseprite/aseprite#4265
  • Loading branch information
Gasparoken committed Nov 22, 2024
1 parent a1c113a commit d9c0665
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
6 changes: 5 additions & 1 deletion os/osx/app_delegate.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF OS Library
// Copyright (C) 2020-2021 Igara Studio S.A.
// Copyright (C) 2020-2024 Igara Studio S.A.
// Copyright (C) 2012-2017 David Capello
//
// This file is released under the terms of the MIT license.
Expand All @@ -23,6 +23,10 @@
// generate a DropFiles event.
std::set<std::string> m_cliFiles;
}

@property (strong) NSMutableArray<NSWindow*>* floatingWindows;

- (id)init;
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender;
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)app;
- (void)applicationWillTerminate:(NSNotification*)notification;
Expand Down
16 changes: 15 additions & 1 deletion os/osx/app_delegate.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF OS Library
// Copyright (C) 2020-2022 Igara Studio S.A.
// Copyright (C) 2020-2024 Igara Studio S.A.
// Copyright (C) 2012-2017 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -65,6 +65,14 @@ - (void)validateLafMenuItem;

@implementation AppDelegateOSX

- (id)init
{
self = [super init];
if (self)
_floatingWindows = [NSMutableArray array];
return self;
}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender
{
os::Event ev;
Expand All @@ -84,6 +92,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 @@ -94,6 +105,9 @@ - (void)applicationDidBecomeActive:(NSNotification*)notification
NSEvent* event = [NSApp currentEvent];
if (event != nil)
[ViewOSX updateKeyFlags:event];

for (NSWindow* floatingWindow in self.floatingWindows)
floatingWindow.level = NSFloatingWindowLevel;
}

- (BOOL)application:(NSApplication*)app openFiles:(NSArray*)filenames
Expand Down
15 changes: 13 additions & 2 deletions os/osx/window.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF OS Library
// Copyright (C) 2019-2021 Igara Studio S.A.
// Copyright (C) 2019-2024 Igara Studio S.A.
// Copyright (C) 2012-2017 David Capello
//
// This file is released under the terms of the MIT license.
Expand All @@ -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/event_queue.h"
#include "os/osx/view.h"
Expand Down Expand Up @@ -119,7 +120,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
Expand All @@ -136,6 +143,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 d9c0665

Please sign in to comment.