Skip to content

Commit 796ce73

Browse files
authored
internal/tray: change connection status item to be a connecction toggle instead (#138)
* internal/tray: change connection status item to eb a connection toggle * internal/ui: hook up tray menu connection toggle * meta: add v0.12.7 to metainfo
1 parent 5b60334 commit 796ce73

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

Diff for: dev.deedles.Trayscale.metainfo.xml

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
<content_rating type="oars-1.1" />
5555

5656
<releases>
57+
<release version="v0.12.7" date="2024-07-09">
58+
<description>
59+
<ul>Add a connection toggle to the tray icon menu.</ul>
60+
</description>
61+
</release>
5762
<release version="v0.12.6" date="2024-07-03">
5863
<description>
5964
<ul>Add online status indicators to peer list.</ul>

Diff for: internal/tray/tray.go

+21-18
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,50 @@ func statusIcon(online bool) []byte {
2424
}
2525

2626
type Tray struct {
27-
showItem *systray.MenuItem
28-
quitItem *systray.MenuItem
29-
selfNodeItem *systray.MenuItem
30-
connectionStatusMenuItem *systray.MenuItem
27+
connToggleItem *systray.MenuItem
28+
selfNodeItem *systray.MenuItem
29+
showItem *systray.MenuItem
30+
quitItem *systray.MenuItem
3131
}
3232

3333
func New(online bool) *Tray {
3434
systray.SetIcon(statusIcon(online))
3535
systray.SetTitle("Trayscale")
3636

37+
connToggleItem := systray.AddMenuItem(connToggleText(online), "")
3738
selfNodeItem := systray.AddMenuItem("", "")
38-
connectionStatusMenuItem := systray.AddMenuItem(connectionStatusText(online), "")
39-
connectionStatusMenuItem.Disable()
4039
systray.AddSeparator()
4140
showWindow := systray.AddMenuItem("Show", "")
4241
systray.AddSeparator()
4342
quit := systray.AddMenuItem("Quit", "")
4443

4544
return &Tray{
46-
showItem: showWindow,
47-
quitItem: quit,
48-
selfNodeItem: selfNodeItem,
49-
connectionStatusMenuItem: connectionStatusMenuItem,
45+
connToggleItem: connToggleItem,
46+
selfNodeItem: selfNodeItem,
47+
showItem: showWindow,
48+
quitItem: quit,
5049
}
5150
}
5251

53-
func (t *Tray) QuitChan() <-chan struct{} {
54-
return t.quitItem.ClickedCh
52+
func (t *Tray) ConnToggleChan() <-chan struct{} {
53+
return t.connToggleItem.ClickedCh
54+
}
55+
56+
func (t *Tray) SelfNodeChan() <-chan struct{} {
57+
return t.selfNodeItem.ClickedCh
5558
}
5659

5760
func (t *Tray) ShowChan() <-chan struct{} {
5861
return t.showItem.ClickedCh
5962
}
6063

61-
func (t *Tray) SelfNodeChan() <-chan struct{} {
62-
return t.selfNodeItem.ClickedCh
64+
func (t *Tray) QuitChan() <-chan struct{} {
65+
return t.quitItem.ClickedCh
6366
}
6467

6568
func (t *Tray) setOnlineStatus(online bool) {
6669
systray.SetIcon(statusIcon(online))
67-
t.connectionStatusMenuItem.SetTitle(connectionStatusText(online))
70+
t.connToggleItem.SetTitle(connToggleText(online))
6871
}
6972

7073
func (t *Tray) Update(s tsutil.Status, previousOnlineStatus bool) {
@@ -119,10 +122,10 @@ func selfTitle(s tsutil.Status) (string, bool) {
119122
return fmt.Sprintf("%v (%v)", tsutil.DNSOrQuoteHostname(s.Status, s.Status.Self), addr), true
120123
}
121124

122-
func connectionStatusText(online bool) string {
125+
func connToggleText(online bool) string {
123126
if online {
124-
return "Connected"
127+
return "Disconnect"
125128
}
126129

127-
return "Disconnected"
130+
return "Connect"
128131
}

Diff for: internal/ui/app.go

+30-11
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,36 @@ func (a *App) initTray(ctx context.Context) {
356356
case <-ctx.Done():
357357
return
358358

359+
case <-a.tray.ConnToggleChan():
360+
glib.IdleAdd(func() {
361+
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
362+
defer cancel()
363+
364+
f := a.stopTS
365+
if !a.online {
366+
f = a.startTS
367+
}
368+
369+
err := f(ctx)
370+
if err != nil {
371+
slog.Error("set Tailscale status from tray", "err", err)
372+
return
373+
}
374+
})
375+
376+
case <-a.tray.SelfNodeChan():
377+
glib.IdleAdd(func() {
378+
s := <-a.poller.Get()
379+
addr, ok := s.SelfAddr()
380+
if !ok {
381+
return
382+
}
383+
a.clip(glib.NewValue(addr.String()))
384+
if a.win != nil {
385+
a.notify("Trayscale", "Copied address to clipboard")
386+
}
387+
})
388+
359389
case <-a.tray.ShowChan():
360390
glib.IdleAdd(func() {
361391
if a.app != nil {
@@ -365,17 +395,6 @@ func (a *App) initTray(ctx context.Context) {
365395

366396
case <-a.tray.QuitChan():
367397
a.Quit()
368-
369-
case <-a.tray.SelfNodeChan():
370-
s := <-a.poller.Get()
371-
addr, ok := s.SelfAddr()
372-
if !ok {
373-
continue
374-
}
375-
a.clip(glib.NewValue(addr.String()))
376-
if a.win != nil {
377-
a.notify("Trayscale", "Copied address to clipboard")
378-
}
379398
}
380399
}
381400
}

0 commit comments

Comments
 (0)