Преглед на файлове

【2025】【综合】Undo,Redo刷新

niehaoyu преди 3 седмици
родител
ревизия
333ecc4e5e

+ 25 - 18
PDF Office/KMComponentLibrary/KMComponentLibrary/View/Message/ComponentMessage.swift

@@ -18,6 +18,8 @@ public class ComponentMessage: ComponentBaseXibView {
     // MARK: Private Property
     private var _properties : ComponentMessageProperty = ComponentMessageProperty()
     
+    private var autoHideWorkItem: DispatchWorkItem?
+    
     public override func draw(_ dirtyRect: NSRect) {
         super.draw(dirtyRect)
         
@@ -166,25 +168,30 @@ public class ComponentMessage: ComponentBaseXibView {
         
     }
     
-    public func showWithPoint(_ point: CGPoint, inView positioningView: NSView?) {
-        var rect = self.frame
-        rect.origin.x = point.x
-        rect.origin.y = point.y
-        rect.size.width = properties.propertyInfo.viewWidth
-        rect.size.height = properties.propertyInfo.viewHeight
-        self.frame = rect
-        
-        positioningView?.addSubview(self)
-        
-        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
-            NSAnimationContext.runAnimationGroup({ context in
-                context.duration = 0.35 // 设置动画持续时间
-                context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
-                self.animator().alphaValue = 0.0 // 动画化透明度
-            }, completionHandler: {
-                self.removeFromSuperview()
-            })
+    public func showSelf(inView view: NSView, autoHideSeconde: CGFloat = 0) {
+        self.animator().alphaValue = 1
+        view.addSubview(self)
+        
+        if autoHideSeconde > 0 {
+            autoHideWorkItem?.cancel()
+            let newWorkItem = DispatchWorkItem {
+                self.autoHideAction()
+            }
+            autoHideWorkItem = newWorkItem
+            if let item = autoHideWorkItem {
+                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + autoHideSeconde, execute: item)
+            }
         }
     }
     
+    func autoHideAction() {
+        NSAnimationContext.runAnimationGroup({ context in
+            context.duration = 0.3 // 设置动画持续时间
+            context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
+            self.animator().alphaValue = 0.0 // 动画化透明度
+        }, completionHandler: {
+            self.removeFromSuperview()
+        })
+    }
+    
 }

+ 1 - 0
PDF Office/PDF Master/Class/Document/KMMainDocument.swift

@@ -591,6 +591,7 @@ typealias KMMainDocumentCloudUploadHanddler = (@escaping(Bool, String)->()) -> (
                 if document == mainViewController!.document {
                     updateChangeCount(.changeDone)
                 }
+                mainViewController?.updateUndoRedoState()
             }
         }
     }

+ 31 - 4
PDF Office/PDF Master/KMClass/KMPDFViewController/KMMainViewController.swift

@@ -331,6 +331,8 @@ struct KMNMWCFlags {
         Task {
             self.addAutoSaveEvent()
         }
+        
+        updateUndoRedoState()
     }
     
     @objc  func editFontColorItemPanelAction(_ sender: Any) {
@@ -1137,7 +1139,7 @@ struct KMNMWCFlags {
                                                      self.componentMessageView.properties.propertyInfo.viewWidth,
                                                      self.componentMessageView.properties.propertyInfo.viewHeight)
         self.componentMessageView.reloadData()
-        self.componentMessageView.show(inView: self.view, autoHideSeconde: 2)
+        self.componentMessageView.showSelf(inView: self.view, autoHideSeconde: 2)
         
         setUpPDFPageNumberToolbar()
         
@@ -1180,7 +1182,7 @@ struct KMNMWCFlags {
                                                          self.componentMessageView.properties.propertyInfo.viewWidth,
                                                          self.componentMessageView.properties.propertyInfo.viewHeight)
             self.componentMessageView.reloadData()
-            self.componentMessageView.show(inView: self.infoSplitCenterView, autoHideSeconde: 2)
+            self.componentMessageView.showSelf(inView: self.infoSplitCenterView, autoHideSeconde: 2)
         }
     }
     
