diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e49270e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.apk +example/example +example/example.app/Contents/MacOS/example diff --git a/example/example.app/Contents/Info.plist b/example/example.app/Contents/Info.plist new file mode 100644 index 0000000..82bbbd9 --- /dev/null +++ b/example/example.app/Contents/Info.plist @@ -0,0 +1,26 @@ + + + + + CFBundleGetInfoString + Example + CFBundleExecutable + example + CFBundleIdentifier + org.gioui + CFBundleName + example + CFBundleIconFile + example.icns + CFBundleShortVersionString + 0.01 + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + APPL + IFMajorVersion + 0 + IFMinorVersion + 1 + + diff --git a/example/example.app/Contents/Resources/example.icns b/example/example.app/Contents/Resources/example.icns new file mode 100644 index 0000000..9d669ed Binary files /dev/null and b/example/example.app/Contents/Resources/example.icns differ diff --git a/example/example.app/Contents/_CodeSignature/CodeResources b/example/example.app/Contents/_CodeSignature/CodeResources new file mode 100644 index 0000000..89c7c1b --- /dev/null +++ b/example/example.app/Contents/_CodeSignature/CodeResources @@ -0,0 +1,128 @@ + + + + + files + + Resources/example.icns + + QIAQ35jRL6fJLHFE72K0wYzeHUU= + + + files2 + + Resources/example.icns + + hash2 + + DoBwPExI5vZ+IT6hEeHy5hJpWiVq8+4auEoGFwnbmX8= + + + + rules + + ^Resources/ + + ^Resources/.*\.lproj/ + + optional + + weight + 1000 + + ^Resources/.*\.lproj/locversion.plist$ + + omit + + weight + 1100 + + ^Resources/Base\.lproj/ + + weight + 1010 + + ^version.plist$ + + + rules2 + + .*\.dSYM($|/) + + weight + 11 + + ^(.*/)?\.DS_Store$ + + omit + + weight + 2000 + + ^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/ + + nested + + weight + 10 + + ^.* + + ^Info\.plist$ + + omit + + weight + 20 + + ^PkgInfo$ + + omit + + weight + 20 + + ^Resources/ + + weight + 20 + + ^Resources/.*\.lproj/ + + optional + + weight + 1000 + + ^Resources/.*\.lproj/locversion.plist$ + + omit + + weight + 1100 + + ^Resources/Base\.lproj/ + + weight + 1010 + + ^[^/]+$ + + nested + + weight + 10 + + ^embedded\.provisionprofile$ + + weight + 20 + + ^version\.plist$ + + weight + 20 + + + + diff --git a/example/hello_macos.go b/example/hello_macos.go new file mode 100644 index 0000000..8c8467e --- /dev/null +++ b/example/hello_macos.go @@ -0,0 +1,6 @@ +//go:generate go build +//go:generate cp -f example example.app/Contents/MacOS +//go:generate codesign -s - example.app + +package main + diff --git a/go.mod b/go.mod index f442566..68e61ce 100644 --- a/go.mod +++ b/go.mod @@ -4,9 +4,7 @@ go 1.14 require ( gioui.org v0.0.0-20200619180744-e2f3bbdfc367 - git.wow.st/gmp/jni v0.0.0-20200619201040-d0d3f316ae09 + git.wow.st/gmp/jni v0.0.0-20200626194017-b74a17279b1f github.com/esiqveland/notify v0.9.1 github.com/godbus/dbus/v5 v5.0.3 ) - -replace git.wow.st/gmp/jni => git.wow.st/whereswaldon/jni v0.0.0-20200620152723-b380472956a0 diff --git a/go.sum b/go.sum index f3eb249..c0745ca 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,12 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= gioui.org v0.0.0-20200619180744-e2f3bbdfc367 h1:uLmvlsenFUA5XkLheXgdQnX3T/oeYzO9aokBrbSsi/w= gioui.org v0.0.0-20200619180744-e2f3bbdfc367/go.mod h1:jiUwifN9cRl/zmco43aAqh0aV+s9GbhG13KcD+gEpkU= +git.wow.st/gmp/jni v0.0.0-20200619201040-d0d3f316ae09 h1:7RCYJp8PlMO5mPt1yzDriJmAE+QnDfgpJXgoKkp7xxE= +git.wow.st/gmp/jni v0.0.0-20200619201040-d0d3f316ae09/go.mod h1:lfKZKu2afBKJODTFgDNIaBfhq6qmQS0xsS/phLO5Urk= +git.wow.st/gmp/jni v0.0.0-20200623180511-2e7d20a1b472 h1:YlBjj/ZrWg1Vs/h7ibZSXBpX9GUtvZ1ATNUVs+jhcXg= +git.wow.st/gmp/jni v0.0.0-20200623180511-2e7d20a1b472/go.mod h1:+axXBRUTIDlCeE73IKeD/os7LoEnTKdkp8/gQOFjqyo= +git.wow.st/gmp/jni v0.0.0-20200626194017-b74a17279b1f h1:YTUB3a7eS51OBVh2aoEUM4emxDu3Zwee098AJ12t2KU= +git.wow.st/gmp/jni v0.0.0-20200626194017-b74a17279b1f/go.mod h1:+axXBRUTIDlCeE73IKeD/os7LoEnTKdkp8/gQOFjqyo= git.wow.st/whereswaldon/jni v0.0.0-20200620152723-b380472956a0 h1:+S/LqMv8h+w4LE/3XtAq1roX8OAZ/E6rqsl/EQ1eaK8= git.wow.st/whereswaldon/jni v0.0.0-20200620152723-b380472956a0/go.mod h1:ro8WfqxHdXc4wFC+3uP98fVT5uZqrq3yQPMV+LjP6MA= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= diff --git a/macos/notify_macos.go b/macos/notify_macos.go new file mode 100644 index 0000000..53e0ece --- /dev/null +++ b/macos/notify_macos.go @@ -0,0 +1,107 @@ +package macos + +//#cgo LDFLAGS: -framework Foundation -framework UserNotifications + +/* +#cgo CFLAGS: -x objective-c -fno-objc-arc -fmodules +#pragma clang diagnostic ignored "-Wformat-security" + +#import + +@import Foundation; +@import UserNotifications; + +UNUserNotificationCenter *nc; +BOOL enabled; + +void setup() { + @autoreleasepool { + NSLog(@"Getting application bundle"); + enabled = NO; + NSBundle *main = [NSBundle mainBundle]; + if (main.bundleIdentifier == nil) { + NSLog(@"No app bundle."); + return; + } + NSLog(@"Bundle ID: %@", main.bundleIdentifier); + NSLog(@"Getting notification center"); + nc = [UNUserNotificationCenter currentNotificationCenter]; + NSLog(@"Requesting authorization"); + [nc requestAuthorizationWithOptions: UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler: ^(BOOL granted, NSError *error){ + NSLog(@"Granted = %s", granted?"true":"false"); + NSLog(@"Error = %@", error); + enabled = granted; + }]; + } +} + +NSString* +notify(char *id, char *title, char *content) { + if (enabled != YES) { + return nil; + } + NSString *ret; + @autoreleasepool { + NSLog(@"Creating notification"); + UNMutableNotificationContent *note = [[UNMutableNotificationContent alloc] init]; + note.title = [[NSString alloc] initWithUTF8String: title]; + note.body = [[NSString alloc] initWithUTF8String: content]; + + NSLog(@"Creating request"); + UNNotificationRequest *req = [UNNotificationRequest requestWithIdentifier:@"Gio" content: note trigger:nil]; + ret = req.identifier; // FIXME: need to call retain? + NSLog(@"Adding notification request"); + [nc addNotificationRequest:req withCompletionHandler: ^(NSError *error) { + NSLog(@"added notification. Error: %@", error); + }]; + } + return ret; +} + +void +cancel(void *nid) { + [nc removePendingNotificationRequestsWithIdentifiers: @[(NSString*)nid]]; + [nc removeDeliveredNotificationsWithIdentifiers: @[(NSString*)nid]]; +} + +*/ +import "C" + +import ( + "runtime" + "unsafe" +) + +func init() { + runtime.LockOSThread() + C.setup() +} + +type NotificationChannel struct { + id *C.char +} + +func NewNotificationChannel(id string) NotificationChannel { + return NotificationChannel{ id: C.CString(id) } +} + +func (c NotificationChannel) Send(title, text string) (*Notification, error) { + return notify(c.id, title, text), nil +} + +type Notification C.NSString + +func notify(cid *C.char, title, content string) *Notification { + ct := C.CString(title) + defer C.free(unsafe.Pointer(ct)) + cc := C.CString(content) + defer C.free(unsafe.Pointer(cc)) + + id := C.notify(cid, ct, cc) + return (*Notification)(id) +} + +func (n *Notification) Cancel() error { + C.cancel(unsafe.Pointer(n)) + return nil +} diff --git a/niotify_macos.go b/niotify_macos.go new file mode 100644 index 0000000..f5a1dd0 --- /dev/null +++ b/niotify_macos.go @@ -0,0 +1,26 @@ +package niotify + +import ( + "git.sr.ht/~whereswaldon/niotify/macos" +) + +type macosManager struct { + channel macos.NotificationChannel +} + +func newManager() (Manager, error) { + c := macos.NewNotificationChannel("Gio App") + + return Manager{ + &macosManager{ channel: c }, + }, nil +} + +func (a *macosManager) CreateNotification(title, text string) (*Notification, error) { + + notification, err := a.channel.Send(title, text) + if err != nil { + return nil, err + } + return &Notification{notification}, nil +} diff --git a/niotify_unsupported.go b/niotify_unsupported.go index c64952c..cb37b75 100644 --- a/niotify_unsupported.go +++ b/niotify_unsupported.go @@ -1,4 +1,4 @@ -//+build !linux,!android +//+build !linux,!android,!darwin package niotify