Finish independent macOS version

This commit is contained in:
世界
2023-07-25 14:04:13 +08:00
parent 12bf02070e
commit 486a685bb5
12 changed files with 147 additions and 98 deletions
+5 -18
View File
@@ -20,33 +20,20 @@ open class ApplicationDelegate: NSObject, NSApplicationDelegate {
}
Task.detached {
do {
if Variant.useSystemExtension {
if await SystemExtension.isInstalled() {
if let result = try await SystemExtension.install() {
if result == .willCompleteAfterReboot {
return
}
try ProfileUpdateTask.setup()
if launchedAsLogInItem {
if SharedPreferences.startedByUser {
if let profile = try await ExtensionProfile.load() {
try await profile.start()
}
}
}
try await self.postStart(launchedAsLogInItem)
} catch {
NSLog("application setup error: \(error.localizedDescription)")
}
}
}
private func postStart(_ launchedAsLogInItem: Bool) async throws {
try ProfileUpdateTask.setup()
if launchedAsLogInItem {
if SharedPreferences.startedByUser {
if let profile = try await ExtensionProfile.load() {
try await profile.start()
}
}
}
}
public func applicationShouldTerminateAfterLastWindowClosed(_: NSApplication) -> Bool {
!SharedPreferences.menuBarExtraInBackground
}
+63 -38
View File
@@ -10,9 +10,10 @@ public struct MainView: View {
@State private var profileLoading = true
@State private var logClient: LogClient!
@State private var serviceNotificationTitle = ""
@State private var serviceNotificationContent = ""
@State private var serviceNotificationPresented = false
@State private var dialogTitle = ""
@State private var dialogContent = ""
@State private var dialogAction: (() -> Void)?
@State private var dialogPresented = false
public init() {}
public var body: some View {
@@ -20,6 +21,7 @@ public struct MainView: View {
VStack {
SidebarView()
}
.frame(minWidth: 150)
} detail: {
if profileLoading {
@@ -33,45 +35,55 @@ public struct MainView: View {
selection.contentView
}
}
.alert(isPresented: $serviceNotificationPresented, content: {
Alert(
title: Text(serviceNotificationTitle),
message: Text(serviceNotificationContent),
dismissButton: .default(Text("Ok"))
)
})
#if !DEBUG
.onAppear {
ServiceNotification.setServiceNotificationListener { notification in
serviceNotificationTitle = notification.title
serviceNotificationContent = notification.body
serviceNotificationPresented = true
}
}
.onDisappear {
ServiceNotification.removeServiceNotificationListener()
}
.toolbar {
ToolbarItem(placement: .navigation) {
StartStopButton()
}
}
.onChange(of: controlActiveState, perform: { newValue in
if newValue != .inactive {
Task {
await loadProfile()
connectLog()
if Variant.useSystemExtension {
Task.detached {
checkApplicationPath()
}
}
}
})
.onChange(of: selection, perform: { value in
if value == .logs {
connectLog()
#endif
.alert(isPresented: $dialogPresented, content: {
Alert(
title: Text(dialogTitle),
message: Text(dialogContent),
dismissButton: .default(Text("Ok"), action: dialogAction)
)
})
.onAppear {
ServiceNotification.setServiceNotificationListener { notification in
dialogTitle = notification.title
dialogContent = notification.body
dialogAction = nil
dialogPresented = true
}
}
})
.formStyle(.grouped)
.environment(\.selection, $selection)
.environment(\.extensionProfile, $extensionProfile)
.environment(\.logClient, $logClient)
.onDisappear {
ServiceNotification.removeServiceNotificationListener()
}
.toolbar {
ToolbarItem(placement: .navigation) {
StartStopButton()
}
}
.onChange(of: controlActiveState, perform: { newValue in
if newValue != .inactive {
Task {
await loadProfile()
connectLog()
}
}
})
.onChange(of: selection, perform: { value in
if value == .logs {
connectLog()
}
})
.formStyle(.grouped)
.environment(\.selection, $selection)
.environment(\.extensionProfile, $extensionProfile)
.environment(\.logClient, $logClient)
}
private func loadProfile() async {
@@ -99,4 +111,17 @@ public struct MainView: View {
logClient.reconnect()
}
}
private func checkApplicationPath() {
let directoryName = URL(filePath: Bundle.main.bundlePath).deletingLastPathComponent().pathComponents.last
if directoryName != "Applications" {
dialogTitle = "Wrong application location"
dialogContent = "This app needs to be placed under ~/Applications to work."
dialogAction = {
NSWorkspace.shared.selectFile(Bundle.main.bundlePath, inFileViewerRootedAtPath: "")
NSApp.terminate(nil)
}
dialogPresented = true
}
}
}