KMSnapshotWindowController.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. //
  2. // KMSnapshotWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/12.
  6. //
  7. import Cocoa
  8. @objc protocol KMSnapshotWindowControllerDelegate: NSObjectProtocol {
  9. @objc optional func snapshotControllerDidFinishSetup(_ controller: KMSnapshotWindowController)
  10. @objc optional func snapshotControllerWillClose(_ controller: KMSnapshotWindowController)
  11. @objc optional func snapshotControllerDidChange(_ controller: KMSnapshotWindowController)
  12. @objc optional func snapshotController(_ controller: KMSnapshotWindowController, miniaturizedRect isMiniaturize: Bool) -> NSRect
  13. }
  14. class KMSnapshotWindowController: NSWindowController {
  15. @IBOutlet var pdfView: KMSnapshotPDFView!
  16. var hasWindow = false
  17. var forceOnTop = false
  18. weak var delegate: KMSnapshotWindowControllerDelegate?
  19. var string: String = ""
  20. var pageLabel: String = "" {
  21. didSet {
  22. self.synchronizeWindowTitleWithDocumentName()
  23. }
  24. }
  25. var animating = false
  26. var thumbnail: NSImage?
  27. var windowImage: NSImage?
  28. /*
  29. #define EM_DASH_CHARACTER (unichar)0x2014
  30. NSString *SKSnapshotCurrentSetupKey = @"currentSetup";
  31. static char SKSnaphotWindowDefaultsObservationContext;
  32. */
  33. private let SMALL_DELAY = 0.1
  34. private let RESIZE_TIME_FACTOR = 0.6
  35. private let PAGE_KEY = "page"
  36. private let RECT_KEY = "rect"
  37. private let SCALEFACTOR_KEY = "scaleFactor"
  38. private let AUTOFITS_KEY = "autoFits"
  39. private let WINDOWFRAME_KEY = "windowFrame"
  40. private let HASWINDOW_KEY = "hasWindow"
  41. private let PAGELABEL_KEY = "pageLabel"
  42. private let PAGEANDWINDOW_KEY = "pageAndWindow"
  43. private let SKSnapshotWindowFrameAutosaveName = "SKSnapshotWindow"
  44. private let SKSnapshotViewChangedNotification = "SKSnapshotViewChangedNotification"
  45. private let SKSnapshotPageCellLabelKey = "label"
  46. private let SKSnapshotPageCellHasWindowKey = "hasWindow"
  47. deinit {
  48. KMPrint("KMSnapshotWindowController deinit.")
  49. NotificationCenter.default.removeObserver(self)
  50. }
  51. override var windowNibName: NSNib.Name? {
  52. return "SnapshotWindow"
  53. }
  54. override func windowDidLoad() {
  55. super.windowDidLoad()
  56. // if ([NSWindow instancesRespondToSelector:@selector(setTabbingMode:)])
  57. // [[self window] setTabbingMode:NSWindowTabbingModeDisallowed];
  58. // if ([NSWindow instancesRespondToSelector:@selector(toggleFullScreen:)])
  59. // [[self window] setCollectionBehavior:[[self window] collectionBehavior] | NSWindowCollectionBehaviorFullScreenAuxiliary];
  60. // if ([[self window] respondsToSelector:@selector(contentLayoutRect)]) {
  61. // [[self window] setStyleMask:[[self window] styleMask] | NSFullSizeContentViewWindowMask];
  62. // [pdfView setFrame:[[self window] contentLayoutRect]];
  63. // }
  64. if let data = self.window?.responds(to: NSSelectorFromString("contentLayoutRect")), data {
  65. self.window?.styleMask.insert(.fullSizeContentView)
  66. self.pdfView.frame = self.window?.contentLayoutRect ?? NSMakeRect(0, 0, 400, 400)
  67. }
  68. self._updateWindowLevel()
  69. // [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeys:[NSArray arrayWithObjects:SKSnapshotsOnTopKey, SKShouldAntiAliasKey, SKGreekingThresholdKey, SKBackgroundColorKey, SKPageBackgroundColorKey, nil] context:&SKSnaphotWindowDefaultsObservationContext];
  70. // the window is initialially exposed. The windowDidExpose notification is useless, it has nothing to do with showing the window
  71. self.hasWindow = true
  72. }
  73. /*
  74. + (NSSet *)keyPathsForValuesAffectingPageAndWindow {
  75. return [NSSet setWithObjects:PAGELABEL_KEY, HASWINDOW_KEY, nil];
  76. }
  77. - (NSString *)windowTitleForDocumentDisplayName:(NSString *)displayName {
  78. return [NSString stringWithFormat:@"%@ %C %@", displayName, EM_DASH_CHARACTER, [NSString stringWithFormat:NSLocalizedString(@"Page %@", @""), [self pageLabel]]];
  79. }
  80. - (void)setNeedsDisplayInRect:(NSRect)rect ofPage:(PDFPage *)page {
  81. NSRect aRect = [pdfView convertRect:rect fromPage:page];
  82. CGFloat scale = [pdfView scaleFactor];
  83. CGFloat maxX = ceil(NSMaxX(aRect) + scale);
  84. CGFloat maxY = ceil(NSMaxY(aRect) + scale);
  85. CGFloat minX = floor(NSMinX(aRect) - scale);
  86. CGFloat minY = floor(NSMinY(aRect) - scale);
  87. aRect = NSIntersectionRect([pdfView bounds], NSMakeRect(minX, minY, maxX - minX, maxY - minY));
  88. if (NSIsEmptyRect(aRect) == NO)
  89. [pdfView setNeedsDisplayInRect:aRect];
  90. }
  91. - (void)setNeedsDisplayForAnnotation:(PDFAnnotation *)annotation onPage:(PDFPage *)page {
  92. [self setNeedsDisplayInRect:[annotation displayRect] ofPage:page];
  93. }
  94. - (void)redisplay {
  95. [pdfView requiresDisplay];
  96. }
  97. */
  98. @objc func handlePageChangedNotification(_ notification: NSNotification?) {
  99. // [self setPageLabel:[[pdfView currentPage] displayLabel]];
  100. let label = "\(self.pdfView.currentPage().pageIndex()+1)"
  101. self.pageLabel = label
  102. self.handlePDFViewFrameChangedNotification(nil)
  103. }
  104. @objc func handleDocumentDidUnlockNotification(_ notification: NSNotification) {
  105. let label = "\(self.pdfView.currentPage().pageIndex()+1)"
  106. self.pageLabel = label
  107. self.handlePDFViewFrameChangedNotification(nil)
  108. }
  109. @objc func handlePDFViewFrameChangedNotification(_ notification: NSNotification?) {
  110. if let _ = self.delegate?.snapshotControllerDidChange?(self) {
  111. let note = NSNotification(name: NSNotification.Name(rawValue: SKSnapshotViewChangedNotification), object: self)
  112. NotificationQueue.default.enqueue(note as Notification, postingStyle: .whenIdle, coalesceMask: .onName, forModes: nil)
  113. }
  114. }
  115. @objc func handleViewChangedNotification(_ notification: NSNotification) {
  116. self.updateString()
  117. self.delegate?.snapshotControllerDidChange?(self)
  118. }
  119. @objc func handleDidAddRemoveAnnotationNotification(_ notification: NSNotification) {
  120. // PDFAnnotation *annotation = [[notification userInfo] objectForKey:SKPDFViewAnnotationKey];
  121. // PDFPage *page = [[notification userInfo] objectForKey:SKPDFViewPageKey];
  122. guard let annotation = notification.userInfo?["annotation"] as? CPDFAnnotation else {
  123. return
  124. }
  125. guard let page = notification.userInfo?["page"] as? CPDFPage else {
  126. return
  127. }
  128. // if ([[page document] isEqual:[pdfView document]] && [self isPageVisible:page])
  129. // [self setNeedsDisplayForAnnotation:annotation onPage:page];
  130. }
  131. @objc func notifiyDidFinishSetup() {
  132. self.delegate?.snapshotControllerDidFinishSetup?(self)
  133. }
  134. /*
  135. - (void)handleDidMoveAnnotationNotification:(NSNotification *)notification {
  136. PDFAnnotation *annotation = [[notification userInfo] objectForKey:SKPDFViewAnnotationKey];
  137. PDFPage *oldPage = [[notification userInfo] objectForKey:SKPDFViewOldPageKey];
  138. PDFPage *newPage = [[notification userInfo] objectForKey:SKPDFViewNewPageKey];
  139. if ([[newPage document] isEqual:[pdfView document]]) {
  140. if ([self isPageVisible:oldPage])
  141. [self setNeedsDisplayForAnnotation:annotation onPage:oldPage];
  142. if ([self isPageVisible:newPage])
  143. [self setNeedsDisplayForAnnotation:annotation onPage:newPage];
  144. }
  145. }
  146. - (void)setPdfDocument:(PDFDocument *)pdfDocument setup:(NSDictionary *)setup {
  147. [self setPdfDocument:pdfDocument
  148. goToPageNumber:[[setup objectForKey:PAGE_KEY] unsignedIntegerValue]
  149. rect:NSRectFromString([setup objectForKey:RECT_KEY])
  150. scaleFactor:[[setup objectForKey:SCALEFACTOR_KEY] doubleValue]
  151. autoFits:[[setup objectForKey:AUTOFITS_KEY] boolValue]];
  152. [self setHasWindow:[[setup objectForKey:HASWINDOW_KEY] boolValue]];
  153. if ([setup objectForKey:WINDOWFRAME_KEY])
  154. [[self window] setFrame:NSRectFromString([setup objectForKey:WINDOWFRAME_KEY]) display:NO];
  155. }
  156. - (BOOL)isPageVisible:(PDFPage *)page {
  157. return [[page document] isEqual:[pdfView document]] && NSLocationInRange([page pageIndex], [pdfView displayedPageIndexRange]);
  158. }
  159. #pragma mark Acessors
  160. - (NSRect)bounds {
  161. NSView *clipView = [[[pdfView documentView] enclosingScrollView] contentView];
  162. return [pdfView convertRect:[pdfView convertRect:[clipView bounds] fromView:clipView] toPage:[pdfView currentPage]];
  163. }
  164. */
  165. func pageIndex() -> UInt {
  166. return self.pdfView.currentPage().pageIndex()
  167. }
  168. func pageAndWindow() -> NSDictionary {
  169. return [SKSnapshotPageCellLabelKey : self.pageLabel, SKSnapshotPageCellHasWindowKey : NSNumber(booleanLiteral: self.hasWindow)]
  170. }
  171. func setForceOnTop(_ flag: Bool) {
  172. self.forceOnTop = flag
  173. if let data = self.window?.isVisible, data {
  174. self._updateWindowLevel()
  175. }
  176. }
  177. /*
  178. - (NSDictionary *)currentSetup {
  179. NSView *clipView = [[[pdfView documentView] enclosingScrollView] contentView];
  180. NSRect rect = [pdfView convertRect:[pdfView convertRect:[clipView bounds] fromView:clipView] toPage:[pdfView currentPage]];
  181. BOOL autoFits = [pdfView autoFits];
  182. 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];
  183. }
  184. */
  185. // MARK: - Actions
  186. @IBAction func doGoToNextPage(_ sender: AnyObject?) {
  187. self.pdfView.goToNextPage(sender)
  188. }
  189. @IBAction func doGoToPreviousPage(_ sender: AnyObject?) {
  190. self.pdfView.goToPreviousPage(sender)
  191. }
  192. @IBAction func doGoToFirstPage(_ sender: AnyObject?) {
  193. self.pdfView.goToFirstPage(sender)
  194. }
  195. @IBAction func doGoToLastPage(_ sender: AnyObject?) {
  196. self.pdfView.goToLastPage(sender)
  197. }
  198. @IBAction func doGoBack(_ sender: AnyObject?) {
  199. self.pdfView.km_goBack(sender)
  200. }
  201. @IBAction func doGoForward(_ sender: AnyObject?) {
  202. self.pdfView.km_goForward(sender)
  203. }
  204. @IBAction func doZoomIn(_ sender: AnyObject?) {
  205. self.pdfView.zoomIn(sender)
  206. }
  207. @IBAction func doZoomOut(_ sender: AnyObject?) {
  208. self.pdfView.zoomOut(sender)
  209. }
  210. // @IBAction func doZoomToPhysicalSize(_ sender: AnyObject?) {
  211. // self.pdfv
  212. // }
  213. // - (IBAction):(id)sender {
  214. // [pdfView setPhysicalScaleFactor:1.0];
  215. // }
  216. @IBAction func doZoomToActualSize(_ sender: AnyObject?) {
  217. self.pdfView.scaleFactor = 1.0
  218. }
  219. @IBAction func toggleAutoScale(_ sender: AnyObject?) {
  220. self.pdfView.autoFits = !self.pdfView.autoFits
  221. }
  222. // MARK: - Thumbnails
  223. func thumbnailWithSize(_ size: CGFloat) -> NSImage? {
  224. let clipView = self.pdfView.documentView().contentView
  225. var bounds = self.pdfView.convert(clipView.bounds, from: clipView)
  226. if (CGRectEqualToRect(CGRectZero, bounds) || CGRectIsNull(bounds)) {
  227. return nil
  228. }
  229. let imageRep = self.pdfView.bitmapImageRepForCachingDisplay(in: bounds)
  230. var transform: NSAffineTransform?
  231. var thumbnailSize = bounds.size
  232. var shadowBlurRadius: CGFloat = 0.0
  233. var shadowOffset: CGFloat = 0.0
  234. var image: NSImage?
  235. if let data = imageRep {
  236. self.pdfView.cacheDisplay(in: bounds, to: data)
  237. }
  238. bounds.origin = NSZeroPoint
  239. if (size > 0.0) {
  240. shadowBlurRadius = round(size / 32.0)
  241. shadowOffset = -ceil(shadowBlurRadius * 0.75)
  242. if (NSHeight(bounds) > NSWidth(bounds)) {
  243. thumbnailSize = NSMakeSize(round((size - 2.0 * shadowBlurRadius) * NSWidth(bounds) / NSHeight(bounds) + 2.0 * shadowBlurRadius), size);
  244. } else {
  245. thumbnailSize = NSMakeSize(size, round((size - 2.0 * shadowBlurRadius) * NSHeight(bounds) / NSWidth(bounds) + 2.0 * shadowBlurRadius));
  246. }
  247. transform = NSAffineTransform()
  248. transform?.translateX(by: shadowBlurRadius, yBy: shadowBlurRadius - shadowOffset)
  249. transform?.scaleX(by: (thumbnailSize.width - 2.0 * shadowBlurRadius) / NSWidth(bounds), yBy: (thumbnailSize.height - 2.0 * shadowBlurRadius) / NSHeight(bounds))
  250. }
  251. if (CGSizeEqualToSize(CGSizeZero, thumbnailSize)) {
  252. return nil
  253. }
  254. image = NSImage(size: thumbnailSize)
  255. if (CGSizeEqualToSize(CGSizeZero, image!.size)) {
  256. return nil
  257. }
  258. image?.lockFocus()
  259. NSGraphicsContext.current?.imageInterpolation = .high
  260. transform?.concat()
  261. NSGraphicsContext.saveGraphicsState()
  262. // [[PDFView defaultPageBackgroundColor] set];
  263. if (shadowBlurRadius > 0.0) {
  264. // [NSShadow setShadowWithColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.5] blurRadius:shadowBlurRadius yOffset:shadowOffset];
  265. }
  266. __NSRectFill(bounds)
  267. NSGraphicsContext.current?.imageInterpolation = .default
  268. NSGraphicsContext.restoreGraphicsState()
  269. imageRep?.draw(in: bounds)
  270. image?.unlockFocus()
  271. return image
  272. }
  273. func thumbnailAttachment() -> NSAttributedString? {
  274. return self._thumbnailAttachment(size: 0)
  275. }
  276. func thumbnail512Attachment() -> NSAttributedString? {
  277. return self._thumbnailAttachment(size: 512)
  278. }
  279. func thumbnail256Attachment() -> NSAttributedString? {
  280. return self._thumbnailAttachment(size: 256)
  281. }
  282. func thumbnail128Attachment() -> NSAttributedString? {
  283. return self._thumbnailAttachment(size: 128)
  284. }
  285. func thumbnail64Attachment() -> NSAttributedString? {
  286. return self._thumbnailAttachment(size: 64)
  287. }
  288. func thumbnail32Attachment() -> NSAttributedString? {
  289. return self._thumbnailAttachment(size: 32)
  290. }
  291. // MARK: - Miniaturize / Deminiaturize
  292. func miniaturizedRectForDockingRect(_ dockRect: NSRect) -> NSRect {
  293. let clipView = self.pdfView.documentView().contentView
  294. let sourceRect = clipView.convert(clipView.bounds, to: nil)
  295. var targetRect: NSRect = .zero
  296. let windowSize = self.window?.frame.size ?? .zero
  297. let thumbSize = self.thumbnail?.size ?? .zero
  298. var thumbRatio: CGFloat = 0
  299. if thumbSize.width > 0 {
  300. thumbRatio = thumbSize.height / thumbSize.width
  301. }
  302. var dockRatio: CGFloat = 0
  303. if NSWidth(dockRect) > 0 {
  304. dockRatio = NSHeight(dockRect) / NSWidth(dockRect)
  305. }
  306. var scaleFactor: CGFloat = 0
  307. var shadowRadius = round(fmax(thumbSize.width, thumbSize.height) / 32.0)
  308. var shadowOffset = ceil(0.75 * shadowRadius)
  309. if (thumbRatio > dockRatio) {
  310. targetRect = NSInsetRect(dockRect, 0.5 * NSWidth(dockRect) * (1.0 - dockRatio / thumbRatio), 0.0)
  311. scaleFactor = NSHeight(targetRect) / thumbSize.height
  312. } else {
  313. targetRect = NSInsetRect(dockRect, 0.0, 0.5 * NSHeight(dockRect) * (1.0 - thumbRatio / dockRatio))
  314. scaleFactor = NSWidth(targetRect) / thumbSize.width
  315. }
  316. shadowRadius *= scaleFactor
  317. shadowOffset *= scaleFactor
  318. targetRect = NSOffsetRect(NSInsetRect(targetRect, shadowRadius, shadowRadius), 0.0, shadowOffset)
  319. if thumbRatio > dockRatio {
  320. if NSHeight(sourceRect) != 0 {
  321. scaleFactor = NSHeight(targetRect) / NSHeight(sourceRect)
  322. }
  323. } else {
  324. if NSWidth(sourceRect) != 0 {
  325. scaleFactor = NSWidth(targetRect) / NSWidth(sourceRect)
  326. }
  327. }
  328. return NSMakeRect(NSMinX(targetRect) - scaleFactor * NSMinX(sourceRect), NSMinY(targetRect) - scaleFactor * NSMinY(sourceRect), scaleFactor * windowSize.width, scaleFactor * windowSize.height);
  329. }
  330. func miniaturizeWindowFromRect(_ startRect: NSRect, toRect endRect: NSRect) {
  331. if (self.windowImage == nil) {
  332. self.windowImage = (self.window as? KMSnapshotWindow)?.windowImage
  333. }
  334. let miniaturizeWindow = KMAnimatedBorderlessWindow(contentRect: startRect)
  335. miniaturizeWindow.level = .floating
  336. miniaturizeWindow.backgroundImage = self.windowImage
  337. miniaturizeWindow.orderFront(nil)
  338. self.animating = true
  339. NSAnimationContext.runAnimationGroup { context in
  340. context.duration = RESIZE_TIME_FACTOR * miniaturizeWindow.animationResizeTime(endRect)
  341. miniaturizeWindow.animator().setFrame(endRect, display: true)
  342. } completionHandler: {
  343. if self.hasWindow {
  344. if let data = self.window?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  345. self.window?.animationBehavior = .none
  346. }
  347. self.window?.orderFront(nil)
  348. self._updateWindowLevel()
  349. if let data = self.window?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  350. self.window?.animationBehavior = .default
  351. }
  352. }
  353. miniaturizeWindow.orderOut(nil)
  354. self.animating = false
  355. }
  356. }
  357. func miniaturize() {
  358. if (self.animating) {
  359. return
  360. }
  361. if let dockRect = self.delegate?.snapshotController?(self, miniaturizedRect: true) {
  362. let startRect = self.window?.frame ?? .zero
  363. let endRect = self.miniaturizedRectForDockingRect(dockRect)
  364. self.miniaturizeWindowFromRect(startRect, toRect: endRect)
  365. if let data = self.window?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  366. self.window?.animationBehavior = .none
  367. }
  368. }
  369. self.window?.orderOut(nil)
  370. if let data = self.window?.responds(to: NSSelectorFromString("setAnimationBehavior:")), data {
  371. self.window?.animationBehavior = .default
  372. }
  373. self.hasWindow = false
  374. }
  375. func deminiaturize() {
  376. if (self.animating) {
  377. return
  378. }
  379. if let dockRect = self.delegate?.snapshotController?(self, miniaturizedRect: false) {
  380. let endRect = self.window?.frame ?? .zero
  381. let startRect = self.miniaturizedRectForDockingRect(dockRect)
  382. self.miniaturizeWindowFromRect(startRect, toRect: endRect)
  383. // SKDESTROY(windowImage);
  384. } else {
  385. self.showWindow(self)
  386. }
  387. self.hasWindow = true
  388. }
  389. /*
  390. #pragma mark KVO
  391. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  392. if (context == &SKSnaphotWindowDefaultsObservationContext) {
  393. NSString *key = [keyPath substringFromIndex:7];
  394. if ([key isEqualToString:SKSnapshotsOnTopKey]) {
  395. if ([[self window] isVisible])
  396. [self updateWindowLevel];
  397. } else if ([key isEqualToString:SKShouldAntiAliasKey]) {
  398. [pdfView setShouldAntiAlias:[[NSUserDefaults standardUserDefaults] boolForKey:SKShouldAntiAliasKey]];
  399. [pdfView applyDefaultInterpolationQuality];
  400. } else if ([key isEqualToString:SKGreekingThresholdKey]) {
  401. [pdfView setGreekingThreshold:[[NSUserDefaults standardUserDefaults] floatForKey:SKGreekingThresholdKey]];
  402. } else if ([key isEqualToString:SKBackgroundColorKey]) {
  403. [pdfView setBackgroundColor:[[NSUserDefaults standardUserDefaults] colorForKey:SKBackgroundColorKey]];
  404. } else if ([key isEqualToString:SKPageBackgroundColorKey]) {
  405. [pdfView applyDefaultPageBackgroundColor];
  406. }
  407. } else {
  408. [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
  409. }
  410. }
  411. */
  412. func updateString() {
  413. var mutableString = ""
  414. let clipView = self.pdfView.documentView().contentView
  415. let rect = clipView.convert(clipView.visibleRect, to: self.pdfView)
  416. for page in self.pdfView.displayedPages() {
  417. guard let _page = page as? CPDFPage else {
  418. continue
  419. }
  420. let frame = self.pdfView.convert(rect, to: _page)
  421. let sel = _page.selection(for: frame)
  422. if let data = sel?.hasCharacters(), data {
  423. if mutableString.isEmpty == false {
  424. mutableString.append("\n")
  425. }
  426. mutableString.append(sel?.string() ?? "")
  427. }
  428. }
  429. self.string = mutableString
  430. }
  431. @objc func goToRect(_ rectValue: NSValue) {
  432. self.pdfView.go(to: rectValue.rectValue, on: self.pdfView.currentPage())
  433. self.pdfView.resetHistory()
  434. self.updateString()
  435. self.window?.makeFirstResponder(self.pdfView)
  436. self.handlePageChangedNotification(nil)
  437. NotificationCenter.default.addObserver(self, selector: #selector(handlePageChangedNotification), name: NSNotification.Name.CPDFViewPageChanged, object: self.pdfView)
  438. NotificationCenter.default.addObserver(self, selector: #selector(handleDocumentDidUnlockNotification), name: NSNotification.Name.CPDFDocumentDidUnlock, object: self.pdfView.document)
  439. let clipView = self.pdfView.documentView().contentView
  440. NotificationCenter.default.addObserver(self, selector: #selector(handlePDFViewFrameChangedNotification), name: NSView.frameDidChangeNotification, object: clipView)
  441. NotificationCenter.default.addObserver(self, selector: #selector(handlePDFViewFrameChangedNotification), name: NSView.boundsDidChangeNotification, object: clipView)
  442. NotificationCenter.default.addObserver(self, selector: #selector(handleViewChangedNotification), name: NSNotification.Name(SKSnapshotViewChangedNotification), object: self)
  443. // NotificationCenter.default.addObserver(self, selector: #selector(handleDidAddRemoveAnnotationNotification), name: SKPDFViewDidAddAnnotationNotification, object: nil)
  444. // NotificationCenter.default.addObserver(self, selector: #selector(handleDidAddRemoveAnnotationNotification), name: SKPDFViewDidRemoveAnnotationNotification, object: nil)
  445. // NotificationCenter.default.addObserver(self, selector: #selector(handleDidMoveAnnotationNotification), name: SKPDFViewDidMoveAnnotationNotification, object: nil)
  446. self.perform(#selector(notifiyDidFinishSetup), with: nil, afterDelay: SMALL_DELAY)
  447. if self.hasWindow {
  448. self.showWindow(nil)
  449. }
  450. }
  451. func setPdfDocument(_ pdfDocument: CPDFDocument, goToPageNumber pageNum: Int, rect: NSRect, scaleFactor factor: CGFloat, autoFits: Bool) {
  452. let window = self.window
  453. self.pdfView.scaleFactor = factor
  454. self.pdfView.autoScales = false
  455. self.pdfView.displaysPageBreaks = false
  456. self.pdfView.displayBox = .cropBox
  457. // [pdfView setShouldAntiAlias:[[NSUserDefaults standardUserDefaults] floatForKey:]];
  458. self.pdfView.setShouldAntiAlias(UserDefaults.standard.bool(forKey: SKShouldAntiAliasKey))
  459. self.pdfView.setGreekingThreshold(UserDefaults.standard.float(forKey: SKGreekingThresholdKey).cgFloat)
  460. self.pdfView.backgroundColor = UserDefaults.standard.color(forKey: SKBackgroundColorKey)
  461. // [pdfView applyDefaultPageBackgroundColor];
  462. // [pdfView applyDefaultInterpolationQuality];
  463. self.pdfView.document = pdfDocument
  464. // [self setWindowFrameAutosaveNameOrCascade:SKSnapshotWindowFrameAutosaveName];
  465. if let controlView = self.pdfView.scalePopUpButton {
  466. var controlFrame: NSRect = .zero
  467. var frame: NSRect = .zero
  468. NSDivideRect(self.pdfView.frame, &controlFrame, &frame, NSHeight(controlView.frame), .minY)
  469. // NSDivideRect([pdfView frame], &controlFrame, &frame, NSHeight([controlView frame]), NSMinYEdge);
  470. controlFrame.size.width = NSWidth(controlView.frame)
  471. controlView.frame = controlFrame
  472. controlView.autoresizingMask = [.maxXMargin, .maxYMargin]
  473. window?.contentView?.addSubview(controlView)
  474. self.pdfView.frame = frame
  475. window?.backgroundColor = NSColor(calibratedWhite: 0.97, alpha: 1)
  476. let page = pdfDocument.page(at: UInt(pageNum))
  477. frame = self.pdfView.convert(rect, from: page)
  478. var view: NSView?
  479. frame = self.pdfView.convert(frame, to: view)
  480. frame.size.height += NSHeight(controlFrame)
  481. // frame = [NSWindow frameRectForContentRect:frame styleMask:[window styleMask] & ~NSWindowStyleMaskFullSizeContentView];
  482. var styleMask = window?.styleMask
  483. styleMask?.remove(.fullSizeContentView)
  484. frame = NSWindow.frameRect(forContentRect: frame, styleMask: styleMask!)
  485. frame.origin.x = NSMinX(window!.frame)
  486. frame.origin.y = NSMaxY(window!.frame) - NSHeight(frame)
  487. self.window?.setFrame(frame, display: false, animate: false)
  488. }
  489. self.pdfView.go(toPageIndex: pageNum, animated: false)
  490. // if (autoFits) {
  491. // self.pdfView.autoFits = autoFits
  492. // }
  493. self.pdfView.autoFits = true
  494. self.pdfView.autoScales = true
  495. // Delayed to allow PDFView to finish its bookkeeping
  496. // fixes bug of apparently ignoring the point but getting the page right.
  497. self.perform(#selector(goToRect), with: NSValue(rect: rect), afterDelay: SMALL_DELAY)
  498. }
  499. }
  500. // MARK: - Private Methods
  501. extension KMSnapshotWindowController {
  502. private func _updateWindowLevel() {
  503. let onTop = self.forceOnTop || UserDefaults.standard.bool(forKey: SKSnapshotsOnTopKey)
  504. self.window?.level = onTop ? .floating : .normal
  505. self.window?.hidesOnDeactivate = onTop
  506. }
  507. private func _thumbnailAttachment(size: CGFloat) -> NSAttributedString? {
  508. guard let imageData = self.thumbnailWithSize(size)?.tiffRepresentation else {
  509. return nil
  510. }
  511. let wrapper = FileWrapper(regularFileWithContents: imageData)
  512. let filename = String(format: "snapshot_page_%lu.tiff", self.pageIndex() + 1)
  513. wrapper.filename = filename
  514. wrapper.preferredFilename = filename
  515. let attachment = NSTextAttachment(fileWrapper: wrapper)
  516. return NSAttributedString(attachment: attachment)
  517. }
  518. }
  519. extension KMSnapshotWindowController: NSWindowDelegate {
  520. func windowWillClose(_ notification: Notification) {
  521. // @try { [[NSUserDefaultsController sharedUserDefaultsController] removeObserver:self forKeys:[NSArray arrayWithObjects:SKSnapshotsOnTopKey, SKShouldAntiAliasKey, SKGreekingThresholdKey, SKBackgroundColorKey, SKPageBackgroundColorKey, nil]]; }
  522. // @catch (id e) {}
  523. self.delegate?.snapshotControllerWillClose?(self)
  524. self.delegate = nil
  525. // Yosemite and El Capitan have a retain cycle when we leave the PDFView with a document
  526. // if (RUNNING_AFTER(10_9) && RUNNING_BEFORE(10_12))
  527. self.pdfView.document = nil
  528. }
  529. func windowDidMiniaturize(_ notification: Notification) {
  530. self.window?.orderOut(nil)
  531. self.hasWindow = false
  532. }
  533. func windowDidDeminiaturize(_ notification: Notification) {
  534. self._updateWindowLevel()
  535. self.hasWindow = true
  536. }
  537. }
  538. extension KMSnapshotWindowController: NSMenuItemValidation {
  539. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  540. let action = menuItem.action
  541. if (action == #selector(doGoToNextPage)) {
  542. return self.pdfView.canGoToNextPage()
  543. } else if (action == #selector(doGoToPreviousPage)) {
  544. return self.pdfView.canGoToPreviousPage()
  545. } else if (action == #selector(doGoToFirstPage)) {
  546. return self.pdfView.canGoToFirstPage()
  547. } else if (action == #selector(doGoToLastPage)) {
  548. return self.pdfView.canGoToLastPage()
  549. } else if (action == #selector(doGoBack)) {
  550. return self.pdfView.km_canGoBack()
  551. } else if (action == #selector(doGoForward)) {
  552. return self.pdfView.km_canGoForward()
  553. } else if (action == #selector(doZoomIn)) {
  554. return self.pdfView.canZoomIn
  555. } else if (action == #selector(doZoomOut)) {
  556. return self.pdfView.canZoomOut
  557. } else if (action == #selector(doZoomToActualSize)) {
  558. return abs(pdfView.scaleFactor - 1.0 ) > 0.01
  559. }
  560. // else if (action == #selector(doZoomToPhysicalSize)) {
  561. // return fabs(pdfView.physicalScaleFactor - 1.0 ) > 0.01
  562. // }
  563. else if (action == #selector(toggleAutoScale)) {
  564. menuItem.state = self.pdfView.autoFits ? .on : .off
  565. return true
  566. }
  567. return true
  568. }
  569. }