1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- //
- // KMSplitView.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2023/10/7.
- //
- import Cocoa
- private let CPDFOfficeSplitViewAnimationDidEndNotification = NSNotification.Name(rawValue: "CPDFOfficeSplitViewAnimationDidEndNotification")
- let CPDFOfficeDisableAnimationsKey = "CPDFOfficeDisableAnimationsKey"
- class KMSplitView: NSSplitView {
-
- static let animationDidEndNotification = CPDFOfficeSplitViewAnimationDidEndNotification
- var animating: Bool = false
-
- override class func defaultAnimation(forKey key: NSAnimatablePropertyKey) -> Any? {
- if key == NSAnimatablePropertyKey("firstSplitPosition") || key == NSAnimatablePropertyKey("secondSplitPosition") {
- return CABasicAnimation()
- } else {
- return super.defaultAnimation(forKey: key)
- }
- }
-
- override var dividerColor: NSColor {
- return NSColor.white
- }
-
- @objc dynamic var firstSplitPosition: CGFloat {
- get {
- if let view = subviews.first, !isSubviewCollapsed(view) {
- if isVertical {
- return view.frame.maxX
- } else {
- return view.frame.maxY
- }
- }
- return minPossiblePositionOfDivider(at: 0)
- }
- set {
- self.setPosition(newValue, ofDividerAt: 0)
- }
- }
-
- @objc dynamic var secondSplitPosition: CGFloat {
- get {
- if subviews.count > 1 {
- let view = subviews[1]
- if !isSubviewCollapsed(view) {
- if isVertical {
- return view.frame.maxX
- } else {
- return view.frame.maxY
- }
- }
- }
- return minPossiblePositionOfDivider(at: 1)
- }
- set {
- self.setPosition(newValue, ofDividerAt: 1)
- }
- }
- func setPosition(_ position: CGFloat, ofDividerAt dividerIndex: Int, animate: Bool = false) {
- if UserDefaults.standard.bool(forKey: SKDisableAnimationsKey) || dividerIndex > 1 {
- return
- }
- if animating {
- return
- } else if !animate {
- setPosition(position, ofDividerAt: dividerIndex)
- } else {
- animating = true
- NSAnimationContext.runAnimationGroup { context in
- if dividerIndex == 0 {
- self.animator().firstSplitPosition = position
- } else if dividerIndex == 1 {
- self.animator().secondSplitPosition = position
- } else {
- setPosition(position, ofDividerAt: dividerIndex)
- }
- } completionHandler: {
- self.animating = false
- NotificationCenter.default.post(name: CPDFOfficeSplitViewAnimationDidEndNotification , object: self)
- }
- }
- }
- }
|