aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/macOS/ViewController
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2019-01-03 01:16:27 +0530
committerRoopesh Chander <roop@roopc.net>2019-01-14 14:52:31 +0530
commitada7db3dca0a2756f5a24f0fcbdfcbfa8f092457 (patch)
tree0c135c02af0c08f698a87a7dbd55525f489971f0 /WireGuard/WireGuard/UI/macOS/ViewController
parentmacOS: Manage tunnels: Add a filler button (diff)
downloadwireguard-apple-ada7db3dca0a2756f5a24f0fcbdfcbfa8f092457.tar.xz
wireguard-apple-ada7db3dca0a2756f5a24f0fcbdfcbfa8f092457.zip
macOS: Manage tunnels: Tunnel detail view
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to '')
-rw-r--r--WireGuard/WireGuard/UI/macOS/ViewController/ManageTunnelsRootViewController.swift44
-rw-r--r--WireGuard/WireGuard/UI/macOS/ViewController/TunnelDetailTableViewController.swift129
-rw-r--r--WireGuard/WireGuard/UI/macOS/ViewController/TunnelsListTableViewController.swift17
3 files changed, 183 insertions, 7 deletions
diff --git a/WireGuard/WireGuard/UI/macOS/ViewController/ManageTunnelsRootViewController.swift b/WireGuard/WireGuard/UI/macOS/ViewController/ManageTunnelsRootViewController.swift
index e63235ab..3f16673f 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 00000000..f1bed18e
--- /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 05aabbef..c99f15b6 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 {