diff --git a/WireGuard/WireGuard.xcodeproj/project.pbxproj b/WireGuard/WireGuard.xcodeproj/project.pbxproj index 783efae..c80564b 100644 --- a/WireGuard/WireGuard.xcodeproj/project.pbxproj +++ b/WireGuard/WireGuard.xcodeproj/project.pbxproj @@ -33,6 +33,7 @@ 6F5A2B4821AFF49A0081EDD8 /* FileManager+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F5A2B4421AFDE020081EDD8 /* FileManager+Extension.swift */; }; 6F5D0C1D218352EF000F85AD /* PacketTunnelProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F5D0C1C218352EF000F85AD /* PacketTunnelProvider.swift */; }; 6F5D0C22218352EF000F85AD /* WireGuardNetworkExtensioniOS.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 6F5D0C1A218352EF000F85AD /* WireGuardNetworkExtensioniOS.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; + 6F613D9B21DE33B8004B217A /* KeyValueCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F613D9A21DE33B8004B217A /* KeyValueCell.swift */; }; 6F61F1E921B932F700483816 /* WireGuardAppError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F61F1E821B932F700483816 /* WireGuardAppError.swift */; }; 6F61F1EB21B937EF00483816 /* WireGuardResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F61F1EA21B937EF00483816 /* WireGuardResult.swift */; }; 6F628C3D217F09E9003482A3 /* TunnelViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F628C3C217F09E9003482A3 /* TunnelViewModel.swift */; }; @@ -116,6 +117,8 @@ 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 */; }; + 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 */; }; 6FDEF7E62185EFB200D8FBF6 /* QRScanViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FDEF7E52185EFAF00D8FBF6 /* QRScanViewController.swift */; }; 6FDEF7FB21863B6100D8FBF6 /* unzip.c in Sources */ = {isa = PBXBuildFile; fileRef = 6FDEF7F621863B6100D8FBF6 /* unzip.c */; }; @@ -227,6 +230,7 @@ 6F5D0C1F218352EF000F85AD /* WireGuardNetworkExtension_iOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = WireGuardNetworkExtension_iOS.entitlements; sourceTree = ""; }; 6F5D0C3421839E37000F85AD /* WireGuardNetworkExtension-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WireGuardNetworkExtension-Bridging-Header.h"; sourceTree = ""; }; 6F5D0C472183C6A3000F85AD /* PacketTunnelSettingsGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PacketTunnelSettingsGenerator.swift; sourceTree = ""; }; + 6F613D9A21DE33B8004B217A /* KeyValueCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyValueCell.swift; sourceTree = ""; }; 6F61F1E821B932F700483816 /* WireGuardAppError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WireGuardAppError.swift; sourceTree = ""; }; 6F61F1EA21B937EF00483816 /* WireGuardResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WireGuardResult.swift; sourceTree = ""; }; 6F628C3C217F09E9003482A3 /* TunnelViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelViewModel.swift; sourceTree = ""; }; @@ -266,6 +270,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 = ""; }; + 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 = ""; }; 6FDEF7F621863B6100D8FBF6 /* unzip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = unzip.c; sourceTree = ""; }; @@ -362,6 +367,7 @@ isa = PBXGroup; children = ( 6F4DD16A21DA558800690EAE /* TunnelListCell.swift */, + 6F613D9A21DE33B8004B217A /* KeyValueCell.swift */, ); path = View; sourceTree = ""; @@ -499,6 +505,7 @@ children = ( 6FBA104521D7EBFA0051C35F /* TunnelsListTableViewController.swift */, 6F4DD16D21DBEA0700690EAE /* ManageTunnelsRootViewController.swift */, + 6FDB3C3A21DCF47400A0C0BF /* TunnelDetailTableViewController.swift */, ); path = ViewController; sourceTree = ""; @@ -1052,8 +1059,10 @@ 6FBA104621D7EBFA0051C35F /* TunnelsListTableViewController.swift in Sources */, 6FB1BDD321D50F5300A991BF /* ZipArchive.swift in Sources */, 6FB1BDD421D50F5300A991BF /* ioapi.c in Sources */, + 6FDB3C3C21DCF6BB00A0C0BF /* TunnelViewModel.swift in Sources */, 6FB1BDD521D50F5300A991BF /* unzip.c in Sources */, 6FB1BDD621D50F5300A991BF /* zip.c in Sources */, + 6FDB3C3B21DCF47400A0C0BF /* TunnelDetailTableViewController.swift in Sources */, 6FB1BDD721D50F5300A991BF /* WireGuardAppError.swift in Sources */, 6F4DD16E21DBEA0700690EAE /* ManageTunnelsRootViewController.swift in Sources */, 6F4DD16C21DA558F00690EAE /* NSTableView+Reuse.swift in Sources */, @@ -1070,6 +1079,7 @@ 6FB1BDBF21D50F0200A991BF /* TunnelConfiguration+WgQuickConfig.swift in Sources */, 6FB1BDC021D50F0200A991BF /* NETunnelProviderProtocol+Extension.swift in Sources */, 6FBA101821D656000051C35F /* StatusMenu.swift in Sources */, + 6F613D9B21DE33B8004B217A /* KeyValueCell.swift in Sources */, 6FB1BDC121D50F0200A991BF /* String+ArrayConversion.swift in Sources */, 6FB1BDC221D50F0300A991BF /* LegacyConfigMigration.swift in Sources */, 6FBA104021D6B7040051C35F /* ErrorPresenterProtocol.swift in Sources */, diff --git a/WireGuard/WireGuard/Base.lproj/Localizable.strings b/WireGuard/WireGuard/Base.lproj/Localizable.strings index f5e12ef..b1aa979 100644 --- a/WireGuard/WireGuard/Base.lproj/Localizable.strings +++ b/WireGuard/WireGuard/Base.lproj/Localizable.strings @@ -230,3 +230,7 @@ "macMenuManageTunnels" = "Manage tunnels"; "macMenuImportTunnels" = "Import tunnel(s) from file..."; + +// Mac detail view fields + +"macDetailFieldKey (%@)" = "%@:"; diff --git a/WireGuard/WireGuard/UI/macOS/View/KeyValueCell.swift b/WireGuard/WireGuard/UI/macOS/View/KeyValueCell.swift new file mode 100644 index 0000000..47f9263 --- /dev/null +++ b/WireGuard/WireGuard/UI/macOS/View/KeyValueCell.swift @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +import Cocoa + +class KeyValueCell: NSView { + let keyLabel: NSTextField = { + let keyLabel = NSTextField() + keyLabel.isEditable = false + keyLabel.isSelectable = false + keyLabel.isBordered = false + keyLabel.alignment = .right + keyLabel.maximumNumberOfLines = 1 + keyLabel.lineBreakMode = .byTruncatingTail + keyLabel.backgroundColor = .clear + return keyLabel + }() + + let valueLabel: NSTextField = { + let valueLabel = NSTextField() + valueLabel.isEditable = false + valueLabel.isSelectable = true + valueLabel.isBordered = false + valueLabel.maximumNumberOfLines = 1 + valueLabel.lineBreakMode = .byTruncatingTail + valueLabel.backgroundColor = .clear + return valueLabel + }() + + var key: String { + get { return keyLabel.stringValue } + set(value) { keyLabel.stringValue = value } + } + var value: String { + get { return valueLabel.stringValue } + set(value) { valueLabel.stringValue = value } + } + var isKeyInBold: Bool { + get { return keyLabel.font == NSFont.boldSystemFont(ofSize: 0) } + set(value) { + if value { + keyLabel.font = NSFont.boldSystemFont(ofSize: 0) + } else { + keyLabel.font = NSFont.systemFont(ofSize: 0) + } + } + } + + init() { + super.init(frame: CGRect.zero) + + addSubview(keyLabel) + addSubview(valueLabel) + keyLabel.translatesAutoresizingMaskIntoConstraints = false + valueLabel.translatesAutoresizingMaskIntoConstraints = false + + NSLayoutConstraint.activate([ + keyLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor), + keyLabel.firstBaselineAnchor.constraint(equalTo: valueLabel.firstBaselineAnchor), + self.leadingAnchor.constraint(equalTo: keyLabel.leadingAnchor), + keyLabel.trailingAnchor.constraint(equalTo: valueLabel.leadingAnchor, constant: -5), + valueLabel.trailingAnchor.constraint(equalTo: self.trailingAnchor), + keyLabel.widthAnchor.constraint(equalToConstant: 120) + ]) + } + + required init?(coder decoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func prepareForReuse() { + key = "" + value = "" + isKeyInBold = false + } +} diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/ManageTunnelsRootViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/ManageTunnelsRootViewController.swift index e63235a..3f16673 100644 --- a/WireGuard/WireGuard/UI/macOS/ViewController/ManageTunnelsRootViewController.swift +++ b/WireGuard/WireGuard/UI/macOS/ViewController/ManageTunnelsRootViewController.swift @@ -6,6 +6,8 @@ import Cocoa class ManageTunnelsRootViewController: NSViewController { let tunnelsManager: TunnelsManager + let tunnelDetailContainerView = NSView() + var tunnelDetailContentVC: NSViewController? init(tunnelsManager: TunnelsManager) { self.tunnelsManager = tunnelsManager @@ -32,25 +34,53 @@ class ManageTunnelsRootViewController: NSViewController { ]) let tunnelsListVC = TunnelsListTableViewController(tunnelsManager: tunnelsManager) + tunnelsListVC.delegate = self let tunnelsListView = tunnelsListVC.view - let tunnelDetailView = NSView() - tunnelDetailView.wantsLayer = true - tunnelDetailView.layer?.backgroundColor = NSColor.gray.cgColor addChild(tunnelsListVC) view.addSubview(tunnelsListView) - view.addSubview(tunnelDetailView) + view.addSubview(tunnelDetailContainerView) tunnelsListView.translatesAutoresizingMaskIntoConstraints = false - tunnelDetailView.translatesAutoresizingMaskIntoConstraints = false + tunnelDetailContainerView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ tunnelsListView.topAnchor.constraint(equalTo: container.topAnchor), tunnelsListView.bottomAnchor.constraint(equalTo: container.bottomAnchor), tunnelsListView.leadingAnchor.constraint(equalTo: container.leadingAnchor), - tunnelDetailView.leadingAnchor.constraint(equalTo: tunnelsListView.trailingAnchor, constant: horizontalSpacing), - tunnelDetailView.trailingAnchor.constraint(equalTo: container.trailingAnchor), + tunnelDetailContainerView.topAnchor.constraint(equalTo: container.topAnchor), + tunnelDetailContainerView.bottomAnchor.constraint(equalTo: container.bottomAnchor), + tunnelDetailContainerView.leadingAnchor.constraint(equalTo: tunnelsListView.trailingAnchor, constant: horizontalSpacing), + tunnelDetailContainerView.trailingAnchor.constraint(equalTo: container.trailingAnchor), tunnelsListView.widthAnchor.constraint(equalTo: container.widthAnchor, multiplier: 0.3) ]) } + + private func setTunnelDetailContentVC(_ contentVC: NSViewController) { + if let currentContentVC = tunnelDetailContentVC { + currentContentVC.view.removeFromSuperview() + currentContentVC.removeFromParent() + } + addChild(contentVC) + tunnelDetailContainerView.addSubview(contentVC.view) + contentVC.view.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + tunnelDetailContainerView.topAnchor.constraint(equalTo: contentVC.view.topAnchor), + tunnelDetailContainerView.bottomAnchor.constraint(equalTo: contentVC.view.bottomAnchor), + tunnelDetailContainerView.leadingAnchor.constraint(equalTo: contentVC.view.leadingAnchor), + tunnelDetailContainerView.trailingAnchor.constraint(equalTo: contentVC.view.trailingAnchor) + ]) + tunnelDetailContentVC = contentVC + } +} + +extension ManageTunnelsRootViewController: TunnelsListTableViewControllerDelegate { + func tunnelSelected(tunnel: TunnelContainer) { + let tunnelDetailVC = TunnelDetailTableViewController(tunnelsManager: tunnelsManager, tunnel: tunnel) + setTunnelDetailContentVC(tunnelDetailVC) + } + + func tunnelListEmpty() { + // TODO + } } diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelDetailTableViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelDetailTableViewController.swift new file mode 100644 index 0000000..f1bed18 --- /dev/null +++ b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelDetailTableViewController.swift @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018 WireGuard LLC. All Rights Reserved. + +import Cocoa + +class TunnelDetailTableViewController: NSViewController { + + private enum TableViewModelRow { + case interfaceFieldRow(TunnelViewModel.InterfaceField) + case peerFieldRow(peer: TunnelViewModel.PeerData, field: TunnelViewModel.PeerField) + case spacerRow + + func localizedSectionKeyString() -> String { + switch self { + case .interfaceFieldRow: return tr("tunnelSectionTitleInterface") + case .peerFieldRow: return tr("tunnelSectionTitlePeer") + case .spacerRow: return "" + } + } + + func isTitleRow() -> Bool { + switch self { + case .interfaceFieldRow(let field): return field == .name + case .peerFieldRow(_, let field): return field == .publicKey + case .spacerRow: return false + } + } + } + + let interfaceFields: [TunnelViewModel.InterfaceField] = [ + .name, .publicKey, .addresses, + .listenPort, .mtu, .dns + ] + + let peerFields: [TunnelViewModel.PeerField] = [ + .publicKey, .preSharedKey, .endpoint, + .allowedIPs, .persistentKeepAlive + ] + + let tableView: NSTableView = { + let tableView = NSTableView() + tableView.addTableColumn(NSTableColumn(identifier: NSUserInterfaceItemIdentifier("TunnelDetail"))) + tableView.headerView = nil + tableView.rowSizeStyle = .medium + tableView.backgroundColor = .clear + tableView.selectionHighlightStyle = .none + return tableView + }() + + let tunnelsManager: TunnelsManager + let tunnel: TunnelContainer + var tunnelViewModel: TunnelViewModel { + didSet { + updateTableViewModelRows() + } + } + private var tableViewModelRows = [TableViewModelRow]() + + init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer) { + self.tunnelsManager = tunnelsManager + self.tunnel = tunnel + tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration) + super.init(nibName: nil, bundle: nil) + updateTableViewModelRows() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func loadView() { + tableView.dataSource = self + tableView.delegate = self + + let clipView = NSClipView() + clipView.documentView = tableView + + let scrollView = NSScrollView() + scrollView.contentView = clipView // Set contentView before setting drawsBackground + scrollView.drawsBackground = false + scrollView.hasVerticalScroller = true + scrollView.autohidesScrollers = true + + view = scrollView + } + + func updateTableViewModelRows() { + tableViewModelRows = [] + for field in interfaceFields where !tunnelViewModel.interfaceData[field].isEmpty { + tableViewModelRows.append(.interfaceFieldRow(field)) + } + for peerData in tunnelViewModel.peersData { + tableViewModelRows.append(.spacerRow) + for field in peerFields where !peerData[field].isEmpty { + tableViewModelRows.append(.peerFieldRow(peer: peerData, field: field)) + } + } + } +} + +extension TunnelDetailTableViewController: NSTableViewDataSource { + func numberOfRows(in tableView: NSTableView) -> Int { + return tableViewModelRows.count + } +} + +extension TunnelDetailTableViewController: NSTableViewDelegate { + func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { + let modelRow = tableViewModelRows[row] + switch modelRow { + case .interfaceFieldRow(let field): + let cell: KeyValueCell = tableView.dequeueReusableCell() + let localizedKeyString = modelRow.isTitleRow() ? modelRow.localizedSectionKeyString() : field.localizedUIString + cell.key = tr(format: "macDetailFieldKey (%@)", localizedKeyString) + cell.value = tunnelViewModel.interfaceData[field] + cell.isKeyInBold = modelRow.isTitleRow() + return cell + case .peerFieldRow(let peerData, let field): + let cell: KeyValueCell = tableView.dequeueReusableCell() + let localizedKeyString = modelRow.isTitleRow() ? modelRow.localizedSectionKeyString() : field.localizedUIString + cell.key = tr(format: "macDetailFieldKey (%@)", localizedKeyString) + cell.value = peerData[field] + cell.isKeyInBold = modelRow.isTitleRow() + return cell + case .spacerRow: + return NSView() + } + } +} diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelsListTableViewController.swift index 05aabbe..c99f15b 100644 --- a/WireGuard/WireGuard/UI/macOS/ViewController/TunnelsListTableViewController.swift +++ b/WireGuard/WireGuard/UI/macOS/ViewController/TunnelsListTableViewController.swift @@ -3,9 +3,15 @@ import Cocoa +protocol TunnelsListTableViewControllerDelegate: class { + func tunnelSelected(tunnel: TunnelContainer) + func tunnelListEmpty() +} + class TunnelsListTableViewController: NSViewController { let tunnelsManager: TunnelsManager + weak var delegate: TunnelsListTableViewControllerDelegate? let tableView: NSTableView = { let tableView = NSTableView() @@ -148,6 +154,17 @@ extension TunnelsListTableViewController: NSTableViewDelegate { cell.tunnel = tunnelsManager.tunnel(at: row) return cell } + + func tableViewSelectionDidChange(_ notification: Notification) { + guard tableView.selectedRow >= 0 else { + if tunnelsManager.numberOfTunnels() == 0 { + delegate?.tunnelListEmpty() + } + return + } + let selectedTunnel = tunnelsManager.tunnel(at: tableView.selectedRow) + delegate?.tunnelSelected(tunnel: selectedTunnel) + } } class FillerButton: NSButton {