-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
258 lines (212 loc) · 7.1 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
}
var appContext context.Context
var app *App
// NewApp creates a new App application struct
func NewApp() *App {
app = &App{}
return app
}
type Notification struct {
Title string `json:"title"`
Message string `json:"message"`
Path string `json:"path"`
Variant string `json:"variant"`
Parameters map[string]string `json:"parameters,omitempty"`
}
// startup is called at application startup
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
appContext = ctx
runtime.LogInfo(appContext, "Starting application")
// Set window position
if *config.WindowStartPositionX >= 0 && *config.WindowStartPositionY >= 0 {
runtime.LogInfo(appContext, "Setting window position")
runtime.WindowSetPosition(appContext, *config.WindowStartPositionX, *config.WindowStartPositionY)
}
// Set window size
if *config.WindowStartSizeX >= 0 && *config.WindowStartSizeY >= 0 && runtime.WindowIsNormal(appContext) {
runtime.LogInfo(appContext, "Setting window size")
runtime.WindowSetSize(appContext, *config.WindowStartSizeX, *config.WindowStartSizeY)
}
// Initiate paths
runtime.LogInfo(appContext, "Initiating paths")
err := path_init()
if err != nil {
runtime.LogError(appContext, err.Error())
}
// Delete old log files
runtime.LogInfo(appContext, "Deleting old log files")
delete_old_logs()
// Check if configPath exists
if _, err := os.Stat(configPath); os.IsNotExist(err) {
onFirstRun()
}
// Initiate notifications for Windows
notification_init()
}
// domReady is called after front-end resources have been loaded
func (a *App) domReady(ctx context.Context) {
// Get version from wails.json
var wailsDeccodedJSON map[string]interface{}
err := json.Unmarshal(wailsJSON, &wailsDeccodedJSON)
if err != nil {
runtime.LogError(appContext, "Failed to decode wails.json: "+err.Error())
}
version = wailsDeccodedJSON["info"].(map[string]interface{})["productVersion"].(string)
// Get launch args
args = os.Args[1:]
runtime.LogInfo(appContext, "Launch args: "+strings.Join(args, " "))
// Show window
runtime.WindowShow(appContext)
runtime.Show(appContext)
// Check updates
if *config.CheckForUpdates {
updateInfo := a.CheckForUpdate()
if updateInfo.UpdateAvailable {
a.SendNotification(Notification{
Title: "settings.setting.update.update_available",
Message: "v" + updateInfo.CurrentVersion + " ⭢ " + updateInfo.LatestVersion,
Path: "__settings__update",
Variant: "info",
})
}
}
for i := 0; i < len(args); i++ {
switch args[i] {
case "--goto":
if i+1 < len(args) {
runtime.LogInfo(a.ctx, fmt.Sprintf("Goto: %s", args[i+1]))
runtime.WindowExecJS(a.ctx, fmt.Sprintf(`window.goto("%s");`, args[i+1]))
i++
}
case "--notify":
if i+4 < len(args) {
runtime.LogInfo(a.ctx, "Notify: "+args[i+1]+" "+args[i+2]+" "+args[i+3]+" "+args[i+4])
a.SendNotification(Notification{
Title: args[i+1],
Message: args[i+2],
Path: args[i+3],
Variant: args[i+4],
})
i += 4
}
default:
runtime.LogInfo(a.ctx, fmt.Sprintf("Pack path: %s", args[i]))
}
}
}
// beforeClose is called when the application is about to quit,
// either by clicking the window close button or calling runtime.Quit.
// Returning true will cause the application to continue, false will continue shutdown as normal.
func (a *App) beforeClose(ctx context.Context) (prevent bool) {
if *config.SaveWindowStatus {
if runtime.WindowIsMaximised(a.ctx) {
var windowState = 2
config.WindowStartState = &windowState
runtime.LogInfo(a.ctx, "Setting window state to maximized")
} else {
var windowState = 0
config.WindowStartState = &windowState
runtime.LogInfo(a.ctx, "Setting window state to normal")
}
windowPositionX, windowPositionY := runtime.WindowGetPosition(a.ctx)
if windowPositionX < 0 {
windowPositionX = 0
}
if windowPositionY < 0 {
windowPositionY = 0
}
config.WindowStartPositionX, config.WindowStartPositionY = &windowPositionX, &windowPositionY
runtime.LogInfo(a.ctx, fmt.Sprintf("Setting window position to %d,%d", windowPositionX, windowPositionY))
windowSizeX, windowSizeY := runtime.WindowGetSize(a.ctx)
config.WindowStartSizeX, config.WindowStartSizeY = &windowSizeX, &windowSizeY
runtime.LogInfo(a.ctx, fmt.Sprintf("Setting window size to %d,%d", windowSizeX, windowSizeY))
}
runtime.LogInfo(a.ctx, "Saving config")
err := WriteConfig(configPath)
if err != nil {
runtime.LogError(a.ctx, err.Error())
return false
}
runtime.LogInfo(a.ctx, "Saving config complete")
// Disconnect
app.DisconnectRcon()
return false
}
// shutdown is called at application termination
func (a *App) shutdown(ctx context.Context) {
// Perform your teardown here
}
// onSecondInstanceLaunch is called when the application is launched from a second instance
func (a *App) onSecondInstanceLaunch(secondInstanceData options.SecondInstanceData) {
secondInstanceArgs := secondInstanceData.Args
runtime.LogDebug(a.ctx, "User opened a second instance "+strings.Join(secondInstanceArgs, ","))
runtime.LogDebug(a.ctx, "User opened a second instance from "+secondInstanceData.WorkingDirectory)
runtime.WindowUnminimise(a.ctx)
runtime.Show(a.ctx)
go runtime.EventsEmit(a.ctx, "launchArgs", secondInstanceArgs)
}
func onFirstRun() {
runtime.LogInfo(appContext, "First run detected")
runtime.LogInfo(appContext, "Setting default system language")
set_system_language()
}
func (a *App) GetVersion() string {
return version
}
// Send notification
func (a *App) SendNotification(notification Notification) {
runtime.LogInfo(a.ctx, "Sending notification")
if a.GetOs() != "windows" || runtime.WindowIsNormal(a.ctx) || runtime.WindowIsMaximised(a.ctx) || runtime.WindowIsFullscreen(a.ctx) {
runtime.LogInfo(a.ctx, "Sending notification to toast")
runtime.EventsEmit(a.ctx, "toast", notification)
} else {
runtime.EventsEmit(a.ctx, "sendNotification", notification)
}
}
func (a *App) SendWindowsNotification(notification Notification) {
err := SendSystemNotification(notification)
if err != nil {
runtime.LogError(a.ctx, "Error sending notification: "+err.Error())
}
}
func (a *App) RestartApplication(args []string) error {
// Get the path to the current executable
executable, err := os.Executable()
if err != nil {
runtime.LogError(a.ctx, "failed to get executable path: "+err.Error())
return err
}
// Create the new process with the same arguments as the current process
cmd := exec.Command(executable)
cmd.Args = append(cmd.Args, args...)
cmd.Env = os.Environ()
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
runtime.LogDebug(a.ctx, "Attempting to restart")
// Start the new process
if err := cmd.Start(); err != nil {
runtime.LogError(a.ctx, "failed to start new process: "+err.Error())
return err
}
runtime.LogDebug(a.ctx, "Successfully started new process")
a.beforeClose(a.ctx)
// Exit the current process
os.Exit(0)
return nil
}