macOS: Tunnel detail: Activate / Deactivate is now a button

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2019-03-17 02:26:56 +05:30
parent 50bc994762
commit e23c221aff
2 changed files with 33 additions and 35 deletions

View File

@ -47,6 +47,14 @@
"tunnelStatusRestarting" = "Restarting"; "tunnelStatusRestarting" = "Restarting";
"tunnelStatusWaiting" = "Waiting"; "tunnelStatusWaiting" = "Waiting";
"macToggleStatusButtonActivate" = "Activate";
"macToggleStatusButtonActivating" = "Activating…";
"macToggleStatusButtonDeactivate" = "Deactivate";
"macToggleStatusButtonDeactivating" = "Deactivating…";
"macToggleStatusButtonReasserting" = "Reactivating…";
"macToggleStatusButtonRestarting" = "Restarting…";
"macToggleStatusButtonWaiting" = "Waiting…";
"tunnelSectionTitleInterface" = "Interface"; "tunnelSectionTitleInterface" = "Interface";
"tunnelInterfaceName" = "Name"; "tunnelInterfaceName" = "Name";

View File

@ -51,13 +51,14 @@ class TunnelDetailTableViewController: NSViewController {
return tableView return tableView
}() }()
let statusCheckbox: NSButton = { let toggleStatusButton: NSButton = {
let checkbox = NSButton() let button = NSButton()
checkbox.title = "" button.title = ""
checkbox.setButtonType(.switch) button.setButtonType(.momentaryPushIn)
checkbox.state = .off button.bezelStyle = .rounded
checkbox.toolTip = "Toggle status (⌘T)" button.toolTip = "Toggle status (⌘T)"
return checkbox button.widthAnchor.constraint(greaterThanOrEqualToConstant: 100).isActive = true
return button
}() }()
let editButton: NSButton = { let editButton: NSButton = {
@ -113,8 +114,8 @@ class TunnelDetailTableViewController: NSViewController {
tableView.dataSource = self tableView.dataSource = self
tableView.delegate = self tableView.delegate = self
statusCheckbox.target = self toggleStatusButton.target = self
statusCheckbox.action = #selector(statusCheckboxToggled(sender:)) toggleStatusButton.action = #selector(handleToggleActiveStatusAction)
editButton.target = self editButton.target = self
editButton.action = #selector(handleEditTunnelAction) editButton.action = #selector(handleEditTunnelAction)
@ -133,11 +134,11 @@ class TunnelDetailTableViewController: NSViewController {
containerView.addLayoutGuide(bottomControlsContainer) containerView.addLayoutGuide(bottomControlsContainer)
containerView.addSubview(box) containerView.addSubview(box)
containerView.addSubview(scrollView) containerView.addSubview(scrollView)
containerView.addSubview(statusCheckbox) containerView.addSubview(toggleStatusButton)
containerView.addSubview(editButton) containerView.addSubview(editButton)
box.translatesAutoresizingMaskIntoConstraints = false box.translatesAutoresizingMaskIntoConstraints = false
scrollView.translatesAutoresizingMaskIntoConstraints = false scrollView.translatesAutoresizingMaskIntoConstraints = false
statusCheckbox.translatesAutoresizingMaskIntoConstraints = false toggleStatusButton.translatesAutoresizingMaskIntoConstraints = false
editButton.translatesAutoresizingMaskIntoConstraints = false editButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
@ -149,10 +150,10 @@ class TunnelDetailTableViewController: NSViewController {
bottomControlsContainer.heightAnchor.constraint(equalToConstant: 32), bottomControlsContainer.heightAnchor.constraint(equalToConstant: 32),
scrollView.bottomAnchor.constraint(equalTo: bottomControlsContainer.topAnchor), scrollView.bottomAnchor.constraint(equalTo: bottomControlsContainer.topAnchor),
bottomControlsContainer.bottomAnchor.constraint(equalTo: containerView.bottomAnchor), bottomControlsContainer.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
statusCheckbox.leadingAnchor.constraint(equalTo: bottomControlsContainer.leadingAnchor), toggleStatusButton.leadingAnchor.constraint(equalTo: bottomControlsContainer.leadingAnchor),
bottomControlsContainer.bottomAnchor.constraint(equalTo: statusCheckbox.bottomAnchor, constant: 4), bottomControlsContainer.bottomAnchor.constraint(equalTo: toggleStatusButton.bottomAnchor, constant: 0),
editButton.trailingAnchor.constraint(equalTo: bottomControlsContainer.trailingAnchor), editButton.trailingAnchor.constraint(equalTo: bottomControlsContainer.trailingAnchor),
bottomControlsContainer.bottomAnchor.constraint(equalTo: editButton.bottomAnchor, constant: 4) bottomControlsContainer.bottomAnchor.constraint(equalTo: editButton.bottomAnchor, constant: 0)
]) ])
NSLayoutConstraint.activate([ NSLayoutConstraint.activate([
@ -203,28 +204,26 @@ class TunnelDetailTableViewController: NSViewController {
} }
func updateStatus() { func updateStatus() {
let statusText: String let toggleStatusButtonText: String
switch tunnel.status { switch tunnel.status {
case .waiting: case .waiting:
statusText = tr("tunnelStatusWaiting") toggleStatusButtonText = tr("macToggleStatusButtonWaiting")
case .inactive: case .inactive:
statusText = tr("tunnelStatusInactive") toggleStatusButtonText = tr("macToggleStatusButtonActivate")
case .activating: case .activating:
statusText = tr("tunnelStatusActivating") toggleStatusButtonText = tr("macToggleStatusButtonActivating")
case .active: case .active:
statusText = tr("tunnelStatusActive") toggleStatusButtonText = tr("macToggleStatusButtonDeactivate")
case .deactivating: case .deactivating:
statusText = tr("tunnelStatusDeactivating") toggleStatusButtonText = tr("macToggleStatusButtonDeactivating")
case .reasserting: case .reasserting:
statusText = tr("tunnelStatusReasserting") toggleStatusButtonText = tr("macToggleStatusButtonReasserting")
case .restarting: case .restarting:
statusText = tr("tunnelStatusRestarting") toggleStatusButtonText = tr("macToggleStatusButtonRestarting")
} }
statusCheckbox.title = tr(format: "macStatus (%@)", statusText) toggleStatusButton.title = toggleStatusButtonText
let shouldBeChecked = (tunnel.status != .inactive && tunnel.status != .deactivating)
let shouldBeEnabled = (tunnel.status == .active || tunnel.status == .inactive) let shouldBeEnabled = (tunnel.status == .active || tunnel.status == .inactive)
statusCheckbox.state = shouldBeChecked ? .on : .off toggleStatusButton.isEnabled = shouldBeEnabled
statusCheckbox.isEnabled = shouldBeEnabled
if tunnel.status == .active { if tunnel.status == .active {
startUpdatingRuntimeConfiguration() startUpdatingRuntimeConfiguration()
} else if tunnel.status == .inactive { } else if tunnel.status == .inactive {
@ -251,15 +250,6 @@ class TunnelDetailTableViewController: NSViewController {
} }
} }
@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)
}
}
override func viewWillDisappear() { override func viewWillDisappear() {
super.viewWillDisappear() super.viewWillDisappear()
if let tunnelEditVC = tunnelEditVC { if let tunnelEditVC = tunnelEditVC {