Skip to content

Commit

Permalink
dexc-desktop: checkbox to persist after window close
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Jun 25, 2023
1 parent 46d740e commit 36a9a1a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 14 deletions.
71 changes: 63 additions & 8 deletions client/cmd/dexc-desktop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import (
"runtime/debug"
"runtime/pprof"
"sync"
"sync/atomic"
"syscall"
"time"

Expand Down Expand Up @@ -335,6 +336,9 @@ func mainCore() error {
var backgroundNoteSent bool
select {
case <-windowManager.zeroLeft:
if windowManager.persist.Load() {
continue // ignore
}
logout:
for {
err := clientCore.Logout()
Expand Down Expand Up @@ -374,8 +378,29 @@ func mainCore() error {
}
}()

activeState := make(chan bool, 8)
wg.Add(1)
go func() {
defer wg.Done()
active := true // starting with "Force Quit"
for {
select {
case <-time.After(time.Second):
// inelegant polling? Even Core doesn't know until it checks, so
// can't feasibly rig a signal from Core without changes there.
coreActive := clientCore.Active()
if coreActive != active {
active = coreActive
activeState <- coreActive
}
case <-appCtx.Done():
return
}
}
}()

systray.Run(func() {
systrayOnReady(appCtx, filepath.Dir(cfg.LogPath), openC, killChan)
systrayOnReady(appCtx, filepath.Dir(cfg.LogPath), openC, killChan, activeState)
}, nil)

closeAllWindows()
Expand All @@ -392,6 +417,7 @@ var windowManager = &struct {
counter uint32
windows map[uint32]*exec.Cmd
zeroLeft chan struct{}
persist atomic.Bool
}{
windows: make(map[uint32]*exec.Cmd),
zeroLeft: make(chan struct{}, 1),
Expand Down Expand Up @@ -486,7 +512,8 @@ var FavIcon []byte
//go:embed src/symbol-bw-round.png
var SymbolBWIcon []byte

func systrayOnReady(ctx context.Context, logDirectory string, openC chan<- struct{}, killC chan<- os.Signal) {
func systrayOnReady(ctx context.Context, logDirectory string, openC chan<- struct{},
killC chan<- os.Signal, activeState <-chan bool) {
systray.SetIcon(FavIcon)
systray.SetTitle("DEX client")
systray.SetTooltip("Self-custodial multi-wallet")
Expand Down Expand Up @@ -529,20 +556,48 @@ func systrayOnReady(ctx context.Context, logDirectory string, openC chan<- struc
mLogs := systray.AddMenuItem("Open logs folder", "Open the folder with your DEX logs.")
go func() {
for range mLogs.ClickedCh {
log.Debug("Opening browswer to log directory at", logDirURL)
log.Debug("Opening browser to log directory at", logDirURL)
runWebviewSubprocess(ctx, logDirURL)
}
}()
}

systray.AddSeparator()

mQuit := systray.AddMenuItem("Force Quit", "Force DEX client to close.")
mPersist := systray.AddMenuItemCheckbox("Persist after windows closed.",
"Keep the process running after all windows are closed. "+
"Shutdown is initiated through the Quit item in the system tray menu.", false)
go func() {
for range mPersist.ClickedCh {
var persisting = !mPersist.Checked() // toggle
if persisting {
mPersist.Check()
} else {
mPersist.Uncheck()
}
windowManager.persist.Store(persisting)
}
}()

mQuit := systray.AddMenuItem("Force Quit", "Force DEX client to close with active orders.")
go func() {
<-mQuit.ClickedCh
mOpen.Disable()
mQuit.Disable()
killC <- os.Interrupt
for {
select {
case active := <-activeState:
if active {
mQuit.SetTitle("Force Quit")
mQuit.SetTooltip("Force DEX client to close with active orders.")
} else {
mQuit.SetTitle("Quit")
mQuit.SetTooltip("Shutdown the DEX client. You have no active orders.")
}
case <-mQuit.ClickedCh:
mOpen.Disable()
mQuit.Disable()
killC <- os.Interrupt
return
}
}
}()
}

Expand Down
20 changes: 14 additions & 6 deletions client/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4762,6 +4762,17 @@ func (c *Core) initializePrimaryCredentials(pw []byte, oldKeyParams []byte) erro
return nil
}

// Active indicates if there are any active orders across all configured
// accounts. This includes booked orders and trades that are settling.
func (c *Core) Active() bool {
for _, dc := range c.dexConnections() {
if dc.hasActiveOrders() {
return true
}
}
return false
}

// Logout logs the user out
func (c *Core) Logout() error {
c.loginMtx.Lock()
Expand All @@ -4772,11 +4783,8 @@ func (c *Core) Logout() error {
}

// Check active orders
conns := c.dexConnections()
for _, dc := range conns {
if dc.hasActiveOrders() {
return codedError(activeOrdersErr, ActiveOrdersLogoutErr)
}
if c.Active() {
return codedError(activeOrdersErr, ActiveOrdersLogoutErr)
}

// Lock wallets
Expand All @@ -4794,7 +4802,7 @@ func (c *Core) Logout() error {

// With no open orders for any of the dex connections, and all wallets locked,
// lock each dex account.
for _, dc := range conns {
for _, dc := range c.dexConnections() {
dc.acct.lock()
}

Expand Down

0 comments on commit 36a9a1a

Please sign in to comment.