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…"; "macToggleStatusButtonReasserting" = "Reactivating…";
"macToggleStatusButtonRestarting" = "Restarting…"; "macToggleStatusButtonRestarting" = "Restarting…";
"macToggleStatusButtonWaiting" = "Waiting…"; "macToggleStatusButtonWaiting" = "Waiting…";
"macToggleStatusButtonEnableOnDemand" = "Enable On-Demand";
"macToggleStatusButtonDisableOnDemand" = "Disable On-Demand";
"macToggleStatusButtonDisableOnDemandDeactivate" = "Disable On-Demand and Deactivate";
"tunnelSectionTitleInterface" = "Interface"; "tunnelSectionTitleInterface" = "Interface";

View File

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

View File

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