-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
227 lines (200 loc) · 4.91 KB
/
app.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"sync"
"github.com/kettek/staxie/pkg/data"
"runtime/debug"
"github.com/kettek/go-updater"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
versionString string
fsAccess sync.Mutex
RichPresence
Platform
}
// NewApp creates a new App application struct
func NewApp() *App {
a := &App{
versionString: "unknown",
}
if bld, ok := debug.ReadBuildInfo(); ok {
version := bld.Main.Version
for _, kv := range bld.Settings {
switch kv.Key {
case "vcs.revision":
version = kv.Value
case "vcs.modified":
if kv.Value == "true" {
version += "-dirty"
}
}
}
a.versionString = version
}
return a
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
func (a *App) GetOSSeparator() string {
return string(filepath.Separator)
}
func (a *App) AppDirectory(p string) string {
return filepath.Join(data.AppDirectory, p)
}
func (a *App) CacheDirectory(p string) string {
return filepath.Join(data.CacheDirectory, p)
}
func (a *App) ReadBytes(name string) ([]byte, error) {
return os.ReadFile(name)
}
func (a *App) GetFilesInDir(dir string) ([]string, error) {
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
var names []string
for _, f := range files {
names = append(names, f.Name())
}
return names, nil
}
func (a *App) GetFilePath(names []string, patterns []string) (string, error) {
a.fsAccess.Lock()
defer a.fsAccess.Unlock()
var f []runtime.FileFilter
for i, n := range names {
ff := runtime.FileFilter{
DisplayName: n,
}
if i < len(patterns) {
ff.Pattern = patterns[i]
}
f = append(f, ff)
}
file, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Filters: f,
})
if err != nil {
return "", err
}
return file, nil
}
func (a *App) GetFileSavePath(names []string, patterns []string) (string, error) {
a.fsAccess.Lock()
defer a.fsAccess.Unlock()
var f []runtime.FileFilter
for i, n := range names {
ff := runtime.FileFilter{
DisplayName: n,
}
if i < len(patterns) {
ff.Pattern = patterns[i]
}
f = append(f, ff)
}
file, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Filters: f,
})
if err != nil {
return "", err
}
return file, nil
}
func (a *App) GetFolderPath() (string, error) {
a.fsAccess.Lock()
defer a.fsAccess.Unlock()
file, err := runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{})
if err != nil {
return "", err
}
return file, nil
}
func (a *App) OpenFileBytes(p string) ([]byte, error) {
b, err := os.ReadFile(p)
if err != nil {
return nil, err
}
return b, nil
}
func (a *App) SaveFilePath(p string) (string, error) {
a.fsAccess.Lock()
defer a.fsAccess.Unlock()
file, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
DefaultDirectory: filepath.Dir(p),
DefaultFilename: filepath.Base(p),
Filters: []runtime.FileFilter{
{
DisplayName: "pee enn gees",
Pattern: "*.png",
},
},
})
if err != nil {
return "", err
}
return file, nil
}
func (a *App) SaveFileBytes(p string, b []byte) error {
return os.WriteFile(p, b, 0644)
}
func (a *App) onResize(v ...interface{}) {
w, h := runtime.WindowGetSize(a.ctx)
isFullscreen := runtime.WindowIsFullscreen(a.ctx)
isMaximized := runtime.WindowIsMaximised(a.ctx)
isMinimized := runtime.WindowIsMinimised(a.ctx)
settings := data.WindowingFromAny(data.Settings["Windowing"])
settings.Fullscreen = &isFullscreen
settings.Maximized = &isMaximized
settings.Minimized = &isMinimized
if !isFullscreen && !isMaximized && !isMinimized {
settings.Width = &w
settings.Height = &h
}
data.Settings["Windowing"] = settings
if err := data.SaveSettings(); err != nil {
log.Println("Error saving settings:", err)
}
}
// Version returns the version string. This is set at compile time.
func (a *App) Version() string {
return a.versionString
}
// SetSetting sets a setting in the settings store.
func (a *App) SetSetting(key string, value string) {
data.Settings[key] = value
data.SaveSettings()
}
// ClearSetting clears a setting in the settings store.
func (a *App) ClearSetting(key string) {
delete(data.Settings, key)
data.SaveSettings()
}
// GetSetting gets a setting from the settings store.
func (a *App) GetSetting(key string) any {
return data.Settings[key]
}
// ToggleFullscreen toggles the fullscreen state of the window.
func (a *App) ToggleFullscreen() {
// NOTE: we lazily call onResize here to update the underlying settings.
if runtime.WindowIsFullscreen(a.ctx) {
runtime.WindowUnfullscreen(a.ctx)
a.onResize(nil)
return
}
runtime.WindowFullscreen(a.ctx)
a.onResize(nil)
}
// Update replaces the running staxie application with the given source.
func (a *App) Update(source string) {
updater.Update(source, os.Args[0], fmt.Sprintf("%d", os.Getpid()))
}