Fix menu bar height calculation on macOS
authorClaudio Cambra <claudio.cambra@gmail.com>
Fri, 9 Sep 2022 12:23:39 +0000 (14:23 +0200)
committerMatthieu Gallien <matthieu_gallien@yahoo.fr>
Sat, 10 Sep 2022 10:07:26 +0000 (12:07 +0200)
Signed-off-by: Claudio Cambra <claudio.cambra@gmail.com>
src/gui/systray.cpp
src/gui/systray.h
src/gui/systray.mm

index 51ba0e267f2f170218ee0d277cf1d986969fb00f..e1dbee17959bd66e7fc3ed13c2b4b0d6c09a6064 100644 (file)
@@ -540,9 +540,8 @@ QRect Systray::taskbarGeometry() const
     }
     return tbRect;
 #elif defined(Q_OS_MACOS)
-    // Finder bar is always 22px height on macOS (when treating as effective pixels)
     const auto screenWidth = currentScreenRect().width();
-    const auto statusBarHeight = static_cast<int>(OCC::statusBarThickness());
+    const auto statusBarHeight = static_cast<int>(OCC::menuBarThickness());
     return {0, 0, screenWidth, statusBarHeight};
 #else
     if (taskbarOrientation() == TaskBarPosition::Bottom || taskbarOrientation() == TaskBarPosition::Top) {
index 522ae17309ed95f0a1c405372745f09597ad2eaa..945f5999be491e01361693dd19806f640b104180 100644 (file)
@@ -52,7 +52,7 @@ bool canOsXSendUserNotification();
 void sendOsXUserNotification(const QString &title, const QString &message);
 void sendOsXUpdateNotification(const QString &title, const QString &message, const QUrl &webUrl);
 void setTrayWindowLevelAndVisibleOnAllSpaces(QWindow *window);
-double statusBarThickness();
+double menuBarThickness();
 #endif
 
 /**
index 8798e125e2672dd1e1ae3f1ee272a2865251d2d8..ddcd4f362c0450af5e5fd2ac223e1ccf685212ec 100644 (file)
@@ -52,9 +52,18 @@ enum MacNotificationAuthorizationOptions {
     Provisional
 };
 
-double statusBarThickness()
+double menuBarThickness()
 {
-    return [NSStatusBar systemStatusBar].thickness;
+    const NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
+
+    if (mainMenu == nil) {
+        // Return this educated guess if something goes wrong.
+        // As of macOS 12.4 this will always return 22, even on notched Macbooks.
+        qCWarning(lcMacSystray) << "Got nil for main menu. Going with reasonable menu bar height guess.";
+        return [[NSStatusBar systemStatusBar] thickness];
+    }
+
+    return mainMenu.menuBarHeight;
 }
 
 // TODO: Get this to actually check for permissions