2024-11-06 15:18:57 +08:00
|
|
|
import Cocoa
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
2025-03-27 01:05:20 +08:00
|
|
|
struct BlurView: NSViewRepresentable {
|
|
|
|
|
func makeNSView(context: Context) -> NSVisualEffectView {
|
|
|
|
|
let effectView = NSVisualEffectView()
|
|
|
|
|
effectView.blendingMode = .behindWindow
|
|
|
|
|
effectView.material = .hudWindow
|
|
|
|
|
effectView.state = .active
|
|
|
|
|
effectView.isEmphasized = true
|
|
|
|
|
return effectView
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updateNSView(_ nsView: NSVisualEffectView, context: Context) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 15:18:57 +08:00
|
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
2024-11-09 23:15:50 +08:00
|
|
|
private var eventMonitor: Any?
|
|
|
|
|
|
2024-11-06 15:18:57 +08:00
|
|
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
2025-03-06 02:15:24 +08:00
|
|
|
if let window = NSApp.windows.first {
|
2025-03-27 20:51:02 +08:00
|
|
|
window.minSize = NSSize(width: 800, height: 765)
|
2025-03-06 02:15:24 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-09 23:15:50 +08:00
|
|
|
eventMonitor = NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
|
|
|
|
|
if event.modifierFlags.contains(.command) && event.characters?.lowercased() == "q" {
|
|
|
|
|
if let mainWindow = NSApp.mainWindow,
|
|
|
|
|
mainWindow.sheets.isEmpty && !mainWindow.isSheet {
|
2025-02-06 16:29:59 +08:00
|
|
|
_ = self?.applicationShouldTerminate(NSApp)
|
2024-11-09 23:15:50 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return event
|
|
|
|
|
}
|
2025-03-27 15:36:13 +08:00
|
|
|
|
2025-08-17 14:04:13 +08:00
|
|
|
PrivilegedHelperAdapter.shared.executeCommand("id -u") { _ in }
|
2024-11-09 23:15:50 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-06 16:29:59 +08:00
|
|
|
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
|
2025-02-27 23:02:40 +08:00
|
|
|
let hasActiveDownloads = globalNetworkManager.downloadTasks.contains { task in
|
2025-02-06 16:29:59 +08:00
|
|
|
if case .downloading = task.totalStatus { return true }
|
2024-11-09 23:15:50 +08:00
|
|
|
return false
|
|
|
|
|
}
|
2025-02-06 16:29:59 +08:00
|
|
|
|
2024-11-09 23:15:50 +08:00
|
|
|
if hasActiveDownloads {
|
|
|
|
|
Task {
|
2025-02-27 23:02:40 +08:00
|
|
|
for task in globalNetworkManager.downloadTasks {
|
2024-11-09 23:15:50 +08:00
|
|
|
if case .downloading = task.totalStatus {
|
2025-02-27 23:02:40 +08:00
|
|
|
await globalNewDownloadUtils.pauseDownloadTask(
|
2024-11-09 23:15:50 +08:00
|
|
|
taskId: task.id,
|
|
|
|
|
reason: .other(String(localized: "程序即将退出"))
|
|
|
|
|
)
|
2025-02-27 23:02:40 +08:00
|
|
|
await globalNetworkManager.saveTask(task)
|
2024-11-09 23:15:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await MainActor.run {
|
|
|
|
|
let alert = NSAlert()
|
|
|
|
|
alert.messageText = String(localized: "确认退出")
|
|
|
|
|
alert.informativeText = String(localized:"有正在进行的下载任务,确定要退出吗?\n所有下载任务的进度已保存,下次启动可以继续下载")
|
|
|
|
|
alert.alertStyle = .warning
|
|
|
|
|
alert.addButton(withTitle: String(localized:"退出"))
|
|
|
|
|
alert.addButton(withTitle: String(localized:"取消"))
|
|
|
|
|
|
|
|
|
|
let response = alert.runModal()
|
|
|
|
|
if response == .alertSecondButtonReturn {
|
|
|
|
|
Task {
|
2025-02-27 23:02:40 +08:00
|
|
|
for task in globalNetworkManager.downloadTasks {
|
2024-11-09 23:15:50 +08:00
|
|
|
if case .paused = task.totalStatus {
|
2025-02-27 23:02:40 +08:00
|
|
|
await globalNewDownloadUtils.resumeDownloadTask(taskId: task.id)
|
2024-11-09 23:15:50 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
NSApplication.shared.terminate(0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
NSApplication.shared.terminate(nil)
|
|
|
|
|
}
|
2025-02-06 16:29:59 +08:00
|
|
|
return .terminateCancel
|
2024-11-09 23:15:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
|
if let monitor = eventMonitor {
|
|
|
|
|
NSEvent.removeMonitor(monitor)
|
|
|
|
|
}
|
2024-11-06 15:18:57 +08:00
|
|
|
}
|
|
|
|
|
}
|