macOS: Add 'Deactivate' status menu item

This commit is contained in:
Roopesh Chander 2019-03-19 15:35:15 +05:30 committed by Jason A. Donenfeld
parent 19efbcf679
commit dbeb389d26
1 changed files with 15 additions and 1 deletions

View File

@ -13,6 +13,7 @@ class StatusMenu: NSMenu {
var statusMenuItem: NSMenuItem?
var networksMenuItem: NSMenuItem?
var deactivateMenuItem: NSMenuItem?
var firstTunnelMenuItemIndex = 0
var numberOfTunnelMenuItems = 0
@ -53,16 +54,22 @@ class StatusMenu: NSMenu {
networksMenuItem.isEnabled = false
networksMenuItem.isHidden = true
addItem(networksMenuItem)
let deactivateMenuItem = NSMenuItem(title: tr("macToggleStatusButtonDeactivate"), action: #selector(deactivateClicked), keyEquivalent: "")
deactivateMenuItem.target = self
deactivateMenuItem.isHidden = true
addItem(deactivateMenuItem)
self.statusMenuItem = statusMenuItem
self.networksMenuItem = networksMenuItem
self.deactivateMenuItem = deactivateMenuItem
}
func updateStatusMenuItems(with tunnel: TunnelContainer?) {
guard let statusMenuItem = statusMenuItem, let networksMenuItem = networksMenuItem else { return }
guard let statusMenuItem = statusMenuItem, let networksMenuItem = networksMenuItem, let deactivateMenuItem = deactivateMenuItem else { return }
guard let tunnel = tunnel else {
statusMenuItem.title = tr(format: "macStatus (%@)", tr("tunnelStatusInactive"))
networksMenuItem.title = ""
networksMenuItem.isHidden = true
deactivateMenuItem.isHidden = true
return
}
var statusText: String
@ -98,6 +105,7 @@ class StatusMenu: NSMenu {
}
networksMenuItem.isHidden = false
}
deactivateMenuItem.isHidden = tunnel.status != .active
}
func addTunnelMenuItems() -> Bool {
@ -127,6 +135,12 @@ class StatusMenu: NSMenu {
addItem(quitItem)
}
@objc func deactivateClicked() {
if let currentTunnel = currentTunnel {
tunnelsManager.startDeactivation(of: currentTunnel)
}
}
@objc func tunnelClicked(sender: AnyObject) {
guard let tunnelMenuItem = sender as? TunnelMenuItem else { return }
if tunnelMenuItem.state == .off {