2024-10-29 10:40:11 +00:00
|
|
|
//
|
|
|
|
// AppMenu.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 10/29/24.
|
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
|
|
|
|
import Combine
|
2024-11-03 12:16:13 +00:00
|
|
|
import CommonLibrary
|
2024-10-29 10:40:11 +00:00
|
|
|
import PassepartoutKit
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
public struct AppMenu: View {
|
|
|
|
|
2024-11-01 08:47:50 +00:00
|
|
|
@ObservedObject
|
2024-10-29 10:40:11 +00:00
|
|
|
private var profileManager: ProfileManager
|
|
|
|
|
2024-11-01 08:47:50 +00:00
|
|
|
@ObservedObject
|
2024-10-29 10:40:11 +00:00
|
|
|
private var profileProcessor: ProfileProcessor
|
|
|
|
|
2024-11-01 08:47:50 +00:00
|
|
|
@ObservedObject
|
|
|
|
private var tunnel: ExtendedTunnel
|
2024-10-29 10:40:11 +00:00
|
|
|
|
|
|
|
@StateObject
|
|
|
|
private var model = Model()
|
|
|
|
|
2024-11-01 08:47:50 +00:00
|
|
|
public init(profileManager: ProfileManager, profileProcessor: ProfileProcessor, tunnel: ExtendedTunnel) {
|
|
|
|
self.profileManager = profileManager
|
|
|
|
self.profileProcessor = profileProcessor
|
|
|
|
self.tunnel = tunnel
|
2024-10-29 10:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public var body: some View {
|
|
|
|
versionItem
|
|
|
|
Divider()
|
2024-10-30 09:37:45 +00:00
|
|
|
showToggle
|
2024-10-29 10:40:11 +00:00
|
|
|
loginToggle
|
|
|
|
Divider()
|
|
|
|
profilesList
|
|
|
|
Divider()
|
2024-10-30 09:37:45 +00:00
|
|
|
aboutButton
|
2024-10-29 10:40:11 +00:00
|
|
|
quitButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension AppMenu {
|
|
|
|
var versionItem: some View {
|
|
|
|
Text(BundleConfiguration.mainVersionString)
|
|
|
|
}
|
|
|
|
|
2024-10-30 09:37:45 +00:00
|
|
|
var showToggle: some View {
|
|
|
|
Button(Strings.Global.show) {
|
|
|
|
model.isVisible = true
|
2024-10-29 10:40:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var loginToggle: some View {
|
|
|
|
Toggle(Strings.AppMenu.Items.launchOnLogin, isOn: $model.launchesOnLogin)
|
|
|
|
}
|
|
|
|
|
|
|
|
var profilesList: some View {
|
2024-10-29 13:30:41 +00:00
|
|
|
ForEach(profileManager.headers, id: \.id, content: profileToggle)
|
2024-10-29 10:40:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func profileToggle(for header: ProfileHeader) -> some View {
|
|
|
|
Toggle(header.name, isOn: profileToggleBinding(for: header))
|
|
|
|
}
|
|
|
|
|
|
|
|
func profileToggleBinding(for header: ProfileHeader) -> Binding<Bool> {
|
|
|
|
Binding {
|
|
|
|
header.id == tunnel.currentProfile?.id && tunnel.status != .inactive
|
|
|
|
} set: { isOn in
|
|
|
|
Task {
|
|
|
|
guard let profile = profileManager.profile(withId: header.id) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if isOn {
|
|
|
|
try await tunnel.connect(with: profile, processor: profileProcessor)
|
|
|
|
} else {
|
|
|
|
try await tunnel.disconnect()
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
pp_log(.app, .error, "Unable to toggle profile \(header.id) from menu: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 09:37:45 +00:00
|
|
|
var aboutButton: some View {
|
|
|
|
Button("\(Strings.Global.about)...") {
|
|
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
|
|
NSApp.orderFrontStandardAboutPanel(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-29 10:40:11 +00:00
|
|
|
var quitButton: some View {
|
|
|
|
Button(Strings.AppMenu.Items.quit(BundleConfiguration.mainDisplayName)) {
|
|
|
|
NSApp.terminate(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|