macOS: Show status as disabled menu items
This commit is contained in:
parent
e61da844bd
commit
b024887052
|
@ -223,5 +223,10 @@
|
||||||
|
|
||||||
// Mac status bar menu
|
// Mac status bar menu
|
||||||
|
|
||||||
|
"macMenuStatus (%@)" = "Status: %@";
|
||||||
|
"macMenuNetworks (%@)" = "Networks: %@";
|
||||||
|
"macMenuNetworksInactive" = "Networks: -";
|
||||||
|
"macMenuNetworksNone" = "Networks: None";
|
||||||
|
|
||||||
"macMenuManageTunnels" = "Manage tunnels";
|
"macMenuManageTunnels" = "Manage tunnels";
|
||||||
"macMenuImportTunnels" = "Import tunnel(s) from file...";
|
"macMenuImportTunnels" = "Import tunnel(s) from file...";
|
||||||
|
|
|
@ -8,12 +8,24 @@ class StatusMenu: NSMenu {
|
||||||
let tunnelsManager: TunnelsManager
|
let tunnelsManager: TunnelsManager
|
||||||
var tunnelStatusObservers = [AnyObject]()
|
var tunnelStatusObservers = [AnyObject]()
|
||||||
|
|
||||||
|
var statusMenuItem: NSMenuItem?
|
||||||
|
var networksMenuItem: NSMenuItem?
|
||||||
var firstTunnelMenuItemIndex: Int = 0
|
var firstTunnelMenuItemIndex: Int = 0
|
||||||
var numberOfTunnelMenuItems: Int = 0
|
var numberOfTunnelMenuItems: Int = 0
|
||||||
|
|
||||||
init(tunnelsManager: TunnelsManager) {
|
init(tunnelsManager: TunnelsManager) {
|
||||||
self.tunnelsManager = tunnelsManager
|
self.tunnelsManager = tunnelsManager
|
||||||
super.init(title: "WireGuard Status Bar Menu")
|
super.init(title: "WireGuard Status Bar Menu")
|
||||||
|
|
||||||
|
addStatusMenuItems()
|
||||||
|
addItem(NSMenuItem.separator())
|
||||||
|
for index in 0 ..< tunnelsManager.numberOfTunnels() {
|
||||||
|
let isUpdated = updateStatusMenuItems(with: tunnelsManager.tunnel(at: index), ignoreInactive: true)
|
||||||
|
if isUpdated {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
firstTunnelMenuItemIndex = numberOfItems
|
firstTunnelMenuItemIndex = numberOfItems
|
||||||
let isAdded = addTunnelMenuItems()
|
let isAdded = addTunnelMenuItems()
|
||||||
if isAdded {
|
if isAdded {
|
||||||
|
@ -26,6 +38,55 @@ class StatusMenu: NSMenu {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func addStatusMenuItems() {
|
||||||
|
let statusTitle = tr(format: "macMenuStatus (%@)", tr("tunnelStatusInactive"))
|
||||||
|
let statusMenuItem = NSMenuItem(title: statusTitle, action: #selector(manageTunnelsClicked), keyEquivalent: "")
|
||||||
|
statusMenuItem.isEnabled = false
|
||||||
|
addItem(statusMenuItem)
|
||||||
|
let networksMenuItem = NSMenuItem(title: tr("macMenuNetworksInactive"), action: #selector(manageTunnelsClicked), keyEquivalent: "")
|
||||||
|
networksMenuItem.isEnabled = false
|
||||||
|
addItem(networksMenuItem)
|
||||||
|
self.statusMenuItem = statusMenuItem
|
||||||
|
self.networksMenuItem = networksMenuItem
|
||||||
|
}
|
||||||
|
|
||||||
|
@discardableResult
|
||||||
|
func updateStatusMenuItems(with tunnel: TunnelContainer, ignoreInactive: Bool) -> Bool {
|
||||||
|
guard let statusMenuItem = statusMenuItem, let networksMenuItem = networksMenuItem else { return false }
|
||||||
|
var statusText: String
|
||||||
|
|
||||||
|
switch tunnel.status {
|
||||||
|
case .waiting:
|
||||||
|
return false
|
||||||
|
case .inactive:
|
||||||
|
if ignoreInactive {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
statusText = tr("tunnelStatusInactive")
|
||||||
|
case .activating:
|
||||||
|
statusText = tr("tunnelStatusActivating")
|
||||||
|
case .active:
|
||||||
|
statusText = tr("tunnelStatusActive")
|
||||||
|
case .deactivating:
|
||||||
|
statusText = tr("tunnelStatusDeactivating")
|
||||||
|
case .reasserting:
|
||||||
|
statusText = tr("tunnelStatusReasserting")
|
||||||
|
case .restarting:
|
||||||
|
statusText = tr("tunnelStatusRestarting")
|
||||||
|
}
|
||||||
|
|
||||||
|
statusMenuItem.title = tr(format: "macMenuStatus (%@)", statusText)
|
||||||
|
|
||||||
|
let addresses = tunnel.tunnelConfiguration?.interface.addresses ?? []
|
||||||
|
let addressesString = addresses.map { $0.stringRepresentation }.joined(separator: ", ")
|
||||||
|
if addressesString.isEmpty {
|
||||||
|
networksMenuItem.title = tr("macMenuNetworksNone")
|
||||||
|
} else {
|
||||||
|
networksMenuItem.title = tr(format: "macMenuNetworks (%@)", addressesString)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func addTunnelMenuItems() -> Bool {
|
func addTunnelMenuItems() -> Bool {
|
||||||
let numberOfTunnels = tunnelsManager.numberOfTunnels()
|
let numberOfTunnels = tunnelsManager.numberOfTunnels()
|
||||||
for index in 0 ..< tunnelsManager.numberOfTunnels() {
|
for index in 0 ..< tunnelsManager.numberOfTunnels() {
|
||||||
|
@ -76,8 +137,9 @@ extension StatusMenu {
|
||||||
menuItem.target = self
|
menuItem.target = self
|
||||||
menuItem.representedObject = tunnel
|
menuItem.representedObject = tunnel
|
||||||
updateTunnelMenuItem(menuItem)
|
updateTunnelMenuItem(menuItem)
|
||||||
let statusObservationToken = tunnel.observe(\.status) { _, _ in
|
let statusObservationToken = tunnel.observe(\.status) { [weak self] tunnel, _ in
|
||||||
updateTunnelMenuItem(menuItem)
|
updateTunnelMenuItem(menuItem)
|
||||||
|
self?.updateStatusMenuItems(with: tunnel, ignoreInactive: false)
|
||||||
}
|
}
|
||||||
tunnelStatusObservers.insert(statusObservationToken, at: tunnelIndex)
|
tunnelStatusObservers.insert(statusObservationToken, at: tunnelIndex)
|
||||||
insertItem(menuItem, at: firstTunnelMenuItemIndex + tunnelIndex)
|
insertItem(menuItem, at: firstTunnelMenuItemIndex + tunnelIndex)
|
||||||
|
|
Loading…
Reference in New Issue