fix: Use modern notification API on macOS

This commit is contained in:
WerWolv
2025-08-17 23:35:48 +02:00
parent 3c9aa97f6c
commit d8f8e61a11
2 changed files with 38 additions and 6 deletions

View File

@@ -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)

View File

@@ -18,6 +18,7 @@
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <AppleScriptObjC/AppleScriptObjC.h>
#import <UserNotifications/UserNotifications.h>
#include <hex/helpers/keys.hpp>
@@ -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