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 09:52:29 +0000 (11:52 +0200)
Signed-off-by: Claudio Cambra <claudio.cambra@gmail.com>
src/gui/systray.cpp
src/gui/systray.h
src/gui/systray.mm

index 05ced5e36bd78a84425c95e38a45537dfa71443b..6f6189dd17c65f89bee2dc8d1de66d0c5ee5ad17 100644 (file)
@@ -513,9 +513,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