Skip to content

Commit

Permalink
fix(app): quit from tray icon runs appropriate actions
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Oct 14, 2023
1 parent 02b3ba9 commit 6681999
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 8 additions & 3 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type App struct {
tray fyne.Window
Name, Version string
showNotifications bool
Done chan struct{}
}

func New() *App {
Expand All @@ -52,12 +53,12 @@ func New() *App {
Name: Name,
Version: Version,
showNotifications: false,
Done: make(chan struct{}),
}
}

func (a *App) Run() {
appCtx, cancelfunc := context.WithCancel(context.Background())
doneCh := make(chan struct{})
handler := handler.NewHandler()
correctionsList = corrections.NewCorrections()
if err := createDir(configPath); err != nil {
Expand Down Expand Up @@ -113,10 +114,10 @@ func (a *App) Run() {
go func() {
<-c
log.Debug().Msg("Ctrl-C pressed.")
close(doneCh)
close(a.Done)
}()
go func() {
<-doneCh
<-a.Done
stats.Save()
log.Debug().Msg("Agent done.")
os.Exit(0)
Expand All @@ -132,6 +133,10 @@ func (a *App) Run() {
cancelfunc()
}

func (a *App) Stop() {
close(a.Done)
}

func createDir(path string) error {
_, err := os.Stat(path)
if os.IsNotExist(err) {
Expand Down
7 changes: 6 additions & 1 deletion internal/app/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (a *App) setupSystemTray(stats *db.Stats) {
a.tray = a.app.NewWindow("System Tray")
a.tray.SetMaster()
if desk, ok := a.app.(desktop.App); ok {
menuItemQuit := fyne.NewMenuItem("Quit", func() {
a.Stop()
})
menuItemQuit.IsQuit = true
menuItemAbout := fyne.
NewMenuItem("About",
func() {
Expand Down Expand Up @@ -91,7 +95,8 @@ func (a *App) setupSystemTray(stats *db.Stats) {
menuItemToggleNotifications,
menuItemToggleKeyTracker,
menuItemIssue,
menuItemFeatureRequest)
menuItemFeatureRequest,
menuItemQuit)
desk.SetSystemTrayMenu(menu)
}
a.tray.Hide()
Expand Down

0 comments on commit 6681999

Please sign in to comment.