From 35de7263e9752be3d8295aea34b6ee5ffa7c8081 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 24 Jun 2026 19:50:54 +0800 Subject: [PATCH] Fix: app quit on playlist close, request screen capture permission, playlist as panel --- Sources/MiniPlayerApp.swift | 11 +++++++++++ Sources/PlayerBridge.swift | 19 +++++++++++-------- Sources/ScreenRecorder.swift | 6 ++++-- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Sources/MiniPlayerApp.swift b/Sources/MiniPlayerApp.swift index e88a43d..53ca852 100644 --- a/Sources/MiniPlayerApp.swift +++ b/Sources/MiniPlayerApp.swift @@ -2,9 +2,20 @@ import SwiftUI import AVFoundation import Combine import SwiftBricks +#if os(macOS) +import AppKit + +// 防止关闭所有窗口时退出应用 +class AppDel: NSObject, NSApplicationDelegate { + func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { false } +} +#endif @main struct MiniPlayerApp: App { + #if os(macOS) + @NSApplicationDelegateAdaptor(AppDel.self) var appDelegate + #endif @StateObject private var bridge = PlayerBridge() @StateObject private var engine = BricksEngine() diff --git a/Sources/PlayerBridge.swift b/Sources/PlayerBridge.swift index 967d47d..11726c3 100644 --- a/Sources/PlayerBridge.swift +++ b/Sources/PlayerBridge.swift @@ -434,16 +434,19 @@ final class PlayerBridge: ObservableObject { if let win = playlistWindow, win.isVisible { win.close(); playlistWindow = nil; return } - let win = NSWindow( + let panel = NSPanel( contentRect: NSRect(x: 0, y: 0, width: 400, height: 500), - styleMask: [.titled, .closable, .resizable], backing: .buffered, defer: false + styleMask: [.titled, .closable, .resizable, .utilityWindow], backing: .buffered, defer: false ) - win.title = "\(L.playlist) (\(queue.count))" - win.isReleasedWhenClosed = false - win.center() - win.contentView = NSHostingView(rootView: PlaylistWindowView(bridge: self)) - win.makeKeyAndOrderFront(nil) - playlistWindow = win + panel.title = "\(L.playlist) (\(queue.count))" + panel.isReleasedWhenClosed = false + panel.isMovableByWindowBackground = true + panel.hidesOnDeactivate = false + panel.becomesKeyOnlyIfNeeded = true + panel.center() + panel.contentView = NSHostingView(rootView: PlaylistWindowView(bridge: self)) + panel.makeKeyAndOrderFront(NSApp) + playlistWindow = panel #else showPlaylistSheet.toggle() #endif diff --git a/Sources/ScreenRecorder.swift b/Sources/ScreenRecorder.swift index b2414d9..9efd15f 100644 --- a/Sources/ScreenRecorder.swift +++ b/Sources/ScreenRecorder.swift @@ -104,9 +104,11 @@ final class ScreenRecorder: NSObject, AVCaptureFileOutputRecordingDelegate { self.fileOutput = output // 请求屏幕录制权限(macOS 10.15+) - // CGPreflightScreenCaptureAccess 在首次调用时触发权限弹窗 + // CGRequestScreenCaptureAccess 在首次调用时触发权限弹窗 #if canImport(ScreenCaptureKit) - CGPreflightScreenCaptureAccess() + if !CGPreflightScreenCaptureAccess() { + CGRequestScreenCaptureAccess() + } #endif return true