1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
// 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 statusCheckbox: NSButton = {
let checkbox = NSButton()
checkbox.title = ""
checkbox.setButtonType(.switch)
checkbox.state = .off
return checkbox
}()
let editButton: NSButton = {
let button = NSButton()
button.title = tr("Edit")
button.setButtonType(.momentaryPushIn)
button.bezelStyle = .rounded
return button
}()
let box: NSBox = {
let box = NSBox()
box.titlePosition = .noTitle
box.fillColor = .unemphasizedSelectedContentBackgroundColor
return box
}()
let tunnelsManager: TunnelsManager
let tunnel: TunnelContainer
var tunnelViewModel: TunnelViewModel {
didSet {
updateTableViewModelRows()
}
}
private var tableViewModelRows = [TableViewModelRow]()
private var statusObservationToken: AnyObject?
init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer) {
self.tunnelsManager = tunnelsManager
self.tunnel = tunnel
tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration)
super.init(nibName: nil, bundle: nil)
updateTableViewModelRows()
updateStatus()
statusObservationToken = tunnel.observe(\TunnelContainer.status) { [weak self] _, _ in
self?.updateStatus()
}
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func loadView() {
tableView.dataSource = self
tableView.delegate = self
statusCheckbox.target = self
statusCheckbox.action = #selector(statusCheckboxToggled(sender:))
editButton.target = self
editButton.action = #selector(editButtonClicked)
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
let containerView = NSView()
let bottomControlsContainer = NSLayoutGuide()
containerView.addLayoutGuide(bottomControlsContainer)
containerView.addSubview(box)
containerView.addSubview(scrollView)
containerView.addSubview(statusCheckbox)
containerView.addSubview(editButton)
box.translatesAutoresizingMaskIntoConstraints = false
scrollView.translatesAutoresizingMaskIntoConstraints = false
statusCheckbox.translatesAutoresizingMaskIntoConstraints = false
editButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: scrollView.topAnchor),
containerView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
containerView.leadingAnchor.constraint(equalTo: bottomControlsContainer.leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: bottomControlsContainer.trailingAnchor),
bottomControlsContainer.heightAnchor.constraint(equalToConstant: 32),
scrollView.bottomAnchor.constraint(equalTo: bottomControlsContainer.topAnchor),
bottomControlsContainer.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
statusCheckbox.leadingAnchor.constraint(equalTo: bottomControlsContainer.leadingAnchor),
statusCheckbox.centerYAnchor.constraint(equalTo: bottomControlsContainer.centerYAnchor),
editButton.trailingAnchor.constraint(equalTo: bottomControlsContainer.trailingAnchor),
editButton.centerYAnchor.constraint(equalTo: bottomControlsContainer.centerYAnchor)
])
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: box.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: box.bottomAnchor),
scrollView.leadingAnchor.constraint(equalTo: box.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: box.trailingAnchor)
])
view = containerView
}
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))
}
}
}
func updateStatus() {
let statusText: String
switch tunnel.status {
case .waiting:
statusText = tr("tunnelStatusWaiting")
case .inactive:
statusText = tr("tunnelStatusInactive")
case .activating:
statusText = tr("tunnelStatusActivating")
case .active:
statusText = tr("tunnelStatusActive")
case .deactivating:
statusText = tr("tunnelStatusDeactivating")
case .reasserting:
statusText = tr("tunnelStatusReasserting")
case .restarting:
statusText = tr("tunnelStatusRestarting")
}
statusCheckbox.title = tr(format: "macStatus (%@)", statusText)
let shouldBeChecked = (tunnel.status != .inactive && tunnel.status != .deactivating)
let shouldBeEnabled = (tunnel.status == .active || tunnel.status == .inactive)
statusCheckbox.state = shouldBeChecked ? .on : .off
statusCheckbox.isEnabled = shouldBeEnabled
}
@objc func editButtonClicked() {
print("editButtonClicked")
}
@objc func statusCheckboxToggled(sender: AnyObject?) {
guard let statusCheckbox = sender as? NSButton else { return }
if statusCheckbox.state == .on {
tunnelsManager.startActivation(of: tunnel)
} else if statusCheckbox.state == .off {
tunnelsManager.startDeactivation(of: tunnel)
}
}
}
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()
}
}
}
|