Skip to content

Commit 3a0ae43

Browse files
committed
[UPD] Ask for user confirmation before quitting in all cases
Previously, when quitting the application on macOS without using the menu, the confirmation dialog was not being called and the app was hidden instead. Now, for each case, we ask for confirmation and if not confirmed, the window is minimized. On macOS, hiding the window does not allow the user to display the window again when clicking the icon.
1 parent a73f676 commit 3a0ae43

4 files changed

Lines changed: 33 additions & 42 deletions

File tree

app.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ type App struct {
2323
webserver *server.Server
2424
config *config.Manager
2525
printerManager *printer.Manager
26-
allowClose bool
2726
autoStart *autostart.App
2827
}
2928

3029
func NewApp() *App {
31-
a := &App{allowClose: false}
30+
a := &App{}
3231

3332
a.autoStart = &autostart.App{
3433
Name: "epos-proxy",
@@ -214,12 +213,6 @@ func (a *App) CheckLANPrinterStatus(ip string) bool {
214213
return printer.CheckLANPrinter(ip) == nil
215214
}
216215

217-
func (a *App) Quit() {
218-
logger.Infof("Quit requested by user")
219-
a.allowClose = true
220-
wailsruntime.Quit(a.ctx)
221-
}
222-
223216
func (a *App) DownloadLogs() {
224217
logger.Debugf("Download logs requested")
225218
logDir := logger.LogDirectory()

logger/logger.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,55 @@ package logger
33
import (
44
"os"
55
"path/filepath"
6-
"time"
76

87
"github.com/sirupsen/logrus"
98
"gopkg.in/natefinch/lumberjack.v2"
109
)
1110

12-
var Log = logrus.New()
11+
var log = logrus.New()
1312

1413
var logDir string
1514

1615
func InitLogger() {
1716
dir, err := os.UserConfigDir()
1817
if err != nil {
19-
Log.Fatalf("Failed to get user config dir: %v", err)
18+
Fatalf("Failed to get user config dir: %v", err)
2019
}
2120
logDir = filepath.Join(dir, "epos-proxy", "logs")
2221
if err := os.MkdirAll(logDir, 0755); err != nil {
23-
Log.Fatalf("Failed to create log directory: %v", err)
22+
Fatalf("Failed to create log directory: %v", err)
2423
}
2524

2625
filename := filepath.Join(logDir, "epos-proxy.log")
2726

28-
Log.SetOutput(&lumberjack.Logger{
27+
log.SetOutput(&lumberjack.Logger{
2928
Filename: filename,
3029
MaxSize: 20, // MB
3130
MaxBackups: 5, // keep last x files
3231
MaxAge: 5, // days
3332
Compress: false,
3433
})
3534

36-
Log.SetReportCaller(true)
35+
// log.SetReportCaller(true)
3736

38-
Log.SetFormatter(&logrus.TextFormatter{
37+
log.SetFormatter(&logrus.TextFormatter{
3938
FullTimestamp: true,
4039
})
4140

42-
Log.SetLevel(logrus.InfoLevel)
43-
44-
Log.Info("Logger initialized at ", time.Now())
41+
log.SetLevel(logrus.InfoLevel)
42+
Info("Logger initialized")
4543
}
4644

4745
// Wrappers
48-
func Info(args ...interface{}) { Log.Info(args...) }
49-
func Infof(format string, args ...interface{}) { Log.Infof(format, args...) }
50-
func Warn(args ...interface{}) { Log.Warn(args...) }
51-
func Warnf(format string, args ...interface{}) { Log.Warnf(format, args...) }
52-
func Error(args ...interface{}) { Log.Error(args...) }
53-
func Errorf(format string, args ...interface{}) { Log.Errorf(format, args...) }
54-
func Fatal(args ...interface{}) { Log.Fatal(args...) }
55-
func Fatalf(format string, args ...interface{}) { Log.Fatalf(format, args...) }
56-
func Debug(args ...interface{}) { Log.Debug(args...) }
57-
func Debugf(format string, args ...interface{}) { Log.Debugf(format, args...) }
46+
func Info(args ...interface{}) { log.Info(args...) }
47+
func Infof(format string, args ...interface{}) { log.Infof(format, args...) }
48+
func Warn(args ...interface{}) { log.Warn(args...) }
49+
func Warnf(format string, args ...interface{}) { log.Warnf(format, args...) }
50+
func Error(args ...interface{}) { log.Error(args...) }
51+
func Errorf(format string, args ...interface{}) { log.Errorf(format, args...) }
52+
func Fatal(args ...interface{}) { log.Fatal(args...) }
53+
func Fatalf(format string, args ...interface{}) { log.Fatalf(format, args...) }
54+
func Debug(args ...interface{}) { log.Debug(args...) }
55+
func Debugf(format string, args ...interface{}) { log.Debugf(format, args...) }
5856

5957
func LogDirectory() string { return logDir }

main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ func main() {
5555
},
5656
},
5757
OnBeforeClose: func(ctx context.Context) (prevent bool) {
58-
if !app.allowClose {
59-
logger.Infof("Close requested, hiding window instead of quitting")
60-
wailsruntime.WindowHide(ctx)
61-
return true
58+
if app.ConfirmQuit() {
59+
logger.Infof("User confirmed quit")
60+
return false
6261
}
63-
logger.Infof("Application closing")
64-
return false
62+
63+
logger.Infof("Close requested, minimizing window instead of quitting")
64+
wailsruntime.WindowMinimise(ctx)
65+
return true
6566
},
6667
BackgroundColour: &options.RGBA{R: 255, G: 255, B: 255, A: 1},
6768
OnStartup: app.startup,

menu.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ func createMenu(app *App) *menu.Menu {
2020
})
2121

2222
appMenu.AddText("Quit", nil, func(_ *menu.CallbackData) {
23-
handleQuit(app)
23+
logger.Infof("Quit requested by user")
24+
wailsruntime.Quit(app.ctx)
2425
})
2526

2627
return mainMenu
@@ -43,9 +44,7 @@ func handleAutoStartToggle(app *App, cb *menu.CallbackData) {
4344
}
4445
}
4546

46-
func handleQuit(app *App) {
47-
logger.Debug("Quit menu item selected")
48-
47+
func (app *App) ConfirmQuit() bool {
4948
result, err := wailsruntime.MessageDialog(app.ctx, wailsruntime.MessageDialogOptions{
5049
Type: wailsruntime.QuestionDialog,
5150
Title: "Quit ePOS Proxy",
@@ -56,14 +55,14 @@ func handleQuit(app *App) {
5655

5756
if err != nil {
5857
logger.Errorf("Failed to show quit dialog: %v", err)
59-
return
58+
return false
6059
}
6160

62-
// linux doesnot use Buttons ovverrides and uses No | Yes for quetion dialog
61+
// linux doesn't use Buttons overrides and uses No | Yes for question dialog
6362
if result != "Yes" && result != "Quit" {
64-
return
63+
return false
6564
}
6665

6766
logger.Debug("Confirmed quit action")
68-
app.Quit()
67+
return true
6968
}

0 commit comments

Comments
 (0)