Prepare system extension support

This commit is contained in:
世界
2023-07-24 20:08:02 +08:00
parent 8a5c1a80d1
commit 12bf02070e
44 changed files with 1469 additions and 382 deletions
@@ -1,12 +1,60 @@
import Library
import SwiftUI
public struct DashboardView: View {
@Environment(\.extensionProfile) private var extensionProfile
#if os(macOS)
@Environment(\.controlActiveState) private var controlActiveState
@State private var isLoading = true
@State private var systemExtensionInstalled = true
#endif
public init() {}
public var body: some View {
viewBuilder {
#if os(macOS)
if Variant.useSystemExtension {
viewBuilder {
if !systemExtensionInstalled {
FormView {
InstallSystemExtensionButton(reload)
}
} else {
DashboardView0()
}
}.onAppear(perform: reload)
} else {
DashboardView0()
}
#else
DashboardView0()
#endif
}
#if os(macOS)
.onChange(of: controlActiveState, perform: { newValue in
if newValue != .inactive {
if Variant.useSystemExtension {
if !isLoading {
reload()
}
}
}
})
#endif
.navigationTitle("Dashboard")
}
#if os(macOS)
private func reload() {
Task {
systemExtensionInstalled = await SystemExtension.isInstalled()
isLoading = false
}
}
#endif
struct DashboardView0: View {
@Environment(\.extensionProfile) private var extensionProfile
var body: some View {
if ApplicationLibrary.inPreview {
ActiveDashboardView()
} else if let profile = extensionProfile.wrappedValue {
@@ -16,6 +64,6 @@ public struct DashboardView: View {
InstallProfileButton()
}
}
}.navigationTitle("Dashboard")
}
}
}
@@ -0,0 +1,45 @@
#if os(macOS)
import Library
import SwiftUI
public struct InstallSystemExtensionButton: View {
@State private var errorPresented = false
@State private var errorMessage = ""
private let callback: () -> Void
public init(_ callback: @escaping () -> Void) {
self.callback = callback
}
public var body: some View {
Button("Install SystemExtension") {
Task {
await installSystemExtension()
}
}
.alert(isPresented: $errorPresented) {
Alert(
title: Text("Error"),
message: Text(errorMessage),
dismissButton: .default(Text("Ok"))
)
}
}
private func installSystemExtension() async {
do {
if let result = try await SystemExtension.install() {
if result == .willCompleteAfterReboot {
errorMessage = "Need reboot"
errorPresented = true
}
}
callback()
} catch {
errorMessage = error.localizedDescription
errorPresented = true
}
}
}
#endif