Просмотр исходного кода

【快照】KMSnapshotWindowController OC转Swift

tangchao 1 год назад
Родитель
Сommit
c0f883f0b0

+ 16 - 16
PDF Office/PDF Master.xcodeproj/xcuserdata/kdanmobile.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -356,22 +356,6 @@
             landmarkType = "7">
          </BreakpointContent>
       </BreakpointProxy>
-      <BreakpointProxy
-         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
-         <BreakpointContent
-            uuid = "E6699853-FF9B-462E-8EA1-E74734DC7310"
-            shouldBeEnabled = "Yes"
-            ignoreCount = "0"
-            continueAfterRunningActions = "No"
-            filePath = "PDF Master/Class/PDFTools/Snapshot/Window/KMSnapshotWindowController.swift"
-            startingColumnNumber = "9223372036854775807"
-            endingColumnNumber = "9223372036854775807"
-            startingLineNumber = "725"
-            endingLineNumber = "725"
-            landmarkName = "windowDidMiniaturize(_:)"
-            landmarkType = "7">
-         </BreakpointContent>
-      </BreakpointProxy>
       <BreakpointProxy
          BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
          <BreakpointContent
@@ -420,5 +404,21 @@
             </Locations>
          </BreakpointContent>
       </BreakpointProxy>
+      <BreakpointProxy
+         BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
+         <BreakpointContent
+            uuid = "B0866F10-4EC0-4E66-9618-6A2C978CD7B9"
+            shouldBeEnabled = "Yes"
+            ignoreCount = "0"
+            continueAfterRunningActions = "No"
+            filePath = "PDF Master/Class/PDFWindowController/ViewController/KMMainViewController.swift"
+            startingColumnNumber = "9223372036854775807"
+            endingColumnNumber = "9223372036854775807"
+            startingLineNumber = "2332"
+            endingLineNumber = "2332"
+            landmarkName = "pdfListView(_:showSnapshotAtPageNumber:for:scaleFactor:autoFits:)"
+            landmarkType = "7">
+         </BreakpointContent>
+      </BreakpointProxy>
    </Breakpoints>
 </Bucket>

+ 0 - 5
PDF Office/PDF Master/Class/PDFTools/Snapshot/View/KMSnapshotPDFView.swift

