|
@@ -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
|
|
|
+ }
|
|
|
}
|