Browse Source

Merge branch 'develop_PDFReaderProV4.6.0' of git.kdan.cc:Mac_PDF/PDF_Office into develop_PDFReaderProV4.6.0

tangchao 7 months ago
parent
commit
9ea879950c

+ 1 - 2
PDF Office/PDF Master/Class/ChromiumTabs/src/Tab/CTTabController.m

@@ -253,12 +253,11 @@ static NSString* const kBrowserThemeDidChangeNotification =
 }
 
 - (IBAction)closeTab:(id)sender {
-    [[NSNotificationCenter defaultCenter] postNotificationName:@"KMTabControllerWillCloseTabs" object:self];
     if ([[self target] respondsToSelector:@selector(closeTab:)]) {
-        [[NSNotificationCenter defaultCenter] postNotificationName:@"KMTabControllerCloseTabs" object:self];
 		[[self target] performSelector:@selector(closeTab:)
 							withObject:[self view]];
 	}
+    [[NSNotificationCenter defaultCenter] postNotificationName:@"KMTabControllerCloseTabs" object:self];
     [[iRate sharedInstance] appLaunched];
 }
 

+ 5 - 3
PDF Office/PDF Master/Class/PDFTools/Merge/View/KMPDFThumbnialPageView.swift

@@ -11,7 +11,8 @@ class KMPDFThumbnialPageView: NSView {
     private var pendingWordItem: DispatchWorkItem?
     var page: CPDFPage? {
         didSet {
-            let image = KMThumbnailCache.shared.thumbnail(for: Int(self.page?.pageIndex() ?? UInt(Int.max)))
+            let path = page?.document.documentURL.path ?? ""
+            let image = KMThumbnailCache.shared.thumbnail(for: path, id: Int(self.page?.pageIndex() ?? UInt(Int.max)))
             if image != nil {
                 self.pageImageIv.image = image
             } else {
@@ -60,7 +61,8 @@ class KMPDFThumbnialPageView: NSView {
     }
     
     func updateThumbnial(needReset: Bool = true) {
-        let image = KMThumbnailCache.shared.thumbnail(for: Int(self.page?.pageIndex() ?? UInt(Int.max)))
+        let path = page?.document.documentURL.path ?? ""
+        let image = KMThumbnailCache.shared.thumbnail(for: path, id: Int(self.page?.pageIndex() ?? UInt(Int.max)))
         if image != nil {
             self.pageImageIv.image = image
         } else {
@@ -68,7 +70,7 @@ class KMPDFThumbnialPageView: NSView {
             self.page?.thumbnail(of: self.bounds.size, needReset: needReset, completion: { [unowned self] image in
                 if (image != nil) {
                     self.pageImageIv.image = image
-                    KMThumbnailCache.shared.addThumbnail(id: Int(pageIndex ?? UInt(Int.max)), image: image!)
+                    KMThumbnailCache.shared.addThumbnail(for: path, id: Int(pageIndex ?? UInt(Int.max)), image: image!)
                 }
             })
         }

+ 58 - 22
PDF Office/PDF Master/Class/PDFWindowController/Side/LeftSide/Model/KMThumbnailCache.swift

@@ -9,36 +9,72 @@ class KMThumbnailCache: NSObject {
     static let shared = KMThumbnailCache()
     
     private let maxCacheSize = 50
-    private var thumbnails: [Int: NSImage] = [:]
-    private var order: [Int] = [] // 记录插入顺序的数组
+    // 外层字典以文档路径为键,内层字典以缩略图 ID 为键
+    private var thumbnails: [String: [Int: NSImage]] = [:]
+    private var order: [String: [Int]] = [:] // 记录每个文档路径的插入顺序
 
-    func addThumbnail(id: Int, image: NSImage) {
-        if thumbnails.count >= maxCacheSize {
-            if let oldestID = order.first {
-                thumbnails.removeValue(forKey: oldestID)
-                order.removeFirst()
+    // 添加缩略图及其文档路径
+    func addThumbnail(for path: String, id: Int, image: NSImage) {
+        // 确保每个路径都有自己的字典和顺序数组
+        if thumbnails[path] == nil {
+            thumbnails[path] = [:]
+            order[path] = []
+        }
+        
+        // 获取当前路径的字典和顺序数组
+        var currentThumbnails = thumbnails[path]!
+        var currentOrder = order[path]!
+        
+        // 检查并维持每个路径的最大缓存大小
+        if currentThumbnails.count >= maxCacheSize {
+            if let oldestID = currentOrder.first {
+                currentThumbnails.removeValue(forKey: oldestID)
+                currentOrder.removeFirst()
             }
         }
         
-        thumbnails[id] = image
-        order.append(id)
+        // 添加新缩略图
+        currentThumbnails[id] = image
+        currentOrder.append(id)
+        
+        // 更新字典和顺序数组
+        thumbnails[path] = currentThumbnails
+        order[path] = currentOrder
     }
-    
-    func thumbnail(for id: Int) -> NSImage? {
-        return thumbnails[id]
+
+    // 根据路径和 ID 获取缩略图
+    func thumbnail(for path: String, id: Int) -> NSImage? {
+        return thumbnails[path]?[id]
     }
-    
-    func removeThumbnail(for id: Int) {
-        if let index = order.firstIndex(of: id) {
-            thumbnails.removeValue(forKey: id)
-            order.remove(at: index)
+
+    // 根据路径和 ID 移除缩略图
+    func removeThumbnail(for path: String, id: Int) {
+        guard var currentThumbnails = thumbnails[path],
+              var currentOrder = order[path] else {
+            print("Path or ID not found")
+            return
+        }
+        
+        if let index = currentOrder.firstIndex(of: id) {
+            currentThumbnails.removeValue(forKey: id)
+            currentOrder.remove(at: index)
         } else {
-            print("ID not found")
+            print("ID not found in the given path")
         }
+        
+        // 更新字典和顺序数组
+        thumbnails[path] = currentThumbnails
+        order[path] = currentOrder
     }
-    
-    func clearCache() {
-        thumbnails.removeAll()
-        order.removeAll()
+
+    // 清空缓存
+    func clearCache(for path: String? = nil) {
+        if let path = path {
+            thumbnails[path]?.removeAll()
+            order[path]?.removeAll()
+        } else {
+            thumbnails.removeAll()
+            order.removeAll()
+        }
     }
 }

+ 7 - 17
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController+Action.swift

@@ -2523,23 +2523,13 @@ extension KMMainViewController {
     }
     
     func closeTab(_ sender: NSNotification) -> Void {
-        let tabController = sender.object as? CTTabController
-        let path = self.document?.documentURL.deletingPathExtension().lastPathComponent
-        if tabController?.title == path {
-            print("closeTab")
-            self.leftSideViewController.clearAnnotationFilterData()
-            self.leftSideViewController.clearNotification()
-        }
-    }
-    
-    func willCloseTab (_ sender: NSNotification) -> Void {
-        KMThumbnailCache.shared.clearCache()
-        let tabController = sender.object as? CTTabController
-        let path = self.document?.documentURL.deletingPathExtension().lastPathComponent
-        if tabController?.title == path {
-            print("closeTab")
-            self.listView.clearScrollCache()
-        }
+//        let tabController = sender.object as? CTTabController
+//        let path = self.document?.documentURL.deletingPathExtension().lastPathComponent
+//        if tabController?.title == path {
+//            print("closeTab")
+//            self.leftSideViewController.clearAnnotationFilterData()
+//            self.leftSideViewController.clearNotification()
+//        }
     }
     
     @IBAction func toggleSplitPDF(_ sender: Any) {

+ 2 - 1
PDF Office/PDF Master/Class/PDFWindowController/ViewController/KMMainViewController.swift

@@ -419,7 +419,6 @@ import Cocoa
         }
 
         NotificationCenter.default.addObserver(self, selector: #selector(rename(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerRename"), object: nil)
-        NotificationCenter.default.addObserver(self, selector: #selector(willCloseTab(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerWillCloseTabs"), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(closeTab(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerCloseTabs"), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(showInFinder(_:)), name: NSNotification.Name.init(rawValue: "KMTabControllerShowInFinder"), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(preferenceDidChangeNotification), name: KMPreferenceManager.didChangeNotification, object: nil)
@@ -1994,6 +1993,8 @@ import Cocoa
     // MARK: - Public Methods
     // 清理数据 [eg. 通知]
     public func clearData() {
+        KMThumbnailCache.shared.clearCache()
+        
         self.removeNotifications()
         if (self.listView.spellingTag() > 0) {
             NSSpellChecker.shared.closeSpellDocument(withTag: self.listView.spellingTag())