Bläddra i källkod

【综合】SKPresentationOptionsSheetController整理代码

tangchao 1 år sedan
förälder
incheckning
10e6bb3f70

+ 11 - 0
PDF Office/PDF Master/Class/Common/OC/KMTransitionController/SKTransitionController.m

@@ -525,6 +525,17 @@ static inline CGRect scaleRect(NSRect rect, CGFloat scale) {
     }];
 }
 
+- (void)setPageTransitions:(NSArray *)pageTransitions {
+    NSArray *oldValue = self.pageTransitions;
+    _pageTransitions = pageTransitions;
+    
+    [self.delegate transitionController:self valueDidChanged:@{
+        KMPageTransitionsName : @{
+            KMOldValueName : oldValue ? oldValue : @[],
+            KMNewValueName : pageTransitions ? pageTransitions : @[]
+        }
+    }];
+}
 
 @end
 

+ 147 - 143
PDF Office/PDF Master/Class/PDFWindowController/MainWindowController/SKPresentationOptionsSheetController.swift

@@ -23,7 +23,7 @@ let TABLE_OFFSET: CGFloat = 8.0
 var SKTransitionPropertiesObservationContext = UnsafeMutableRawPointer(mutating: "SKTransitionPropertiesObservationContext")
 var SKPDFViewTransitionsObservationContext: CChar?
 
