Create systray_mac_common file for common systray functions
authorClaudio Cambra <claudio.cambra@nextcloud.com>
Mon, 13 Nov 2023 01:50:27 +0000 (09:50 +0800)
committerCamila Ayres <hello@camilasan.com>
Mon, 11 Mar 2024 14:20:51 +0000 (15:20 +0100)
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
src/gui/CMakeLists.txt
src/gui/systray.mm
src/gui/systray_mac_common.mm [new file with mode: 0644]

index a921ea1a6ea844f6d4917923e9377c70464f219e..3e9c8e296c3bc23e0228ac9825f92021ef9abe6c 100644 (file)
@@ -283,7 +283,7 @@ endif()
 
 IF( APPLE )
     list(APPEND client_SRCS cocoainitializer_mac.mm)
-    list(APPEND client_SRCS systray.mm)
+    list(APPEND client_SRCS systray.mm systray_mac_common.mm)
 
     if (BUILD_FILE_PROVIDER_MODULE)
         list(APPEND client_SRCS
index 707e3269fd429b388b0a23188a7ce655c7880e5d..2cfde7405810e5dded1d9d5f22880c19ed16f18f 100644 (file)
@@ -1,13 +1,13 @@
-#include "QtCore/qurl.h"
+#include <QLoggingCategory>
+#include <QString>
+#include <QUrl>
+
 #include "account.h"
 #include "accountstate.h"
 #include "accountmanager.h"
 #include "config.h"
 #include "systray.h"
 #include "tray/talkreply.h"
-#include <QString>
-#include <QWindow>
-#include <QLoggingCategory>
 
 #import <Cocoa/Cocoa.h>
 #import <UserNotifications/UserNotifications.h>
@@ -76,7 +76,7 @@ void sendTalkReply(UNNotificationResponse *response, UNNotificationContent* cont
     willPresentNotification:(UNNotification *)notification
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
 {
-#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 110000
+#if __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_11_0
         completionHandler(UNNotificationPresentationOptionSound + UNNotificationPresentationOptionBanner);
 #else
         completionHandler(UNNotificationPresentationOptionSound + UNNotificationPresentationOptionAlert);
@@ -111,20 +111,6 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response
 
 namespace OCC {
 
-double menuBarThickness()
-{
-    NSMenu * const 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
 bool canOsXSendUserNotification()
 {
@@ -266,19 +252,4 @@ void sendOsXTalkNotification(const QString &title, const QString &message, const
     [center addNotificationRequest:request withCompletionHandler:nil];
 }
 
-void setTrayWindowLevelAndVisibleOnAllSpaces(QWindow *window)
-{
-    NSView * const nativeView = (NSView *)window->winId();
-    NSWindow * const nativeWindow = (NSWindow *)(nativeView.window);
-    [nativeWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces | NSWindowCollectionBehaviorIgnoresCycle |
-                  NSWindowCollectionBehaviorTransient];
-    [nativeWindow setLevel:NSMainMenuWindowLevel];
-}
-
-bool osXInDarkMode()
-{
-    NSString * const osxMode = [NSUserDefaults.standardUserDefaults stringForKey:@"AppleInterfaceStyle"];
-    return [osxMode containsString:@"Dark"];
-}
-
 } // OCC namespace
diff --git a/src/gui/systray_mac_common.mm b/src/gui/systray_mac_common.mm
new file mode 100644 (file)
index 0000000..8189acd
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2023 by Claudio Cambra <claudio.cambra@nextcloud.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#include <QWindow>
+
+#import <Cocoa/Cocoa.h>
+
+#include "systray.h"
+
+Q_LOGGING_CATEGORY(lcMacSystrayCommon, "nextcloud.gui.macsystraycommon")
+
+namespace OCC {
+
+double menuBarThickness()
+{
+    NSMenu * const 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(lcMacSystrayCommon) << "Got nil for main menu. "
+                                      << "Going with reasonable menu bar height guess.";
+        return NSStatusBar.systemStatusBar.thickness;
+    }
+
+    return mainMenu.menuBarHeight;
+}
+
+void setTrayWindowLevelAndVisibleOnAllSpaces(QWindow *const window)
+{
+    NSView * const nativeView = (NSView *)window->winId();
+    NSWindow * const nativeWindow = (NSWindow *)(nativeView.window);
+    [nativeWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces | NSWindowCollectionBehaviorIgnoresCycle |
+                  NSWindowCollectionBehaviorTransient];
+    [nativeWindow setLevel:NSMainMenuWindowLevel];
+}
+
+bool osXInDarkMode()
+{
+    NSString * const osxMode = [NSUserDefaults.standardUserDefaults stringForKey:@"AppleInterfaceStyle"];
+    return [osxMode containsString:@"Dark"];
+}
+
+}