Prepare system extension support
This commit is contained in:
@@ -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
|
||||
@@ -48,7 +48,7 @@ public struct EditProfileContentView: View {
|
||||
}
|
||||
}
|
||||
.font(Font.system(.caption2, design: .monospaced))
|
||||
.disableAutocorrection(true)
|
||||
.autocorrectionDisabled()
|
||||
#if os(iOS)
|
||||
.textInputAutocapitalization(.none)
|
||||
.background(Color(UIColor.secondarySystemGroupedBackground))
|
||||
|
||||
@@ -74,6 +74,27 @@ public struct SettingView: View {
|
||||
SharedPreferences.disableMemoryLimit = newValue
|
||||
}
|
||||
}
|
||||
#if os(macOS)
|
||||
if Variant.useSystemExtension {
|
||||
HStack {
|
||||
Button("Update System Extension") {
|
||||
Task {
|
||||
do {
|
||||
if let result = try await SystemExtension.install(forceUpdate: true) {
|
||||
if result == .willCompleteAfterReboot {
|
||||
errorMessage = "Need reboot"
|
||||
errorPresented = true
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
errorPresented = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}.frame(maxWidth: .infinity, alignment: .trailing)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Section("Core") {
|
||||
FormTextItem("Version", version)
|
||||
|
||||
Reference in New Issue
Block a user