macOS: Manage tunnels: Remove tunnel

This commit is contained in:
Roopesh Chander 2019-01-04 20:09:28 +05:30
parent 78c11a1f14
commit ca5e90a70e
2 changed files with 37 additions and 7 deletions

View File

@ -233,10 +233,15 @@
"macMenuExportLog" = "Export log to file...";
"macMenuExportTunnels" = "Export tunnels to zip...";
// Mac window title
// Mac manage tunnels window
"macWindowTitleManageTunnels" = "Manage WireGuard Tunnels";
"macDeleteTunnelConfirmationAlertMessage (%@)" = "Are you sure you want to delete tunnel \"%@\"?";
"macDeleteTunnelConfirmationAlertInfo" = "You can't undo this action";
"macDeleteTunnelConfirmationAlertButtonTitleDelete" = "Delete";
"macDeleteTunnelConfirmationAlertButtonTitleCancel" = "Cancel";
// Mac detail view fields
"macDetailFieldKey (%@)" = "%@:";

View File

@ -61,7 +61,7 @@ class TunnelsListTableViewController: NSViewController {
override func loadView() {
tableView.dataSource = self
tableView.delegate = self
selectFirstTunnel()
selectTunnel(at: 0)
let scrollView = NSScrollView()
scrollView.hasVerticalScroller = true
@ -140,7 +140,30 @@ class TunnelsListTableViewController: NSViewController {
}
@objc func removeTunnelClicked() {
print("removeTunnelClicked")
guard let window = view.window else { return }
let selectedTunnelIndex = tableView.selectedRow
let selectedTunnel = tunnelsManager.tunnel(at: selectedTunnelIndex)
let alert = NSAlert()
alert.messageText = tr(format: "macDeleteTunnelConfirmationAlertMessage (%@)", selectedTunnel.name)
alert.informativeText = tr("macDeleteTunnelConfirmationAlertInfo")
alert.addButton(withTitle: tr("macDeleteTunnelConfirmationAlertButtonTitleDelete"))
alert.addButton(withTitle: tr("macDeleteTunnelConfirmationAlertButtonTitleCancel"))
alert.beginSheetModal(for: window) { [weak self] response in
guard response == .alertFirstButtonReturn else { return }
self?.buttonBar.setEnabled(false, forSegment: 1)
self?.tunnelsManager.remove(tunnel: selectedTunnel) { [weak self] error in
guard let self = self else { return }
self.buttonBar.setEnabled(true, forSegment: 1)
if let error = error {
ErrorPresenter.showErrorAlert(error: error, from: self)
return
}
let tunnelIndex = min(selectedTunnelIndex, self.tunnelsManager.numberOfTunnels() - 1)
if tunnelIndex > 0 {
self.selectTunnel(at: tunnelIndex)
}
}
}
}
@objc func exportLogClicked() {
@ -199,10 +222,12 @@ class TunnelsListTableViewController: NSViewController {
}
@discardableResult
private func selectFirstTunnel() -> Bool {
guard tunnelsManager.numberOfTunnels() > 0 else { return false }
tableView.selectRowIndexes(IndexSet(integer: 0), byExtendingSelection: false)
return true
private func selectTunnel(at index: Int) -> Bool {
if index < tunnelsManager.numberOfTunnels() {
tableView.selectRowIndexes(IndexSet(integer: index), byExtendingSelection: false)
return true
}
return false
}
}