-class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate {
+class SKPresentationOptionsSheetController: NSWindowController {
     @IBOutlet var notesDocumentPopUpButton: NSPopUpButton!
     @IBOutlet var tableView: KMBotaTableView!
     @IBOutlet var separateCheckButton: NSButton!
@@ -45,61 +45,51 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
     var closeCallBack: closePresentationControllerCallBack?
     var separate = false
     var transition: KMTransitionInfo = KMTransitionInfo()
-    var transitions: NSArray?{
-        willSet{
-           
+    private var transitions_: NSArray?
+    var transitions: NSArray? {
+        get {
+            return self.transitions_
         }
-        didSet{
-            if transitions != oldValue {
+        set{
+            if self.transitions_ != newValue {
 //                (undoRedoManager?.prepare(withInvocationTarget: self) as AnyObject).transitions = transitions
+                self.transitions_ = newValue
                 stopObservingTransitions(infos: transitions as! [KMTransitionInfo])
                 startObservingTransitions(transitions as! [KMTransitionInfo])
             }
         }
     }
-    var currentTransitions: NSArray?{
+    var currentTransitions: NSArray? {
         get{
-            return separate ? transitions : [transition as Any]
-        }
-        set{
-            
+            return self.separate ? self.transitions : [transition as Any]
         }
     }
-    var pageTransitions: NSArray?{
+    var pageTransitions: NSArray? {
         get{
-            if separate && transitions?.count ?? 0 > 0 {
+            if self.separate && self.transitions?.count ?? 0 > 0 {
                 return [transitions?.value(forKey: PROPERTIES_KEY) as Any]
-            } else {
-                return nil
             }
-        }
-        set{
-            
+            return nil
         }
     }
-    var notesDocument: NSDocument?{
+    var notesDocument: NSDocument? {
         get{
-            return notesDocumentPopUpButton.selectedItem?.representedObject as? NSDocument
-        }
-        set{
-            
+            _ = self.window
+            return self.notesDocumentPopUpButton.selectedItem?.representedObject as? NSDocument
         }
     }
-    var isScrolling: Bool{
+    var isScrolling: Bool {
         get{
-            if let scroller = tableView?.enclosingScrollView?.verticalScroller as? KMScroller {
-                return scroller.isScrolling
-            }
-            return false
-        }
-        set{
-            
+            let scroller = self.tableView?.enclosingScrollView?.verticalScroller as? KMScroller
+            return scroller?.isScrolling ?? false
         }
     }
     var undoRedoManager = UndoManager()
     var controller: KMMainViewController?
     var transitionController: SKTransitionController!
     
+    private weak var proxyDelegate_: SKTransitionControllerDelegate?
+    
     class func keyPathsForValuesAffectingCurrentTransitions() -> Set<String> { 
         return Set<String>(["separate", "transitions", "transition"])
     }
@@ -132,88 +122,54 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
             self.effectPopUpButton.addItem(withTitle: SKTransitionController.localizedName(for: SKAnimationTransitionStyle(rawValue: UInt(i)) ?? .noTransition))
             self.effectPopUpButton.lastItem?.tag = i
         }
-
         self.notesDocumentPopUpButton.item(at: 0)?.title = NSLocalizedString("None", comment: "Menu item title")
 
-        creatTransitionController()
-        transition.transitionStyle = transitionController?.transitionStyle ?? .noTransition
-        transition.duration = Float(transitionController?.duration ?? 0)
-        transition.shouldRestrict = transitionController?.shouldRestrict ?? false
-        startObservingTransitions([transition])
+//        creatTransitionController()
+        let transitionController = self.controller?.listView.transitionController
+        self.proxyDelegate_ = transitionController?.delegate
+        transitionController?.delegate = self
+        self.transition.transitionStyle = transitionController?.transitionStyle ?? .noTransition
+        self.transition.duration = Float(transitionController?.duration ?? 0)
+        self.transition.shouldRestrict = transitionController?.shouldRestrict ?? false
+        self.startObservingTransitions([self.transition])
 
-        separateCheckButton.sizeToFit()
+        self.separateCheckButton.sizeToFit()
 //        transitionControls.lastObject.sizeToFit()
 
         SKAutoSizeButtons(buttons as? [Any], true)
-
         let dw = SKAutoSizeLabelFields(transitionLabels as? [Any], transitionControls as? [Any], false)
-
         if abs(dw) > 0.0 {
-            SKResizeWindow(window, dw)
-            SKShiftAndResizeViews(boxes as? [Any], -dw, dw)
-            SKShiftAndResizeView(separateCheckButton, -dw, 0.0)
+            SKResizeWindow(self.window, dw)
+            SKShiftAndResizeViews(self.boxes as? [Any], -dw, dw)
+            SKShiftAndResizeView(self.separateCheckButton, -dw, 0.0)
         }
 
         // collapse the table
-        window?.setFrame(NSInsetRect(window?.frame ?? .zero, 0.5 * (NSWidth(tableView.enclosingScrollView!.frame) + TABLE_OFFSET), 0.0), display: false)
-
-        tableView.registerForDraggedTypes(KMTransitionInfo.readableTypesForPasteboard(pasteboard: NSPasteboard(name: NSPasteboard.Name.drag)) as! [NSPasteboard.PasteboardType])
-        
-        tableView.delegate = self
-        tableView.dataSource = self
-        tableView.botaDelegate = self
-
-        tableView.setTypeSelectHelper(SKTypeSelectHelper(matchOption: .SKFullStringMatch))
-
-        tableView.hasImageToolTips = true
+        self.window?.setFrame(NSInsetRect(window?.frame ?? .zero, 0.5 * (NSWidth(tableView.enclosingScrollView!.frame) + TABLE_OFFSET), 0.0), display: false)
 
-        tableView.backgroundColor = NSColor.mainSourceListBackgroundColor()
+        self.tableView.registerForDraggedTypes(KMTransitionInfo.readableTypesForPasteboard(pasteboard: NSPasteboard(name: NSPasteboard.Name.drag)) as! [NSPasteboard.PasteboardType])
+        self.tableView.delegate = self
+        self.tableView.dataSource = self
+        self.tableView.botaDelegate = self
+        self.tableView.setTypeSelectHelper(SKTypeSelectHelper(matchOption: .SKFullStringMatch))
+        self.tableView.hasImageToolTips = true
+        self.tableView.backgroundColor = NSColor.mainSourceListBackgroundColor()
 
         if (transitionController?.pageTransitions != nil && transitionController?.pageTransitions.count ?? 0 > 0) {
             undoManager?.disableUndoRegistration()
-            separate = true
+            self.separate = true
             undoManager?.enableUndoRegistration()
         }
 
         // set the current notes document and observe changes for the popup
-        handleDocumentsDidChangeNotification(notification: nil)
-        let docIndex = notesDocumentPopUpButton.indexOfItem(withRepresentedObject: controller?.myDocument/*presentationNotesDocument*/)
-        notesDocumentPopUpButton.selectItem(at: docIndex > 0 ? docIndex : 0)
+        self.handleDocumentsDidChangeNotification(notification: nil)
+        let docIndex = self.notesDocumentPopUpButton.indexOfItem(withRepresentedObject: self.controller?.myDocument/*presentationNotesDocument*/)
+        self.notesDocumentPopUpButton.selectItem(at: docIndex > 0 ? docIndex : 0)
         
         NotificationCenter.default.addObserver(self, selector: #selector(handleDocumentsDidChangeNotification(notification:)), name: NSNotification.Name("SKDocumentDidShowNotification"), object: nil)
         NotificationCenter.default.addObserver(self, selector: #selector(handleDocumentsDidChangeNotification(notification:)), name: NSNotification.Name("SKDocumentControllerDidRemoveDocumentNotification"), object: nil)
     }
     
-    @objc func handleDocumentsDidChangeNotification(notification: Notification?) {
-        guard let currentDoc = notesDocumentPopUpButton.selectedItem?.representedObject else { return }
-        while notesDocumentPopUpButton.numberOfItems > 1 {
-            notesDocumentPopUpButton.removeItem(at: notesDocumentPopUpButton.numberOfItems - 1)
-        }
-
-        guard let document = controller?.document as? NSDocument,
-              let pageCount = controller?.listView.document?.pageCount else { return }
-
-        let documents = NSMutableArray()
-        for doc in NSDocumentController.shared.documents {
-            if let pdfDoc = doc as? CPDFDocument,
-                doc != document,
-                pdfDoc.pageCount == pageCount {
-                documents.add(doc)
-            }
-        }
-
-        let sortDescriptor = NSSortDescriptor(key: "displayName", ascending: true)
-        documents.sort(using: [sortDescriptor])
-
-        for doc in documents {
-            notesDocumentPopUpButton.addItem(withTitle: (doc as AnyObject).displayName)
-            notesDocumentPopUpButton.lastItem?.representedObject = doc
-        }
-
-        let docIndex = notesDocumentPopUpButton.indexOfItem(withRepresentedObject: currentDoc)
-        notesDocumentPopUpButton.selectItem(at: docIndex == -1 ? 0 : docIndex)
-    }
-    
     func stopObservingTransitions(infos: [KMTransitionInfo]) {
 //        for info in infos {
 //            info.removeObserver(self, forKeyPath: TRANSITIONSTYLE_KEY) 
@@ -222,14 +178,14 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
 //        }
     }
     
-    func creatTransitionController() {
-        transitionController = SKTransitionController(for: controller?.listView)//controller?.mainViewController.listView.transitionController
-        let options: NSKeyValueObservingOptions = [.new, .old]
-        transitionController?.addObserver(self, forKeyPath: "transitionStyle", options: options, context: &SKPDFViewTransitionsObservationContext)
-        transitionController?.addObserver(self, forKeyPath: "duration", options: options, context: &SKPDFViewTransitionsObservationContext)
-        transitionController?.addObserver(self, forKeyPath: "shouldRestrict", options: options, context: &SKPDFViewTransitionsObservationContext)
-        transitionController?.addObserver(self, forKeyPath: "pageTransitions", options: options, context: &SKPDFViewTransitionsObservationContext)
-    }
+//    func creatTransitionController() {
+//        transitionController = SKTransitionController(for: controller?.listView)//controller?.mainViewController.listView.transitionController
+//        let options: NSKeyValueObservingOptions = [.new, .old]
+//        transitionController?.addObserver(self, forKeyPath: "transitionStyle", options: options, context: &SKPDFViewTransitionsObservationContext)
+//        transitionController?.addObserver(self, forKeyPath: "duration", options: options, context: &SKPDFViewTransitionsObservationContext)
+//        transitionController?.addObserver(self, forKeyPath: "shouldRestrict", options: options, context: &SKPDFViewTransitionsObservationContext)
+//        transitionController?.addObserver(self, forKeyPath: "pageTransitions", options: options, context: &SKPDFViewTransitionsObservationContext)
+//    }
     
     func startObservingTransitions(_ infos: [KMTransitionInfo]) {
 //        for info in infos {
@@ -239,14 +195,45 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
 //        }
     }
     
-    func makeTransitions() { 
-        if transitions != nil { return }
-        let tableColumn = tableView?.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: PAGE_COLUMNID))
+    func setValue(_ value: Any?, forKey key: String, ofTransition info: KMTransitionInfo) {
+        info.setValue(value, forKey: key)
+    }
+    override class func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
+        if context == &SKTransitionPropertiesObservationContext {
+            guard let info = object as? KMTransitionInfo else {
+                super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
+                return
+            }
+            let newValue = change?[.newKey]
+            let oldValue = change?[.oldKey]
+            
+            let newNonNullValue = newValue as? NSNull == nil ? newValue : nil
+            let oldNonNullValue = oldValue as? NSNull == nil ? oldValue : nil
+            
+//            if (newNonNullValue != nil || oldNonNullValue != nil) && newNonNullValue != oldNonNullValue {
+//                (undoManager?.prepare(withInvocationTarget: self) as AnyObject).setValue(oldNonNullValue, forKey: keyPath, ofTransition: info)
+//            }
+            if (newNonNullValue != nil || oldNonNullValue != nil){
+               
+            }
+        } else {
+            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
+        }
+    }
+    
+    // MARK: - Private
+    
+    private func makeTransitions() {
+        if transitions != nil {
+            return
+        }
+        
+        let tableColumn = self.tableView?.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: PAGE_COLUMNID))
         let cell = tableColumn?.dataCell
         var labelWidth: CGFloat = 0.0
         
         var array = [Any]()
-        let dictionary: NSDictionary? = transition.properties
+        let dictionary: NSDictionary? = self.transition.properties
         let arr: NSArray? = transitionController.pageTransitions as NSArray?
         let ptEnum = arr?.objectEnumerator()
         var tn: KMThumbnail? = nil
@@ -269,16 +256,15 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
         tableColumn?.maxWidth = labelWidth
         tableColumn?.width = labelWidth
         
-        var frame: NSRect = tableView?.enclosingScrollView?.frame ?? .zero
+        var frame: NSRect = self.tableView?.enclosingScrollView?.frame ?? .zero
         let wi: CGFloat = 61//tableColumn?.value(forKeyPath: "@sum.width") as! CGFloat
         frame.size.width = 19.0 + wi
-        tableView?.enclosingScrollView?.frame = frame
+        self.tableView?.enclosingScrollView?.frame = frame
         
-        transitions = array as NSArray
+        self.transitions = array as NSArray
     }
-
     
-    func changePageTransitionType() {
+    private func changePageTransitionType() {
 //                SKImageToolTipWindow.sharedToolTipWindow().orderOut(nil)
         let window = self.window
         let isVisible = window?.isVisible ?? false
@@ -318,65 +304,83 @@ class SKPresentationOptionsSheetController: NSWindowController, NSWindowDelegate
         //                (undoRedoManager.prepare(withInvocationTarget: self) as AnyObject).separate = sparate == fasle
         
     }
-   
+    
+    // Actions
+    
     @IBAction func pageTransitionAction(_ sender: NSButton) {
-        separate = sender.state == .on
+        self.separate = sender.state == .on
        
-        changePageTransitionType()
+        self.changePageTransitionType()
     }
     
     @IBAction func changeDurationSlider(_ sender: NSSlider) {
         self.durationTF.stringValue = sender.stringValue
     }
+    
     @IBAction func cancelAction(_ sender: Any) {
-        if let handle = closeCallBack {
+        if let handle = self.closeCallBack {
             handle(self)
         }
     }
+    
     @IBAction func okAction(_ sender: Any) {
-        if let handle = closeCallBack {
+        if let handle = self.closeCallBack {
             handle(self)
         }
         if ((undoManager?.canUndo) != nil) {
-            transitionController?.transitionStyle = transition.transitionStyle
-            transitionController?.duration = CGFloat(transition.duration)
-            transitionController?.shouldRestrict = transition.shouldRestrict
-            transitionController?.pageTransitions = pageTransitions as? [Any]
-            (controller?.undoManager)?.setActionName(NSLocalizedString("Change Transitions", comment: "Undo action name"))
+            self.transitionController?.transitionStyle = transition.transitionStyle
+            self.transitionController?.duration = CGFloat(transition.duration)
+            self.transitionController?.shouldRestrict = transition.shouldRestrict
+            self.transitionController?.pageTransitions = pageTransitions as? [Any]
+            (self.controller?.undoManager)?.setActionName(NSLocalizedString("Change Transitions", comment: "Undo action name"))
         }
-        controller?.myDocument/*presentationNotesDocument*/ = notesDocument
+        self.controller?.myDocument/*presentationNotesDocument*/ = notesDocument
     }
     
-    func windowWillReturnUndoManager(_ window: NSWindow) -> UndoManager? {
-        return undoRedoManager
-    }
+    // Noti Actions
     
-    func setValue(_ value: Any?, forKey key: String, ofTransition info: KMTransitionInfo) {
-        info.setValue(value, forKey: key)
-    }
-    override class func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
-        if context == &SKTransitionPropertiesObservationContext {
-            guard let info = object as? KMTransitionInfo else {
-                super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
-                return
-            }
-            let newValue = change?[.newKey]
-            let oldValue = change?[.oldKey]
-            
-            let newNonNullValue = newValue as? NSNull == nil ? newValue : nil
-            let oldNonNullValue = oldValue as? NSNull == nil ? oldValue : nil
-            
-//            if (newNonNullValue != nil || oldNonNullValue != nil) && newNonNullValue != oldNonNullValue {
-//                (undoManager?.prepare(withInvocationTarget: self) as AnyObject).setValue(oldNonNullValue, forKey: keyPath, ofTransition: info)
-//            }
-            if (newNonNullValue != nil || oldNonNullValue != nil){
-               
+    @objc private func handleDocumentsDidChangeNotification(notification: Notification?) {
+        guard let currentDoc = notesDocumentPopUpButton.selectedItem?.representedObject else { return }
+        while notesDocumentPopUpButton.numberOfItems > 1 {
+            notesDocumentPopUpButton.removeItem(at: notesDocumentPopUpButton.numberOfItems - 1)
+        }
+
+        guard let document = controller?.document as? NSDocument,
+              let pageCount = controller?.listView.document?.pageCount else { return }
+
+        let documents = NSMutableArray()
+        for doc in NSDocumentController.shared.documents {
+            if let pdfDoc = doc as? CPDFDocument,
+                doc != document,
+                pdfDoc.pageCount == pageCount {
+                documents.add(doc)
             }
-        } else {
-            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
         }
+
+        let sortDescriptor = NSSortDescriptor(key: "displayName", ascending: true)
+        documents.sort(using: [sortDescriptor])
+
+        for doc in documents {
+            notesDocumentPopUpButton.addItem(withTitle: (doc as AnyObject).displayName)
+            notesDocumentPopUpButton.lastItem?.representedObject = doc
+        }
+
+        let docIndex = notesDocumentPopUpButton.indexOfItem(withRepresentedObject: currentDoc)
+        notesDocumentPopUpButton.selectItem(at: docIndex == -1 ? 0 : docIndex)
+    }
+}
+
+extension SKPresentationOptionsSheetController: NSWindowDelegate {
+    func windowWillReturnUndoManager(_ window: NSWindow) -> UndoManager? {
+        return self.undoRedoManager
+    }
+}
+
+extension SKPresentationOptionsSheetController: SKTransitionControllerDelegate {
+    func transitionController(_ controller: SKTransitionController!, valueDidChanged info: [AnyHashable : Any]!) {
+        // 消息转发
+        self.proxyDelegate_?.transitionController(controller, valueDidChanged: info)
     }
-    
 }
 
 extension SKPresentationOptionsSheetController: NSTableViewDelegate, NSTableViewDataSource {

+ 1 - 1
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Extension.h

@@ -14,7 +14,7 @@
 #import "SKTransitionController.h"
 
 @class KMAnnotationStamp, KMSignature;
-@interface CPDFListView (Extension)
+@interface CPDFListView (Extension) <SKTransitionControllerDelegate>
 
 - (void)setSpellingTag:(NSInteger)spellingTag;
 - (NSInteger)spellingTag;

+ 9 - 0
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFListViewExtension/CPDFListView+Extension.m

@@ -800,11 +800,20 @@ CGFloat DEFAULT_SNAPSHOT_HEIGHT = 200.0;
     SKTransitionController *con = objc_getAssociatedObject(self, @selector(transitionController));
     if (con == nil) {
         con = [[SKTransitionController alloc] initForView:self];
+        con.delegate = self;
         objc_setAssociatedObject(self, @selector(transitionController), con, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
     }
     return con;
 }
 
+// SKTransitionControllerDelegate
+
+- (void)transitionController:(SKTransitionController *)controller valueDidChanged:(NSDictionary *)info {
+#if DEBUG
+    NSLog(@"%s", __func__);
+#endif
+}
+
 @end
 
 @implementation CPDFView (KMExtension)