new: triangle animation in outline view

This commit is contained in:
daboe01 committed 2026-01-26 22:38:49 +01:00
1 parent ef3ee2e660
commit 5540ef8ca7
1 file changed
+91 -36
+91 -36
View File
@@ -2215,22 +2215,103 @@ var CPOutlineViewCoalesceSelectionNotificationStateOff = 0,
self = [super initWithFrame:aFrame];
if (self)
{
[self setBordered:NO];
[self setWantsLayer:YES];
}
return self;
}
- (void)setAngle:(float)anAngle
{
_angle = anAngle;
[self display];
}
- (float)angle
{
return _angle;
}
// Standard setState behavior: ALWAYS snap to the correct angle immediately.
// The animation clone will hide this snap from the user.
- (void)setState:(CPInteger)aState
{
[super setState:aState];
if ([self state] === CPOnState)
_angle = 0.0;
else
_angle = -PI_2;
}
// We interpret mouseUp as the "Click" that triggers the toggle.
- (void)mouseDown:(CPEvent)anEvent
{
var bounds = [self bounds],
point = [self convertPoint:[anEvent locationInWindow] fromView:nil];
// Only animate if the user released the mouse INSIDE the button (valid click)
if (CGRectContainsPoint(bounds, point))
{
// 1. Create a "Ghost" clone of this button
var clone = [[CPDisclosureButton alloc] initWithFrame:[self frame]];
// 2. Copy the visual appearance to the clone
[clone setWantsLayer:YES];
[clone setThemeState:[self themeState]];
// Ensure the clone is NOT highlighted (since the click is finishing),
// so it looks like the resting state of the button.
[clone unsetThemeState:CPThemeStateHighlighted];
[clone setHighlighted:NO];
// Set the clone's initial angle to match the current button's angle
[clone setAngle:_angle];
// Make the clone ignore mouse events (transparency)
[clone setHitTests:NO];
// 3. Add the clone to the OutlineView (superview) directly.
// We place it visually above 'self'.
[[self superview] addSubview:clone positioned:CPWindowAbove relativeTo:self];
// 4. Calculate the target angle based on what the state WILL be.
// Current state is On -> going to Off (-PI_2). Current is Off -> going to On (0.0).
var isCurrentlyExpanded = ([self state] === CPOnState),
targetAngle = isCurrentlyExpanded ? -PI_2 : 0.0;
// 5. Animate the CLONE
var anim = [CABasicAnimation animationWithKeyPath:@"angle"];
[anim setFromValue:_angle];
[anim setToValue:targetAngle];
[anim setDuration:0.25];
[anim setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
// Set the clone as the layer-delegate so the angle-property can be animated on it
[[clone layer] setDelegate:clone];
// Set the clone as the delegate so it can remove itself when done
[anim setDelegate:clone];
[[clone layer] addAnimation:anim forKey:@"angle"];
// Set final angle on clone so it doesn't flicker back at the end of the frame
[clone setAngle:targetAngle];
}
// 6. Call super.
// This triggers the OutlineView action, which reloads the data, recycles 'self',
// and snaps 'self' (or its replacement) to the new angle immediately.
// The clone covers this instant change with the smooth animation.
[super mouseDown:anEvent];
}
// This method is called on the CLONE instance when its animation finishes
- (void)animationDidStop:(CAAnimation)anim finished:(BOOL)flag
{
[self removeFromSuperview];
}
- (void)drawRect:(CGRect)aRect
{
var bounds = [self bounds],
@@ -2247,75 +2328,50 @@ var CPOutlineViewCoalesceSelectionNotificationStateOff = 0,
CGContextRotateCTM(context, _angle);
CGContextTranslateCTM(context, -centre.x, -centre.y);
}
// Center, but crisp.
CGContextTranslateCTM(context, FLOOR((width - 9.0) / 2.0), FLOOR((height - 8.0) / 2.0));
CGContextMoveToPoint(context, 0.0, 0.0);
CGContextAddLineToPoint(context, 9.0, 0.0);
CGContextAddLineToPoint(context, 4.5, 8.0);
CGContextClosePath(context);
// Calculate Color based on HUD and KeyWindow states ---
var isSelected = [self hasThemeState:CPThemeStateSelected],
isHighlighted = [self hasThemeState:CPThemeStateHighlighted],
isKeyWindow = [self hasThemeState:CPThemeStateKeyWindow],
// Detect HUD style mask on the window
isHUD = [self window] && ([[self window] styleMask] & CPHUDBackgroundWindowMask),
triangleColor = nil;
if (isHUD)
{
// 2. HUD Logic
if (isSelected)
triangleColor = [CPColor blackColor]; // Selected HUD row = White BG -> Black Triangle
else
triangleColor = [CPColor whiteColor]; // Normal HUD row = Dark BG -> White Triangle
// Handle click highlight in HUD (dim it)
if (isHighlighted)
triangleColor = [triangleColor colorWithAlphaComponent:0.5];
triangleColor = isSelected ? [CPColor blackColor] : [CPColor whiteColor];
if (isHighlighted) triangleColor = [triangleColor colorWithAlphaComponent:0.5];
}
else
{
// Standard Logic
if (isSelected)
{
if (isKeyWindow)
triangleColor = [CPColor whiteColor]; // Key Window + Selected = Blue BG -> White Triangle
else
triangleColor = [CPColor blackColor]; // 1. Non-Key Window + Selected = Gray BG -> Black Triangle
}
else
{
// Standard Unselected
triangleColor = [CPColor colorWithCalibratedWhite:0.45 alpha: 1.0];
}
if (isSelected) triangleColor = isKeyWindow ? [CPColor whiteColor] : [CPColor blackColor];
else triangleColor = [CPColor colorWithCalibratedWhite:0.45 alpha: 1.0];
// Handle click highlight in Standard
if (isHighlighted)
{
if (isSelected && isKeyWindow)
triangleColor = [CPColor colorWithCalibratedWhite:0.9 alpha: 1.0];
else if (isSelected && !isKeyWindow)
triangleColor = [CPColor colorWithCalibratedWhite:0.2 alpha: 1.0];
else
else
triangleColor = [CPColor colorWithCalibratedWhite:0.25 alpha: 1.0];
}
}
CGContextSetFillColor(context, triangleColor);
CGContextFillPath(context);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0.0, 0.0);
CGContextAddLineToPoint(context, 4.5, 8.0);
if (_angle === 0.0)
CGContextAddLineToPoint(context, 9.0, 0.0);
// Adjust stroke opacity for HUD/Non-Key to avoid "ghostly" white borders on light backgrounds
var strokeAlpha = (isHUD || (isSelected && !isKeyWindow)) ? 0.3 : 0.7;
CGContextSetStrokeColor(context, [CPColor colorWithCalibratedWhite:1.0 alpha:strokeAlpha]);
CGContextStrokePath(context);
@@ -2323,7 +2379,6 @@ var CPOutlineViewCoalesceSelectionNotificationStateOff = 0,
@end
var CPOutlineViewIndentationPerLevelKey = @"CPOutlineViewIndentationPerLevelKey",
CPOutlineViewOutlineTableColumnKey = @"CPOutlineViewOutlineTableColumnKey",
CPOutlineViewDataSourceKey = @"CPOutlineViewDataSourceKey",