/
app_darwin.go
89 lines (72 loc) · 1.87 KB
/
app_darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// +build !ci
// +build !ios
package app
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation
#include <AppKit/AppKit.h>
bool isBundled();
bool isDarkMode();
void sendNotification(const char *title, const char *content);
void watchTheme();
*/
import "C"
import (
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"unsafe"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
)
func defaultVariant() fyne.ThemeVariant {
if fyne.CurrentDevice().IsMobile() { // this is called in mobile simulate mode
return theme.VariantLight
}
if C.isDarkMode() {
return theme.VariantDark
}
return theme.VariantLight
}
func rootConfigDir() string {
homeDir, _ := os.UserHomeDir()
desktopConfig := filepath.Join(filepath.Join(homeDir, "Library"), "Preferences")
return filepath.Join(desktopConfig, "fyne")
}
func (app *fyneApp) OpenURL(url *url.URL) error {
cmd := app.exec("open", url.String())
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
return cmd.Run()
}
func (app *fyneApp) SendNotification(n *fyne.Notification) {
if C.isBundled() {
title := C.CString(n.Title)
defer C.free(unsafe.Pointer(title))
content := C.CString(n.Content)
defer C.free(unsafe.Pointer(content))
C.sendNotification(title, content)
return
}
title := escapeNotificationString(n.Title)
content := escapeNotificationString(n.Content)
template := `display notification "%s" with title "%s"`
script := fmt.Sprintf(template, content, title)
err := exec.Command("osascript", "-e", script).Start()
if err != nil {
fyne.LogError("Failed to launch darwin notify script", err)
}
}
func escapeNotificationString(in string) string {
noSlash := strings.ReplaceAll(in, "\\", "\\\\")
return strings.ReplaceAll(noSlash, "\"", "\\\"")
}
//export themeChanged
func themeChanged() {
fyne.CurrentApp().Settings().(*settings).setupTheme()
}
func watchTheme() {
C.watchTheme()
}