aboutsummaryrefslogtreecommitdiffstats
path: root/Sources/WireGuardApp/UI/macOS/View/OnDemandWiFiControls.swift
blob: 790d1ac709f2155d63b65412edec0f940ba93810 (plain) (blame)
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
// SPDX-License-Identifier: MIT
// Copyright © 2018-2020 WireGuard LLC. All Rights Reserved.

import Cocoa
import CoreWLAN

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

    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([])
        tokenField.tokenStyle = .squared
        NSLayoutConstraint.activate([
            tokenField.widthAnchor.constraint(greaterThanOrEqualToConstant: 180)
        ])
        return tokenField
    }()

    override var intrinsicContentSize: NSSize {
        let minHeight: CGFloat = 22
        let height = max(minHeight, keyLabel.intrinsicContentSize.height,
                         onDemandEthernetCheckbox.intrinsicContentSize.height, onDemandWiFiCheckbox.intrinsicContentSize.height,
                         onDemandSSIDOptionsPopup.intrinsicContentSize.height, onDemandSSIDsField.intrinsicContentSize.height)
        return NSSize(width: NSView.noIntrinsicMetric, height: height)
    }

    var onDemandViewModel: ActivateOnDemandViewModel? {
        didSet { updateControls() }
    }

    var currentSSIDs: [String]

    init() {
        currentSSIDs = getCurrentSSIDs()
        super.init(frame: CGRect.zero)

        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

        NSLayoutConstraint.activate([
            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)
        ])

        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)

        onDemandWiFiCheckbox.target = self
        onDemandWiFiCheckbox.action = #selector(wiFiCheckboxToggled)

        onDemandSSIDOptionsPopup.target = self
        onDemandSSIDOptionsPopup.action = #selector(ssidOptionsPopupValueChanged)

        onDemandSSIDsField.delegate = self

        updateControls()
    }

    required init?(coder decoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func saveToViewModel() {
        guard let onDemandViewModel = onDemandViewModel else { return }
        onDemandViewModel.isNonWiFiInterfaceEnabled = onDemandEthernetCheckbox.state == .on
        onDemandViewModel.isWiFiInterfaceEnabled = onDemandWiFiCheckbox.state == .on
        onDemandViewModel.ssidOption = OnDemandControlsRow.onDemandSSIDOptions[onDemandSSIDOptionsPopup.indexOfSelectedItem]
        onDemandViewModel.selectedSSIDs = (onDemandSSIDsField.objectValue as? [String]) ?? []
    }

    func updateControls() {
        guard let onDemandViewModel = onDemandViewModel else { return }
        onDemandEthernetCheckbox.state = onDemandViewModel.isNonWiFiInterfaceEnabled ? .on : .off
        onDemandWiFiCheckbox.state = onDemandViewModel.isWiFiInterfaceEnabled ? .on : .off
        let optionIndex = OnDemandControlsRow.onDemandSSIDOptions.firstIndex(of: onDemandViewModel.ssidOption)
        onDemandSSIDOptionsPopup.selectItem(at: optionIndex ?? 0)
        onDemandSSIDsField.objectValue = onDemandViewModel.selectedSSIDs
        onDemandSSIDOptionsPopup.isHidden = !onDemandViewModel.isWiFiInterfaceEnabled
        onDemandSSIDsField.isHidden = !onDemandViewModel.isWiFiInterfaceEnabled || onDemandViewModel.ssidOption == .anySSID
    }

    @objc func ethernetCheckboxToggled() {
        onDemandViewModel?.isNonWiFiInterfaceEnabled = onDemandEthernetCheckbox.state == .on
    }

    @objc func wiFiCheckboxToggled() {
        onDemandViewModel?.isWiFiInterfaceEnabled = onDemandWiFiCheckbox.state == .on
        updateControls()
    }

    @objc func ssidOptionsPopupValueChanged() {
        let selectedIndex = onDemandSSIDOptionsPopup.indexOfSelectedItem
        onDemandViewModel?.ssidOption = OnDemandControlsRow.onDemandSSIDOptions[selectedIndex]
        onDemandViewModel?.selectedSSIDs = (onDemandSSIDsField.objectValue as? [String]) ?? []
        updateControls()
        if !onDemandSSIDsField.isHidden {
            onDemandSSIDsField.becomeFirstResponder()
        }
    }
}

extension OnDemandControlsRow: NSTokenFieldDelegate {
    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() } ?? []
}