@@ -3274,6 +3276,17 @@ struct KMNMWCFlags {
         self.showPrintWindow()
     }
     
+    //MARK: - Undo,Redo
+    func updateUndoRedoState() {
+        let undo: Bool = self.listView.undoManager?.canUndo ?? false
+        let redo: Bool = self.listView.undoManager?.canRedo ?? false
+        
+        toolbarManager.undoProperty.isDisabled = undo ? false : true
+        toolbarManager.redoProperty.isDisabled = redo ? false : true
+        
+        pdfToolbarController?.reloadUndoRedoButtonState()
+    }
+    
     //MARK: - Flattened
     func saveAsFlattenedPDFAction() {
         DispatchQueue.main.async {
@@ -4471,7 +4484,7 @@ extension KMMainViewController: KMPDFToolbarControllerDelegate {
             updatePDFDisplaySettingView()
             
         } else if toolbarManager.getSubToolItemIdentifys(KMPDFToolbar_Right_Identifiers).contains(itemIdentifier) {
-            if(itemIdentifier == KMPDFToolbar_undo_Identifier) {
+            if (itemIdentifier == KMPDFToolbar_undo_Identifier) {
                 //MARK: -Undo
                 if(listView.isEditing()) {
                     if(listView.canEditTextUndo()){
@@ -4480,6 +4493,13 @@ extension KMMainViewController: KMPDFToolbarControllerDelegate {
                 } else {
                     listView.undoManager?.undo()
                     
+                    self.componentMessageView.properties = ComponentMessageProperty(messageType: .normal_custom, title: KMLocalizedString("Undo"))
+                    self.componentMessageView.frame = CGRectMake((CGRectGetWidth(self.view.frame) - self.componentMessageView.properties.propertyInfo.viewWidth)/2,
+                                                                 CGRectGetHeight(listView.frame) - self.componentMessageView.properties.propertyInfo.viewHeight - 8,
+                                                                 self.componentMessageView.properties.propertyInfo.viewWidth,
+                                                                 self.componentMessageView.properties.propertyInfo.viewHeight)
+                    self.componentMessageView.reloadData()
+                    self.componentMessageView.showSelf(inView: self.view, autoHideSeconde: 2)
                 }
             } else if(itemIdentifier == KMPDFToolbar_redo_Identifier) {
                 //MARK: -Redo
@@ -4489,7 +4509,14 @@ extension KMMainViewController: KMPDFToolbarControllerDelegate {
                     }
                 } else {
                     listView.undoManager?.redo()
-
+                    
+                    self.componentMessageView.properties = ComponentMessageProperty(messageType: .normal_custom, title: KMLocalizedString("Redo"))
+                    self.componentMessageView.frame = CGRectMake((CGRectGetWidth(self.view.frame) - self.componentMessageView.properties.propertyInfo.viewWidth)/2,
+                                                                 CGRectGetHeight(listView.frame) - self.componentMessageView.properties.propertyInfo.viewHeight - 8,
+                                                                 self.componentMessageView.properties.propertyInfo.viewWidth,
+                                                                 self.componentMessageView.properties.propertyInfo.viewHeight)
+                    self.componentMessageView.reloadData()
+                    self.componentMessageView.showSelf(inView: self.view, autoHideSeconde: 2)
                 }
             } else if(itemIdentifier == KMPDFToolbar_fileInfo_Identifier) {
                 self.showFileInfo()

+ 13 - 0
PDF Office/PDF Master/KMClass/KMPDFViewController/Toolbar/KMPDFToolbarController.swift

@@ -454,6 +454,19 @@ class KMPDFToolbarController: NSViewController {
         
     }
     
+    func reloadUndoRedoButtonState() {
+        let subviews = rightToolsView.subviews
+        for view in subviews {
+            if view is ComponentButton {
+                let button = view as! ComponentButton
+                if button.properties.identifier == KMPDFToolbar_undo_Identifier ||
+                    button.properties.identifier == KMPDFToolbar_redo_Identifier {
+                    button.reloadData()
+                }
+            }
+        }
+    }
+    
     //MARK: - 二级工具栏
     func setupSecondToolbar() {
         secondToolBar.view.frame = secondContendView.bounds