macOS: Ability to activate / deactivate a tunnel
This commit is contained in:
parent
c1a5fb7ddd
commit
e61da844bd
|
@ -18,10 +18,29 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||||
self.statusItem = createStatusBarItem(with: statusMenu)
|
self.statusItem = createStatusBarItem(with: statusMenu)
|
||||||
|
|
||||||
tunnelsManager.tunnelsListDelegate = statusMenu
|
tunnelsManager.tunnelsListDelegate = statusMenu
|
||||||
|
tunnelsManager.activationDelegate = self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension AppDelegate: TunnelsManagerActivationDelegate {
|
||||||
|
func tunnelActivationAttemptFailed(tunnel: TunnelContainer, error: TunnelsManagerActivationAttemptError) {
|
||||||
|
ErrorPresenter.showErrorAlert(error: error, from: nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tunnelActivationAttemptSucceeded(tunnel: TunnelContainer) {
|
||||||
|
// Nothing to do
|
||||||
|
}
|
||||||
|
|
||||||
|
func tunnelActivationFailed(tunnel: TunnelContainer, error: TunnelsManagerActivationError) {
|
||||||
|
ErrorPresenter.showErrorAlert(error: error, from: nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tunnelActivationSucceeded(tunnel: TunnelContainer) {
|
||||||
|
// Nothing to do
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createStatusBarItem(with statusMenu: StatusMenu) -> NSStatusItem {
|
func createStatusBarItem(with statusMenu: StatusMenu) -> NSStatusItem {
|
||||||
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
|
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
|
||||||
if let statusBarImage = NSImage(named: "WireGuardMacStatusBarIcon") {
|
if let statusBarImage = NSImage(named: "WireGuardMacStatusBarIcon") {
|
||||||
|
|
|
@ -6,7 +6,10 @@ import Cocoa
|
||||||
class StatusMenu: NSMenu {
|
class StatusMenu: NSMenu {
|
||||||
|
|
||||||
let tunnelsManager: TunnelsManager
|
let tunnelsManager: TunnelsManager
|
||||||
|
var tunnelStatusObservers = [AnyObject]()
|
||||||
|
|
||||||
var firstTunnelMenuItemIndex: Int = 0
|
var firstTunnelMenuItemIndex: Int = 0
|
||||||
|
var numberOfTunnelMenuItems: Int = 0
|
||||||
|
|
||||||
init(tunnelsManager: TunnelsManager) {
|
init(tunnelsManager: TunnelsManager) {
|
||||||
self.tunnelsManager = tunnelsManager
|
self.tunnelsManager = tunnelsManager
|
||||||
|
@ -27,24 +30,11 @@ class StatusMenu: NSMenu {
|
||||||
let numberOfTunnels = tunnelsManager.numberOfTunnels()
|
let numberOfTunnels = tunnelsManager.numberOfTunnels()
|
||||||
for index in 0 ..< tunnelsManager.numberOfTunnels() {
|
for index in 0 ..< tunnelsManager.numberOfTunnels() {
|
||||||
let tunnel = tunnelsManager.tunnel(at: index)
|
let tunnel = tunnelsManager.tunnel(at: index)
|
||||||
let menuItem = createTunnelMenuItem(for: tunnel)
|
insertTunnelMenuItem(for: tunnel, at: numberOfTunnelMenuItems)
|
||||||
addItem(menuItem)
|
|
||||||
}
|
}
|
||||||
return numberOfTunnels > 0
|
return numberOfTunnels > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTunnelMenuItem(for tunnel: TunnelContainer) -> NSMenuItem {
|
|
||||||
let menuItem = NSMenuItem(title: tunnel.name, action: #selector(tunnelClicked(sender:)), keyEquivalent: "")
|
|
||||||
menuItem.target = self
|
|
||||||
menuItem.representedObject = tunnel
|
|
||||||
return menuItem
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc func tunnelClicked(sender: AnyObject) {
|
|
||||||
guard let tunnel = sender.representedObject as? TunnelContainer else { return }
|
|
||||||
print("Tunnel \(tunnel.name) clicked")
|
|
||||||
}
|
|
||||||
|
|
||||||
func addTunnelManagementItems() {
|
func addTunnelManagementItems() {
|
||||||
let manageItem = NSMenuItem(title: tr("macMenuManageTunnels"), action: #selector(manageTunnelsClicked), keyEquivalent: "")
|
let manageItem = NSMenuItem(title: tr("macMenuManageTunnels"), action: #selector(manageTunnelsClicked), keyEquivalent: "")
|
||||||
manageItem.target = self
|
manageItem.target = self
|
||||||
|
@ -54,6 +44,16 @@ class StatusMenu: NSMenu {
|
||||||
addItem(importItem)
|
addItem(importItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc func tunnelClicked(sender: AnyObject) {
|
||||||
|
guard let tunnelMenuItem = sender as? NSMenuItem else { return }
|
||||||
|
guard let tunnel = tunnelMenuItem.representedObject as? TunnelContainer else { return }
|
||||||
|
if tunnelMenuItem.state == .off {
|
||||||
|
tunnelsManager.startActivation(of: tunnel)
|
||||||
|
} else {
|
||||||
|
tunnelsManager.startDeactivation(of: tunnel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@objc func manageTunnelsClicked() {
|
@objc func manageTunnelsClicked() {
|
||||||
print("Unimplemented")
|
print("Unimplemented")
|
||||||
}
|
}
|
||||||
|
@ -70,36 +70,72 @@ class StatusMenu: NSMenu {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension StatusMenu: TunnelsManagerListDelegate {
|
extension StatusMenu {
|
||||||
func tunnelAdded(at index: Int) {
|
func insertTunnelMenuItem(for tunnel: TunnelContainer, at tunnelIndex: Int) {
|
||||||
let tunnel = tunnelsManager.tunnel(at: index)
|
let menuItem = NSMenuItem(title: tunnel.name, action: #selector(tunnelClicked(sender:)), keyEquivalent: "")
|
||||||
let menuItem = createTunnelMenuItem(for: tunnel)
|
menuItem.target = self
|
||||||
if tunnelsManager.numberOfTunnels() == 1 {
|
menuItem.representedObject = tunnel
|
||||||
insertItem(NSMenuItem.separator(), at: firstTunnelMenuItemIndex + index)
|
updateTunnelMenuItem(menuItem)
|
||||||
|
let statusObservationToken = tunnel.observe(\.status) { _, _ in
|
||||||
|
updateTunnelMenuItem(menuItem)
|
||||||
}
|
}
|
||||||
insertItem(menuItem, at: firstTunnelMenuItemIndex + index)
|
tunnelStatusObservers.insert(statusObservationToken, at: tunnelIndex)
|
||||||
|
insertItem(menuItem, at: firstTunnelMenuItemIndex + tunnelIndex)
|
||||||
|
if numberOfTunnelMenuItems == 0 {
|
||||||
|
insertItem(NSMenuItem.separator(), at: firstTunnelMenuItemIndex + tunnelIndex + 1)
|
||||||
|
}
|
||||||
|
numberOfTunnelMenuItems += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
func tunnelModified(at index: Int) {
|
func removeTunnelMenuItem(at tunnelIndex: Int) {
|
||||||
let tunnel = tunnelsManager.tunnel(at: index)
|
removeItem(at: firstTunnelMenuItemIndex + tunnelIndex)
|
||||||
if let menuItem = item(at: firstTunnelMenuItemIndex + index) {
|
tunnelStatusObservers.remove(at: tunnelIndex)
|
||||||
menuItem.title = tunnel.name
|
numberOfTunnelMenuItems -= 1
|
||||||
}
|
if numberOfTunnelMenuItems == 0 {
|
||||||
}
|
|
||||||
|
|
||||||
func tunnelMoved(from oldIndex: Int, to newIndex: Int) {
|
|
||||||
let tunnel = tunnelsManager.tunnel(at: oldIndex)
|
|
||||||
let menuItem = createTunnelMenuItem(for: tunnel)
|
|
||||||
removeItem(at: firstTunnelMenuItemIndex + oldIndex)
|
|
||||||
insertItem(menuItem, at: firstTunnelMenuItemIndex + newIndex)
|
|
||||||
}
|
|
||||||
|
|
||||||
func tunnelRemoved(at index: Int) {
|
|
||||||
removeItem(at: firstTunnelMenuItemIndex + index)
|
|
||||||
if tunnelsManager.numberOfTunnels() == 0 {
|
|
||||||
if let firstItem = item(at: firstTunnelMenuItemIndex), firstItem.isSeparatorItem {
|
if let firstItem = item(at: firstTunnelMenuItemIndex), firstItem.isSeparatorItem {
|
||||||
removeItem(at: firstTunnelMenuItemIndex)
|
removeItem(at: firstTunnelMenuItemIndex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func moveTunnelMenuItem(from oldTunnelIndex: Int, to newTunnelIndex: Int) {
|
||||||
|
let oldMenuItem = item(at: firstTunnelMenuItemIndex + oldTunnelIndex)!
|
||||||
|
let oldMenuItemTitle = oldMenuItem.title
|
||||||
|
let oldMenuItemTunnel = oldMenuItem.representedObject
|
||||||
|
removeItem(at: firstTunnelMenuItemIndex + oldTunnelIndex)
|
||||||
|
let menuItem = NSMenuItem(title: oldMenuItemTitle, action: #selector(tunnelClicked(sender:)), keyEquivalent: "")
|
||||||
|
menuItem.target = self
|
||||||
|
menuItem.representedObject = oldMenuItemTunnel
|
||||||
|
insertItem(menuItem, at: firstTunnelMenuItemIndex + newTunnelIndex)
|
||||||
|
let statusObserver = tunnelStatusObservers.remove(at: oldTunnelIndex)
|
||||||
|
tunnelStatusObservers.insert(statusObserver, at: newTunnelIndex)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func updateTunnelMenuItem(_ tunnelMenuItem: NSMenuItem) {
|
||||||
|
guard let tunnel = tunnelMenuItem.representedObject as? TunnelContainer else { return }
|
||||||
|
tunnelMenuItem.title = tunnel.name
|
||||||
|
let shouldShowCheckmark = (tunnel.status != .inactive && tunnel.status != .deactivating)
|
||||||
|
tunnelMenuItem.state = shouldShowCheckmark ? .on : .off
|
||||||
|
}
|
||||||
|
|
||||||
|
extension StatusMenu: TunnelsManagerListDelegate {
|
||||||
|
func tunnelAdded(at index: Int) {
|
||||||
|
let tunnel = tunnelsManager.tunnel(at: index)
|
||||||
|
insertTunnelMenuItem(for: tunnel, at: index)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tunnelModified(at index: Int) {
|
||||||
|
if let tunnelMenuItem = item(at: firstTunnelMenuItemIndex + index) {
|
||||||
|
updateTunnelMenuItem(tunnelMenuItem)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tunnelMoved(from oldIndex: Int, to newIndex: Int) {
|
||||||
|
moveTunnelMenuItem(from: oldIndex, to: newIndex)
|
||||||
|
}
|
||||||
|
|
||||||
|
func tunnelRemoved(at index: Int) {
|
||||||
|
removeTunnelMenuItem(at: index)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue