123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //
- // KMSplitView.swift
- // PDF Master
- //
- // Created by wanjun on 2023/10/7.
- //
- import Cocoa
- let CPDFOfficeSplitViewAnimationDidEndNotification = "CPDFOfficeSplitViewAnimationDidEndNotification"
- let CPDFOfficeDisableAnimationsKey = "CPDFOfficeDisableAnimationsKey"
- class KMSplitView: NSSplitView {
- 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
- }
-
- 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)
- }
- }
-
- 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: CPDFOfficeDisableAnimationsKey) || 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: NSNotification.Name(rawValue: CPDFOfficeSplitViewAnimationDidEndNotification) , object: self)
- }
- }
- }
- }
|