CPOutlineView: Animate expand/collapse only for animator and user actions

Remove the `animates` flag so direct `expandItem:` and `collapseItem:`
calls take effect immediately, matching Cocoa. Calls via the animator
use the current context's duration (0.25s by default, or immediate
when explicitly 0). Disclosure-control clicks and left/right arrow
keys animate for 0.25s. Disable animation when reduced motion is
active.

Add tests covering direct calls, animator calls, explicit zero
duration, reduced motion, arrow keys, and caller animation groups.
This commit is contained in:
David Richardson committed 2026-11-27 12:35:30 -06:00
1 parent b6632ab5ff
commit 5b4192e597
2 files changed
+283 -25

No files matched your search

+82 -25
View File
@@ -25,6 +25,8 @@
@import "CPTableColumn.j"
@import "CPTableView.j"
@import "CPViewAnimation.j"
@import "CPWorkspace.j"
@import "CPAnimationContext.j"
@import "CPTreeNode.j"
@global CPApp
@@ -304,7 +306,7 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
CPIndexSet _animatingDisclosureRows;
BOOL _animates @accessors(property=animates);
BOOL _isUserDisclosureOperation;
CPViewAnimation _rowsAnimation;
CPArray _animationGhosts;
}
@@ -339,7 +341,7 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
[self setDisclosureControlPrototype:[[CPDisclosureButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 10.0, 10.0)]];
_animatingDisclosureRows = [CPIndexSet indexSet];
_animates = YES;
_isUserDisclosureOperation = NO;
}
return self;
@@ -711,7 +713,8 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
- (void)expandItem:(id)anItem expandChildren:(BOOL)shouldExpandChildren
{
var oldRowCount = [self numberOfRows],
parentIndex = [self rowForItem:anItem];
parentIndex = [self rowForItem:anItem],
animationDuration = [self _disclosureAnimationDuration];
if ([self _delegateRespondsToShouldExpandItem])
if ([_outlineViewDelegate outlineView:self shouldExpandItem:anItem] == NO)
@@ -788,10 +791,10 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
var newRowCount = [self numberOfRows],
addedCount = newRowCount - oldRowCount;
if (_animates && addedCount > 0)
if (animationDuration > 0 && addedCount > 0)
{
[self _layoutSubviewsInEffectiveAppearance]; // Force views to spawn so we can animate them
[self _animateExpandFromIndex:parentIndex addedCount:addedCount];
[self _animateExpandFromIndex:parentIndex addedCount:addedCount duration:animationDuration];
}
}
@@ -854,8 +857,10 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
}
// Capture visual ghosts BEFORE the data is removed
var ghosts = nil;
if (_animates && collapseRange.length > 0)
var ghosts = nil,
animationDuration = [self _disclosureAnimationDuration];
if (animationDuration > 0 && collapseRange.length > 0)
ghosts = [self _createGhostsForRowsFrom:collapseRange.location to:CPMaxRange(collapseRange) - 1];
// Collapse the model
@@ -864,10 +869,10 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
[self _noteItemDidCollapse:anItem];
// Trigger Animation
if (_animates && collapseRange.length > 0)
if (animationDuration > 0 && collapseRange.length > 0)
{
[self _layoutSubviewsInEffectiveAppearance];
[self _animateCollapseFromIndex:collapseTopIndex removedCount:collapseRange.length ghosts:ghosts];
[self _layoutSubviewsInEffectiveAppearance];
[self _animateCollapseFromIndex:collapseTopIndex removedCount:collapseRange.length ghosts:ghosts duration:animationDuration];
}
var r = _coalesceSelectionNotificationState;
@@ -877,7 +882,50 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
[self _noteSelectionDidChange];
}
- (void)_animateCollapseFromIndex:(CPInteger)parentIndex removedCount:(CPInteger)removedCount ghosts:(CPArray)ghosts
/*
Returns the duration of the animation of an expand or a collapse that is starting, or 0 when
it does not animate. As in AppKit, a direct -expandItem: or -collapseItem: does not animate.
It animates when the developer calls it through the animator, for the context's duration, or
when the user clicks a disclosure control or presses an arrow key. Under reduced motion
nothing animates, and the rows take their end state at once.
2026-11-26: every expand and collapse animated, by an animates property that NSOutlineView
does not have. A collapse put copies of the removed rows over the table while it animated,
in front of what the table draws on its rows.
*/
- (double)_disclosureAnimationDuration
{
if ([[CPWorkspace sharedWorkspace] accessibilityDisplayShouldReduceMotion])
return 0.0;
if (_isUserDisclosureOperation)
return 0.25;
var context = [CPAnimationContext _currentContextIfAny];
return context ? [context _animatorOperationDuration] : 0.0;
}
/*
Runs anOperation, an expand or a collapse that the user started, so that it animates.
*/
- (void)_performUserDisclosureOperation:(Function)anOperation
{
var wasUserDisclosureOperation = _isUserDisclosureOperation;
_isUserDisclosureOperation = YES;
try
{
anOperation();
}
finally
{
_isUserDisclosureOperation = wasUserDisclosureOperation;
}
}
- (void)_animateCollapseFromIndex:(CPInteger)parentIndex removedCount:(CPInteger)removedCount ghosts:(CPArray)ghosts duration:(double)aDuration
{
// Ensure we don't animate if parent is invalid
if (parentIndex < 0)
@@ -977,7 +1025,7 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
}
}
[self _runAnimation:viewAnimations withGhosts:ghosts];
[self _runAnimation:viewAnimations withGhosts:ghosts duration:aDuration];
}
// MARK: - CPRuleEditor Style Animations
@@ -1011,7 +1059,7 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
return ghosts;
}
- (void)_animateExpandFromIndex:(CPInteger)parentIndex addedCount:(CPInteger)addedCount
- (void)_animateExpandFromIndex:(CPInteger)parentIndex addedCount:(CPInteger)addedCount duration:(double)aDuration
{
if (_rowsAnimation && [_rowsAnimation isAnimating])
[_rowsAnimation stopAnimation];
@@ -1076,10 +1124,10 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
}
}
[self _runAnimation:viewAnimations withGhosts:nil];
[self _runAnimation:viewAnimations withGhosts:nil duration:aDuration];
}
- (void)_runAnimation:(CPArray)viewAnimations withGhosts:(CPArray)ghosts
- (void)_runAnimation:(CPArray)viewAnimations withGhosts:(CPArray)ghosts duration:(double)aDuration
{
// Clean up interrupted animations
if (_animationGhosts)
@@ -1092,7 +1140,8 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
if (viewAnimations.length > 0)
{
_rowsAnimation = [[CPViewAnimation alloc] initWithViewAnimations:viewAnimations];
[_rowsAnimation setDuration:0.25];[_rowsAnimation setAnimationCurve:CPAnimationEaseInOut];
[_rowsAnimation setDuration:aDuration];
[_rowsAnimation setAnimationCurve:CPAnimationEaseInOut];
[_rowsAnimation setDelegate:self];
[_rowsAnimation startAnimation];
}
@@ -2168,11 +2217,13 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
var controlFrame = [aControl frame],
item = [self itemAtRow:[self rowAtPoint:CGPointMake(CGRectGetMinX(controlFrame), CGRectGetMidY(controlFrame))]];
if ([self isItemExpanded:item])
[self collapseItem:item];
else
[self expandItem:item expandChildren:([[CPApp currentEvent] modifierFlags] & CPAlternateKeyMask)];
[self _performUserDisclosureOperation:function()
{
if ([self isItemExpanded:item])
[self collapseItem:item];
else
[self expandItem:item expandChildren:([[CPApp currentEvent] modifierFlags] & CPAlternateKeyMask)];
}];
}
/*!
@@ -2308,8 +2359,11 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
if (character === CPRightArrowFunctionKey)
{
for (var i = 0; i < c; i++)
[self expandItem:items[i]];
[self _performUserDisclosureOperation:function()
{
for (var i = 0; i < c; i++)
[self expandItem:items[i]];
}];
}
else if (character === CPLeftArrowFunctionKey)
{
@@ -2332,8 +2386,11 @@ function SHOULD_SELECT_ITEM(anOutlineView, anItem)
}
}
for (var i = 0; i < c; i++)
[self collapseItem:items[i]];
[self _performUserDisclosureOperation:function()
{
for (var i = 0; i < c; i++)
[self collapseItem:items[i]];
}];
}
[super keyDown:anEvent];
@@ -0,0 +1,201 @@
/*
* CPOutlineViewDisclosureAnimationTest.j
* AppKit
*
* Created by David Richardson on 2026-11-26.
* Copyright David Richardson 2026.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <AppKit/AppKit.j>
/*
As in AppKit, a direct -expandItem: or -collapseItem: does not animate. It animates when the
developer calls it through the animator, for the animation context's duration, or 0.25
seconds when the context sets none; an explicit duration of 0 turns the animation off. It
also animates when the user presses an arrow key. Under reduced motion nothing animates.
A collapse that animates shows copies of the removed rows, plain views, while the rows slide.
The animation does not run in a unit test, so the copies stay, and their presence shows
whether the collapse animated.
*/
@implementation DisclosureAnimationItem : CPObject
{
CPArray children @accessors;
}
+ (id)itemWithChildren:(CPArray)someChildren
{
var item = [[self alloc] init];
[item setChildren:someChildren];
return item;
}
@end
@implementation CPOutlineViewDisclosureAnimationTest : OJTestCase
{
CPWindow theWindow;
CPOutlineView outlineView;
CPArray rootItems;
}
- (void)setUp
{
// This will init the global var CPApp which are used internally in the AppKit
[[CPApplication alloc] init];
[[CPWorkspace sharedWorkspace] _setSystemReduceMotionOverride:NO];
[[CPWorkspace sharedWorkspace] setApplicationShouldReduceMotion:NO];
theWindow = [[CPWindow alloc] initWithContentRect:CGRectMake(0.0, 0.0, 400.0, 400.0) styleMask:CPWindowNotSizable];
rootItems = [];
for (var i = 0; i < 5; i++)
rootItems.push([DisclosureAnimationItem itemWithChildren:[[DisclosureAnimationItem itemWithChildren:[]], [DisclosureAnimationItem itemWithChildren:[]]]]);
var column = [[CPTableColumn alloc] initWithIdentifier:@"Column"];
outlineView = [[CPOutlineView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 200.0)];
[outlineView addTableColumn:column];
[outlineView setOutlineTableColumn:column];
var scrollView = [[CPScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 200.0)];
[scrollView setDocumentView:outlineView];
[[theWindow contentView] addSubview:scrollView];
[outlineView setDataSource:self];
[outlineView reloadData];
[outlineView expandItem:rootItems[1]];
[outlineView layoutIfNeeded];
}
- (void)tearDown
{
[[CPWorkspace sharedWorkspace] setApplicationShouldReduceMotion:NO];
[[CPWorkspace sharedWorkspace] _setSystemReduceMotionOverride:nil];
}
- (id)outlineView:(CPOutlineView)anOutlineView child:(CPInteger)anIndex ofItem:(id)anItem
{
return anItem ? [anItem children][anIndex] : rootItems[anIndex];
}
- (BOOL)outlineView:(CPOutlineView)anOutlineView isItemExpandable:(id)anItem
{
return [[anItem children] count] > 0;
}
- (int)outlineView:(CPOutlineView)anOutlineView numberOfChildrenOfItem:(id)anItem
{
return anItem ? [[anItem children] count] : rootItems.length;
}
- (id)outlineView:(CPOutlineView)anOutlineView objectValueForTableColumn:(CPTableColumn)aColumn byItem:(id)anItem
{
return @"Item";
}
/*
The number of plain views in the outline view: the copies a collapse shows while it animates.
*/
- (int)rowCopyCount
{
return [outlineView subviews].filter(function(aView)
{
return [aView class] === [CPView class];
}).length;
}
- (void)testADirectCollapseDoesNotAnimate
{
var copiesBefore = [self rowCopyCount];
[outlineView collapseItem:rootItems[1]];
[self assertFalse:[outlineView isItemExpanded:rootItems[1]] message:@"the item collapsed"];
[self assert:copiesBefore equals:[self rowCopyCount] message:@"no row copies"];
}
- (void)testACollapseThroughTheAnimatorAnimates
{
var copiesBefore = [self rowCopyCount];
[[outlineView animator] collapseItem:rootItems[1]];
[self assertFalse:[outlineView isItemExpanded:rootItems[1]] message:@"the item collapsed"];
[self assertTrue:[self rowCopyCount] > copiesBefore message:@"row copies are shown while the rows slide"];
}
- (void)testAnExplicitZeroDurationTurnsTheAnimationOff
{
var copiesBefore = [self rowCopyCount];
[CPAnimationContext runAnimationGroup:function(aContext)
{
[aContext setDuration:0.0];
[[outlineView animator] collapseItem:rootItems[1]];
} completionHandler:nil];
[self assertFalse:[outlineView isItemExpanded:rootItems[1]] message:@"the item collapsed"];
[self assert:copiesBefore equals:[self rowCopyCount] message:@"no row copies"];
}
- (void)testReducedMotionStopsTheAnimation
{
var copiesBefore = [self rowCopyCount];
[[CPWorkspace sharedWorkspace] _setSystemReduceMotionOverride:YES];
[[outlineView animator] collapseItem:rootItems[1]];
[self assertFalse:[outlineView isItemExpanded:rootItems[1]] message:@"the item collapsed"];
[self assert:copiesBefore equals:[self rowCopyCount] message:@"no row copies"];
}
- (void)testACollapseByTheUserWithTheLeftArrowKeyAnimates
{
var copiesBefore = [self rowCopyCount],
arrow = [CPEvent keyEventWithType:CPKeyDown location:CGPointMakeZero() modifierFlags:0
timestamp:0 windowNumber:[theWindow windowNumber] context:nil
characters:CPLeftArrowFunctionKey charactersIgnoringModifiers:CPLeftArrowFunctionKey
isARepeat:NO keyCode:123];
[outlineView selectRowIndexes:[CPIndexSet indexSetWithIndex:[outlineView rowForItem:rootItems[1]]] byExtendingSelection:NO];
[outlineView keyDown:arrow];
[self assertFalse:[outlineView isItemExpanded:rootItems[1]] message:@"the item collapsed"];
[self assertTrue:[self rowCopyCount] > copiesBefore message:@"row copies are shown while the rows slide"];
}
- (void)testAnOperationCalledThroughTheAnimatorStaysInTheCallersGroup
{
var context = nil;
[CPAnimationContext runAnimationGroup:function(aContext)
{
context = aContext;
[[outlineView animator] collapseItem:rootItems[1]];
[self assertTrue:[CPAnimationContext currentContext] === aContext message:@"the call left the current group in place"];
} completionHandler:nil];
[self assertFalse:[context _isAnimatorOperation] message:@"the mark ends with the call"];
}
@end