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,12 +216,21 @@ class TunnelDetailTableViewController: NSViewController {
} }
@objc func handleToggleActiveStatusAction() { @objc func handleToggleActiveStatusAction() {
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 { if tunnel.status == .inactive {
tunnelsManager.startActivation(of: tunnel) tunnelsManager.startActivation(of: tunnel)
} else if tunnel.status == .active { } else if tunnel.status == .active {
tunnelsManager.startDeactivation(of: tunnel) tunnelsManager.startDeactivation(of: tunnel)
} }
} }
}
override func viewWillAppear() { override func viewWillAppear() {
if tunnel.status == .active { if tunnel.status == .active {
@ -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,8 +509,20 @@ extension TunnelDetailTableViewController: NSTableViewDelegate {
} }
} }
private static func localizedToggleStatusActionText(forStatus status: TunnelStatus) -> String { private static func localizedToggleStatusActionText(for tunnel: TunnelContainer) -> String {
switch status { 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: case .waiting:
return tr("macToggleStatusButtonWaiting") return tr("macToggleStatusButtonWaiting")
case .inactive: case .inactive:
@ -516,6 +540,7 @@ extension TunnelDetailTableViewController: NSTableViewDelegate {
} }
} }
} }
}
extension TunnelDetailTableViewController: TunnelEditViewControllerDelegate { extension TunnelDetailTableViewController: TunnelEditViewControllerDelegate {
func tunnelSaved(tunnel: TunnelContainer) { func tunnelSaved(tunnel: TunnelContainer) {