@@ -4,40 +4,18 @@ package main
44
55/*
66#cgo CFLAGS: -x objective-c
7- #cgo LDFLAGS: -lobjc -framework WebKit -framework AppKit -framework UserNotifications
7+ #cgo LDFLAGS: -lobjc -framework WebKit
88
99#import <objc/runtime.h>
1010#import <WebKit/WebKit.h>
11- #import <AppKit/AppKit.h>
12- #import <UserNotifications/UserNotifications.h>
1311
1412// CompletionHandlerDelegate implements methods required for executing
1513// completion and decision handlers.
1614@interface CompletionHandlerDelegate:NSObject
17- - (void)deliverNotificationWithTitle:(NSString *)title message:(NSString *)message icon:(NSImage *)icon;
1815- (WKWebView *)newWebView;
1916@end
2017
2118@implementation CompletionHandlerDelegate
22- // Implements "deliverNotificationWithTitle:message:icon:" handler with the NSUserNotification
23- // and NSUserNotificationCenter classes which have been marked deprecated
24- // but it is what works atm for macOS apps. The newer UNUserNotificationCenter has some
25- // implementation issues and very little information to aid debugging.
26- // See: https://developer.apple.com/documentation/foundation/nsusernotification?language=objc and
27- // https://github.com/progrium/macdriver/discussions/258
28- - (void)deliverNotificationWithTitle:(NSString *)title message:(NSString *)message icon:(NSImage *)icon{
29- NSUserNotification *notification = [NSUserNotification new];
30- notification.title = title;
31- notification.informativeText = message;
32- notification.actionButtonTitle = @"Ok";
33- notification.hasActionButton = 1;
34- notification.soundName = NSUserNotificationDefaultSoundName;
35- [notification setValue:icon forKey:@"_identityImage"];
36- [notification setValue:@(false) forKey:@"_identityImageHasBorder"];
37- [notification setValue:@YES forKey:@"_ignoresDoNotDisturb"];
38- [[NSUserNotificationCenter defaultUserNotificationCenter]deliverNotification:notification];
39- }
40-
4119// Implements "newWebview". This is because the inspectable property is not available via
4220// darwinkit at the time of writing. Only MacOs 13.3+ has this property.
4321// This means that the webview is not inspectable on older versions of macOS.
@@ -607,7 +585,6 @@ func setAppMainMenuBar(app appkit.Application) {
607585 windowMenu .AddItem (appkit .NewMenuItemWithSelector ("Bring All to Front" , "" , objc .Sel ("arrangeInFront:" )))
608586 windowMenu .AddItem (appkit .MenuItem_SeparatorItem ())
609587 windowMenu .AddItem (appkit .NewMenuItemWithSelector ("Enter Full Screen" , "f" , objc .Sel ("toggleFullScreen:" )))
610- windowMenu .Delegate ()
611588
612589 // Create the "Edit" menu.
613590 editMenu := appkit .NewMenuWithTitle ("Edit" )
@@ -636,8 +613,8 @@ func setAppMainMenuBar(app appkit.Application) {
636613 // Also, MacOS will automatically add other default Window menu
637614 // items. See:
638615 // https://developer.apple.com/documentation/appkit/nsapplication/1428547-windowsmenu?language=objc.
639- // TODO: Since the new update, this is not working as expected. The
640- // windows are not beingg grouped.
616+ // TODO: Since the new update, this is not working as expected, the
617+ // windows are not grouped as expected .
641618 app .SetWindowsMenu (m )
642619 }
643620 }
@@ -718,7 +695,7 @@ func parseJSCallbackArgsString(msg objc.Object) []string {
718695 var argsAsStr []string
719696 for i := 0 ; i < int (count ); i ++ {
720697 ob := args .ObjectAtIndex (uint (i ))
721- if ob .IsNil () {
698+ if ob .IsNil () || ob . Description () == "<null>" {
722699 continue // ignore
723700 }
724701 argsAsStr = append (argsAsStr , ob .Description ())
@@ -774,5 +751,14 @@ func (sc *shutdownCloser) Done() {
774751}
775752
776753func sendDesktopNotification (title , msg string ) {
777- objc .Call [objc.Void ](completionHandler , objc .Sel ("deliverNotificationWithTitle:message:icon:" ), title , msg , bisonwAppIcon )
754+ // This API is deprecated but still functional.
755+ notif := objc .Call [objc.Object ](objc .GetClass ("NSUserNotification" ), objc .Sel ("new" ))
756+ objc .Retain (& notif )
757+ objc .Call [objc.Void ](notif , objc .Sel ("setTitle:" ), title )
758+ objc .Call [objc.Void ](notif , objc .Sel ("setInformativeText:" ), msg )
759+
760+ center := objc .Call [objc.Object ](objc .GetClass ("NSUserNotificationCenter" ), objc .Sel ("defaultUserNotificationCenter" ))
761+ if center .Ptr () != nil {
762+ objc .Call [objc.Void ](center , objc .Sel ("deliverNotification:" ), notif )
763+ }
778764}
0 commit comments