UI: macOS: Tunnel detail: Incorporate on-demand-ness in the status row

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2021-07-29 15:57:04 +05:30
parent 95e1409bfb
commit 274c4cd092
3 changed files with 43 additions and 19 deletions

View File

@ -58,6 +58,8 @@
"tunnelStatusAddendumOnDemand" = " (On Demand)"; "tunnelStatusAddendumOnDemand" = " (On Demand)";
"tunnelStatusOnDemandDisabled" = "On Demand Disabled"; "tunnelStatusOnDemandDisabled" = "On Demand Disabled";
"tunnelStatusAddendumOnDemandEnabled" = ", On Demand Enabled";
"tunnelStatusAddendumOnDemandDisabled" = ", On Demand Disabled";
"macToggleStatusButtonActivate" = "Activate"; "macToggleStatusButtonActivate" = "Activate";
"macToggleStatusButtonActivating" = "Activating…"; "macToggleStatusButtonActivating" = "Activating…";

View File

@ -49,7 +49,9 @@ class EditableKeyValueRow: NSView {
set(value) { valueImageView?.image = value } set(value) { valueImageView?.image = value }
} }
var observationToken: AnyObject? var statusObservationToken: AnyObject?
var isOnDemandEnabledObservationToken: AnyObject?
var hasOnDemandRulesObservationToken: AnyObject?
override var intrinsicContentSize: NSSize { override var intrinsicContentSize: NSSize {
let height = max(keyLabel.intrinsicContentSize.height, valueLabel.intrinsicContentSize.height) let height = max(keyLabel.intrinsicContentSize.height, valueLabel.intrinsicContentSize.height)
@ -108,7 +110,9 @@ class EditableKeyValueRow: NSView {
key = "" key = ""
value = "" value = ""
isKeyInBold = false isKeyInBold = false
observationToken = nil statusObservationToken = nil
isOnDemandEnabledObservationToken = nil
hasOnDemandRulesObservationToken = nil
} }
} }

View File

@ -422,13 +422,16 @@ extension TunnelDetailTableViewController: NSTableViewDelegate {
func statusCell() -> NSView { func statusCell() -> NSView {
let cell: KeyValueImageRow = tableView.dequeueReusableCell() let cell: KeyValueImageRow = tableView.dequeueReusableCell()
cell.key = tr(format: "macFieldKey (%@)", tr("tunnelInterfaceStatus")) cell.key = tr(format: "macFieldKey (%@)", tr("tunnelInterfaceStatus"))
cell.value = TunnelDetailTableViewController.localizedStatusDescription(forStatus: tunnel.status) cell.value = TunnelDetailTableViewController.localizedStatusDescription(for: tunnel)
cell.valueImage = TunnelDetailTableViewController.image(forStatus: tunnel.status) cell.valueImage = TunnelDetailTableViewController.image(for: tunnel)
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.value = TunnelDetailTableViewController.localizedStatusDescription(forStatus: tunnel.status) cell.value = TunnelDetailTableViewController.localizedStatusDescription(for: tunnel)
cell.valueImage = TunnelDetailTableViewController.image(forStatus: tunnel.status) cell.valueImage = TunnelDetailTableViewController.image(for: tunnel)
} }
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
} }
@ -448,36 +451,51 @@ extension TunnelDetailTableViewController: NSTableViewDelegate {
return cell return cell
} }
private static func localizedStatusDescription(forStatus status: TunnelStatus) -> String { private static func localizedStatusDescription(for tunnel: TunnelContainer) -> String {
let status = tunnel.status
let isOnDemandEngaged = tunnel.isActivateOnDemandEnabled
var text: String
switch status { switch status {
case .inactive: case .inactive:
return tr("tunnelStatusInactive") text = tr("tunnelStatusInactive")
case .activating: case .activating:
return tr("tunnelStatusActivating") text = tr("tunnelStatusActivating")
case .active: case .active:
return tr("tunnelStatusActive") text = tr("tunnelStatusActive")
case .deactivating: case .deactivating:
return tr("tunnelStatusDeactivating") text = tr("tunnelStatusDeactivating")
case .reasserting: case .reasserting:
return tr("tunnelStatusReasserting") text = tr("tunnelStatusReasserting")
case .restarting: case .restarting:
return tr("tunnelStatusRestarting") text = tr("tunnelStatusRestarting")
case .waiting: case .waiting:
return tr("tunnelStatusWaiting") text = tr("tunnelStatusWaiting")
}
} }
private static func image(forStatus status: TunnelStatus?) -> NSImage? { if tunnel.hasOnDemandRules {
guard let status = status else { return nil } text += isOnDemandEngaged ?
switch status { tr("tunnelStatusAddendumOnDemandEnabled") : tr("tunnelStatusAddendumOnDemandDisabled")
}
return text
}
private static func image(for tunnel: TunnelContainer?) -> NSImage? {
guard let tunnel = tunnel else { return nil }
switch tunnel.status {
case .active, .restarting, .reasserting: case .active, .restarting, .reasserting:
return NSImage(named: NSImage.statusAvailableName) return NSImage(named: NSImage.statusAvailableName)
case .activating, .waiting, .deactivating: case .activating, .waiting, .deactivating:
return NSImage(named: NSImage.statusPartiallyAvailableName) return NSImage(named: NSImage.statusPartiallyAvailableName)
case .inactive: case .inactive:
if tunnel.isActivateOnDemandEnabled {
return NSImage(named: NSImage.Name.statusOnDemandEnabled)
} else {
return NSImage(named: NSImage.statusNoneName) return NSImage(named: NSImage.statusNoneName)
} }
} }
}
private static func localizedToggleStatusActionText(forStatus status: TunnelStatus) -> String { private static func localizedToggleStatusActionText(forStatus status: TunnelStatus) -> String {
switch status { switch status {