-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
152 lines (130 loc) · 4.61 KB
/
Copy pathmain.go
File metadata and controls
152 lines (130 loc) · 4.61 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
package main
import (
"embed"
"os"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
"github.com/wailsapp/wails/v3/pkg/updater"
"github.com/wailsapp/wails/v3/pkg/updater/providers/github"
"xensql/internal/app"
"xensql/internal/appmenu"
"xensql/internal/paths"
"xensql/internal/windowstate"
"xensql/internal/windowtheme"
)
//go:embed all:frontend/dist
var assets embed.FS
// singleInstanceKey encrypts the IPC payload the OS hands a second instance back
// to the first. It only needs to be stable across builds of this app, not secret.
var singleInstanceKey = [32]byte{
0x78, 0x65, 0x6e, 0x73, 0x71, 0x6c, 0x2d, 0x62,
0x37, 0x61, 0x2d, 0x73, 0x69, 0x6e, 0x67, 0x6c,
0x65, 0x2d, 0x69, 0x6e, 0x73, 0x74, 0x61, 0x6e,
0x63, 0x65, 0x2d, 0x6b, 0x65, 0x79, 0x21, 0x00,
}
func main() {
svc := app.NewApp()
configDir, err := paths.EnsureDataDir()
if err != nil {
println("Warning: data directory:", err.Error())
}
// Open all stores up front, before the window, so saved settings are ready at creation time.
svc.InitStores(configDir)
settings := svc.SettingsStore()
if f := app.FindSQLiteArg(os.Args[1:]); f != "" {
svc.SetPendingFile(f)
}
var window *application.WebviewWindow
wailsApp := application.New(application.Options{
Name: "XenSQL",
Description: "A fast, native SQL client built with Go and Wails.",
Services: []application.Service{
application.NewService(svc),
},
Assets: application.AssetOptions{
Handler: application.AssetFileServerFS(assets),
},
SingleInstance: &application.SingleInstanceOptions{
UniqueID: "xensql-b7a-single-instance-lock",
EncryptionKey: singleInstanceKey,
OnSecondInstanceLaunch: func(data application.SecondInstanceData) {
if f := app.FindSQLiteArg(data.Args); f != "" {
svc.EmitOpenSQLite(f)
}
if window != nil {
window.Restore()
window.Focus()
}
},
},
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
})
// macOS delivers Finder opens as Apple Events, never argv or a second
// instance; pending covers launch, the emit covers a running app.
wailsApp.Event.OnApplicationEvent(events.Common.ApplicationOpenedWithFile, func(e *application.ApplicationEvent) {
if f := app.FindSQLiteArg([]string{e.Context().Filename()}); f != "" {
svc.SetPendingFile(f)
svc.EmitOpenSQLite(f)
}
})
// In-app updates from GitHub Releases. application.New already wired the
// helper-mode hook, so the built-in window's "Restart & Apply" just works.
if ghProvider, perr := github.New(github.Config{
Repository: "Bare7a/XenSQL",
ChecksumAsset: "SHA256SUMS",
}); perr != nil {
println("Warning: updater provider:", perr.Error())
} else if perr := wailsApp.Updater.Init(updater.Config{
CurrentVersion: app.Version,
Providers: []updater.Provider{ghProvider},
}); perr != nil {
println("Warning: updater init:", perr.Error())
}
// mac menus live in the system menu bar; set pre-Run, installed once running.
if application.System.IsPlatform(application.PlatformMacOS) {
nativeMenu := appmenu.Build(svc.EmitMenuAction)
wailsApp.Menu.Set(nativeMenu.Root())
svc.SetNativeMenu(nativeMenu)
}
opts := application.WebviewWindowOptions{
Title: "XenSQL",
MinWidth: 800,
MinHeight: 600,
EnableFileDrop: true,
URL: "/",
}
// Windows/Linux: frameless - the in-page title bar is the single-bar chrome.
if !application.System.IsPlatform(application.PlatformMacOS) {
opts.Frameless = true
}
// Colour the window chrome to the saved app theme.
var prefs map[string]string
if settings != nil {
prefs = settings.GetAll()
}
windowtheme.Configure(&opts, windowtheme.Load(prefs))
// Restore size/position/state from the last session, or the default on first run.
var saved windowstate.State
restorable := false
if settings != nil {
saved, restorable = windowstate.Load(settings)
}
windowstate.Apply(&opts, saved, restorable, wailsApp.Screen.GetPrimary(), opts.MinWidth, opts.MinHeight)
window = wailsApp.Window.NewWithOptions(opts)
// Track geometry changes; the returned flush captures the final state on shutdown.
if settings != nil {
svc.SetWindowStateFlush(windowstate.Track(window, settings))
}
// Recolour the window chrome when the frontend persists a theme change.
svc.SetOnSettingChanged(func(key, value string) {
windowtheme.Update(window, key, value)
})
window.OnWindowEvent(events.Common.WindowFilesDropped, func(e *application.WindowEvent) {
wailsApp.Event.Emit("files-dropped", e.Context().DroppedFiles())
})
if err := wailsApp.Run(); err != nil {
println("Error:", err.Error())
}
}