aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorAlexander Neumann <alexander.neumann@picos-software.com>2019-04-08 15:33:00 +0200
committerAlexander Neumann <alexander.neumann@picos-software.com>2019-04-23 11:04:59 +0200
commitea53b8bb8b2f60f6ed5c26a720d614ac530eb0af (patch)
tree7c962a0d6aad06aa37c8fa3f537aca7754fa577b
parentui: replace PrivateKeyEvent with walk.StringEvent (diff)
downloadwireguard-windows-ea53b8bb8b2f60f6ed5c26a720d614ac530eb0af.tar.xz
wireguard-windows-ea53b8bb8b2f60f6ed5c26a720d614ac530eb0af.zip
ui: handle adding and removing of tunnels for tray
Signed-off-by: Alexander Neumann <alexander.neumann@picos-software.com> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to '')
-rw-r--r--ui/manage_tunnels.go20
-rw-r--r--ui/tray.go101
2 files changed, 77 insertions, 44 deletions
diff --git a/ui/manage_tunnels.go b/ui/manage_tunnels.go
index 662de850..4a3985d0 100644
--- a/ui/manage_tunnels.go
+++ b/ui/manage_tunnels.go
@@ -25,8 +25,10 @@ type ManageTunnelsWindow struct {
icon *walk.Icon
- tunnelsView *TunnelsView
- confView *ConfView
+ tunnelsView *TunnelsView
+ confView *ConfView
+ tunnelAddedPublisher walk.StringEventPublisher
+ tunnelDeletedPublisher walk.StringEventPublisher
}
func NewManageTunnelsWindow(icon *walk.Icon) (*ManageTunnelsWindow, error) {
@@ -417,6 +419,8 @@ func (mtw *ManageTunnelsWindow) addTunnel(config *conf.Config) {
}
mtw.confView.SetTunnel(&tunnel)
+
+ mtw.tunnelAddedPublisher.Publish(tunnel.Name)
}
func (mtw *ManageTunnelsWindow) deleteTunnel(tunnel *service.Tunnel) {
@@ -431,6 +435,16 @@ func (mtw *ManageTunnelsWindow) deleteTunnel(tunnel *service.Tunnel) {
break
}
}
+
+ mtw.tunnelDeletedPublisher.Publish(tunnel.Name)
+}
+
+func (mtw *ManageTunnelsWindow) TunnelAdded() *walk.StringEvent {
+ return mtw.tunnelAddedPublisher.Event()
+}
+
+func (mtw *ManageTunnelsWindow) TunnelDeleted() *walk.StringEvent {
+ return mtw.tunnelDeletedPublisher.Event()
}
// Handlers
@@ -474,6 +488,8 @@ func (mtw *ManageTunnelsWindow) onDelete() {
}
mtw.deleteTunnel(currentTunnel)
+
+ mtw.tunnelDeletedPublisher.Publish(currentTunnel.Name)
}
func (mtw *ManageTunnelsWindow) onImport() {
diff --git a/ui/tray.go b/ui/tray.go
index a7133638..e7e1758a 100644
--- a/ui/tray.go
+++ b/ui/tray.go
@@ -20,19 +20,19 @@ type Tray struct {
// Current known tunnels by name
tunnels map[string]*walk.Action
- parent *ManageTunnelsWindow
- icon *walk.Icon
+ mtw *ManageTunnelsWindow
+ icon *walk.Icon
}
-func NewTray(parent *ManageTunnelsWindow, icon *walk.Icon) (*Tray, error) {
+func NewTray(mtw *ManageTunnelsWindow, icon *walk.Icon) (*Tray, error) {
var err error
tray := &Tray{
- parent: parent,
+ mtw: mtw,
icon: icon,
tunnels: make(map[string]*walk.Action),
}
- tray.NotifyIcon, err = walk.NewNotifyIcon(parent.MainWindow)
+ tray.NotifyIcon, err = walk.NewNotifyIcon(mtw.MainWindow)
if err != nil {
return nil, err
}
@@ -47,7 +47,7 @@ func (tray *Tray) setup() error {
tray.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
if button == walk.LeftButton {
- tray.parent.Show()
+ tray.mtw.Show()
}
})
@@ -62,8 +62,8 @@ func (tray *Tray) setup() error {
{label: "Networks: None"},
{separator: true},
{separator: true},
- {label: "&Manage tunnels...", handler: tray.parent.Show, enabled: true},
- {label: "&Import tunnel(s) from file...", handler: tray.parent.onImport, enabled: true},
+ {label: "&Manage tunnels...", handler: tray.mtw.Show, enabled: true},
+ {label: "&Import tunnel(s) from file...", handler: tray.mtw.onImport, enabled: true},
{separator: true},
{label: "&About WireGuard", handler: onAbout, enabled: true},
{label: "&Quit", handler: onQuit, enabled: true},
@@ -83,48 +83,65 @@ func (tray *Tray) setup() error {
tray.ContextMenu().Actions().Add(action)
}
+ tunnels, err := service.IPCClientTunnels()
+ if err != nil {
+ return err
+ }
+
+ for _, tunnel := range tunnels {
+ tray.addTunnelAction(tunnel.Name)
+ }
+
+ tray.mtw.TunnelAdded().Attach(tray.addTunnelAction)
+ tray.mtw.TunnelDeleted().Attach(tray.removeTunnelAction)
+
return nil
}
-func (tray *Tray) SetTunnelState(tunnel *service.Tunnel, state service.TunnelState) {
- tray.SetTunnelStateWithNotification(tunnel, state, true)
-}
+func (tray *Tray) addTunnelAction(tunnelName string) {
+ tunnelAction := walk.NewAction()
+ tunnelAction.SetText(tunnelName)
+ tunnelAction.SetEnabled(true)
+ tunnelAction.SetCheckable(true)
+ tunnelAction.Triggered().Attach(func() {
-func (tray *Tray) SetTunnelStateWithNotification(tunnel *service.Tunnel, state service.TunnelState, showNotifications bool) {
- tunnelAction, ok := tray.tunnels[tunnel.Name]
- if !ok {
- // First time seeing this tunnel, create a new action
- tunnelAction = walk.NewAction()
- tunnelAction.SetText(tunnel.Name)
- tunnelAction.SetEnabled(true)
- tunnelAction.SetCheckable(true)
- // TODO: Wire up the click event
- tray.tunnels[tunnel.Name] = tunnelAction
-
- // Add the action at the right spot
- var names []string
- for name, _ := range tray.tunnels {
- names = append(names, name)
- }
- sort.Strings(names)
-
- var (
- idx int
- name string
- )
- for idx, name = range names {
- if name == tunnel.Name {
- break
- }
+ })
+ tray.tunnels[tunnelName] = tunnelAction
+
+ // Add the action at the right spot
+ var names []string
+ for name, _ := range tray.tunnels {
+ names = append(names, name)
+ }
+ sort.Strings(names)
+
+ var (
+ idx int
+ name string
+ )
+ for idx, name = range names {
+ if name == tunnelName {
+ break
}
+ }
- // Status + active CIDRs + separator
- const offset = 3
+ // Status + active CIDRs + separator
+ const offset = 3
- tray.ContextMenu().Actions().Insert(idx+offset, tunnelAction)
- }
+ tray.ContextMenu().Actions().Insert(offset+idx, tunnelAction)
+}
- // TODO: No event for deleting a tunnel?
+func (tray *Tray) removeTunnelAction(tunnelName string) {
+ tray.ContextMenu().Actions().Remove(tray.tunnels[tunnelName])
+ delete(tray.tunnels, tunnelName)
+}
+
+func (tray *Tray) SetTunnelState(tunnel *service.Tunnel, state service.TunnelState) {
+ tray.SetTunnelStateWithNotification(tunnel, state, true)
+}
+
+func (tray *Tray) SetTunnelStateWithNotification(tunnel *service.Tunnel, state service.TunnelState, showNotifications bool) {
+ tunnelAction := tray.tunnels[tunnel.Name]
actions := tray.ContextMenu().Actions()
statusAction := actions.At(0)