UI: macOS: Tunnel detail: Incorporate on-demand-ness in toggle button

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2021-07-30 11:04:38 +05:30
parent 274c4cd092
commit 2ac17da7cb
3 changed files with 59 additions and 27 deletions

View File

@ -68,6 +68,9 @@
"macToggleStatusButtonReasserting" = "Reactivating…";
"macToggleStatusButtonRestarting" = "Restarting…";
"macToggleStatusButtonWaiting" = "Waiting…";
"macToggleStatusButtonEnableOnDemand" = "Enable On-Demand";
"macToggleStatusButtonDisableOnDemand" = "Disable On-Demand";
"macToggleStatusButtonDisableOnDemandDeactivate" = "Disable On-Demand and Deactivate";
"tunnelSectionTitleInterface" = "Interface";

View File

@ -28,7 +28,9 @@ class ButtonRow: NSView {
}
var onButtonClicked: (() -> Void)?
var observationToken: AnyObject?
var statusObservationToken: AnyObject?
var isOnDemandEnabledObservationToken: AnyObject?
var hasOnDemandRulesObservationToken: AnyObject?
override var intrinsicContentSize: NSSize {
return NSSize(width: NSView.noIntrinsicMetric, height: button.intrinsicContentSize.height)
@ -62,6 +64,8 @@ class ButtonRow: NSView {
buttonTitle = ""
buttonToolTip = ""
onButtonClicked = nil
observationToken = nil
statusObservationToken = nil
isOnDemandEnabledObservationToken = nil
hasOnDemandRulesObservationToken = nil
}
}

View File

@ -216,10 +216,19 @@ class TunnelDetailTableViewController: NSViewController {
}
@objc func handleToggleActiveStatusAction() {
if tunnel.status == .inactive {
tunnelsManager.startActivation(of: tunnel)
} else if tunnel.status == .active {
tunnelsManager.startDeactivation(of: tunnel)
if self.tunnel.hasOnDemandRules {
let turnOn = !self.tunnel.isActivateOnDemandEnabled
self.tunnelsManager.setOnDemandEnabled(turnOn, on: self.tunnel) { error in
if error == nil && !turnOn {
self.tunnelsManager.startDeactivation(of: self.tunnel)
}
}
} else {
if tunnel.status == .inactive {
tunnelsManager.startActivation(of: tunnel)
} else if tunnel.status == .active {
tunnelsManager.startDeactivation(of: tunnel)
}
}
}
@ -437,17 +446,20 @@ extension TunnelDetailTableViewController: NSTableViewDelegate {
func toggleStatusCell() -> NSView {
let cell: ButtonRow = tableView.dequeueReusableCell()
cell.buttonTitle = TunnelDetailTableViewController.localizedToggleStatusActionText(forStatus: tunnel.status)
cell.isButtonEnabled = (tunnel.status == .active || tunnel.status == .inactive)
cell.buttonTitle = TunnelDetailTableViewController.localizedToggleStatusActionText(for: tunnel)
cell.isButtonEnabled = (tunnel.hasOnDemandRules || tunnel.status == .active || tunnel.status == .inactive)
cell.buttonToolTip = tr("macToolTipToggleStatus")
cell.onButtonClicked = { [weak self] in
self?.handleToggleActiveStatusAction()
}
cell.observationToken = tunnel.observe(\.status) { [weak cell] tunnel, _ in
let changeHandler: (TunnelContainer, Any) -> Void = { [weak cell] tunnel, _ in
guard let cell = cell else { return }
cell.buttonTitle = TunnelDetailTableViewController.localizedToggleStatusActionText(forStatus: tunnel.status)
cell.isButtonEnabled = (tunnel.status == .active || tunnel.status == .inactive)
cell.buttonTitle = TunnelDetailTableViewController.localizedToggleStatusActionText(for: tunnel)
cell.isButtonEnabled = (tunnel.hasOnDemandRules || tunnel.status == .active || tunnel.status == .inactive)
}
cell.statusObservationToken = tunnel.observe(\.status, changeHandler: changeHandler)
cell.isOnDemandEnabledObservationToken = tunnel.observe(\.isActivateOnDemandEnabled, changeHandler: changeHandler)
cell.hasOnDemandRulesObservationToken = tunnel.observe(\.hasOnDemandRules, changeHandler: changeHandler)
return cell
}
@ -497,22 +509,35 @@ extension TunnelDetailTableViewController: NSTableViewDelegate {
}
}
private static func localizedToggleStatusActionText(forStatus status: TunnelStatus) -> String {
switch status {
case .waiting:
return tr("macToggleStatusButtonWaiting")
case .inactive:
return tr("macToggleStatusButtonActivate")
case .activating:
return tr("macToggleStatusButtonActivating")
case .active:
return tr("macToggleStatusButtonDeactivate")
case .deactivating:
return tr("macToggleStatusButtonDeactivating")
case .reasserting:
return tr("macToggleStatusButtonReasserting")
case .restarting:
return tr("macToggleStatusButtonRestarting")
private static func localizedToggleStatusActionText(for tunnel: TunnelContainer) -> String {
if tunnel.hasOnDemandRules {
let turnOn = !tunnel.isActivateOnDemandEnabled
if turnOn {
return tr("macToggleStatusButtonEnableOnDemand")
} else {
if tunnel.status == .active {
return tr("macToggleStatusButtonDisableOnDemandDeactivate")
} else {
return tr("macToggleStatusButtonDisableOnDemand")
}
}
} else {
switch tunnel.status {
case .waiting:
return tr("macToggleStatusButtonWaiting")
case .inactive:
return tr("macToggleStatusButtonActivate")
case .activating:
return tr("macToggleStatusButtonActivating")
case .active:
return tr("macToggleStatusButtonDeactivate")
case .deactivating:
return tr("macToggleStatusButtonDeactivating")
case .reasserting:
return tr("macToggleStatusButtonReasserting")
case .restarting:
return tr("macToggleStatusButtonRestarting")
}
}
}
}