Files
sing-box-for-apple/ApplicationLibrary/Views/Dashboard/StartStopButton.swift
T
2025-11-26 22:25:51 +08:00

69 lines
2.0 KiB
Swift

import Library
import NetworkExtension
import SwiftUI
@MainActor
public struct StartStopButton: View {
@EnvironmentObject private var environments: ExtensionEnvironments
public init() {}
public var body: some View {
viewBuilder {
if ApplicationLibrary.inPreview {
Button {} label: {
Label("Stop", systemImage: "stop.fill")
}
.labelStyle(.iconOnly)
} else if let profile = environments.extensionProfile {
Button0().environmentObject(profile)
} else {
Button {} label: {
Label("Start", systemImage: "play.fill")
}
.labelStyle(.iconOnly)
.disabled(true)
}
}
.disabled(environments.emptyProfiles)
}
private struct Button0: View {
@EnvironmentObject private var environments: ExtensionEnvironments
@EnvironmentObject private var profile: ExtensionProfile
@State private var alert: Alert?
var body: some View {
Button {
Task {
await switchProfile(!profile.status.isConnected)
}
} label: {
if !profile.status.isConnected {
Label("Start", systemImage: "play.fill")
} else {
Label("Stop", systemImage: "stop.fill")
}
}
.labelStyle(.iconOnly)
.disabled(!profile.status.isEnabled)
.alertBinding($alert)
}
private nonisolated func switchProfile(_ isEnabled: Bool) async {
do {
if isEnabled {
try await profile.start()
await environments.commandClient.connect()
} else {
try await profile.stop()
}
} catch {
await MainActor.run {
alert = Alert(error)
}
}
}
}
}