From 5996e84c044aa6b9bcfedd694fb5f297629c56e8 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 29 Apr 2019 15:52:17 +0200 Subject: service: pass global state with notification Signed-off-by: Jason A. Donenfeld --- ui/tray.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'ui/tray.go') diff --git a/ui/tray.go b/ui/tray.go index ca839a83..b2a6446b 100644 --- a/ui/tray.go +++ b/ui/tray.go @@ -92,7 +92,8 @@ func (tray *Tray) setup() error { tray.tunnelChangedCB = service.IPCClientRegisterTunnelChange(tray.onTunnelChange) tray.tunnelsChangedCB = service.IPCClientRegisterTunnelsChange(tray.onTunnelsChange) tray.onTunnelsChange() - tray.updateGlobalState() + globalState, _ := service.IPCClientGlobalState() + tray.updateGlobalState(globalState) return nil } @@ -185,22 +186,18 @@ func (tray *Tray) removeTunnelAction(tunnelName string) { delete(tray.tunnels, tunnelName) } -func (tray *Tray) onTunnelChange(tunnel *service.Tunnel, state service.TunnelState, err error) { +func (tray *Tray) onTunnelChange(tunnel *service.Tunnel, state service.TunnelState, globalState service.TunnelState, err error) { tray.mtw.Synchronize(func() { tray.SetTunnelState(tunnel, state, err == nil) + tray.updateGlobalState(globalState) if !tray.mtw.Visible() && err != nil { tray.ShowError("WireGuard Tunnel Error", err.Error()) } }) } -func (tray *Tray) updateGlobalState() { - state, err := service.IPCClientGlobalState() - if err != nil { - return - } - - if icon, err := iconProvider.IconWithOverlayForState(state); err == nil { +func (tray *Tray) updateGlobalState(globalState service.TunnelState) { + if icon, err := iconProvider.IconWithOverlayForState(globalState); err == nil { tray.SetIcon(icon) } @@ -215,14 +212,14 @@ func (tray *Tray) updateGlobalState() { } } - switch state { + switch globalState { case service.TunnelStarting: statusAction.SetText("Status: Activating") setTunnelActionsEnabled(false) tray.SetToolTip("WireGuard: Activating...") case service.TunnelStarted: - activeCIDRsAction.SetVisible(err == nil) + activeCIDRsAction.SetVisible(true) statusAction.SetText("Status: Active") setTunnelActionsEnabled(true) tray.SetToolTip("WireGuard: Activated") @@ -275,8 +272,6 @@ func (tray *Tray) SetTunnelState(tunnel *service.Tunnel, state service.TunnelSta tray.ShowInfo("WireGuard Deactivated", fmt.Sprintf("The %s tunnel has been deactivated.", tunnel.Name)) } } - - tray.updateGlobalState() } func (tray *Tray) UpdateFound() { -- cgit v1.2.3-59-g8ed1b