]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
on-demand: macOS: Auto-complete SSIDs based on currently connected SSID
authorRoopesh Chander <roop@roopc.net>
Sat, 9 Mar 2019 11:18:46 +0000 (16:48 +0530)
committerJason A. Donenfeld <Jason@zx2c4.com>
Mon, 18 Mar 2019 05:46:56 +0000 (06:46 +0100)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuard/UI/macOS/View/OnDemandWiFiControls.swift

index bf0e52b82473f9c834cec27cc453f55f0b12df19..194075d430c47621b177e82c16bae7c4ee01110e 100644 (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() } ?? []
+}