-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
macos.go
212 lines (176 loc) · 5.36 KB
/
macos.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package icon
import (
"bytes"
_ "image/jpeg" // support JPEG images
"image/png" // PNG support is required as we use it directly
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/jackmordaunt/icns"
"howett.net/plist"
"fyne.io/fyne/v2"
"fyshos.com/fynedesk"
)
type macOSAppBundle struct {
DisplayName string `plist:"CFBundleDisplayName"`
// TODO alternateNames []string
Executable string `plist:"CFBundleExecutable"`
categories []string
runPath string
IconFile string `plist:"CFBundleIconFile"`
iconPath string
iconCache fyne.Resource
}
func (m *macOSAppBundle) Name() string {
return m.DisplayName
}
func (m *macOSAppBundle) Categories() []string {
return m.categories
}
func (m *macOSAppBundle) Hidden() bool {
return false
}
func (m *macOSAppBundle) Icon(_ string, _ int) fyne.Resource {
if m.iconCache != nil {
return m.iconCache
}
src, err := os.Open(m.iconPath)
if err != nil {
fyne.LogError("Failed to read icon data for "+m.iconPath, err)
return nil
}
icon, err := icns.Decode(src)
if err != nil {
fyne.LogError("Failed to parse icon data for "+m.iconPath, err)
return nil
}
var data bytes.Buffer
err = png.Encode(&data, icon)
if err != nil {
fyne.LogError("Failed to encode icon data for "+m.iconPath, err)
return nil
}
iconName := filepath.Base(m.iconPath)
m.iconCache = fyne.NewStaticResource(strings.Replace(iconName, ".icns", ".png", 1), data.Bytes())
return m.iconCache
}
func (m *macOSAppBundle) Run([]string) error {
// in macOS test mode we ignore the wm env flags
return exec.Command("open", "-a", m.runPath).Start()
}
func loadAppBundle(name, path, category string) fynedesk.AppData {
buf, err := os.Open(filepath.Join(path, "Contents", "Info.plist"))
if err != nil {
fyne.LogError("Unable to read application plist", err)
return nil
}
data := macOSAppBundle{DisplayName: name, categories: []string{category}}
decoder := plist.NewDecoder(buf)
err = decoder.Decode(&data)
if err != nil {
fyne.LogError("Unable to parse application plist", err)
return nil
}
data.runPath = filepath.Join(path, "Contents", "MacOS", data.Executable)
data.iconPath = filepath.Join(path, "Contents", "Resources", data.IconFile)
pos := strings.Index(data.iconPath, ".icns")
if pos == -1 {
data.iconPath = data.iconPath + ".icns"
}
return &data
}
type macOSAppProvider struct {
rootDirs []string
cache *appCache
}
func (m *macOSAppProvider) forEachApplication(f func(name, path, category string) bool) {
for _, root := range m.rootDirs {
category := filepath.Base(root)
files, err := os.ReadDir(root)
if err != nil {
fyne.LogError("Could not read applications directory "+root, err)
return
}
for _, file := range files {
if !file.IsDir() || !strings.HasSuffix(file.Name(), ".app") {
continue // skip non-app bundles
}
appDir := filepath.Join(root, file.Name())
if f(file.Name()[0:len(file.Name())-4], appDir, category) {
break
}
}
}
}
func (m *macOSAppProvider) AvailableApps() []fynedesk.AppData {
var icons []fynedesk.AppData
m.forEachApplication(func(name, path, category string) bool {
app := loadAppBundle(name, path, category)
if app != nil {
icons = append(icons, app)
}
return false
})
return icons
}
func (m *macOSAppProvider) AvailableThemes() []string {
//I'm not sure this is relevant on Mac OSX
return []string{}
}
func (m *macOSAppProvider) FindAppFromName(appName string) fynedesk.AppData {
var icon fynedesk.AppData
m.cache.forEachCachedApplication(func(name string, app fynedesk.AppData) bool {
if name == appName {
icon = app
return true
}
return false
})
return icon
}
func (m *macOSAppProvider) FindAppFromWinInfo(win fynedesk.Window) fynedesk.AppData {
return m.FindAppFromName(win.Properties().Title())
}
func (m *macOSAppProvider) DefaultApps() []fynedesk.AppData {
var apps []fynedesk.AppData
apps = appendAppIfExists(apps, findOneAppFromNames(m, "Terminal", "iTerm"))
apps = appendAppIfExists(apps, findOneAppFromNames(m, "Google Chrome", "Firefox", "Safari"))
apps = appendAppIfExists(apps, findOneAppFromNames(m, "Spark", "AirMail", "Mail"))
apps = appendAppIfExists(apps, m.FindAppFromName("Photos"))
apps = appendAppIfExists(apps, m.FindAppFromName("System Preferences"))
return apps
}
func (m *macOSAppProvider) FindAppsMatching(pattern string) []fynedesk.AppData {
var icons []fynedesk.AppData
m.cache.forEachCachedApplication(func(name string, app fynedesk.AppData) bool {
if !strings.Contains(strings.ToLower(name), strings.ToLower(pattern)) {
return false
}
icons = append(icons, app)
return false
})
return icons
}
func (m *macOSAppProvider) CategorizedApps() map[string][]fynedesk.AppData {
var allApps, allUtils []fynedesk.AppData
m.cache.forEachCachedApplication(func(_ string, app fynedesk.AppData) bool {
if app.Categories()[0] == "Applications" {
allApps = append(allApps, app)
} else {
allUtils = append(allUtils, app)
}
return false
})
return map[string][]fynedesk.AppData{
"Applications": allApps,
"Utilities": allUtils,
}
}
// NewMacOSAppProvider creates an instance of an ApplicationProvider that can find and decode macOS apps
func NewMacOSAppProvider() fynedesk.ApplicationProvider {
source := &macOSAppProvider{rootDirs: []string{"/Applications", "/Applications/Utilities",
"/System/Applications", "/System/Applications/Utilities"}}
source.cache = newAppCache(source)
return source
}