CPAnimationContext: Track animator calls, explicit durations, and initial grouping

Record whether a group's duration was set explicitly and inherit it in
nested groups. Add a private animator operation duration that returns 0
outside an animator call, and otherwise returns the explicit duration or
0.25 seconds by default.

Route object animator calls through forwardInvocation: by returning nil
for forwardingTargetForSelector: and returning a placeholder signature
from methodSignatureForSelector: only when the target responds to the
selector (returning nil otherwise to preserve unknown-selector errors).
Mark the current context during the forwarded call so its animations
stay in the caller's group.

Initialize the context stack in beginGrouping when absent so a group
started before a context exists is retained.
This commit is contained in:
David Richardson committed 2026-11-27 12:34:34 -06:00
1 parent 7a3c088011
commit b6632ab5ff
2 files changed
+98 -11

No files matched your search

+66 -6
View File
@@ -12,7 +12,9 @@ var _CPAnimationContextStack = nil,
@implementation CPAnimationContext : CPObject
{
double _duration @accessors(property=duration);
double _duration @accessors(getter=duration);
BOOL _hasExplicitDuration;
BOOL _isAnimatorOperation;
CAMediaTimingFunction _timingFunction @accessors(property=timingFunction);
Function _completionHandlerAgent;
Map _animationsByObject;
@@ -34,6 +36,15 @@ var _CPAnimationContextStack = nil,
return context;
}
/*
Returns the context of the current group, or nil outside every group. Unlike
+currentContext, it creates no context.
*/
+ (CPAnimationContext)_currentContextIfAny
{
return [_CPAnimationContextStack lastObject] || nil;
}
+ (CPArray)contextStack
{
if (!_CPAnimationContextStack)
@@ -54,11 +65,54 @@ var _CPAnimationContextStack = nil,
[CPAnimationContext endGrouping];
}
/*!
Sets the duration of the animations of the group. A duration set here, including 0, also
decides whether an operation called through an animator animates: 0 turns it off.
*/
- (void)setDuration:(double)aDuration
{
_duration = aDuration;
_hasExplicitDuration = YES;
}
/*
An animator proxy marks the current context as an animator operation while it runs a call
it forwards. A framework operation that the developer starts, such as
-[CPOutlineView expandItem:], animates only in such a group, as in AppKit.
2026-11-26: there was no way for such an operation to know that it was called through an
animator, so CPOutlineView animated every expand and collapse.
*/
- (BOOL)_isAnimatorOperation
{
return _isAnimatorOperation;
}
- (void)_setAnimatorOperation:(BOOL)isAnimatorOperation
{
_isAnimatorOperation = !!isAnimatorOperation;
}
/*
Returns the duration of a framework operation in this group: 0 outside an animator
operation, the group's duration when one was set, and 0.25 seconds, as in AppKit, when
none was set.
*/
- (double)_animatorOperationDuration
{
if (!_isAnimatorOperation)
return 0.0;
return _hasExplicitDuration ? _duration : 0.25;
}
- (id)init
{
self = [super init];
_duration = 0.0;
_hasExplicitDuration = NO;
_isAnimatorOperation = NO;
_timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
_completionHandlerAgent = nil;
_animationsByObject = new Map();
@@ -77,16 +131,22 @@ var _CPAnimationContextStack = nil,
+ (void)beginGrouping
{
var newContext = [[CPAnimationContext alloc] init];
var newContext = [[CPAnimationContext alloc] init],
contextStack = [self contextStack];
if ([_CPAnimationContextStack count])
if ([contextStack count])
{
var currentContext = [_CPAnimationContextStack lastObject];
[newContext setDuration:[currentContext duration]];
var currentContext = [contextStack lastObject];
newContext._duration = currentContext._duration;
newContext._hasExplicitDuration = currentContext._hasExplicitDuration;
[newContext setTimingFunction:[currentContext timingFunction]];
}
[_CPAnimationContextStack addObject:newContext];
/*
2026-11-26: the stack was created only by +contextStack. A group begun before any
context existed was added to no stack and lost, and its end did nothing.
*/
[contextStack addObject:newContext];
}
+ (BOOL)endGrouping
+32 -5
View File
@@ -46,21 +46,48 @@ var _supportsCSSAnimations = null;
return [_target isEqual:anObject];
}
/*
Returns nil, so that the runtime sends every call the animator does not implement through
-forwardInvocation:, which runs it as an animator operation. With the target returned here,
the runtime sent the call straight to the target, outside any group.
*/
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return _target;
return nil;
}
/*
The runtime forwards a call through -forwardInvocation: only when it gets a signature.
Method signatures are not implemented, so, as CPUndoManager's proxy does, the animator gives
a placeholder for each selector its target responds to. Any other selector reaches
-doesNotRecognizeSelector:.
*/
- (CPMethodSignature)methodSignatureForSelector:(SEL)aSelector
{
return [_target methodSignatureForSelector:aSelector];
return [_target respondsToSelector:aSelector] ? 1 : nil;
}
/*
Runs a forwarded call as an animator operation, so that a framework operation such as
-[CPOutlineView collapseItem:] can tell that the developer asked for it to animate. The
current context carries the mark for the length of the call. The call stays in the caller's
group, so its animations end with that group and its completion handler waits for them.
*/
- (void)forwardInvocation:(CPInvocation)anInvocation
{
var target = [self forwardingTargetForSelector:[anInvocation selector]];
[anInvocation invokeWithTarget:target];
return;
var context = [CPAnimationContext currentContext],
wasAnimatorOperation = [context _isAnimatorOperation];
[context _setAnimatorOperation:YES];
try
{
[anInvocation invokeWithTarget:_target];
}
finally
{
[context _setAnimatorOperation:wasAnimatorOperation];
}
}
- (void)doesNotRecognizeSelector:(SEL)aSelector