UI: macOS: Incorporate on-demand-ness in status menu

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2021-07-30 13:26:56 +05:30
parent 2ac17da7cb
commit b6831c1aca
1 changed files with 29 additions and 6 deletions

View File

@ -144,10 +144,20 @@ class StatusMenu: NSMenu {
@objc func tunnelClicked(sender: AnyObject) { @objc func tunnelClicked(sender: AnyObject) {
guard let tunnelMenuItem = sender as? TunnelMenuItem else { return } guard let tunnelMenuItem = sender as? TunnelMenuItem else { return }
if tunnelMenuItem.state == .off { let tunnel = tunnelMenuItem.tunnel
tunnelsManager.startActivation(of: tunnelMenuItem.tunnel) if tunnel.hasOnDemandRules {
let turnOn = !tunnel.isActivateOnDemandEnabled
tunnelsManager.setOnDemandEnabled(turnOn, on: tunnel) { error in
if error == nil && !turnOn {
self.tunnelsManager.startDeactivation(of: tunnel)
}
}
} else { } else {
tunnelsManager.startDeactivation(of: tunnelMenuItem.tunnel) if tunnel.status == .inactive {
tunnelsManager.startActivation(of: tunnel)
} else if tunnel.status == .active {
tunnelsManager.startDeactivation(of: tunnel)
}
} }
} }
@ -291,6 +301,7 @@ class TunnelMenuItem: NSMenuItem {
private var statusObservationToken: AnyObject? private var statusObservationToken: AnyObject?
private var nameObservationToken: AnyObject? private var nameObservationToken: AnyObject?
private var isOnDemandEnabledObservationToken: AnyObject?
init(tunnel: TunnelContainer, action selector: Selector?) { init(tunnel: TunnelContainer, action selector: Selector?) {
self.tunnel = tunnel self.tunnel = tunnel
@ -303,7 +314,12 @@ class TunnelMenuItem: NSMenuItem {
let nameObservationToken = tunnel.observe(\TunnelContainer.name) { [weak self] _, _ in let nameObservationToken = tunnel.observe(\TunnelContainer.name) { [weak self] _, _ in
self?.updateTitle() self?.updateTitle()
} }
let isOnDemandEnabledObservationToken = tunnel.observe(\.isActivateOnDemandEnabled) { [weak self] _, _ in
self?.updateTitle()
self?.updateStatus()
}
self.statusObservationToken = statusObservationToken self.statusObservationToken = statusObservationToken
self.isOnDemandEnabledObservationToken = isOnDemandEnabledObservationToken
self.nameObservationToken = nameObservationToken self.nameObservationToken = nameObservationToken
} }
@ -312,12 +328,19 @@ class TunnelMenuItem: NSMenuItem {
} }
func updateTitle() { func updateTitle() {
title = tunnel.name if tunnel.isActivateOnDemandEnabled {
title = tunnel.name + " (On-Demand)"
} else {
title = tunnel.name
}
} }
func updateStatus() { func updateStatus() {
let shouldShowCheckmark = (tunnel.status != .inactive && tunnel.status != .deactivating) if tunnel.isActivateOnDemandEnabled {
state = shouldShowCheckmark ? .on : .off state = (tunnel.status == .inactive || tunnel.status == .deactivating) ? .mixed : .on
} else {
state = (tunnel.status == .inactive || tunnel.status == .deactivating) ? .off : .on
}
} }
} }