From 503ac6c8a2eec262348f11d9648b9d18f380b3ad Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Sat, 9 Mar 2019 16:48:46 +0530 Subject: on-demand: macOS: Auto-complete SSIDs based on currently connected SSID Signed-off-by: Roopesh Chander --- .../WireGuard/UI/macOS/View/OnDemandWiFiControls.swift | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'WireGuard/WireGuard/UI/macOS/View') diff --git a/WireGuard/WireGuard/UI/macOS/View/OnDemandWiFiControls.swift b/WireGuard/WireGuard/UI/macOS/View/OnDemandWiFiControls.swift index bf0e52b..194075d 100644 --- a/WireGuard/WireGuard/UI/macOS/View/OnDemandWiFiControls.swift +++ b/WireGuard/WireGuard/UI/macOS/View/OnDemandWiFiControls.swift @@ -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?) -> [Any]? { + return currentSSIDs.filter { $0.hasPrefix(substring) } + } +} + +private func getCurrentSSIDs() -> [String] { + return CWWiFiClient.shared().interfaces()?.compactMap { $0.ssid() } ?? [] +} -- cgit v1.2.3-59-g8ed1b