Atoll/DynamicIsland/components/Shelf/Services/QuickLookService.swift
2026-01-29 12:26:04 +05:30

149 lines
5.1 KiB
Swift

/*
* Atoll (DynamicIsland)
* Copyright (C) 2024-2026 Atoll Contributors
*
* Originally from boring.notch project
* Modified and adapted for Atoll (DynamicIsland)
* See NOTICE for details.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import Foundation
import UniformTypeIdentifiers
import SwiftUI
import QuickLookUI
import AppKit
@MainActor
final class QuickLookService: ObservableObject {
@Published var urls: [URL] = []
@Published var selectedURL: URL?
@Published var isQuickLookOpen: Bool = false
private var previewPanel: QLPreviewPanel?
private var dataSource: QuickLookDataSource?
private var accessingURLs: [URL] = []
private var previewPanelObserver: Any?
func show(urls: [URL], selectFirst: Bool = true, slideshow: Bool = false) {
guard !urls.isEmpty else { return }
stopAccessingCurrentURLs()
accessingURLs = urls.filter { url in
if url.isFileURL {
return url.startAccessingSecurityScopedResource()
}
return true
}
self.urls = accessingURLs
self.isQuickLookOpen = true
if selectFirst {
self.selectedURL = accessingURLs.first
}
// Observe the shared Quick Look preview panel closing so we can relinquish security scope
let panel = QLPreviewPanel.shared()
// Remove any existing observer for previous panel
if let prev = previewPanel {
NotificationCenter.default.removeObserver(self, name: NSWindow.willCloseNotification, object: prev)
}
previewPanel = panel
NotificationCenter.default.addObserver(self, selector: #selector(previewPanelWillClose(_:)), name: NSWindow.willCloseNotification, object: panel)
}
func hide() {
stopAccessingCurrentURLs()
selectedURL = nil
urls.removeAll()
isQuickLookOpen = false
if let panel = previewPanel, panel.isVisible {
panel.orderOut(nil)
}
if let panel = previewPanel {
NotificationCenter.default.removeObserver(self, name: NSWindow.willCloseNotification, object: panel)
previewPanel = nil
}
}
private func stopAccessingCurrentURLs() {
NSLog("Stopping access to \(accessingURLs.count) URLs")
for url in accessingURLs where url.isFileURL {
url.stopAccessingSecurityScopedResource()
}
accessingURLs.removeAll()
// If Quick Look panel was closed externally, also remove observer and clear reference
if let panel = previewPanel {
NotificationCenter.default.removeObserver(self, name: NSWindow.willCloseNotification, object: panel)
previewPanel = nil
}
}
func showQuickLook(urls: [URL]) {
show(urls: urls, selectFirst: true, slideshow: false)
}
func updateSelection(urls: [URL]) {
guard isQuickLookOpen else { return }
show(urls: urls, selectFirst: true)
}
}
extension QuickLookService {
@objc private func previewPanelWillClose(_ notification: Notification) {
guard let panel = notification.object as? QLPreviewPanel, panel === previewPanel else { return }
// Ensure cleanup happens on main actor
Task { @MainActor in
stopAccessingCurrentURLs()
selectedURL = nil
urls.removeAll()
isQuickLookOpen = false
// Remove observer and clear reference
NotificationCenter.default.removeObserver(self, name: NSWindow.willCloseNotification, object: panel)
previewPanel = nil
}
}
}
struct QuickLookPresenter: ViewModifier {
@ObservedObject var service: QuickLookService
func body(content: Content) -> some View {
content
.quickLookPreview($service.selectedURL, in: service.urls)
}
}
extension View {
func quickLookPresenter(using service: QuickLookService) -> some View {
self.modifier(QuickLookPresenter(service: service))
}
}
final class QuickLookDataSource: NSObject, QLPreviewPanelDataSource, QLPreviewPanelDelegate {
private let urls: [URL]
init(urls: [URL]) {
self.urls = urls
super.init()
}
nonisolated func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int {
return urls.count
}
nonisolated func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
guard index >= 0 && index < urls.count else { return nil }
return urls[index] as QLPreviewItem
}
}