KMSideViewController.swift 4.2 KB

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