diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj index 0ca1d03..7ad1bbf 100644 --- a/WireGuard/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj @@ -117,6 +117,7 @@ 6FBA104021D6B7040051C35F /* ErrorPresenterProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FBA103A21D6B4280051C35F /* ErrorPresenterProtocol.swift */; }; 6FBA104321D6BC250051C35F /* ErrorPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FBA104121D6BC210051C35F /* ErrorPresenter.swift */; }; 6FBA104621D7EBFA0051C35F /* TunnelsListTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FBA104521D7EBFA0051C35F /* TunnelsListTableViewController.swift */; }; + 6FCD99AA21E0E14700BA4C82 /* NoTunnelsDetailViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FCD99A821E0E0C700BA4C82 /* NoTunnelsDetailViewController.swift */; }; 6FDB3C3B21DCF47400A0C0BF /* TunnelDetailTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FDB3C3A21DCF47400A0C0BF /* TunnelDetailTableViewController.swift */; }; 6FDB3C3C21DCF6BB00A0C0BF /* TunnelViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F628C3C217F09E9003482A3 /* TunnelViewModel.swift */; }; 6FDEF7E421846C1A00D8FBF6 /* libwg-go.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6FDEF7E321846C1A00D8FBF6 /* libwg-go.a */; }; @@ -270,6 +271,7 @@ 6FBA103D21D6B6D70051C35F /* TunnelImporter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TunnelImporter.swift; sourceTree = ""; }; 6FBA104121D6BC210051C35F /* ErrorPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorPresenter.swift; sourceTree = ""; }; 6FBA104521D7EBFA0051C35F /* TunnelsListTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelsListTableViewController.swift; sourceTree = ""; }; + 6FCD99A821E0E0C700BA4C82 /* NoTunnelsDetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoTunnelsDetailViewController.swift; sourceTree = ""; }; 6FDB3C3A21DCF47400A0C0BF /* TunnelDetailTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelDetailTableViewController.swift; sourceTree = ""; }; 6FDEF7E321846C1A00D8FBF6 /* libwg-go.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = "libwg-go.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 6FDEF7E52185EFAF00D8FBF6 /* QRScanViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QRScanViewController.swift; sourceTree = ""; }; @@ -506,6 +508,7 @@ 6FBA104521D7EBFA0051C35F /* TunnelsListTableViewController.swift */, 6F4DD16D21DBEA0700690EAE /* ManageTunnelsRootViewController.swift */, 6FDB3C3A21DCF47400A0C0BF /* TunnelDetailTableViewController.swift */, + 6FCD99A821E0E0C700BA4C82 /* NoTunnelsDetailViewController.swift */, ); path = ViewController; sourceTree = ""; @@ -1086,6 +1089,7 @@ 6FB1BDC121D50F0200A991BF /* String+ArrayConversion.swift in Sources */, 6FB1BDC221D50F0300A991BF /* LegacyConfigMigration.swift in Sources */, 6FBA104021D6B7040051C35F /* ErrorPresenterProtocol.swift in Sources */, + 6FCD99AA21E0E14700BA4C82 /* NoTunnelsDetailViewController.swift in Sources */, 6FB1BDC321D50F0300A991BF /* TunnelConfiguration.swift in Sources */, 6FB1BDC421D50F0300A991BF /* IPAddressRange.swift in Sources */, 6FBA104321D6BC250051C35F /* ErrorPresenter.swift in Sources */, diff --git a/WireGuard/WireGuard/Base.lproj/Localizable.strings b/WireGuard/WireGuard/Base.lproj/Localizable.strings index dd1f118..ecbc087 100644 --- a/WireGuard/WireGuard/Base.lproj/Localizable.strings +++ b/WireGuard/WireGuard/Base.lproj/Localizable.strings @@ -242,6 +242,8 @@ "macDeleteTunnelConfirmationAlertButtonTitleDelete" = "Delete"; "macDeleteTunnelConfirmationAlertButtonTitleCancel" = "Cancel"; +"macButtonImportTunnels" = "Import tunnel(s) from file"; + // Mac detail view fields "macDetailFieldKey (%@)" = "%@:"; diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/ManageTunnelsRootViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/ManageTunnelsRootViewController.swift index c951b45..1b1dc73 100644 --- a/WireGuard/WireGuard/UI/macOS/ViewController/ManageTunnelsRootViewController.swift +++ b/WireGuard/WireGuard/UI/macOS/ViewController/ManageTunnelsRootViewController.swift @@ -82,7 +82,8 @@ extension ManageTunnelsRootViewController: TunnelsListTableViewControllerDelegat setTunnelDetailContentVC(tunnelDetailVC) } - func tunnelListEmpty() { - // TODO + func tunnelsListEmpty() { + let noTunnelsVC = NoTunnelsDetailViewController(tunnelsManager: tunnelsManager) + setTunnelDetailContentVC(noTunnelsVC) } } diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/NoTunnelsDetailViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/NoTunnelsDetailViewController.swift new file mode 100644 index 0000000..059403a --- /dev/null +++ b/WireGuard/WireGuard/UI/macOS/ViewController/NoTunnelsDetailViewController.swift @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +import Cocoa + +class NoTunnelsDetailViewController: NSViewController { + + let tunnelsManager: TunnelsManager + + let importButton: NSButton = { + let button = NSButton() + button.title = tr("macButtonImportTunnels") + button.setButtonType(.momentaryPushIn) + button.bezelStyle = .rounded + return button + }() + + init(tunnelsManager: TunnelsManager) { + self.tunnelsManager = tunnelsManager + super.init(nibName: nil, bundle: nil) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func loadView() { + let view = NSView() + + importButton.target = self + importButton.action = #selector(importTunnelClicked) + + view.addSubview(importButton) + importButton.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + importButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), + importButton.centerYAnchor.constraint(equalTo: view.centerYAnchor) + ]) + self.view = view + } + + @objc func importTunnelClicked() { + guard let window = view.window else { return } + let openPanel = NSOpenPanel() + openPanel.allowedFileTypes = ["conf", "zip"] + openPanel.beginSheetModal(for: window) { [weak tunnelsManager] response in + guard let tunnelsManager = tunnelsManager else { return } + guard response == .OK else { return } + guard let url = openPanel.url else { return } + TunnelImporter.importFromFile(url: url, into: tunnelsManager, sourceVC: nil, errorPresenterType: ErrorPresenter.self) + } + } +} diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelsListTableViewController.swift index 643529c..dff583a 100644 --- a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelsListTableViewController.swift +++ b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelsListTableViewController.swift @@ -5,7 +5,7 @@ import Cocoa protocol TunnelsListTableViewControllerDelegate: class { func tunnelSelected(tunnel: TunnelContainer) - func tunnelListEmpty() + func tunnelsListEmpty() } class TunnelsListTableViewController: NSViewController { @@ -61,7 +61,10 @@ class TunnelsListTableViewController: NSViewController { override func loadView() { tableView.dataSource = self tableView.delegate = self - selectTunnel(at: 0) + let isSelected = selectTunnel(at: 0) + if !isSelected { + delegate?.tunnelsListEmpty() + } let scrollView = NSScrollView() scrollView.hasVerticalScroller = true @@ -246,6 +249,9 @@ extension TunnelsListTableViewController { func tunnelRemoved(at index: Int) { tableView.removeRows(at: IndexSet(integer: index), withAnimation: .slideLeft) + if tunnelsManager.numberOfTunnels() == 0 { + delegate?.tunnelsListEmpty() + } } } @@ -263,12 +269,7 @@ extension TunnelsListTableViewController: NSTableViewDelegate { } func tableViewSelectionDidChange(_ notification: Notification) { - guard tableView.selectedRow >= 0 else { - if tunnelsManager.numberOfTunnels() == 0 { - delegate?.tunnelListEmpty() - } - return - } + guard tableView.selectedRow >= 0 else { return } let selectedTunnel = tunnelsManager.tunnel(at: tableView.selectedRow) delegate?.tunnelSelected(tunnel: selectedTunnel) }