macOS: Manage tunnels: Show status checkbox and edit button

This commit is contained in:
Roopesh Chander 2019-01-03 19:10:31 +05:30
parent 9535a0c585
commit 5ed147aa3e
1 changed files with 91 additions and 1 deletions

View File

@ -47,6 +47,22 @@ class TunnelDetailTableViewController: NSViewController {
return tableView return tableView
}() }()
let statusCheckbox: NSButton = {
let checkbox = NSButton()
checkbox.title = ""
checkbox.setButtonType(.switch)
checkbox.state = .off
return checkbox
}()
let editButton: NSButton = {
let button = NSButton()
button.title = tr("Edit")
button.setButtonType(.momentaryPushIn)
button.bezelStyle = .rounded
return button
}()
let tunnelsManager: TunnelsManager let tunnelsManager: TunnelsManager
let tunnel: TunnelContainer let tunnel: TunnelContainer
var tunnelViewModel: TunnelViewModel { var tunnelViewModel: TunnelViewModel {
@ -55,6 +71,7 @@ class TunnelDetailTableViewController: NSViewController {
} }
} }
private var tableViewModelRows = [TableViewModelRow]() private var tableViewModelRows = [TableViewModelRow]()
private var statusObservationToken: AnyObject?
init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer) { init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer) {
self.tunnelsManager = tunnelsManager self.tunnelsManager = tunnelsManager
@ -62,6 +79,10 @@ class TunnelDetailTableViewController: NSViewController {
tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration) tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration)
super.init(nibName: nil, bundle: nil) super.init(nibName: nil, bundle: nil)
updateTableViewModelRows() updateTableViewModelRows()
updateStatus()
statusObservationToken = tunnel.observe(\TunnelContainer.status) { [weak self] _, _ in
self?.updateStatus()
}
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
@ -72,6 +93,12 @@ class TunnelDetailTableViewController: NSViewController {
tableView.dataSource = self tableView.dataSource = self
tableView.delegate = self tableView.delegate = self
statusCheckbox.target = self
statusCheckbox.action = #selector(statusCheckboxToggled(sender:))
editButton.target = self
editButton.action = #selector(editButtonClicked)
let clipView = NSClipView() let clipView = NSClipView()
clipView.documentView = tableView clipView.documentView = tableView
@ -81,7 +108,32 @@ class TunnelDetailTableViewController: NSViewController {
scrollView.hasVerticalScroller = true scrollView.hasVerticalScroller = true
scrollView.autohidesScrollers = true scrollView.autohidesScrollers = true
view = scrollView let containerView = NSView()
let bottomControlsContainer = NSLayoutGuide()
containerView.addLayoutGuide(bottomControlsContainer)
containerView.addSubview(scrollView)
containerView.addSubview(statusCheckbox)
containerView.addSubview(editButton)
scrollView.translatesAutoresizingMaskIntoConstraints = false
statusCheckbox.translatesAutoresizingMaskIntoConstraints = false
editButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: scrollView.topAnchor),
containerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
containerView.leadingAnchor.constraint(equalTo: bottomControlsContainer.leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: bottomControlsContainer.trailingAnchor),
bottomControlsContainer.heightAnchor.constraint(equalToConstant: 60),
scrollView.bottomAnchor.constraint(equalTo: bottomControlsContainer.topAnchor),
bottomControlsContainer.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
statusCheckbox.leadingAnchor.constraint(equalTo: bottomControlsContainer.leadingAnchor),
statusCheckbox.centerYAnchor.constraint(equalTo: bottomControlsContainer.centerYAnchor),
editButton.trailingAnchor.constraint(equalTo: bottomControlsContainer.trailingAnchor),
editButton.centerYAnchor.constraint(equalTo: bottomControlsContainer.centerYAnchor)
])
view = containerView
} }
func updateTableViewModelRows() { func updateTableViewModelRows() {
@ -96,6 +148,44 @@ class TunnelDetailTableViewController: NSViewController {
} }
} }
} }
func updateStatus() {
let statusText: String
switch tunnel.status {
case .waiting:
statusText = tr("tunnelStatusWaiting")
case .inactive:
statusText = tr("tunnelStatusInactive")
case .activating:
statusText = tr("tunnelStatusActivating")
case .active:
statusText = tr("tunnelStatusActive")
case .deactivating:
statusText = tr("tunnelStatusDeactivating")
case .reasserting:
statusText = tr("tunnelStatusReasserting")
case .restarting:
statusText = tr("tunnelStatusRestarting")
}
statusCheckbox.title = tr(format: "macStatus (%@)", statusText)
let shouldBeChecked = (tunnel.status != .inactive && tunnel.status != .deactivating)
let shouldBeEnabled = (tunnel.status == .active || tunnel.status == .inactive)
statusCheckbox.state = shouldBeChecked ? .on : .off
statusCheckbox.isEnabled = shouldBeEnabled
}
@objc func editButtonClicked() {
print("editButtonClicked")
}
@objc func statusCheckboxToggled(sender: AnyObject?) {
guard let statusCheckbox = sender as? NSButton else { return }
if statusCheckbox.state == .on {
tunnelsManager.startActivation(of: tunnel)
} else if statusCheckbox.state == .off {
tunnelsManager.startDeactivation(of: tunnel)
}
}
} }
extension TunnelDetailTableViewController: NSTableViewDataSource { extension TunnelDetailTableViewController: NSTableViewDataSource {