app_darwin.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //go:build !ci && !js && !wasm && !test_web_driver
  2. // +build !ci,!js,!wasm,!test_web_driver
  3. package app
  4. /*
  5. #cgo CFLAGS: -x objective-c
  6. #cgo LDFLAGS: -framework Foundation
  7. #include <stdbool.h>
  8. #include <stdlib.h>
  9. bool isBundled();
  10. void sendNotification(char *title, char *content);
  11. */
  12. import "C"
  13. import (
  14. "fmt"
  15. "strings"
  16. "unsafe"
  17. "fyne.io/fyne/v2"
  18. "golang.org/x/sys/execabs"
  19. )
  20. func (a *fyneApp) SendNotification(n *fyne.Notification) {
  21. if C.isBundled() {
  22. titleStr := C.CString(n.Title)
  23. defer C.free(unsafe.Pointer(titleStr))
  24. contentStr := C.CString(n.Content)
  25. defer C.free(unsafe.Pointer(contentStr))
  26. C.sendNotification(titleStr, contentStr)
  27. return
  28. }
  29. fallbackNotification(n.Title, n.Content)
  30. }
  31. func escapeNotificationString(in string) string {
  32. noSlash := strings.ReplaceAll(in, "\\", "\\\\")
  33. return strings.ReplaceAll(noSlash, "\"", "\\\"")
  34. }
  35. //export fallbackSend
  36. func fallbackSend(cTitle, cContent *C.char) {
  37. title := C.GoString(cTitle)
  38. content := C.GoString(cContent)
  39. fallbackNotification(title, content)
  40. }
  41. func fallbackNotification(title, content string) {
  42. template := `display notification "%s" with title "%s"`
  43. script := fmt.Sprintf(template, escapeNotificationString(content), escapeNotificationString(title))
  44. err := execabs.Command("osascript", "-e", script).Start()
  45. if err != nil {
  46. fyne.LogError("Failed to launch darwin notify script", err)
  47. }
  48. }