Procházet zdrojové kódy

【2025】【Home】缩图逻辑优化

niehaoyu před 2 měsíci
rodič
revize
748fc808de

+ 30 - 26
PDF Office/KMComponentLibrary/KMComponentLibrary/View/Card/ComponentCardFile/ComponentCardFile.swift

@@ -9,13 +9,13 @@ import Cocoa
 import AppKit
 
 public class ComponentCardFile: ComponentBaseXibView {
-
+    
     @IBOutlet var contendBox: NSBox!
     @IBOutlet var iconImage: NSImageView!
     @IBOutlet var infoLabel: NSTextField!
     @IBOutlet var subTitleLabel: NSTextField!
     @IBOutlet var markImage: NSImageView!
-
+    
     @IBOutlet var iconImageWidthConstraint:NSLayoutConstraint!
     @IBOutlet var iconImageHeightConstraint:NSLayoutConstraint!
     @IBOutlet var infoLabelHeightConstraint:NSLayoutConstraint!
@@ -24,12 +24,12 @@ public class ComponentCardFile: ComponentBaseXibView {
     @IBOutlet var iconImageTopConstraint:NSLayoutConstraint!
     @IBOutlet var infoLabelTopConstraint:NSLayoutConstraint!
     @IBOutlet var bottomConstraint:NSLayoutConstraint!
-
+    
     private var _properties : ComponentCardFileProperty = ComponentCardFileProperty()
-     
+    
     public override func draw(_ dirtyRect: NSRect) {
         super.draw(dirtyRect)
-
+        
         
         
     }
@@ -51,28 +51,33 @@ public class ComponentCardFile: ComponentBaseXibView {
     
     public override func awakeFromNib() {
         super.awakeFromNib()
-         
+        
     }
     
     //MARK: - Setter and Getter
     public var properties : ComponentCardFileProperty {
         get {
-           return _properties
+            return _properties
         }
         set {
             _properties = newValue
             
             ComponentLibrary.shared.configCardFileComponent(properties: _properties)
             
-           setupUI()
+            setupUI()
             
-           refreshUI()
+            refreshUI()
         }
     }
     
     
     //MARK: - SetupUI
     func setupUI() {
+        iconImage.wantsLayer = true
+        iconImage.layer?.cornerRadius = ComponentLibrary.shared.getComponentValueFromKey("comp-field/radius") as? CGFloat ?? 4
+        iconImage.layer?.borderWidth = 1
+        iconImage.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider").cgColor
+        
         iconImage.image = properties.icon
         infoLabel.stringValue = properties.text ?? ""
         subTitleLabel.stringValue = properties.subText ?? ""
@@ -82,8 +87,7 @@ public class ComponentCardFile: ComponentBaseXibView {
     }
     
     func refreshUI() {
-        
-       contendBox.cornerRadius = properties.propertyInfo.cornerRadius
+        contendBox.cornerRadius = properties.propertyInfo.cornerRadius
         var borderWidth: CGFloat = 0
         var borderColor: NSColor?
         var fillColor: NSColor?
@@ -91,7 +95,7 @@ public class ComponentCardFile: ComponentBaseXibView {
         var textFont: NSFont?
         var subTextColor: NSColor?
         var subTextFont: NSFont?
-
+        
         if properties.state == .normal {
             borderWidth = properties.propertyInfo.borderWidth
             borderColor = properties.propertyInfo.borderColor_nor
@@ -118,18 +122,18 @@ public class ComponentCardFile: ComponentBaseXibView {
             subTextFont = properties.propertyInfo.subTextFont
         }
         
-       contendBox.borderWidth = borderWidth
+        contendBox.borderWidth = borderWidth
         if let color = borderColor {
-           contendBox.borderColor = color
+            contendBox.borderColor = color
         }
         if let color = fillColor {
-           contendBox.fillColor = color
+            contendBox.fillColor = color
         }
         if let color = textColor {
-           infoLabel.textColor = color
+            infoLabel.textColor = color
         }
         if let font = textFont {
-           infoLabel.font = font
+            infoLabel.font = font
         }
         
         if let color = subTextColor {
@@ -149,15 +153,15 @@ public class ComponentCardFile: ComponentBaseXibView {
         properties.propertyInfo.topIconOffset = iconImageTopConstraint.constant
         properties.propertyInfo.topLabelOffset = infoLabelTopConstraint.constant
         properties.propertyInfo.bottomOffset = bottomConstraint.constant
- 
+        
     }
     
     //MARK: - Public Method
     public func reloadData() {
         
-       setupUI()
+        setupUI()
         
-       refreshUI()
+        refreshUI()
         
     }
     
@@ -165,9 +169,9 @@ public class ComponentCardFile: ComponentBaseXibView {
     public override func mouseEntered(with event: NSEvent) {
         super.mouseEntered(with: event)
         if properties.state != .pressed {
-           properties.state = .hover
+            properties.state = .hover
             
-           refreshUI()
+            refreshUI()
         }
         
     }
@@ -179,12 +183,12 @@ public class ComponentCardFile: ComponentBaseXibView {
     
     public override func mouseExited(with event: NSEvent) {
         super.mouseExited(with: event)
-         
+        
         if properties.state != .pressed {
-           properties.state = .normal
+            properties.state = .normal
             
-           refreshUI()
+            refreshUI()
         }
     }
-
+    
 }

+ 27 - 27
PDF Office/PDF Master/KMClass/KMHomeViewController/HistoryFilesManager/KMFileThumbManager.swift

@@ -18,15 +18,23 @@ class KMFileThumbManager: NSObject {
     
     override init() {
         super.init()
-        
-        if (!FileManager.default.fileExists(atPath: thumbFolderPath!)) {
-            try?FileManager.default.createDirectory(atPath: thumbFolderPath!, withIntermediateDirectories: true, attributes: nil)
-        }
-        
+         
+        let tabClosingNoti = NSNotification.Name("CTTabClosingNotification")
+        NotificationCenter.default.addObserver(self, selector: #selector(tabClosingNotification), name: tabClosingNoti, object: nil)
+
         refreshData()
     }
     
-    private func refreshData() {
+    func refreshData() {
+        
+        if let path = thumbFolderPath {
+            try?FileManager.default.removeItem(atPath: path)
+            
+            if (!FileManager.default.fileExists(atPath: path)) {
+                try?FileManager.default.createDirectory(atPath: path, withIntermediateDirectories: true, attributes: nil)
+            }
+        }
+        
         let urls: Array<URL> = NSDocumentController.shared.recentDocumentURLs
         
         var fileNames: [String] = []
@@ -57,32 +65,21 @@ class KMFileThumbManager: NSObject {
     }
     
     private func generateThumbnail(for pdfURL: URL, _ size: NSSize) -> NSImage? {
-        guard let pdfDocument = CGPDFDocument(pdfURL as CFURL),
-              let pdfPage = pdfDocument.page(at: 1) else {
-            
+        guard let pdfDocument = PDFDocument(url: pdfURL),
+              let pdfPage = pdfDocument.page(at: 0) else {
             return nil
         }
         
-        let pageRect = pdfPage.getBoxRect(.mediaBox)
-        
-        // 创建图形上下文
-        let bitsPerComponent = 8
-        let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue
-        let context = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: bitsPerComponent, bytesPerRow: 0, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: bitmapInfo)
+        if pdfDocument.isLocked || pdfDocument.isEncrypted {
+            return NSImage(named: "file_lock")
+        }
+        let pdfPageBounds = pdfPage.bounds(for: .mediaBox)
+        let scale = min(size.width / pdfPageBounds.width, size.height / pdfPageBounds.height)
         
-        // 绘制PDF页面
-        //        context?.saveGState()
-        //        context?.translateBy(x: 0, y: size.height)
-        //        context?.scaleBy(x: 1.0, y: -1.0) // 反转坐标系
-        context?.scaleBy(x: size.width / pageRect.width, y: size.height / pageRect.height) // 缩放
-        context?.drawPDFPage(pdfPage)
-        context?.restoreGState()
+        let thumbnailSize = CGSize(width: pdfPageBounds.width * scale, height: pdfPageBounds.height * scale)
+        let thumbnail = pdfPage.thumbnail(of: thumbnailSize, for: .mediaBox)
         
-        // 创建NSImage
-        if let cgImage = context?.makeImage() {
-            return NSImage(cgImage: cgImage, size: NSSize(width: size.width, height: size.height))
-        }
-        return nil
+        return thumbnail
     }
     
     public func updateFile(_ fileURL: URL, _ iconSize: CGSize) {
@@ -113,5 +110,8 @@ class KMFileThumbManager: NSObject {
         return nil
     } 
     
+    @objc func tabClosingNotification(_ sender: Notification) {
+        self.refreshData()
+    }
     
 }

+ 12 - 0
PDF Office/PDF Master/KMClass/KMHomeViewController/home.xcassets/file_lock.imageset/Contents.json

@@ -0,0 +1,12 @@
+{
+  "images" : [
+    {
+      "filename" : "file_lock.pdf",
+      "idiom" : "universal"
+    }
+  ],
+  "info" : {
+    "author" : "xcode",
+    "version" : 1
+  }
+}

binární
PDF Office/PDF Master/KMClass/KMHomeViewController/home.xcassets/file_lock.imageset/file_lock.pdf


+ 1 - 1
PDF Office/PDF Master/KMClass/KMPDFViewController/RightSideController/KMRightSideController.swift

@@ -310,7 +310,7 @@ class KMRightSideController: NSViewController {
                 }
             }
             
-            if subToolMode == .Form_text || firstAnnotation is CPDFTextWidgetAnnotation {
+            if (subToolMode == .Form_text && firstAnnotation == nil) || firstAnnotation is CPDFTextWidgetAnnotation {
                 if form_fieldController == nil {
                     form_fieldController = FormsTextFieldController.init()
                 }