macOS: Ensure a tunnel is selected when '-' is clicked

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2019-01-09 02:41:42 +05:30
parent df8ab96139
commit 8980b5a524
1 changed files with 3 additions and 2 deletions

View File

@ -138,6 +138,7 @@ class TunnelsListTableViewController: NSViewController {
@objc func removeTunnelClicked() { @objc func removeTunnelClicked() {
guard let window = view.window else { return } guard let window = view.window else { return }
let selectedTunnelIndex = tableView.selectedRow let selectedTunnelIndex = tableView.selectedRow
guard selectedTunnelIndex >= 0 && selectedTunnelIndex < tunnelsManager.numberOfTunnels() else { return }
let selectedTunnel = tunnelsManager.tunnel(at: selectedTunnelIndex) let selectedTunnel = tunnelsManager.tunnel(at: selectedTunnelIndex)
let alert = NSAlert() let alert = NSAlert()
alert.messageText = tr(format: "macDeleteTunnelConfirmationAlertMessage (%@)", selectedTunnel.name) alert.messageText = tr(format: "macDeleteTunnelConfirmationAlertMessage (%@)", selectedTunnel.name)
@ -149,13 +150,13 @@ class TunnelsListTableViewController: NSViewController {
self?.buttonBar.setEnabled(false, forSegment: 1) self?.buttonBar.setEnabled(false, forSegment: 1)
self?.tunnelsManager.remove(tunnel: selectedTunnel) { [weak self] error in self?.tunnelsManager.remove(tunnel: selectedTunnel) { [weak self] error in
guard let self = self else { return } guard let self = self else { return }
self.buttonBar.setEnabled(true, forSegment: 1) defer { self.buttonBar.setEnabled(true, forSegment: 1) }
if let error = error { if let error = error {
ErrorPresenter.showErrorAlert(error: error, from: self) ErrorPresenter.showErrorAlert(error: error, from: self)
return return
} }
let tunnelIndex = min(selectedTunnelIndex, self.tunnelsManager.numberOfTunnels() - 1) let tunnelIndex = min(selectedTunnelIndex, self.tunnelsManager.numberOfTunnels() - 1)
if tunnelIndex > 0 { if tunnelIndex >= 0 {
self.selectTunnel(at: tunnelIndex) self.selectTunnel(at: tunnelIndex)
} }
} }