mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-11-29 09:52:03 +00:00
CPSplitView: Animate divider moves sent through the animator proxy
Previously, the animator proxy forwarded divider moves directly to the split view, setting frames immediately and forcing applications to animate subviews themselves. CPSplitViewAnimator now animates the two adjacent subviews and the divider within the active CPAnimationContext group. Collapsing subviews shrink to zero and are hidden while preserving their saved size; expanding subviews grow from zero. The split view updates to its final state immediately, and only on-screen frames animate.
This commit is contained in:
1 parent
49e0407ef4
commit
156335dc85
3 files changed
+189
-4
No files matched your search
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* CPSplitView+Animation.j
|
||||
* AppKit
|
||||
*
|
||||
* Created by David Richardson on 2026-11-23.
|
||||
* 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 "CPSplitViewCore.j"
|
||||
@import "CPViewAnimator.j"
|
||||
|
||||
/*
|
||||
The animator proxy of a split view. A divider move sent to it animates inside the current
|
||||
CPAnimationContext group: the two subviews beside the divider and the divider itself move
|
||||
to their new frames. A subview that collapses shrinks to nothing and is then hidden with its
|
||||
size kept; a subview that expands grows from nothing. Without a group duration, or where the
|
||||
browser has no CSS animation, the move is the same as the split view's own method.
|
||||
|
||||
2026-11-23: the animator proxy forwarded the move to the split view, which set the frames at
|
||||
once. An application had to animate the subviews itself, and could not do it without
|
||||
knowing how the split view places them and hides a collapsed subview.
|
||||
*/
|
||||
@implementation CPSplitViewAnimator : CPViewAnimator
|
||||
|
||||
- (void)setPosition:(float)position ofDividerAtIndex:(int)dividerIndex
|
||||
{
|
||||
[_target _animateToPosition:position ofDividerAtIndex:dividerIndex];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation CPSplitView (Animation)
|
||||
|
||||
/*
|
||||
Moves a divider as -setPosition:ofDividerAtIndex: does, and animates the views that change.
|
||||
The split view takes its final state at once, so its ratios, autosave and notifications are
|
||||
those of the end of the move; only the frames on screen animate.
|
||||
*/
|
||||
- (void)_animateToPosition:(float)position ofDividerAtIndex:(int)dividerIndex
|
||||
{
|
||||
var views = [_arrangedSubviews[dividerIndex], _arrangedSubviews[dividerIndex + 1], _dividerSubviews[dividerIndex]],
|
||||
startStates = [];
|
||||
|
||||
for (var i = 0, count = views.length; i < count; i++)
|
||||
startStates.push({ frame: CGRectMakeCopy([views[i] frame]), hidden: !![views[i] isHidden] });
|
||||
|
||||
[self setPosition:position ofDividerAtIndex:dividerIndex];
|
||||
|
||||
for (var i = 0, count = views.length; i < count; i++)
|
||||
[self _animateView:views[i] fromState:startStates[i]];
|
||||
}
|
||||
|
||||
/*
|
||||
Animates a view from the state it had before a divider move to the state the move gave it.
|
||||
A view that is hidden at the end animates to no size along the split, and one that was
|
||||
hidden at the start animates from no size; the end state is applied when the animation
|
||||
ends, or at once when there is no animation.
|
||||
*/
|
||||
- (void)_animateView:(CPView)aView fromState:(Object)aStartState
|
||||
{
|
||||
var endFrame = CGRectMakeCopy([aView frame]),
|
||||
endHidden = !![aView isHidden];
|
||||
|
||||
if (aStartState.hidden && endHidden)
|
||||
return;
|
||||
|
||||
if (!aStartState.hidden && !endHidden && CGRectEqualToRect(endFrame, aStartState.frame))
|
||||
return;
|
||||
|
||||
var startFrame = aStartState.frame,
|
||||
targetFrame = endFrame;
|
||||
|
||||
if (aStartState.hidden)
|
||||
{
|
||||
startFrame = CGRectMakeCopy(endFrame);
|
||||
startFrame.size[_sizeComponent] = 0;
|
||||
}
|
||||
|
||||
if (endHidden)
|
||||
{
|
||||
targetFrame = CGRectMakeCopy(endFrame);
|
||||
targetFrame.size[_sizeComponent] = 0;
|
||||
}
|
||||
|
||||
var finish = function()
|
||||
{
|
||||
[aView setFrame:endFrame];
|
||||
[aView setHidden:endHidden];
|
||||
};
|
||||
|
||||
[aView setHidden:NO];
|
||||
[aView setFrame:startFrame];
|
||||
[[aView animator] _setTargetValue:targetFrame withKeyPath:@"frame" fallback:finish completion:finish];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -37,3 +37,4 @@
|
||||
@import "CPSplitView+Autosave.j"
|
||||
@import "CPSplitView+Delegate.j"
|
||||
@import "CPSplitView+Coding.j"
|
||||
@import "CPSplitView+Animation.j"
|
||||
@@ -29,10 +29,11 @@
|
||||
/*
|
||||
These tests state how a split view behaves as AppKit's NSSplitView does: divider drags and
|
||||
their delegate constraints, collapsing, double-clicks, hidden dividers, holding priorities,
|
||||
notifications, hit testing, button bars, autosave, orientation, arranged subviews and
|
||||
archiving. They come before a refactoring and a rework of CPSplitView, so they test through
|
||||
public behaviour only. The one exception is -_restoreFromAutosave: today only a cib load
|
||||
starts a restore, so the autosave tests call it, as CPSplitViewTest does.
|
||||
animated divider moves, notifications, hit testing, button bars, autosave, orientation,
|
||||
arranged subviews and archiving. They come before a refactoring and a rework of
|
||||
CPSplitView, so they test through public behaviour only. The one exception is
|
||||
-_restoreFromAutosave: today only a cib load starts a restore, so the autosave tests call
|
||||
it, as CPSplitViewTest does.
|
||||
|
||||
Each test uses a vertical split view 300 points wide and 100 points high, with thin dividers
|
||||
(1 point under ojtest, where no theme is loaded). The first subview is 100 points wide; the
|
||||
@@ -474,6 +475,79 @@
|
||||
[self assertThrows:function() { [splitView holdingPriorityForSubviewAtIndex:1]; }];
|
||||
}
|
||||
|
||||
// MARK: - Animated divider moves
|
||||
|
||||
/*
|
||||
ojtest has no CSS animation, so a move sent to the animator takes the path without an
|
||||
animation. These tests state the end of a move, which both paths share, and that the move
|
||||
finishes inside an animation group.
|
||||
*/
|
||||
- (void)testTheAnimatorMovesTheDivider
|
||||
{
|
||||
[[splitView animator] setPosition:140 ofDividerAtIndex:0];
|
||||
|
||||
[self assertFrame:CGRectMake(0, 0, 140, 100) of:viewA named:@"the first subview"];
|
||||
[self assertFrame:CGRectMake(141, 0, 159, 100) of:viewB named:@"the second subview"];
|
||||
[self assert:140 equals:CGRectGetMinX([splitView rectOfDividerAtIndex:0]) message:@"the divider is at the new position"];
|
||||
}
|
||||
|
||||
- (void)testAnAnimatedMoveEndsAtThePositionAndCallsTheCompletionHandler
|
||||
{
|
||||
var completed = NO;
|
||||
|
||||
[CPAnimationContext runAnimationGroup:function(context)
|
||||
{
|
||||
[context setDuration:0.25];
|
||||
[[splitView animator] setPosition:140 ofDividerAtIndex:0];
|
||||
}
|
||||
completionHandler:function()
|
||||
{
|
||||
completed = YES;
|
||||
}];
|
||||
|
||||
[self assertTrue:completed message:@"the completion handler is called"];
|
||||
[self assertFrame:CGRectMake(0, 0, 140, 100) of:viewA named:@"the first subview"];
|
||||
[self assertFrame:CGRectMake(141, 0, 159, 100) of:viewB named:@"the second subview"];
|
||||
}
|
||||
|
||||
- (void)testAnAnimatedCollapseHidesTheSubviewAndKeepsItsSize
|
||||
{
|
||||
[testDelegate setMinimum:80];
|
||||
[testDelegate setCollapsibleSubviews:[viewA]];
|
||||
[splitView setDelegate:testDelegate];
|
||||
|
||||
[CPAnimationContext runAnimationGroup:function(context)
|
||||
{
|
||||
[context setDuration:0.25];
|
||||
[[splitView animator] setPosition:0 ofDividerAtIndex:0];
|
||||
}
|
||||
completionHandler:nil];
|
||||
|
||||
[self assertTrue:[splitView isSubviewCollapsed:viewA] message:@"the first subview is collapsed"];
|
||||
[self assertTrue:[viewA isHidden] message:@"the collapsed subview is hidden"];
|
||||
[self assert:100 equals:CGRectGetWidth([viewA frame]) message:@"the collapsed subview keeps its width"];
|
||||
[self assertFrame:CGRectMake(1, 0, 299, 100) of:viewB named:@"the second subview"];
|
||||
}
|
||||
|
||||
- (void)testAnAnimatedExpansionShowsTheSubview
|
||||
{
|
||||
[testDelegate setMinimum:80];
|
||||
[testDelegate setCollapsibleSubviews:[viewA]];
|
||||
[splitView setDelegate:testDelegate];
|
||||
[splitView setPosition:0 ofDividerAtIndex:0];
|
||||
|
||||
[CPAnimationContext runAnimationGroup:function(context)
|
||||
{
|
||||
[context setDuration:0.25];
|
||||
[[splitView animator] setPosition:140 ofDividerAtIndex:0];
|
||||
}
|
||||
completionHandler:nil];
|
||||
|
||||
[self assertFalse:[viewA isHidden] message:@"the first subview is shown"];
|
||||
[self assertFrame:CGRectMake(0, 0, 140, 100) of:viewA named:@"the first subview"];
|
||||
[self assertFrame:CGRectMake(141, 0, 159, 100) of:viewB named:@"the second subview"];
|
||||
}
|
||||
|
||||
// MARK: - Notifications
|
||||
|
||||
- (void)testDraggingTheDividerPostsTheResizeNotifications
|
||||
|
||||
Reference in new issue
Block a user