KMSplitView.swift 2.7 KB

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