wails_app.go raw
1 package wails
2
3 import (
4 "context"
5 "embed"
6
7 "github.com/getAlby/hub/api"
8 "github.com/getAlby/hub/apps"
9 "github.com/getAlby/hub/logger"
10 "github.com/getAlby/hub/service"
11 "github.com/wailsapp/wails/v2"
12 "github.com/wailsapp/wails/v2/pkg/options"
13 "github.com/wailsapp/wails/v2/pkg/options/assetserver"
14 "github.com/wailsapp/wails/v2/pkg/options/linux"
15 "github.com/wailsapp/wails/v2/pkg/options/mac"
16 "github.com/wailsapp/wails/v2/pkg/runtime"
17 "gorm.io/gorm"
18 )
19
20 type WailsApp struct {
21 ctx context.Context
22 svc service.Service
23 api api.API
24 db *gorm.DB
25 appsSvc apps.AppsService
26 }
27
28 func NewApp(svc service.Service) *WailsApp {
29 return &WailsApp{
30 svc: svc,
31 api: api.NewAPI(svc, svc.GetDB(), svc.GetConfig(), svc.GetKeys(), svc.GetAlbySvc(), svc.GetAlbyOAuthSvc(), svc.GetEventPublisher()),
32 db: svc.GetDB(),
33 appsSvc: apps.NewAppsService(svc.GetDB(), svc.GetEventPublisher(), svc.GetKeys(), svc.GetConfig()),
34 }
35 }
36
37 // startup is called when the app starts. The context is saved
38 // so we can call the runtime methods
39 func (app *WailsApp) startup(ctx context.Context) {
40 app.ctx = ctx
41 }
42
43 func (app *WailsApp) onBeforeClose(ctx context.Context) bool {
44 response, err := runtime.MessageDialog(ctx, runtime.MessageDialogOptions{
45 Type: runtime.QuestionDialog,
46 Title: "Confirm Exit",
47 Message: "Are you sure you want to shut down Alby Hub? Alby Hub needs to stay online to send and receive transactions. Channels may be closed if your hub stays offline for an extended period of time.",
48 Buttons: []string{"Yes", "No"},
49 DefaultButton: "No",
50 })
51 if err != nil {
52 logger.Logger.WithError(err).Error("failed to show confirmation dialog")
53 return false
54 }
55 return response != "Yes"
56 }
57
58 func LaunchWailsApp(app *WailsApp, assets embed.FS, appIcon []byte) {
59 err := wails.Run(&options.App{
60 Title: "Alby Hub",
61 Width: 1024,
62 Height: 768,
63 AssetServer: &assetserver.Options{
64 Assets: assets,
65 },
66 Logger: NewWailsLogger(),
67 // HideWindowOnClose: true, // with this on, there is no way to close the app - wait for v3
68
69 //BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
70 OnStartup: app.startup,
71 OnBeforeClose: func(ctx context.Context) bool {
72 return app.onBeforeClose(ctx)
73 },
74 Bind: []interface{}{
75 app,
76 },
77 Mac: &mac.Options{
78 About: &mac.AboutInfo{
79 Title: "Alby Hub",
80 Icon: appIcon,
81 },
82 },
83 Linux: &linux.Options{
84 Icon: appIcon,
85 },
86 })
87
88 if err != nil {
89 logger.Logger.WithError(err).Error("failed to run Wails app")
90 }
91 }
92
93 func NewWailsLogger() WailsLogger {
94 return WailsLogger{}
95 }
96
97 type WailsLogger struct {
98 }
99
100 func (wailsLogger WailsLogger) Print(message string) {
101 logger.Logger.WithField("wails", true).Print(message)
102 }
103
104 func (wailsLogger WailsLogger) Trace(message string) {
105 logger.Logger.WithField("wails", true).Trace(message)
106 }
107
108 func (wailsLogger WailsLogger) Debug(message string) {
109 logger.Logger.WithField("wails", true).Debug(message)
110 }
111
112 func (wailsLogger WailsLogger) Info(message string) {
113 logger.Logger.WithField("wails", true).Info(message)
114 }
115
116 func (wailsLogger WailsLogger) Warning(message string) {
117 logger.Logger.WithField("wails", true).Warning(message)
118 }
119
120 func (wailsLogger WailsLogger) Error(message string) {
121 logger.Logger.WithField("wails", true).Error(message)
122 }
123
124 func (wailsLogger WailsLogger) Fatal(message string) {
125 logger.Logger.WithField("wails", true).Fatal(message)
126 }
127