Files
Adobe-Downloader/Adobe Downloader/AppDelegate.swift

92 lines
3.4 KiB
Swift
Raw Normal View History

import Cocoa
import SwiftUI
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) {
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
2024-11-09 23:15:50 +08:00
private var eventMonitor: Any?
func applicationDidFinishLaunching(_ notification: Notification) {
if let window = NSApp.windows.first {
2025-03-27 20:51:02 +08:00
window.minSize = NSSize(width: 800, height: 765)
}
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 {
_ = self?.applicationShouldTerminate(NSApp)
2024-11-09 23:15:50 +08:00
return nil
}
}
return event
}
PrivilegedHelperAdapter.shared.executeCommand("id -u") { _ in }
2024-11-09 23:15:50 +08:00
}
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
2025-02-27 23:02:40 +08:00
let hasActiveDownloads = globalNetworkManager.downloadTasks.contains { task in
if case .downloading = task.totalStatus { return true }
2024-11-09 23:15:50 +08:00
return false
}
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)
}
return .terminateCancel
2024-11-09 23:15:50 +08:00
}
deinit {
if let monitor = eventMonitor {
NSEvent.removeMonitor(monitor)
}
}
}