forked from adamk33n3r/GoBorderless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_app.go
More file actions
343 lines (304 loc) · 8.64 KB
/
Copy pathgui_app.go
File metadata and controls
343 lines (304 loc) · 8.64 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"strconv"
"sync"
"time"
"github.com/adamk33n3r/GoBorderless/rx"
"github.com/adamk33n3r/GoBorderless/ui"
"github.com/creativeprojects/go-selfupdate"
fynetooltip "github.com/dweymouth/fyne-tooltip"
"github.com/lxn/win"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/driver"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
var windowObs = rx.FromChannel(chWindowList)
func FirstError(args ...error) error {
for _, arg := range args {
if arg != nil {
return arg
}
}
return nil
}
func intValidator(s string) error {
if s == "" {
return fmt.Errorf("invalid number")
}
if _, err := strconv.Atoi(s); err != nil {
return fmt.Errorf("invalid number")
}
return nil
}
var settingsList *widget.List
var currentWindows = make([]Window, 0) // Temporary list to store window titles
var currentWindowsMutex sync.Mutex
func launchAppSettingDialog(parent fyne.Window, new bool, settings *Settings, appSetting AppSetting, onClose func(newSetting *AppSetting)) {
dialog := makeAppSettingWindow(settings, appSetting, new, parent, onClose)
dialog.Show()
}
func checkUpdate(app fyne.App, win fyne.Window) {
selfupdate.SetLogger(log.New(os.Stdout, "", 0))
updater, err := selfupdate.NewUpdater(selfupdate.Config{
Filters: []string{"goborderless.exe"},
})
if err != nil {
fmt.Println("Updater error:", err)
return
}
latest, found, err := updater.DetectLatest(context.Background(), selfupdate.ParseSlug("adamk33n3r/GoBorderless"))
if err != nil {
fmt.Println("Update check failed:", err)
return
}
if !found || latest.LessOrEqual(app.Metadata().Version) {
fmt.Println("App is up to date.")
return
}
dialog.ShowConfirm(
"Update Available",
fmt.Sprintf("A new version (v%s) is available. Would you like to update now?", latest.Version()),
func(confirm bool) {
if confirm {
runUpdate(win, updater, latest)
}
}, win)
}
func runUpdate(win fyne.Window, updater *selfupdate.Updater, latest *selfupdate.Release) {
progress := dialog.NewCustomWithoutButtons("Updating", widget.NewLabel("Downloading and applying update..."), win)
progress.Show()
fyne.Do(func() {
exe, err := selfupdate.ExecutablePath()
if err != nil {
fmt.Println("could not locate executable path")
progress.Hide()
dialog.ShowError(err, win)
return
}
if err := updater.UpdateTo(context.Background(), latest, exe); err != nil {
progress.Hide()
dialog.ShowError(err, win)
return
}
progress.Hide()
restart := widget.NewButtonWithIcon("Restart", theme.ConfirmIcon(), func() {
restartApp(win)
})
restart.Importance = widget.HighImportance
content := container.NewVBox(
widget.NewLabel("Update applied, please restart"),
restart,
)
dialog.ShowCustomWithoutButtons("Update Successful", content, win)
})
}
func restartApp(win fyne.Window) {
self, err := os.Executable()
if err != nil {
fmt.Println("could not locate executable path")
return
}
cmd := exec.Command(self)
// Close mutex so that the new instance will open
closeMutex(MUTEX)
err = cmd.Start()
if err != nil {
dialog.ShowError(err, win)
return
}
os.Exit(0)
}
func buildApp(settings *Settings) fyne.App {
fyneApp := app.New()
before := time.Now()
mainWindow := fyneApp.NewWindow(APP_NAME)
fmt.Println("NewWindow took:", time.Since(before))
fyneApp.Lifecycle().SetOnStarted(func() {
go checkUpdate(fyneApp, mainWindow)
})
windowObs.Subscribe(func(windows []Window) {
// fmt.Println("MainApp: windows updated")
currentWindowsMutex.Lock()
currentWindows = windows
currentWindowsMutex.Unlock()
})
newAppConfig := widget.NewButtonWithIcon("Create New App Config", theme.ContentAddIcon(), func() {
newAppSetting := AppSetting{}
launchAppSettingDialog(mainWindow, true, settings, newAppSetting, func(newSetting *AppSetting) {
if newSetting != nil {
fmt.Println(newSetting)
settings.AddApp(*newSetting)
settings.Save()
settingsList.Refresh()
}
})
})
appName := widget.NewLabel("Go Borderless! v" + fyneApp.Metadata().Version)
appName.TextStyle.Bold = true
appName.SizeName = "headingText"
settingsList = widget.NewList(func() int {
return len(settings.Apps)
}, func() fyne.CanvasObject {
return ui.NewAppSettingRow()
}, func(lii widget.ListItemID, co fyne.CanvasObject) {
appSetting := settings.Apps[lii]
row := co.(*ui.AppSettingRow)
row.Title.SetText(appSetting.Display())
row.Title.SetToolTip(appSetting.Display())
row.AutoApply.SetChecked(appSetting.AutoApply)
row.AutoApply.OnChanged = func(checked bool) {
appSetting := settings.Apps[lii]
if checked {
row.ApplyBtn.Disable()
row.RestoreBtn.Disable()
} else {
row.ApplyBtn.Enable()
row.RestoreBtn.Enable()
}
appSetting.AutoApply = checked
settings.Apps[lii] = appSetting
settings.Save()
}
row.ApplyBtn.OnTapped = func() {
appSetting := settings.Apps[lii]
fmt.Println("clicked apply for:", appSetting.Display())
win := firstInSlice(currentWindows, func(win Window) bool { return matchWindow(win, appSetting) })
if win == nil {
return
}
if !isBorderless(*win) {
originalRect := getWindowRect(win.hwnd)
appSetting.PreWidth = int32(originalRect.Right - originalRect.Left)
appSetting.PreHeight = int32(originalRect.Bottom - originalRect.Top)
appSetting.PreOffsetX = int32(originalRect.Left)
appSetting.PreOffsetY = int32(originalRect.Top)
settings.Apps[lii] = appSetting
settings.Save()
}
makeBorderless(*win, appSetting)
}
row.RestoreBtn.OnTapped = func() {
appSetting := settings.Apps[lii]
fmt.Println("clicked undo for:", appSetting.Display())
win := firstInSlice(currentWindows, func(win Window) bool { return matchWindow(win, appSetting) })
if win == nil {
return
}
restoreWindow(*win, appSetting)
}
if appSetting.AutoApply {
row.ApplyBtn.Disable()
row.RestoreBtn.Disable()
}
row.EditBtn.OnTapped = func() {
// Need to fetch again from array to "reset" the values since this update func is only called on occasion
appSetting := settings.Apps[lii]
launchAppSettingDialog(mainWindow, false, settings, appSetting, func(newSetting *AppSetting) {
if newSetting != nil {
fmt.Println(newSetting)
settings.Apps[lii] = *newSetting
settings.Save()
}
})
}
row.DeleteBtn.OnTapped = func() {
appSetting := settings.Apps[lii]
win := firstInSlice(currentWindows, func(win Window) bool { return matchWindow(win, appSetting) })
if win != nil {
restoreWindow(*win, appSetting)
}
settings.RemoveApp(lii)
settings.Save()
settingsList.Refresh()
}
})
settingsList.OnSelected = func(id widget.ListItemID) {
fmt.Println("selected:", id)
settingsList.UnselectAll()
}
centeredAppLabel := container.NewHBox(
layout.NewSpacer(),
appName,
layout.NewSpacer(),
)
content := container.NewBorder(
nil,
newAppConfig,
nil,
nil,
settingsList,
)
appTabs := container.NewAppTabs(
container.NewTabItemWithIcon("Apps", theme.ListIcon(), content),
container.NewTabItemWithIcon("Defaults", theme.ViewRestoreIcon(), buildDefaultsTab(settings)),
container.NewTabItemWithIcon("Settings", theme.SettingsIcon(), buildSettingsTab(settings)),
)
mainWindow.SetContent(fynetooltip.AddWindowToolTipLayer(container.NewBorder(
centeredAppLabel,
nil,
nil,
nil,
appTabs,
), mainWindow.Canvas()))
mainWindow.CenterOnScreen()
mainWindow.Resize(fyne.NewSquareSize(620))
mainWindow.SetFixedSize(true)
fmt.Println("Running app...")
mainWindow.Show()
handleWindowsInit(fyneApp, mainWindow, settings)
return fyneApp
}
func handleWindowsInit(fyneApp fyne.App, window fyne.Window, settings *Settings) {
window.(driver.NativeWindow).RunNative(func(context any) {
switch ctx := context.(type) {
case driver.WindowsWindowContext:
windowHWND := win.HWND(ctx.HWND)
if desk, ok := fyneApp.(desktop.App); ok {
m := fyne.NewMenu(APP_NAME,
fyne.NewMenuItem("Show", func() {
window.Show()
win.ShowWindow(windowHWND, win.SW_RESTORE)
}))
desk.SetSystemTrayMenu(m)
}
window.SetCloseIntercept(func() {
if !settings.CloseToTray {
fyneApp.Quit()
return
}
window.Hide()
})
// Intercept minimize
go func() {
for {
time.Sleep(250 * time.Millisecond)
if !settings.MinimizeToTray {
continue
}
if win.IsIconic(windowHWND) {
fyne.DoAndWait(func() {
window.Hide()
})
}
}
}()
if settings.StartMinimized {
win.ShowWindow(windowHWND, win.SW_MINIMIZE)
}
default:
fmt.Println("not running in windows....don't do that")
fyneApp.Quit()
}
})
}