KMSplitView.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // KMSplitView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/10/7.
  6. //
  7. import Cocoa
  8. private let CPDFOfficeSplitViewAnimationDidEndNotification = NSNotification.Name(rawValue: "CPDFOfficeSplitViewAnimationDidEndNotification")
  9. let CPDFOfficeDisableAnimationsKey = "CPDFOfficeDisableAnimationsKey"
  10. class KMSplitView: NSSplitView {
  11. static let animationDidEndNotification = CPDFOfficeSplitViewAnimationDidEndNotification
  12. var animating: Bool = false
  13. override class func defaultAnimation(forKey key: NSAnimatablePropertyKey) -> Any? {
  14. if key == NSAnimatablePropertyKey("firstSplitPosition") || key == NSAnimatablePropertyKey("secondSplitPosition") {
  15. return CABasicAnimation()
  16. } else {
  17. return super.defaultAnimation(forKey: key)
  18. }
  19. }
  20. override var dividerColor: NSColor {
  21. return NSColor.white
  22. }
  23. @objc dynamic var firstSplitPosition: CGFloat {
  24. get {
  25. if let view = subviews.first, !isSubviewCollapsed(view) {
  26. if isVertical {
  27. return view.frame.maxX
  28. } else {
  29. return view.frame.maxY
  30. }
  31. }
  32. return minPossiblePositionOfDivider(at: 0)
  33. }
  34. set {
  35. self.setPosition(newValue, ofDividerAt: 0)
  36. }
  37. }
  38. @objc dynamic var secondSplitPosition: CGFloat {
  39. get {
  40. if subviews.count > 1 {
  41. let view = subviews[1]
  42. if !isSubviewCollapsed(view) {
  43. if isVertical {
  44. return view.frame.maxX
  45. } else {
  46. return view.frame.maxY
  47. }
  48. }
  49. }
  50. return minPossiblePositionOfDivider(at: 1)
  51. }
  52. set {
  53. self.setPosition(newValue, ofDividerAt: 1)
  54. }
  55. }
  56. func setPosition(_ position: CGFloat, ofDividerAt dividerIndex: Int, animate: Bool = false) {
  57. if UserDefaults.standard.bool(forKey: SKDisableAnimationsKey) || dividerIndex > 1 {
  58. return
  59. }
  60. if animating {
  61. return
  62. } else if !animate {
  63. setPosition(position, ofDividerAt: dividerIndex)
  64. } else {
  65. animating = true
  66. NSAnimationContext.runAnimationGroup { context in
  67. if dividerIndex == 0 {
  68. self.animator().firstSplitPosition = position
  69. } else if dividerIndex == 1 {
  70. self.animator().secondSplitPosition = position
  71. } else {
  72. setPosition(position, ofDividerAt: dividerIndex)
  73. }
  74. } completionHandler: {
  75. self.animating = false
  76. NotificationCenter.default.post(name: CPDFOfficeSplitViewAnimationDidEndNotification , object: self)
  77. }
  78. }
  79. }
  80. }