From 7e4e1ec68f89a8668677d4444bfde8aa15c67bca Mon Sep 17 00:00:00 2001 From: Antoine Mercadal Date: Fri, 14 Aug 2015 01:44:22 -0700 Subject: [PATCH] FIXED: lot's of error with CPAppearance --- AppKit/CPView.j | 43 +++++++++++++++++-------------------- AppKit/CPVisualEffectView.j | 18 +++++++++------- Tests/AppKit/CPViewTest.j | 25 +++++++++++++++------ 3 files changed, 48 insertions(+), 38 deletions(-) diff --git a/AppKit/CPView.j b/AppKit/CPView.j index cf6e4f3ad..cd589a28b 100644 --- a/AppKit/CPView.j +++ b/AppKit/CPView.j @@ -244,7 +244,6 @@ var CPViewHighDPIDrawingEnabled = YES; BOOL _allowsVibrancy @accessors(property=allowsVibrancy); CPAppearance _appearance @accessors(getter=appearance); CPAppearance _effectiveAppearance; - BOOL _effectiveAppearanceCached; } /* @@ -332,6 +331,13 @@ var CPViewHighDPIDrawingEnabled = YES; return nil; } +- (void)awakeFromCib +{ + [super awakeFromCib]; + + [self _recomputeAppearance]; +} + - (id)init { return [self initWithFrame:CGRectMakeZero()]; @@ -374,8 +380,6 @@ var CPViewHighDPIDrawingEnabled = YES; _theme = [CPTheme defaultTheme]; _themeState = CPThemeStateNormal; - _effectiveAppearanceCached = NO; - #if PLATFORM(DOM) _DOMElement = DOMElementPrototype.cloneNode(false); AppKitTagDOMElement(self, _DOMElement); @@ -846,10 +850,9 @@ var CPViewHighDPIDrawingEnabled = YES; */ - (void)viewDidMoveToSuperview { - [self setNeedsDisplay:YES]; + [self _recomputeAppearance]; - [self _resetCachedEffectiveAppearance]; - [self _updateAppearanceThemeState]; + [self setNeedsDisplay:YES]; } /*! @@ -3529,13 +3532,7 @@ setBoundsOrigin: if (_appearance) return _appearance; - if (_effectiveAppearanceCached) - return _effectiveAppearance; - - _effectiveAppearance = [_superview effectiveAppearance]; - _effectiveAppearanceCached = YES; - - return _effectiveAppearance; + return [_superview effectiveAppearance]; } - (void)setAppearance:(CPAppearance)anAppearance @@ -3547,20 +3544,19 @@ setBoundsOrigin: _appearance = anAppearance; [self didChangeValueForKey:@"appearance"]; - [self _resetCachedEffectiveAppearance]; - [self _updateAppearanceThemeState]; + [self _recomputeAppearance]; } /*! @ignore */ -- (void)_resetCachedEffectiveAppearance +- (void)_recomputeAppearance { - _effectiveAppearanceCached = NO; - [_subviews makeObjectsPerformSelector:@selector(_resetCachedEffectiveAppearance)]; -} + // if we don't have a themeState, it means + // the view is not decoding from a cib, so we just return. + // this method will be called again in awakeFromCib + if (!_themeState) + return; -- (void)_updateAppearanceThemeState -{ var effectiveAppearance = [self effectiveAppearance]; if ([effectiveAppearance isEqual:[CPAppearance appearanceNamed:CPAppearanceNameAqua]]) @@ -3598,6 +3594,8 @@ setBoundsOrigin: [self unsetThemeState:CPThemeStateAppearanceVibrantLight]; [self unsetThemeState:CPThemeStateAppearanceVibrantDark]; } + + [_subviews makeObjectsPerformSelector:@selector(_recomputeAppearance)]; } @@ -3739,8 +3737,7 @@ var CPViewAutoresizingMaskKey = @"CPViewAutoresizingMask", _themeAttributes[attributeName] = CPThemeAttributeDecode(aCoder, attributeName, attributes[count], _theme, themeClass); } - _appearance = [aCoder decodeObjectForKey:CPViewAppearanceKey]; - _effectiveAppearanceCached = NO; + [self setAppearance:[aCoder decodeObjectForKey:CPViewAppearanceKey]]; [self setNeedsDisplay:YES]; [self setNeedsLayout]; diff --git a/AppKit/CPVisualEffectView.j b/AppKit/CPVisualEffectView.j index 6ef3645de..ece566562 100644 --- a/AppKit/CPVisualEffectView.j +++ b/AppKit/CPVisualEffectView.j @@ -161,25 +161,27 @@ CPVisualEffectStateInactive = 2; - (BOOL)setThemeState:(ThemeState)aState { if (aState.isa && [aState isKindOfClass:CPArray]) - aState = CPThemeState.apply(null, aState); + aState = CPThemeState.apply(null, aState); - var ret = [super setThemeState:aState]; + var r = [super setThemeState:aState]; - [self _applyVibrancyState]; + if (r) + [self _applyVibrancyState]; - return ret; + return r; } - (BOOL)unsetThemeState:(ThemeState)aState { if (aState.isa && [aState isKindOfClass:CPArray]) - aState = CPThemeState.apply(null, aState); + aState = CPThemeState.apply(null, aState); - var ret = [super unsetThemeState:aState]; + var r = [super unsetThemeState:aState]; - [self _applyVibrancyState]; + if (r) + [self _applyVibrancyState]; - return ret; + return r; } - (void)viewDidMoveToSuperview diff --git a/Tests/AppKit/CPViewTest.j b/Tests/AppKit/CPViewTest.j index 6e6e0f800..b3f1b80e2 100644 --- a/Tests/AppKit/CPViewTest.j +++ b/Tests/AppKit/CPViewTest.j @@ -774,11 +774,9 @@ var methodCalled; - (void)testAppearanceDefaultvalue { [self assert:nil equals:[view appearance]]; - [self assertFalse:view._effectiveAppearanceCached]; [self assertFalse:[view hasThemeState:CPThemeStateAppearanceVibrantDark]]; [self assertFalse:[view hasThemeState:CPThemeStateAppearanceVibrantLight]]; [self assert:nil equals:[view effectiveAppearance]]; - [self assertTrue:view._effectiveAppearanceCached]; } - (void)testAppearanceWithVibrantDark @@ -816,18 +814,13 @@ var methodCalled; { var secondView = [[CPView alloc] initWithFrame:CGRectMakeZero()]; - [self assertFalse:secondView._effectiveAppearanceCached]; - [view addSubview:secondView]; - [self assertTrue:secondView._effectiveAppearanceCached]; [self assert:nil equals:[secondView appearance]]; [view setAppearance:[CPAppearance appearanceNamed:CPAppearanceNameVibrantLight]]; - [self assertFalse:secondView._effectiveAppearanceCached]; [self assert:[CPAppearance appearanceNamed:CPAppearanceNameVibrantLight] equals:[secondView effectiveAppearance]]; - [self assertTrue:secondView._effectiveAppearanceCached]; } - (void)testEffectiveAppearanceWithMovingViews @@ -851,6 +844,24 @@ var methodCalled; [self assertTrue:[view hasThemeState:CPThemeStateAppearanceVibrantDark]]; } +- (void)testEffectiveAppearanceWithMovingViewHierarchy +{ + var viewC = [[CPView alloc] initWithFrame:CGRectMakeZero()], + viewA = [[CPView alloc] initWithFrame:CGRectMakeZero()], + viewB = [[CPView alloc] initWithFrame:CGRectMakeZero()]; + + [view setAppearance:[CPAppearance appearanceNamed:CPAppearanceNameVibrantLight]]; + [viewC setAppearance:[CPAppearance appearanceNamed:CPAppearanceNameVibrantDark]]; + + [viewA addSubview:viewB]; + [view addSubview:viewA]; + + [self assert:[CPAppearance appearanceNamed:CPAppearanceNameVibrantLight] equals:[viewB effectiveAppearance]]; + + [viewC addSubview:viewA]; + [self assert:[CPAppearance appearanceNamed:CPAppearanceNameVibrantDark] equals:[viewB effectiveAppearance]]; +} + - (void)testEffectiveAppearanceReset { var viewA = [[CPView alloc] initWithFrame:CGRectMakeZero()],