KMSideViewController.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // KMSideViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/11/17.
  6. //
  7. import Cocoa
  8. class KMSideViewController: KMNBaseViewController {
  9. @IBOutlet weak var searchField: NSSearchField!
  10. @IBOutlet weak var currentView: NSView!
  11. var isAnimating = false
  12. weak var listView : CPDFListView?
  13. weak var mainViewController : KMMainViewController?
  14. private let duration_ = 0.7
  15. deinit {
  16. KMPrint("KMSideViewController deinit.")
  17. NotificationCenter.default.removeObserver(self)
  18. }
  19. override func loadView() {
  20. super.loadView()
  21. }
  22. override func viewDidLoad() {
  23. super.viewDidLoad()
  24. // Do view setup here.
  25. }
  26. func replaceSideView(_ newView: NSView, animate: Bool) {
  27. if (newView.window != nil) {
  28. return
  29. }
  30. var animated = animate
  31. if (UserDefaults.standard.bool(forKey: SKDisableAnimationsKey)) {
  32. animated = false
  33. }
  34. let oldView = self.currentView
  35. self.currentView = newView
  36. let contentView = oldView?.superview
  37. var firstResponder = oldView?.window?.firstResponder
  38. if let data = oldView {
  39. let des = (firstResponder as? NSView)?.isDescendant(of: data) ?? false
  40. if des {
  41. firstResponder = newView
  42. } else {
  43. firstResponder = nil
  44. }
  45. } else {
  46. firstResponder = nil
  47. }
  48. newView.frame = oldView?.frame ?? .zero
  49. if (animated == false) {
  50. if let data = oldView {
  51. contentView?.replaceSubview(data, with: newView)
  52. }
  53. firstResponder?.km_window()?.makeFirstResponder(firstResponder)
  54. contentView?.window?.recalculateKeyViewLoop()
  55. } else {
  56. self.isAnimating = true
  57. contentView?.wantsLayer = true
  58. contentView?.displayIfNeeded()
  59. NSAnimationContext.runAnimationGroup { context in
  60. context.duration = self.duration_
  61. if let data = oldView {
  62. contentView?.animator().replaceSubview(data, with: newView)
  63. }
  64. } completionHandler: {
  65. contentView?.wantsLayer = false
  66. firstResponder?.km_window()?.makeFirstResponder(firstResponder)
  67. contentView?.window?.recalculateKeyViewLoop()
  68. self.isAnimating = false
  69. }
  70. }
  71. }
  72. /*
  73. #define GRADIENT_MIN_WIDTH 111.0
  74. */
  75. }
  76. // MARK: - listView
  77. extension KMSideViewController {
  78. func pageCount() -> Int {
  79. return Int(self.pdfDocument()?.pageCount ?? 0)
  80. }
  81. func pdfDocument() -> CPDFDocument? {
  82. return self.listView?.document
  83. }
  84. func hideNotes() -> Bool {
  85. return self.listView?.hideNotes ?? false
  86. }
  87. func allowsNotes() -> Bool {
  88. return self.listView?.allowsNotes() ?? false
  89. }
  90. func currentPage() -> CPDFPage? {
  91. return self.listView?.currentPage()
  92. }
  93. func currentDestination() -> CPDFDestination? {
  94. return self.listView?.currentDestination
  95. }
  96. func currentPageIndex() -> Int {
  97. return self.listView?.currentPageIndex ?? NSNotFound
  98. }
  99. func isLocked() -> Bool {
  100. return self.pdfDocument()?.isLocked ?? true
  101. }
  102. func scaleFactor() -> Float {
  103. return Float(self.listView?.scaleFactor ?? 0)
  104. }
  105. func displayBox() -> CPDFDisplayBox {
  106. return self.listView?.displayBox ?? .cropBox
  107. }
  108. func allowsPrinting() -> Bool {
  109. return self.pdfDocument()?.allowsPrinting ?? true
  110. }
  111. func bookmarks() -> [CPDFBookmark]? {
  112. return self.pdfDocument()?.bookmarks()
  113. }
  114. func outlineRoot() -> CPDFOutline? {
  115. return self.pdfDocument()?.outlineRoot()
  116. }
  117. func setNewOutlineRoot() -> CPDFOutline? {
  118. return self.pdfDocument()?.setNewOutlineRoot()
  119. }
  120. func layoutDocumentView() {
  121. self.listView?.layoutDocumentView()
  122. }
  123. }