Merge pull request #2677 from briankendall/fix-tray-window-spaces-bug

Fix macOS bug where tray window causes spaces to switch
This commit is contained in:
Kevin Ottens 2020-12-02 08:23:19 +01:00 committed by GitHub
commit 7721832ee0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -267,6 +267,14 @@ void Systray::forceWindowInit(QQuickWindow *window) const
// this shouldn't flicker
window->show();
window->hide();
#ifdef Q_OS_MAC
// On macOS we need to designate the tray window as visible on all spaces and
// at the menu bar level, otherwise showing it can cause the current spaces to
// change, or the window could be obscured by another window that shouldn't
// normally cover a menu.
OCC::setTrayWindowLevelAndVisibleOnAllSpaces(window);
#endif
}
QScreen *Systray::currentScreen() const

View File

@ -23,12 +23,14 @@
class QScreen;
class QQmlApplicationEngine;
class QQuickWindow;
class QWindow;
namespace OCC {
#ifdef Q_OS_OSX
bool canOsXSendUserNotification();
void sendOsXUserNotification(const QString &title, const QString &message);
void setTrayWindowLevelAndVisibleOnAllSpaces(QWindow *window);
#endif
/**

View File

@ -1,4 +1,5 @@
#include <QString>
#include <QWindow>
#import <Cocoa/Cocoa.h>
@interface NotificationCenterDelegate : NSObject
@ -41,4 +42,13 @@ void sendOsXUserNotification(const QString &title, const QString &message)
[notification release];
}
void setTrayWindowLevelAndVisibleOnAllSpaces(QWindow *window)
{
NSView *nativeView = (NSView *)window->winId();
NSWindow *nativeWindow = (NSWindow *)[nativeView window];
[nativeWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces | NSWindowCollectionBehaviorIgnoresCycle |
NSWindowCollectionBehaviorTransient];
[nativeWindow setLevel:NSMainMenuWindowLevel];
}
}