KMSplitView.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // KMSplitView.m
  3. // SignFlow
  4. //
  5. // Created by wanjun on 2021/5/26.
  6. //
  7. #import "KMSplitView.h"
  8. #import <QuartzCore/QuartzCore.h>
  9. NSString *CPDFOfficeSplitViewAnimationDidEndNotification = @"CPDFOfficeSplitViewAnimationDidEndNotification";
  10. NSString *CPDFOfficeDisableAnimationsKey = @"CPDFOfficeDisableAnimationsKey";
  11. @implementation KMSplitView
  12. @synthesize animating;
  13. + (id)defaultAnimationForKey:(NSString *)key {
  14. if ([key isEqualToString:@"firstSplitPosition"] || [key isEqualToString:@"secondSplitPosition"])
  15. return [CABasicAnimation animation];
  16. else
  17. return [super defaultAnimationForKey:key];
  18. }
  19. - (NSColor*)dividerColor {
  20. return [NSColor whiteColor];
  21. }
  22. - (CGFloat)firstSplitPosition {
  23. NSView *view = [[self subviews] objectAtIndex:0];
  24. if ([self isSubviewCollapsed:view])
  25. return [self minPossiblePositionOfDividerAtIndex:0];
  26. else if ([self isVertical])
  27. return NSMaxX([view frame]);
  28. else
  29. return NSMaxY([view frame]);
  30. }
  31. - (void)setFirstSplitPosition:(CGFloat)position {
  32. [self setPosition:position ofDividerAtIndex:0];
  33. }
  34. - (CGFloat)secondSplitPosition {
  35. NSView *view = [[self subviews] objectAtIndex:1];
  36. if ([self isSubviewCollapsed:view])
  37. return [self minPossiblePositionOfDividerAtIndex:1];
  38. else if ([self isVertical])
  39. return NSMaxX([view frame]);
  40. else
  41. return NSMaxY([view frame]);
  42. }
  43. - (void)setSecondSplitPosition:(CGFloat)position {
  44. [self setPosition:position ofDividerAtIndex:1];
  45. }
  46. - (void)setPosition:(CGFloat)position ofDividerAtIndex:(NSInteger)dividerIndex animate:(BOOL)animate {
  47. if ([[NSUserDefaults standardUserDefaults] boolForKey:CPDFOfficeDisableAnimationsKey] || dividerIndex > 1)
  48. animate = NO;
  49. if (animating) {
  50. // do nothing
  51. } else if (animate == NO) {
  52. [self setPosition:position ofDividerAtIndex:dividerIndex];
  53. } else {
  54. animating = YES;
  55. [NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
  56. if (dividerIndex == 0)
  57. [[self animator] setFirstSplitPosition:position];
  58. else if (dividerIndex == 1)
  59. [[self animator] setSecondSplitPosition:position];
  60. else
  61. [self setPosition:position ofDividerAtIndex:dividerIndex];
  62. }
  63. completionHandler:^{
  64. animating = NO;
  65. [[NSNotificationCenter defaultCenter] postNotificationName:CPDFOfficeSplitViewAnimationDidEndNotification object:self];
  66. }];
  67. }
  68. }
  69. @end