macOS: Manage tunnels: Tunnel detail view

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2019-01-03 01:16:27 +05:30
parent c946c0ea48
commit ada7db3dca
6 changed files with 273 additions and 7 deletions

View File

@ -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 = "<group>"; };
6F5D0C3421839E37000F85AD /* WireGuardNetworkExtension-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "WireGuardNetworkExtension-Bridging-Header.h"; sourceTree = "<group>"; };
6F5D0C472183C6A3000F85AD /* PacketTunnelSettingsGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PacketTunnelSettingsGenerator.swift; sourceTree = "<group>"; };
6F613D9A21DE33B8004B217A /* KeyValueCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyValueCell.swift; sourceTree = "<group>"; };
6F61F1E821B932F700483816 /* WireGuardAppError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WireGuardAppError.swift; sourceTree = "<group>"; };
6F61F1EA21B937EF00483816 /* WireGuardResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WireGuardResult.swift; sourceTree = "<group>"; };
6F628C3C217F09E9003482A3 /* TunnelViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelViewModel.swift; sourceTree = "<group>"; };
@ -266,6 +270,7 @@
6FBA103D21D6B6D70051C35F /* TunnelImporter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TunnelImporter.swift; sourceTree = "<group>"; };
6FBA104121D6BC210051C35F /* ErrorPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ErrorPresenter.swift; sourceTree = "<group>"; };
6FBA104521D7EBFA0051C35F /* TunnelsListTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelsListTableViewController.swift; sourceTree = "<group>"; };
6FDB3C3A21DCF47400A0C0BF /* TunnelDetailTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TunnelDetailTableViewController.swift; sourceTree = "<group>"; };
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 = "<group>"; };
6FDEF7F621863B6100D8FBF6 /* unzip.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = unzip.c; sourceTree = "<group>"; };
@ -362,6 +367,7 @@
isa = PBXGroup;
children = (
6F4DD16A21DA558800690EAE /* TunnelListCell.swift */,
6F613D9A21DE33B8004B217A /* KeyValueCell.swift */,
);
path = View;
sourceTree = "<group>";
@ -499,6 +505,7 @@
children = (
6FBA104521D7EBFA0051C35F /* TunnelsListTableViewController.swift */,
6F4DD16D21DBEA0700690EAE /* ManageTunnelsRootViewController.swift */,
6FDB3C3A21DCF47400A0C0BF /* TunnelDetailTableViewController.swift */,
);
path = ViewController;
sourceTree = "<group>";
@ -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 */,

View File

@ -230,3 +230,7 @@
"macMenuManageTunnels" = "Manage tunnels";
"macMenuImportTunnels" = "Import tunnel(s) from file...";
// Mac detail view fields
"macDetailFieldKey (%@)" = "%@:";

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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()
}
}
}

View File

@ -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 {