From d8f8e61a11d9dc21f6536209a8b42a5e7f674ae2 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 17 Aug 2025 23:35:48 +0200 Subject: [PATCH] fix: Use modern notification API on macOS --- lib/libimhex/CMakeLists.txt | 3 +- lib/libimhex/source/helpers/utils_macos.m | 41 ++++++++++++++++++++--- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/libimhex/CMakeLists.txt b/lib/libimhex/CMakeLists.txt index c01f95df4..c5a252879 100644 --- a/lib/libimhex/CMakeLists.txt +++ b/lib/libimhex/CMakeLists.txt @@ -155,7 +155,8 @@ if (NOT IMHEX_EXTERNAL_PLUGIN_BUILD) target_compile_definitions(libimhex PRIVATE EXPORT_SYMBOLS=1) elseif (APPLE) find_library(FOUNDATION NAMES Foundation) - target_link_libraries(libimhex PUBLIC ${FOUNDATION}) + find_library(USERNOTIFICATIONS NAMES UserNotifications) + target_link_libraries(libimhex PUBLIC ${FOUNDATION} ${USERNOTIFICATIONS}) endif () target_link_libraries(libimhex PRIVATE libpl microtar ${NFD_LIBRARIES} magic) diff --git a/lib/libimhex/source/helpers/utils_macos.m b/lib/libimhex/source/helpers/utils_macos.m index db94955e9..ef97bb391 100644 --- a/lib/libimhex/source/helpers/utils_macos.m +++ b/lib/libimhex/source/helpers/utils_macos.m @@ -18,6 +18,7 @@ #import #import #import + #import #include @@ -363,18 +364,48 @@ } } + + static bool isRunningInAppBundle(void) { + NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; + return [bundlePath.pathExtension.lowercaseString isEqualToString:@"app"]; + } + void toastMessageMacos(const char *title, const char *message) { @autoreleasepool { + // Only show notification if we're inside a bundle + if (!isRunningInAppBundle()) { + return; + } + NSString *nsTitle = [NSString stringWithUTF8String:title]; NSString *nsMessage = [NSString stringWithUTF8String:message]; - NSUserNotification *notification = [[NSUserNotification alloc] init]; - notification.title = nsTitle; - notification.informativeText = nsMessage; - notification.soundName = NSUserNotificationDefaultSoundName; + UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; - [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; + // Request permission if needed + [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound) + completionHandler:^(BOOL granted, NSError * _Nullable error) { + + (void)error; + if (!granted) return; + + UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; + content.title = nsTitle; + content.body = nsMessage; + content.sound = [UNNotificationSound defaultSound]; + + // Show notification immediately + UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.1 repeats:NO]; + + NSString *identifier = [[NSUUID UUID] UUIDString]; + UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier + content:content + trigger:trigger]; + + [center addNotificationRequest:request withCompletionHandler:nil]; + }]; } } + #endif