@@ -14,12 +14,10 @@ class KMSnapshotPDFView: CPDFView {
         get {
             if (self._scalePopUpButton == nil) {
                 
-//                NSScrollView *scrollView = [self scrollView];
                 let scrollView = self.enclosingScrollView
                 scrollView?.hasHorizontalScroller = true
                 
                 // create it
-//                scalePopUpButton = [[NSPopUpButton allocWithZone:[self zone]] initWithFrame:NSMakeRect(0.0, 0.0, 1.0, 1.0) pullsDown:NO];
                 let scalePopUpButton_ = NSPopUpButton(frame: NSMakeRect(0.0, 0.0, 1.0, 1.0), pullsDown: false)
                 scalePopUpButton_.cell?.controlSize = .small
                 self._scalePopUpButton = scalePopUpButton_
@@ -45,9 +43,7 @@ class KMSnapshotPDFView: CPDFView {
                 
                 // fill it
                 for i in 0 ..< numberOfDefaultItems {
-//                for (cnt = 0; cnt < numberOfDefaultItems; cnt++) {
                     label = Bundle.main.localizedString(forKey: self._SKDefaultScaleMenuLabels[i], value: "", table: "ZoomValues")
-//                    width = NSWidth([label boundingRectWithSize:size options:0 attributes:attrs]);
                     width = NSWidth(label.boundingRect(with: size, options: NSString.DrawingOptions(rawValue: 0), attributes: attrs))
                     if (width > maxWidth) {
                         maxWidth = width
@@ -67,7 +63,6 @@ class KMSnapshotPDFView: CPDFView {
                 // Make sure the popup is big enough to fit the largest cell
                 scalePopUpButton_.selectItem(at: maxIndex)
                 scalePopUpButton_.sizeToFit()
-//                [scalePopUpButton setFrameSize:];
                 scalePopUpButton_.setFrameSize(NSMakeSize(NSWidth(scalePopUpButton_.frame) - CONTROL_WIDTH_OFFSET, CONTROL_HEIGHT))
 //
 //                // select the appropriate item, adjusting the scaleFactor if necessary

+ 130 - 177
PDF Office/PDF Master/Class/PDFTools/Snapshot/Window/KMSnapshotWindowController.swift

@@ -35,47 +35,9 @@ class KMSnapshotWindowController: NSWindowController {
     var thumbnail: NSImage?
     var windowImage: NSImage?
     
-    /*
-     extern NSString *SKSnapshotCurrentSetupKey;
-
-     @property (nonatomic, readonly) NSUInteger pageIndex;
-     @property (nonatomic, readonly) BOOL hasWindow;
-     @property (nonatomic, readonly) NSDictionary *pageAndWindow;
-     @property (nonatomic, readonly) NSDictionary *currentSetup;
-
-     @property (nonatomic, readonly) NSAttributedString *thumbnailAttachment, *thumbnail512Attachment, *thumbnail256Attachment, *thumbnail128Attachment, *thumbnail64Attachment, *thumbnail32Attachment;
-
-     - (void)setPdfDocument:(PDFDocument *)pdfDocument goToPageNumber:(NSInteger)pageNum rect:(NSRect)rect scaleFactor:(CGFloat)factor autoFits:(BOOL)autoFits;
-     - (void)setPdfDocument:(PDFDocument *)pdfDocument setup:(NSDictionary *)setup;
-
-     - (BOOL)isPageVisible:(PDFPage *)page;
-
-     - (void)redisplay;
-
-     - (NSImage *)thumbnailWithSize:(CGFloat)size;
-
-     - (NSAttributedString *)thumbnailAttachmentWithSize:(CGFloat)size;
-
-     - (void)miniaturize;
-     - (void)deminiaturize;
-
-     - (void)handleDocumentDidUnlockNotification:(NSNotification *)notification;
-     - (void)handlePDFViewFrameChangedNotification:(NSNotification *)notification;
-     - (void)handleViewChangedNotification:(NSNotification *)notification;
-     - (void)handleDidAddRemoveAnnotationNotification:(NSNotification *)notification;
-     - (void)handleDidMoveAnnotationNotification:(NSNotification *)notification;
-
-     - (void)setNeedsDisplayInRect:(NSRect)rect ofPage:(PDFPage *)page;
-     - (void)setNeedsDisplayForAnnotation:(PDFAnnotation *)annotation onPage:(PDFPage *)page;
-
-     @end
-
-     */
-    
     /*
      #define EM_DASH_CHARACTER (unichar)0x2014
      NSString *SKSnapshotCurrentSetupKey = @"currentSetup";
-
      static char SKSnaphotWindowDefaultsObservationContext;
      */
     
@@ -133,13 +95,6 @@ class KMSnapshotWindowController: NSWindowController {
          return [NSSet setWithObjects:PAGELABEL_KEY, HASWINDOW_KEY, nil];
      }
 
-     // these should never be reached, but just to be sure
-
-     - (void)windowDidDeminiaturize:(NSNotification *)notification {
-         [self updateWindowLevel];
-         [self setHasWindow:YES];
-     }
-
      - (NSString *)windowTitleForDocumentDisplayName:(NSString *)displayName {
          return [NSString stringWithFormat:@"%@ %C %@", displayName, EM_DASH_CHARACTER, [NSString stringWithFormat:NSLocalizedString(@"Page %@", @""), [self pageLabel]]];
      }
@@ -166,7 +121,6 @@ class KMSnapshotWindowController: NSWindowController {
      }
 */
 
-
     @objc func handlePageChangedNotification(_ notification: NSNotification?) {
 //         [self setPageLabel:[[pdfView currentPage] displayLabel]];
         let label = "\(self.pdfView.currentPage().pageIndex()+1)"
@@ -246,11 +200,10 @@ class KMSnapshotWindowController: NSWindowController {
          NSView *clipView = [[[pdfView documentView] enclosingScrollView] contentView];
          return [pdfView convertRect:[pdfView convertRect:[clipView bounds] fromView:clipView] toPage:[pdfView currentPage]];
      }
-
-     - (NSUInteger)pageIndex {
-         return [[pdfView currentPage] pageIndex];
-     }
 */
+    func pageIndex() -> UInt {
+        return self.pdfView.currentPage().pageIndex()
+    }
     
     func pageAndWindow() -> NSDictionary {
         return [SKSnapshotPageCellLabelKey : self.pageLabel, SKSnapshotPageCellHasWindowKey : NSNumber(booleanLiteral: self.hasWindow)]
@@ -270,83 +223,57 @@ class KMSnapshotWindowController: NSWindowController {
          BOOL autoFits = [pdfView autoFits];
          return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInteger:[self pageIndex]], PAGE_KEY, NSStringFromRect(rect), RECT_KEY, [NSNumber numberWithDouble:[pdfView scaleFactor]], SCALEFACTOR_KEY, [NSNumber numberWithBool:autoFits], AUTOFITS_KEY, [NSNumber numberWithBool:[[self window] isVisible]], HASWINDOW_KEY, NSStringFromRect([[self window] frame]), WINDOWFRAME_KEY, nil];
      }
+ */
+    
+    // MARK: - Actions
 
-     #pragma mark Actions
-
-     - (IBAction)doGoToNextPage:(id)sender {
-         [pdfView goToNextPage:sender];
-     }
-
-     - (IBAction)doGoToPreviousPage:(id)sender {
-         [pdfView goToPreviousPage:sender];
-     }
+    @IBAction func doGoToNextPage(_ sender: AnyObject?) {
+        self.pdfView.goToNextPage(sender)
+    }
 
-     - (IBAction)doGoToFirstPage:(id)sender {
-         [pdfView goToFirstPage:sender];
-     }
+    @IBAction func doGoToPreviousPage(_ sender: AnyObject?) {
+        self.pdfView.goToPreviousPage(sender)
+    }
 
-     - (IBAction)doGoToLastPage:(id)sender {
-         [pdfView goToLastPage:sender];
-     }
+    @IBAction func doGoToFirstPage(_ sender: AnyObject?) {
+        self.pdfView.goToFirstPage(sender)
+    }
 
-     - (IBAction)doGoBack:(id)sender {
-         [pdfView goBack:sender];
-     }
+    @IBAction func doGoToLastPage(_ sender: AnyObject?) {
+        self.pdfView.goToLastPage(sender)
+    }
 
-     - (IBAction)doGoForward:(id)sender {
-         [pdfView goForward:sender];
-     }
+    @IBAction func doGoBack(_ sender: AnyObject?) {
+        self.pdfView.km_goBack(sender)
+    }
 
-     - (IBAction)doZoomIn:(id)sender {
-         [pdfView zoomIn:sender];
-     }
+    @IBAction func doGoForward(_ sender: AnyObject?) {
+        self.pdfView.km_goForward(sender)
+    }
 
-     - (IBAction)doZoomOut:(id)sender {
-         [pdfView zoomOut:sender];
-     }
+    @IBAction func doZoomIn(_ sender: AnyObject?) {
+        self.pdfView.zoomIn(sender)
+    }
 
-     - (IBAction)doZoomToPhysicalSize:(id)sender {
-         [pdfView setPhysicalScaleFactor:1.0];
-     }
+    @IBAction func doZoomOut(_ sender: AnyObject?) {
+        self.pdfView.zoomOut(sender)
+    }
 
-     - (IBAction)doZoomToActualSize:(id)sender {
-         [pdfView setScaleFactor:1.0];
-     }
+//    @IBAction func doZoomToPhysicalSize(_ sender: AnyObject?) {
+//        self.pdfv
+//    }
+//    - (IBAction):(id)sender {
+//        [pdfView setPhysicalScaleFactor:1.0];
+//    }
 
-     - (IBAction)toggleAutoScale:(id)sender {
-         [pdfView setAutoFits:[pdfView autoFits] == NO];
-     }
+    @IBAction func doZoomToActualSize(_ sender: AnyObject?) {
+        self.pdfView.scaleFactor = 1.0
+    }
 
-     - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
-         SEL action = [menuItem action];
-         if (action == @selector(doGoToNextPage:)) {
-             return [pdfView canGoToNextPage];
-         } else if (action == @selector(doGoToPreviousPage:)) {
-             return [pdfView canGoToPreviousPage];
-         } else if (action == @selector(doGoToFirstPage:)) {
-             return [pdfView canGoToFirstPage];
-         } else if (action == @selector(doGoToLastPage:)) {
-             return [pdfView canGoToLastPage];
-         } else if (action == @selector(doGoBack:)) {
-             return [pdfView canGoBack];
-         } else if (action == @selector(doGoForward:)) {
-             return [pdfView canGoForward];
-         } else if (action == @selector(doZoomIn:)) {
-             return [pdfView canZoomIn];
-         } else if (action == @selector(doZoomOut:)) {
-             return [pdfView canZoomOut];
-         } else if (action == @selector(doZoomToActualSize:)) {
-             return fabs([pdfView scaleFactor] - 1.0 ) > 0.01;
-         } else if (action == @selector(doZoomToPhysicalSize:)) {
-             return fabs([pdfView physicalScaleFactor] - 1.0 ) > 0.01;
-         } else if (action == @selector(toggleAutoScale:)) {
-             [menuItem setState:[pdfView autoFits] ? NSOnState : NSOffState];
-             return YES;
-         }
-         return YES;
-     }
+    @IBAction func toggleAutoScale(_ sender: AnyObject?) {
+        self.pdfView.autoFits = !self.pdfView.autoFits
+    }
 
- */
     // MARK: - Thumbnails
 
     func thumbnailWithSize(_ size: CGFloat) -> NSImage? {
@@ -404,49 +331,31 @@ class KMSnapshotWindowController: NSWindowController {
          
          return image
      }
-    /*
-
-     - (NSAttributedString *)thumbnailAttachmentWithSize:(CGFloat)size {
-         NSImage *image = [self thumbnailWithSize:size];
-         
-         NSFileWrapper *wrapper = [[NSFileWrapper alloc] initRegularFileWithContents:[image TIFFRepresentation]];
-         NSString *filename = [NSString stringWithFormat:@"snapshot_page_%lu.tiff",(unsigned long)( [self pageIndex] + 1)];
-         [wrapper setFilename:filename];
-         [wrapper setPreferredFilename:filename];
-
-         NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithFileWrapper:wrapper];
-         [wrapper release];
-         NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachment];
-         [attachment release];
-         
-         return attrString;
-     }
-
-     - (NSAttributedString *)thumbnailAttachment {
-         return [self thumbnailAttachmentWithSize:0.0];
-     }
-
-     - (NSAttributedString *)thumbnail512Attachment {
-         return [self thumbnailAttachmentWithSize:512.0];
-     }
-
-     - (NSAttributedString *)thumbnail256Attachment {
-         return [self thumbnailAttachmentWithSize:256.0];
-     }
-
-     - (NSAttributedString *)thumbnail128Attachment {
-         return [self thumbnailAttachmentWithSize:128.0];
-     }
 
-     - (NSAttributedString *)thumbnail64Attachment {
-         return [self thumbnailAttachmentWithSize:64.0];
-     }
-
-     - (NSAttributedString *)thumbnail32Attachment {
-         return [self thumbnailAttachmentWithSize:32.0];
-     }
-
- */
+    func thumbnailAttachment() -> NSAttributedString? {
+        return self._thumbnailAttachment(size: 0)
+    }
+    
+    func thumbnail512Attachment() -> NSAttributedString? {
+        return self._thumbnailAttachment(size: 512)
+    }
+    
+    func thumbnail256Attachment() -> NSAttributedString? {
+        return self._thumbnailAttachment(size: 256)
+    }
+    
+    func thumbnail128Attachment() -> NSAttributedString? {
+        return self._thumbnailAttachment(size: 128)
+    }
+    
+    func thumbnail64Attachment() -> NSAttributedString? {
+        return self._thumbnailAttachment(size: 64)
+    }
+    
+    func thumbnail32Attachment() -> NSAttributedString? {
+        return self._thumbnailAttachment(size: 32)
+    }
+    
     // MARK: - Miniaturize / Deminiaturize
 
     func miniaturizedRectForDockingRect(_ dockRect: NSRect) -> NSRect {
@@ -612,27 +521,19 @@ class KMSnapshotWindowController: NSWindowController {
 
         self.window?.makeFirstResponder(self.pdfView)
         self.handlePageChangedNotification(nil)
-//
-//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePageChangedNotification:)
-//                                                     name:PDFViewPageChangedNotification object:pdfView];
-//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDocumentDidUnlockNotification:)
-//                                                     name:PDFDocumentDidUnlockNotification object:[pdfView document]];
-//
-//        NSView *clipView = [[[pdfView documentView] enclosingScrollView] contentView];
-//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePDFViewFrameChangedNotification:)
-//                                                     name:NSViewFrameDidChangeNotification object:clipView];
-//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePDFViewFrameChangedNotification:)
-//                                                     name:NSViewBoundsDidChangeNotification object:clipView];
-//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleViewChangedNotification:)
-//                                                     name:SKSnapshotViewChangedNotification object:self];
-//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDidAddRemoveAnnotationNotification:)
-//                                                     name:SKPDFViewDidAddAnnotationNotification object:nil];
-//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDidAddRemoveAnnotationNotification:)
-//                                                     name:SKPDFViewDidRemoveAnnotationNotification object:nil];
-//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDidMoveAnnotationNotification:)
-//                                                     name:SKPDFViewDidMoveAnnotationNotification object:nil];
+
+        NotificationCenter.default.addObserver(self, selector: #selector(handlePageChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self.pdfView)
+        NotificationCenter.default.addObserver(self, selector: #selector(handleDocumentDidUnlockNotification), name: NSNotification.Name.CPDFDocumentDidUnlock, object: self.pdfView.document)
+
+        let clipView = self.pdfView.enclosingScrollView?.contentView
+        NotificationCenter.default.addObserver(self, selector: #selector(handlePDFViewFrameChangedNotification), name: NSView.frameDidChangeNotification, object: clipView)
+        NotificationCenter.default.addObserver(self, selector: #selector(handlePDFViewFrameChangedNotification), name: NSView.boundsDidChangeNotification, object: clipView)
+        NotificationCenter.default.addObserver(self, selector: #selector(handleViewChangedNotification), name: NSNotification.Name(SKSnapshotViewChangedNotification), object: self)
+//        NotificationCenter.default.addObserver(self, selector: #selector(handleDidAddRemoveAnnotationNotification), name: SKPDFViewDidAddAnnotationNotification, object: nil)
+//        NotificationCenter.default.addObserver(self, selector: #selector(handleDidAddRemoveAnnotationNotification), name: SKPDFViewDidRemoveAnnotationNotification, object: nil)
+//        NotificationCenter.default.addObserver(self, selector: #selector(handleDidMoveAnnotationNotification), name: SKPDFViewDidMoveAnnotationNotification, object: nil)
         self.perform(#selector(notifiyDidFinishSetup), with: nil, afterDelay: SMALL_DELAY)
-//
+
         if self.hasWindow {
             self.showWindow(nil)
         }
@@ -705,6 +606,20 @@ extension KMSnapshotWindowController {
         self.window?.level = onTop ? .floating : .normal
         self.window?.hidesOnDeactivate = onTop
     }
+    
+    private func _thumbnailAttachment(size: CGFloat) -> NSAttributedString? {
+        guard let imageData = self.thumbnailWithSize(size)?.tiffRepresentation else {
+            return nil
+        }
+        
+        let wrapper = FileWrapper(regularFileWithContents: imageData)
+        let filename = String(format: "snapshot_page_%lu.tiff", self.pageIndex() + 1)
+        wrapper.filename = filename
+        wrapper.preferredFilename = filename
+        let attachment = NSTextAttachment(fileWrapper: wrapper)
+            
+        return NSAttributedString(attachment: attachment)
+    }
 }
 
 extension KMSnapshotWindowController: NSWindowDelegate {
@@ -724,4 +639,42 @@ extension KMSnapshotWindowController: NSWindowDelegate {
         self.window?.orderOut(nil)
         self.hasWindow = false
     }
+    
+    func windowDidDeminiaturize(_ notification: Notification) {
+        self._updateWindowLevel()
+        self.hasWindow = true
+    }
+}
+
+extension KMSnapshotWindowController: NSMenuItemValidation {
+    func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
+        let action = menuItem.action
+        if (action == #selector(doGoToNextPage)) {
+            return self.pdfView.canGoToNextPage()
+        } else if (action == #selector(doGoToPreviousPage)) {
+            return self.pdfView.canGoToPreviousPage()
+        } else if (action == #selector(doGoToFirstPage)) {
+            return self.pdfView.canGoToFirstPage()
+        } else if (action == #selector(doGoToLastPage)) {
+            return self.pdfView.canGoToLastPage()
+        } else if (action == #selector(doGoBack)) {
+            return self.pdfView.km_canGoBack()
+        } else if (action == #selector(doGoForward)) {
+            return self.pdfView.km_canGoForward()
+        } else if (action == #selector(doZoomIn)) {
+            return self.pdfView.canZoomIn
+        } else if (action == #selector(doZoomOut)) {
+            return self.pdfView.canZoomOut
+        } else if (action == #selector(doZoomToActualSize)) {
+            return abs(pdfView.scaleFactor - 1.0 ) > 0.01
+        }
+//        else if (action == #selector(doZoomToPhysicalSize)) {
+//            return fabs(pdfView.physicalScaleFactor - 1.0 ) > 0.01
+//        }
+        else if (action == #selector(toggleAutoScale)) {
+            menuItem.state = self.pdfView.autoFits ? .on : .off
+            return true
+        }
+        return true
+    }
 }