on-demand: macOS: Auto-complete SSIDs based on currently connected SSID

This commit is contained in:
Roopesh Chander 2019-03-09 16:48:46 +05:30 committed by Jason A. Donenfeld
parent 1333f6dc42
commit c01964de4f
1 changed files with 16 additions and 0 deletions

View File

@ -2,6 +2,7 @@
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
import Cocoa
import CoreWLAN
class OnDemandWiFiControls: NSStackView {
@ -38,7 +39,10 @@ class OnDemandWiFiControls: NSStackView {
didSet { updateSSIDControls() }
}
var currentSSIDs: [String]
init() {
currentSSIDs = getCurrentSSIDs()
super.init(frame: CGRect.zero)
onDemandSSIDOptionsPopup.addItems(withTitles: OnDemandWiFiControls.onDemandSSIDOptions.map { $0.localizedUIString })
setViews([onDemandWiFiCheckbox, onDemandSSIDOptionsPopup, onDemandSSIDsField], in: .leading)
@ -56,6 +60,8 @@ class OnDemandWiFiControls: NSStackView {
onDemandSSIDOptionsPopup.target = self
onDemandSSIDOptionsPopup.action = #selector(ssidOptionsPopupValueChanged)
onDemandSSIDsField.delegate = self
updateSSIDControls()
}
@ -95,3 +101,13 @@ class OnDemandWiFiControls: NSStackView {
}
}
}
extension OnDemandWiFiControls: 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() } ?? []
}