Merge branch 'mac-favorite-locations'

This commit is contained in:
Davide De Rosa 2021-07-30 01:41:31 +02:00
commit c813c677aa
5 changed files with 159 additions and 26 deletions

View File

@ -5,11 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 1.16.0 Beta 2683 (2021-07-23)
## Unreleased
### Added
- Support for `--scramble xormask`. [#38](https://github.com/passepartoutvpn/passepartout-apple/issues/38)
- Favorite provider locations.
## 1.15.3 (2021-07-20)

View File

@ -185,6 +185,10 @@ internal enum L10n {
/// Category
internal static let caption = L10n.tr("App", "service.cells.category.caption")
}
internal enum OnlyShowsFavorites {
/// Only show favorite locations
internal static let caption = L10n.tr("App", "service.cells.only_shows_favorites.caption")
}
internal enum Vpn {
internal enum TurnOff {
/// Disable VPN

View File

@ -43,10 +43,14 @@ class ProviderServiceView: NSView {
@IBOutlet private weak var popupArea: NSPopUpButton!
@IBOutlet private weak var checkOnlyShowsFavorites: NSButton!
@IBOutlet private weak var labelLastInfrastructureUpdate: NSTextField!
@IBOutlet private weak var buttonRefreshInfrastructure: NSButton!
@IBOutlet private weak var buttonFavorite: NSButton!
var isEnabled: Bool = true {
didSet {
popupCategory.isEnabled = isEnabled
@ -73,8 +77,19 @@ class ProviderServiceView: NSView {
}
}
private var onlyShowsFavorites: Bool = false {
didSet {
guard let profile = profile else {
return
}
reloadHierarchy(withProfile: profile)
}
}
private var categories: [PoolCategory] = []
private var filteredGroupsByCategory: [String: [PoolGroup]] = [:]
weak var delegate: ProviderServiceViewDelegate?
override func viewWillMove(toSuperview newSuperview: NSView?) {
@ -82,7 +97,12 @@ class ProviderServiceView: NSView {
labelCategoryCaption.stringValue = L10n.App.Service.Cells.Category.caption.asCaption
labelLocationCaption.stringValue = L10n.Core.Service.Cells.Provider.Pool.caption.asCaption
checkOnlyShowsFavorites.title = L10n.App.Service.Cells.OnlyShowsFavorites.caption
checkOnlyShowsFavorites.state = .off
buttonRefreshInfrastructure.image = NSImage(named: NSImage.refreshTemplateName)
buttonFavorite.image = NSImage(named: NSImage.bookmarksTemplateName)
updateFavoriteState()
}
// MARK: Actions
@ -90,16 +110,12 @@ class ProviderServiceView: NSView {
@IBAction private func selectCategory(_ sender: Any?) {
loadLocations()
loadAreas()
if let pool = selectedPool() {
delegate?.providerView(self, didSelectPool: pool)
}
delegateSelectedPool()
}
@IBAction private func selectLocation(_ sender: Any?) {
loadAreas()
if let pool = selectedPool() {
delegate?.providerView(self, didSelectPool: pool)
}
delegateSelectedPool()
}
@IBAction private func selectArea(_ sender: Any?) {
@ -113,6 +129,44 @@ class ProviderServiceView: NSView {
delegate?.providerViewDidRequestInfrastructureRefresh(self)
}
@IBAction private func toggleFavorite(_ sender: Any?) {
guard let category = selectedCategory(), let group = selectedGroup() else {
return
}
let groupId = group.uniqueId(in: category)
let isFavorite = (buttonFavorite.state == .on)
if isFavorite {
profile?.favoriteGroupIds = profile?.favoriteGroupIds ?? []
profile?.favoriteGroupIds?.append(groupId)
} else {
profile?.favoriteGroupIds?.removeAll { $0 == groupId }
}
// disable favorite while filtering favorites
//
// 1. reload list to select first
// 2. if last, disable filter
if onlyShowsFavorites, let profile = profile, buttonFavorite.state == .off {
if popupLocation.numberOfItems == 1 {
onlyShowsFavorites = false
checkOnlyShowsFavorites.state = .off
}
reloadHierarchy(withProfile: profile)
delegateSelectedPool()
}
if profile?.favoriteGroupIds?.isEmpty ?? true {
checkOnlyShowsFavorites.state = .off
checkOnlyShowsFavorites.isEnabled = false
} else {
checkOnlyShowsFavorites.isEnabled = true
}
}
@IBAction private func toggleOnlyShowsFavorites(_ sender: Any?) {
onlyShowsFavorites = (checkOnlyShowsFavorites.state == .on)
delegateSelectedPool()
}
// MARK: Helpers
func reloadData() {
@ -121,13 +175,16 @@ class ProviderServiceView: NSView {
private func reloadHierarchy(withProfile profile: ProviderConnectionProfile) {
categories = profile.infrastructure.categories.sorted { $0.name.lowercased() < $1.name.lowercased() }
popupCategory.removeAllItems()
filteredGroupsByCategory.removeAll()
let menu = NSMenu()
categories.forEach {
categories.forEach { category in
let item = NSMenuItem()
item.title = !$0.name.isEmpty ? $0.name.capitalized : L10n.Core.Global.Values.default
item.representedObject = $0 // category
item.title = !category.name.isEmpty ? category.name.capitalized : L10n.Core.Global.Values.default
item.representedObject = category
menu.addItem(item)
setFilteredGroups(category.groups, forCategory: category)
}
popupCategory.menu = menu
@ -147,6 +204,8 @@ class ProviderServiceView: NSView {
if let lastInfrastructureUpdate = InfrastructureFactory.shared.modificationDate(forName: profile.name) {
labelLastInfrastructureUpdate.stringValue = L10n.Core.Service.Sections.ProviderInfrastructure.footer(lastInfrastructureUpdate.timestamp)
}
checkOnlyShowsFavorites.isEnabled = !(profile.favoriteGroupIds?.isEmpty ?? true)
}
// FIXME: inefficient, cache sorted pools
@ -154,7 +213,7 @@ class ProviderServiceView: NSView {
var a = 0, b = 0, c = 0
for category in categories {
b = 0
for group in category.groups {
for group in filteredGroups(forCategory: category) {
c = 0
for pool in group.pools.sortedPools() {
if pool.id == profile?.poolId {
@ -176,7 +235,7 @@ class ProviderServiceView: NSView {
popupLocation.removeAllItems()
let menu = NSMenu()
category.groups.sorted().forEach {
filteredGroups(forCategory: category).forEach {
let item = NSMenuItem(title: $0.localizedCountry, action: nil, keyEquivalent: "")
item.image = $0.logo
item.representedObject = $0 // group
@ -210,6 +269,14 @@ class ProviderServiceView: NSView {
popupArea.isHidden = menu.items.isEmpty
}
private func selectedCategory() -> PoolCategory? {
return popupCategory.selectedItem?.representedObject as? PoolCategory
}
private func selectedGroup() -> PoolGroup? {
return popupLocation.selectedItem?.representedObject as? PoolGroup
}
private func selectedPool() -> Pool? {
guard popupArea.numberOfItems > 0 else {
guard let group = popupLocation.selectedItem?.representedObject as? PoolGroup else {
@ -219,4 +286,33 @@ class ProviderServiceView: NSView {
}
return popupArea.itemArray.first?.representedObject as? Pool
}
private func updateFavoriteState() {
guard let category = selectedCategory(), let group = selectedGroup() else {
return
}
let groupId = group.uniqueId(in: category)
let isFavorite = profile?.favoriteGroupIds?.contains(groupId) ?? false
buttonFavorite.state = isFavorite ? .on : .off
}
private func delegateSelectedPool() {
if let pool = selectedPool() {
updateFavoriteState()
delegate?.providerView(self, didSelectPool: pool)
}
}
private func filteredGroups(forCategory category: PoolCategory) -> [PoolGroup] {
return filteredGroupsByCategory[category.name] ?? []
}
private func setFilteredGroups(_ groups: [PoolGroup], forCategory category: PoolCategory) {
filteredGroupsByCategory[category.name] = category.groups.filter {
guard !onlyShowsFavorites else {
return profile?.favoriteGroupIds?.contains($0.uniqueId(in: category)) ?? false
}
return true
}.sorted()
}
}

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="18122" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="18122"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
@ -10,11 +10,11 @@
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="sHl-x0-m0K" customClass="ProviderServiceView" customModule="Passepartout" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="450" height="120"/>
<rect key="frame" x="0.0" y="0.0" width="450" height="138"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="lnT-CF-seE">
<rect key="frame" x="-2" y="101" width="114" height="17"/>
<rect key="frame" x="-2" y="120" width="114" height="16"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="right" title="C:" id="nKl-9a-xar">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
@ -22,7 +22,7 @@
</textFieldCell>
</textField>
<popUpButton horizontalHuggingPriority="249" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="VhE-Zb-FmE">
<rect key="frame" x="118" y="96" width="285" height="25"/>
<rect key="frame" x="117" y="114" width="287" height="25"/>
<popUpButtonCell key="cell" type="push" title="Category" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="qEL-KY-U1o" id="caC-KG-3AH">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
@ -37,7 +37,7 @@
</connections>
</popUpButton>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Q5f-wF-CyU">
<rect key="frame" x="118" y="77" width="77" height="14"/>
<rect key="frame" x="118" y="96" width="77" height="14"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Last updated:" id="rXU-3s-USr">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
@ -45,7 +45,7 @@
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="kSn-Qv-xSD" userLabel="Label Location Caption">
<rect key="frame" x="-2" y="33" width="114" height="17"/>
<rect key="frame" x="-2" y="58" width="114" height="16"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="right" title="L:" id="9nM-rm-VgR">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
@ -53,7 +53,7 @@
</textFieldCell>
</textField>
<popUpButton verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Ri0-Ls-IYO">
<rect key="frame" x="118" y="28" width="285" height="25"/>
<rect key="frame" x="117" y="52" width="287" height="25"/>
<popUpButtonCell key="cell" type="push" title="Location" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="0Iw-Gz-qU0" id="7cD-BZ-osm">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
@ -70,7 +70,7 @@
</connections>
</popUpButton>
<textField hidden="YES" horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="HF2-PC-G6P" userLabel="Label Area Caption">
<rect key="frame" x="-2" y="2" width="114" height="17"/>
<rect key="frame" x="-2" y="28" width="114" height="16"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="right" title="A:" id="UXF-Ej-6Ua">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
@ -78,7 +78,7 @@
</textFieldCell>
</textField>
<popUpButton verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="IsL-OU-zXZ">
<rect key="frame" x="118" y="-3" width="285" height="25"/>
<rect key="frame" x="117" y="22" width="287" height="25"/>
<popUpButtonCell key="cell" type="push" title="Area" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="glB-Ir-wE8" id="7qV-EM-r3X">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="menu"/>
@ -93,7 +93,7 @@
</connections>
</popUpButton>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="bE9-EU-Xzq">
<rect key="frame" x="404" y="92" width="52" height="32"/>
<rect key="frame" x="403" y="111" width="54" height="32"/>
<constraints>
<constraint firstAttribute="width" constant="40" id="Y6u-np-cEO"/>
</constraints>
@ -105,20 +105,47 @@
<action selector="refreshInfrastructure:" target="sHl-x0-m0K" id="aPQ-BZ-Q9m"/>
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="JVN-JS-Vr7">
<rect key="frame" x="403" y="49" width="54" height="32"/>
<constraints>
<constraint firstAttribute="width" constant="40" id="zrS-eu-hUt"/>
</constraints>
<buttonCell key="cell" type="push" title="FAV" bezelStyle="rounded" alignment="center" borderStyle="border" inset="2" id="IWi-Ee-l6X">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES" changeBackground="YES" changeGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="toggleFavorite:" target="sHl-x0-m0K" id="jDS-u5-bI6"/>
</connections>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="x38-cx-Heq">
<rect key="frame" x="118" y="-1" width="282" height="18"/>
<buttonCell key="cell" type="check" title="&lt;only_fav&gt;" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="SoW-Ic-TiJ">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="toggleOnlyShowsFavorites:" target="sHl-x0-m0K" id="Ef1-c5-WqZ"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstItem="VhE-Zb-FmE" firstAttribute="top" secondItem="sHl-x0-m0K" secondAttribute="top" id="3zG-8f-Ij8"/>
<constraint firstItem="HF2-PC-G6P" firstAttribute="trailing" secondItem="lnT-CF-seE" secondAttribute="trailing" id="3zL-CZ-mUe"/>
<constraint firstItem="x38-cx-Heq" firstAttribute="top" secondItem="IsL-OU-zXZ" secondAttribute="bottom" constant="10" id="8kl-U6-9mj"/>
<constraint firstAttribute="trailing" secondItem="bE9-EU-Xzq" secondAttribute="trailing" id="Cou-HK-JNt"/>
<constraint firstItem="Ri0-Ls-IYO" firstAttribute="top" secondItem="Q5f-wF-CyU" secondAttribute="bottom" constant="20" id="DWa-gh-eYd"/>
<constraint firstItem="lnT-CF-seE" firstAttribute="centerY" secondItem="VhE-Zb-FmE" secondAttribute="centerY" id="ElO-QZ-sgv"/>
<constraint firstItem="Q5f-wF-CyU" firstAttribute="leading" secondItem="VhE-Zb-FmE" secondAttribute="leading" id="KFp-CA-AHU"/>
<constraint firstItem="JVN-JS-Vr7" firstAttribute="centerY" secondItem="Ri0-Ls-IYO" secondAttribute="centerY" id="LFQ-32-SgB"/>
<constraint firstItem="HF2-PC-G6P" firstAttribute="centerY" secondItem="IsL-OU-zXZ" secondAttribute="centerY" id="NTF-FX-jMd"/>
<constraint firstAttribute="bottom" secondItem="IsL-OU-zXZ" secondAttribute="bottom" id="Ryi-JT-4C3"/>
<constraint firstItem="HF2-PC-G6P" firstAttribute="leading" secondItem="lnT-CF-seE" secondAttribute="leading" id="Src-yJ-Rgx"/>
<constraint firstItem="Ri0-Ls-IYO" firstAttribute="trailing" secondItem="VhE-Zb-FmE" secondAttribute="trailing" id="TJa-4c-18u"/>
<constraint firstAttribute="trailing" secondItem="JVN-JS-Vr7" secondAttribute="trailing" id="UOH-nd-6bl"/>
<constraint firstItem="IsL-OU-zXZ" firstAttribute="trailing" secondItem="Ri0-Ls-IYO" secondAttribute="trailing" id="YCr-qy-rGi"/>
<constraint firstItem="kSn-Qv-xSD" firstAttribute="trailing" secondItem="lnT-CF-seE" secondAttribute="trailing" id="aP2-Pc-I1b"/>
<constraint firstItem="Q5f-wF-CyU" firstAttribute="top" secondItem="VhE-Zb-FmE" secondAttribute="bottom" constant="8" id="cf6-K8-a1Q"/>
<constraint firstItem="JVN-JS-Vr7" firstAttribute="leading" secondItem="Ri0-Ls-IYO" secondAttribute="trailing" constant="10" id="cqg-c7-ybl"/>
<constraint firstItem="IsL-OU-zXZ" firstAttribute="leading" secondItem="Ri0-Ls-IYO" secondAttribute="leading" id="eVk-E1-11K"/>
<constraint firstItem="Ri0-Ls-IYO" firstAttribute="leading" secondItem="VhE-Zb-FmE" secondAttribute="leading" id="fw7-wV-jJg"/>
<constraint firstItem="VhE-Zb-FmE" firstAttribute="leading" secondItem="sHl-x0-m0K" secondAttribute="leading" constant="120" id="gAw-cm-xhQ"/>
@ -127,12 +154,16 @@
<constraint firstItem="kSn-Qv-xSD" firstAttribute="leading" secondItem="lnT-CF-seE" secondAttribute="leading" id="kHC-Ot-lVj"/>
<constraint firstItem="lnT-CF-seE" firstAttribute="leading" secondItem="sHl-x0-m0K" secondAttribute="leading" id="oho-R0-EAO"/>
<constraint firstItem="VhE-Zb-FmE" firstAttribute="leading" secondItem="lnT-CF-seE" secondAttribute="trailing" constant="10" id="qE0-Vz-1gF"/>
<constraint firstItem="x38-cx-Heq" firstAttribute="leading" secondItem="VhE-Zb-FmE" secondAttribute="leading" id="sZA-Zo-Nyu"/>
<constraint firstItem="x38-cx-Heq" firstAttribute="trailing" secondItem="VhE-Zb-FmE" secondAttribute="trailing" id="ujm-v3-IYq"/>
<constraint firstItem="bE9-EU-Xzq" firstAttribute="centerY" secondItem="VhE-Zb-FmE" secondAttribute="centerY" id="vUe-PU-0Dw"/>
<constraint firstItem="Ri0-Ls-IYO" firstAttribute="top" secondItem="Q5f-wF-CyU" secondAttribute="bottom" constant="25" id="w11-ht-xCL"/>
<constraint firstItem="bE9-EU-Xzq" firstAttribute="leading" secondItem="VhE-Zb-FmE" secondAttribute="trailing" constant="10" id="xNf-cQ-LJw"/>
<constraint firstAttribute="bottom" secondItem="x38-cx-Heq" secondAttribute="bottom" id="yF3-vL-YrF"/>
</constraints>
<connections>
<outlet property="buttonFavorite" destination="JVN-JS-Vr7" id="CW9-0i-1uL"/>
<outlet property="buttonRefreshInfrastructure" destination="bE9-EU-Xzq" id="fna-ol-MOT"/>
<outlet property="checkOnlyShowsFavorites" destination="x38-cx-Heq" id="yty-3n-DMY"/>
<outlet property="labelCategoryCaption" destination="lnT-CF-seE" id="QKu-yJ-nkS"/>
<outlet property="labelLastInfrastructureUpdate" destination="Q5f-wF-CyU" id="SpA-o9-pY2"/>
<outlet property="labelLocationCaption" destination="kSn-Qv-xSD" id="wKk-lh-g1R"/>
@ -140,7 +171,7 @@
<outlet property="popupCategory" destination="VhE-Zb-FmE" id="CN5-KE-lFR"/>
<outlet property="popupLocation" destination="Ri0-Ls-IYO" id="gaf-V2-msA"/>
</connections>
<point key="canvasLocation" x="139" y="198"/>
<point key="canvasLocation" x="139" y="233.5"/>
</customView>
</objects>
</document>

View File

@ -37,6 +37,7 @@
"service.cells.vpn.turn_off.caption" = "Disable VPN";
"service.cells.category.caption" = "Category";
"service.cells.addresses.caption" = "Addresses";
"service.cells.only_shows_favorites.caption" = "Only show favorite locations";
"endpoint.cells.address" = "Address";
"endpoint.cells.protocol" = "Protocol";