Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 61 additions & 6 deletions internal/tray/tray.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import (
"unique"

"deedles.dev/tray"
"deedles.dev/trayscale/internal/metadata"
"deedles.dev/trayscale/internal/tsutil"
"tailscale.com/ipn"
)

var (
Expand Down Expand Up @@ -48,11 +50,12 @@ func handler(f func()) tray.MenuItemProp {
}

type Tray struct {
OnShow func()
OnConnToggle func()
OnExitToggle func()
OnSelfNode func()
OnQuit func()
OnShow func()
OnConnToggle func()
OnExitToggle func()
OnSelfNode func()
OnProfileSwitch func(ipn.ProfileID)
OnQuit func()

m sync.Mutex
item *tray.Item
Expand All @@ -62,10 +65,12 @@ type Tray struct {
connToggleItem *tray.MenuItem
exitToggleItem *tray.MenuItem
selfNodeItem *tray.MenuItem
profileItems map[ipn.ProfileID]*tray.MenuItem
profileNames map[ipn.ProfileID]string
quitItem *tray.MenuItem
}

func (t *Tray) Start(status *tsutil.IPNStatus) error {
func (t *Tray) Start(status *tsutil.IPNStatus, profiles *tsutil.ProfileStatus) error {
if t.item != nil {
return nil
}
Expand Down Expand Up @@ -94,6 +99,33 @@ func (t *Tray) Start(status *tsutil.IPNStatus) error {
t.connToggleItem, _ = menu.AddChild(handler(t.OnConnToggle))
t.exitToggleItem, _ = menu.AddChild(handler(t.OnExitToggle))
t.selfNodeItem, _ = menu.AddChild(handler(t.OnSelfNode))

t.profileItems = make(map[ipn.ProfileID]*tray.MenuItem)
t.profileNames = make(map[ipn.ProfileID]string)
if profiles != nil && len(profiles.Profiles) > 1 {
menu.AddChild(tray.MenuItemType(tray.Separator))
for _, profile := range profiles.Profiles {
id := profile.ID
name := profileName(profile)
t.profileNames[id] = name

label := " " + name
if id == profiles.Profile.ID {
label = "● " + name
}

child, _ := menu.AddChild(
tray.MenuItemLabel(label),
handler(func() {
if t.OnProfileSwitch != nil {
t.OnProfileSwitch(id)
}
}),
)
t.profileItems[id] = child
}
}

menu.AddChild(tray.MenuItemType(tray.Separator))
t.quitItem, _ = menu.AddChild(tray.MenuItemLabel("Quit"), handler(t.OnQuit))

Expand Down Expand Up @@ -220,3 +252,26 @@ func exitToggleText(status *tsutil.IPNStatus) string {

return "Enable exit node"
}

// SetActiveProfile updates the profile indicator labels. It must be
// called from a non-GTK goroutine to avoid deadlocking with D-Bus.
func (t *Tray) SetActiveProfile(id ipn.ProfileID) {
if t == nil {
return
}
for pid, item := range t.profileItems {
name := t.profileNames[pid]
label := " " + name
if pid == id {
label = "● " + name
}
item.SetProps(tray.MenuItemLabel(label))
}
}

func profileName(profile ipn.LoginProfile) string {
if metadata.Private {
return "profile@example.com"
}
return profile.Name
}
26 changes: 24 additions & 2 deletions internal/ui/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/inhies/go-bytesize"
"tailscale.com/client/tailscale/apitype"
"tailscale.com/ipn"
"tailscale.com/tailcfg"
)

Expand Down Expand Up @@ -358,7 +359,7 @@ func (a *App) onAppActivate(ctx context.Context) {

func (a *App) initTray(ctx context.Context) {
if a.tray != nil {
err := a.tray.Start(<-a.poller.GetIPN())
err := a.tray.Start(<-a.poller.GetIPN(), nil)
if err != nil {
slog.Error("failed to start tray icon", "err", err)
}
Expand Down Expand Up @@ -428,12 +429,33 @@ func (a *App) initTray(ctx context.Context) {
})
},

OnProfileSwitch: func(id ipn.ProfileID) {
go func() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

err := tsutil.SwitchProfile(ctx, id)
if err != nil {
slog.Error("switch profile from tray", "err", err)
return
}

a.tray.SetActiveProfile(id)
}()
},

OnQuit: func() {
a.Quit()
},
}

err := a.tray.Start(<-a.poller.GetIPN())
var ps *tsutil.ProfileStatus
profile, profiles, err := tsutil.GetProfileStatus(ctx)
if err == nil {
ps = &tsutil.ProfileStatus{Profile: profile, Profiles: profiles}
}

err = a.tray.Start(<-a.poller.GetIPN(), ps)
if err != nil {
slog.Error("failed to start tray icon", "err", err)
}
Expand Down