2019-03-06 10:00:42 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-04 11:15:29 +00:00
|
|
|
// Copyright © 2018-2020 WireGuard LLC. All Rights Reserved.
|
2019-03-06 10:00:42 +00:00
|
|
|
|
|
|
|
import Cocoa
|
2019-03-09 11:18:46 +00:00
|
|
|
import CoreWLAN
|
2019-03-06 10:00:42 +00:00
|
|
|
|
2019-03-17 14:40:26 +00:00
|
|
|
class OnDemandControlsRow: NSView {
|
|
|
|
let keyLabel: NSTextField = {
|
|
|
|
let keyLabel = NSTextField()
|
|
|
|
keyLabel.stringValue = tr("macFieldOnDemand")
|
|
|
|
keyLabel.isEditable = false
|
|
|
|
keyLabel.isSelectable = false
|
|
|
|
keyLabel.isBordered = false
|
|
|
|
keyLabel.alignment = .right
|
|
|
|
keyLabel.maximumNumberOfLines = 1
|
|
|
|
keyLabel.lineBreakMode = .byTruncatingTail
|
|
|
|
keyLabel.backgroundColor = .clear
|
|
|
|
return keyLabel
|
|
|
|
}()
|
|
|
|
|
|
|
|
let onDemandEthernetCheckbox: NSButton = {
|
|
|
|
let checkbox = NSButton()
|
|
|
|
checkbox.title = tr("tunnelOnDemandEthernet")
|
|
|
|
checkbox.setButtonType(.switch)
|
|
|
|
checkbox.state = .off
|
|
|
|
return checkbox
|
|
|
|
}()
|
2019-03-06 10:00:42 +00:00
|
|
|
|
|
|
|
let onDemandWiFiCheckbox: NSButton = {
|
|
|
|
let checkbox = NSButton()
|
|
|
|
checkbox.title = tr("tunnelOnDemandWiFi")
|
|
|
|
checkbox.setButtonType(.switch)
|
|
|
|
checkbox.state = .off
|
|
|
|
return checkbox
|
|
|
|
}()
|
|
|
|
|
|
|
|
static let onDemandSSIDOptions: [ActivateOnDemandViewModel.OnDemandSSIDOption] = [
|
|
|
|
.anySSID, .onlySpecificSSIDs, .exceptSpecificSSIDs
|
|
|
|
]
|
|
|
|
|
|
|
|
let onDemandSSIDOptionsPopup = NSPopUpButton()
|
|
|
|
|
|
|
|
let onDemandSSIDsField: NSTokenField = {
|
|
|
|
let tokenField = NSTokenField()
|
|
|
|
tokenField.tokenizingCharacterSet = CharacterSet([])
|
2019-03-17 14:40:26 +00:00
|
|
|
tokenField.tokenStyle = .squared
|
2019-03-06 10:00:42 +00:00
|
|
|
NSLayoutConstraint.activate([
|
2019-03-17 14:40:26 +00:00
|
|
|
tokenField.widthAnchor.constraint(greaterThanOrEqualToConstant: 180)
|
2019-03-06 10:00:42 +00:00
|
|
|
])
|
|
|
|
return tokenField
|
|
|
|
}()
|
|
|
|
|
|
|
|
override var intrinsicContentSize: NSSize {
|
|
|
|
let minHeight: CGFloat = 22
|
2019-03-17 14:40:26 +00:00
|
|
|
let height = max(minHeight, keyLabel.intrinsicContentSize.height,
|
|
|
|
onDemandEthernetCheckbox.intrinsicContentSize.height, onDemandWiFiCheckbox.intrinsicContentSize.height,
|
|
|
|
onDemandSSIDOptionsPopup.intrinsicContentSize.height, onDemandSSIDsField.intrinsicContentSize.height)
|
2019-03-06 10:00:42 +00:00
|
|
|
return NSSize(width: NSView.noIntrinsicMetric, height: height)
|
|
|
|
}
|
|
|
|
|
|
|
|
var onDemandViewModel: ActivateOnDemandViewModel? {
|
2019-03-17 14:40:26 +00:00
|
|
|
didSet { updateControls() }
|
2019-03-06 10:00:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 11:18:46 +00:00
|
|
|
var currentSSIDs: [String]
|
|
|
|
|
2019-03-06 10:00:42 +00:00
|
|
|
init() {
|
2019-03-09 11:18:46 +00:00
|
|
|
currentSSIDs = getCurrentSSIDs()
|
2019-03-06 10:00:42 +00:00
|
|
|
super.init(frame: CGRect.zero)
|
2019-03-17 14:40:26 +00:00
|
|
|
|
|
|
|
onDemandSSIDOptionsPopup.addItems(withTitles: OnDemandControlsRow.onDemandSSIDOptions.map { $0.localizedUIString })
|
|
|
|
|
|
|
|
let stackView = NSStackView()
|
|
|
|
stackView.setViews([onDemandEthernetCheckbox, onDemandWiFiCheckbox, onDemandSSIDOptionsPopup, onDemandSSIDsField], in: .leading)
|
|
|
|
stackView.orientation = .horizontal
|
|
|
|
|
|
|
|
addSubview(keyLabel)
|
|
|
|
addSubview(stackView)
|
|
|
|
keyLabel.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
stackView.translatesAutoresizingMaskIntoConstraints = false
|
2019-03-06 10:00:42 +00:00
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
2019-03-17 14:40:26 +00:00
|
|
|
keyLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
|
|
|
stackView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
|
|
|
|
self.leadingAnchor.constraint(equalTo: keyLabel.leadingAnchor),
|
|
|
|
stackView.leadingAnchor.constraint(equalTo: keyLabel.trailingAnchor, constant: 5),
|
|
|
|
stackView.trailingAnchor.constraint(equalTo: self.trailingAnchor)
|
2019-03-06 10:00:42 +00:00
|
|
|
])
|
|
|
|
|
2019-03-17 14:40:26 +00:00
|
|
|
keyLabel.setContentCompressionResistancePriority(.defaultHigh + 2, for: .horizontal)
|
|
|
|
keyLabel.setContentHuggingPriority(.defaultHigh, for: .horizontal)
|
|
|
|
|
|
|
|
let widthConstraint = keyLabel.widthAnchor.constraint(equalToConstant: 150)
|
|
|
|
widthConstraint.priority = .defaultHigh + 1
|
|
|
|
widthConstraint.isActive = true
|
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
onDemandEthernetCheckbox.centerYAnchor.constraint(equalTo: stackView.centerYAnchor),
|
|
|
|
onDemandWiFiCheckbox.lastBaselineAnchor.constraint(equalTo: onDemandEthernetCheckbox.lastBaselineAnchor),
|
|
|
|
onDemandSSIDOptionsPopup.lastBaselineAnchor.constraint(equalTo: onDemandEthernetCheckbox.lastBaselineAnchor),
|
|
|
|
onDemandSSIDsField.lastBaselineAnchor.constraint(equalTo: onDemandEthernetCheckbox.lastBaselineAnchor)
|
|
|
|
])
|
|
|
|
|
|
|
|
onDemandSSIDsField.setContentHuggingPriority(.defaultLow, for: .horizontal)
|
|
|
|
|
|
|
|
onDemandEthernetCheckbox.target = self
|
|
|
|
onDemandEthernetCheckbox.action = #selector(ethernetCheckboxToggled)
|
|
|
|
|
2019-03-06 10:00:42 +00:00
|
|
|
onDemandWiFiCheckbox.target = self
|
|
|
|
onDemandWiFiCheckbox.action = #selector(wiFiCheckboxToggled)
|
|
|
|
|
|
|
|
onDemandSSIDOptionsPopup.target = self
|
|
|
|
onDemandSSIDOptionsPopup.action = #selector(ssidOptionsPopupValueChanged)
|
|
|
|
|
2019-03-09 11:18:46 +00:00
|
|
|
onDemandSSIDsField.delegate = self
|
|
|
|
|
2019-03-17 14:40:26 +00:00
|
|
|
updateControls()
|
2019-03-06 10:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder decoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveToViewModel() {
|
|
|
|
guard let onDemandViewModel = onDemandViewModel else { return }
|
2019-03-17 14:40:26 +00:00
|
|
|
onDemandViewModel.isNonWiFiInterfaceEnabled = onDemandEthernetCheckbox.state == .on
|
2019-03-06 10:00:42 +00:00
|
|
|
onDemandViewModel.isWiFiInterfaceEnabled = onDemandWiFiCheckbox.state == .on
|
2019-03-17 14:40:26 +00:00
|
|
|
onDemandViewModel.ssidOption = OnDemandControlsRow.onDemandSSIDOptions[onDemandSSIDOptionsPopup.indexOfSelectedItem]
|
2019-03-06 10:00:42 +00:00
|
|
|
onDemandViewModel.selectedSSIDs = (onDemandSSIDsField.objectValue as? [String]) ?? []
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:40:26 +00:00
|
|
|
func updateControls() {
|
2019-03-06 10:00:42 +00:00
|
|
|
guard let onDemandViewModel = onDemandViewModel else { return }
|
2019-03-17 14:40:26 +00:00
|
|
|
onDemandEthernetCheckbox.state = onDemandViewModel.isNonWiFiInterfaceEnabled ? .on : .off
|
2019-03-06 10:00:42 +00:00
|
|
|
onDemandWiFiCheckbox.state = onDemandViewModel.isWiFiInterfaceEnabled ? .on : .off
|
2019-03-17 14:40:26 +00:00
|
|
|
let optionIndex = OnDemandControlsRow.onDemandSSIDOptions.firstIndex(of: onDemandViewModel.ssidOption)
|
2019-03-06 10:00:42 +00:00
|
|
|
onDemandSSIDOptionsPopup.selectItem(at: optionIndex ?? 0)
|
|
|
|
onDemandSSIDsField.objectValue = onDemandViewModel.selectedSSIDs
|
|
|
|
onDemandSSIDOptionsPopup.isHidden = !onDemandViewModel.isWiFiInterfaceEnabled
|
|
|
|
onDemandSSIDsField.isHidden = !onDemandViewModel.isWiFiInterfaceEnabled || onDemandViewModel.ssidOption == .anySSID
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:40:26 +00:00
|
|
|
@objc func ethernetCheckboxToggled() {
|
|
|
|
onDemandViewModel?.isNonWiFiInterfaceEnabled = onDemandEthernetCheckbox.state == .on
|
|
|
|
}
|
|
|
|
|
2019-03-06 10:00:42 +00:00
|
|
|
@objc func wiFiCheckboxToggled() {
|
|
|
|
onDemandViewModel?.isWiFiInterfaceEnabled = onDemandWiFiCheckbox.state == .on
|
2019-03-17 14:40:26 +00:00
|
|
|
updateControls()
|
2019-03-06 10:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func ssidOptionsPopupValueChanged() {
|
|
|
|
let selectedIndex = onDemandSSIDOptionsPopup.indexOfSelectedItem
|
2019-03-17 14:40:26 +00:00
|
|
|
onDemandViewModel?.ssidOption = OnDemandControlsRow.onDemandSSIDOptions[selectedIndex]
|
2019-03-06 10:00:42 +00:00
|
|
|
onDemandViewModel?.selectedSSIDs = (onDemandSSIDsField.objectValue as? [String]) ?? []
|
2019-03-17 14:40:26 +00:00
|
|
|
updateControls()
|
2019-03-06 10:00:42 +00:00
|
|
|
if !onDemandSSIDsField.isHidden {
|
|
|
|
onDemandSSIDsField.becomeFirstResponder()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 11:18:46 +00:00
|
|
|
|
2019-03-17 14:40:26 +00:00
|
|
|
extension OnDemandControlsRow: NSTokenFieldDelegate {
|
2019-03-09 11:18:46 +00:00
|
|
|
func tokenField(_ tokenField: NSTokenField, completionsForSubstring substring: String, indexOfToken tokenIndex: Int, indexOfSelectedItem selectedIndex: UnsafeMutablePointer<Int>?) -> [Any]? {
|
|
|
|
return currentSSIDs.filter { $0.hasPrefix(substring) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func getCurrentSSIDs() -> [String] {
|
|
|
|
return CWWiFiClient.shared().interfaces()?.compactMap { $0.ssid() } ?? []
|
|
|
|
}
|