KMSnapshotWindowController.swift 30 KB

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