Skip to content

Commit

Permalink
Merge pull request #2374 from Dogild/ThemeForObject
Browse files Browse the repository at this point in the history
New: possibility to create a theme for every object of the Appkit
  • Loading branch information
Dogild committed Oct 2, 2015
2 parents d5b90c9 + 860decd commit 1fe1e6f
Show file tree
Hide file tree
Showing 8 changed files with 523 additions and 299 deletions.
1 change: 1 addition & 0 deletions AppKit/AppKit.j
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

@import "_CPObject+Theme.j"
@import "_CPToolTip.j"
@import "CALayer.j"
@import "CGGeometry.j"
Expand Down
61 changes: 55 additions & 6 deletions AppKit/CPColor.j
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

@import "CGColor.j"

@import "_CPObject+Theme.j"
@import "CPCompatibility.j"
@import "CPImage.j"

Expand Down Expand Up @@ -65,7 +66,8 @@ var cachedBlackColor,
cachedOrangeColor,
cachedPurpleColor,
cachedShadowColor,
cachedClearColor;
cachedClearColor,
cachedThemeColor;

/// @endcond

Expand All @@ -78,14 +80,35 @@ var cachedBlackColor,
<p>It also provides some class helper methods that
returns instances of commonly used colors.</p>
*/
@implementation CPColor : CPObject
@implementation CPColor : CPObject <CPTheme>
{
CPArray _components;

CPImage _patternImage;
CPString _cssString;
}


#pragma mark -
#pragma mark Theming

+ (CPString)defaultThemeClass
{
return "color";
}

+ (CPDictionary)themeAttributes
{
return @{
@"alternate-selected-control-color": [CPNull null],
@"secondary-selected-control-color" : [CPNull null]
};
}


#pragma mark -
#pragma mark Static methods

/*!
Creates a color in the RGB colorspace, with an alpha value.
Each component should be between the range of 0.0 to 1.0. For
Expand Down Expand Up @@ -436,14 +459,22 @@ var cachedBlackColor,
return cachedClearColor;
}

+ (CPColor)_cachedThemeColor
{
if (!cachedThemeColor)
cachedThemeColor = [self colorWithCalibratedWhite:0.0 alpha:0.0];

return cachedThemeColor;
}

+ (CPColor)alternateSelectedControlColor
{
return [[CPColor alloc] _initWithRGBA:[0.22, 0.46, 0.84, 1.0]];
return [[self _cachedThemeColor] valueForThemeAttribute:@"alternate-selected-control-color"];
}

+ (CPColor)secondarySelectedControlColor
{
return [[CPColor alloc] _initWithRGBA:[0.83, 0.83, 0.83, 1.0]];
return [[self _cachedThemeColor] valueForThemeAttribute:@"secondary-selected-control-color"];
}

/*!
Expand Down Expand Up @@ -489,6 +520,10 @@ var cachedBlackColor,
// use it (issue #1413.)
[self _initCSSStringFromComponents];

_theme = [CPTheme defaultTheme];
_themeState = CPThemeStateNormal;
[self _loadThemeAttributes];

return self;
}

Expand All @@ -502,6 +537,10 @@ var cachedBlackColor,
_components = components;

[self _initCSSStringFromComponents];

_theme = [CPTheme defaultTheme];
_themeState = CPThemeStateNormal;
[self _loadThemeAttributes];
}

return self;
Expand All @@ -528,6 +567,10 @@ var cachedBlackColor,
_patternImage = anImage;
_cssString = "url(\"" + [_patternImage filename] + "\")";
_components = [0.0, 0.0, 0.0, 1.0];

_theme = [CPTheme defaultTheme];
_themeState = CPThemeStateNormal;
[self _loadThemeAttributes];
}

return self;
Expand Down Expand Up @@ -835,9 +878,13 @@ var CPColorComponentsKey = @"CPColorComponentsKey",
- (id)initWithCoder:(CPCoder)aCoder
{
if ([aCoder containsValueForKey:CPColorPatternImageKey])
return [self _initWithPatternImage:[aCoder decodeObjectForKey:CPColorPatternImageKey]];
self = [self _initWithPatternImage:[aCoder decodeObjectForKey:CPColorPatternImageKey]];
else
self = [self _initWithRGBA:[aCoder decodeObjectForKey:CPColorComponentsKey]];

[self _decodeThemeObjectsWithCoder:aCoder];

return [self _initWithRGBA:[aCoder decodeObjectForKey:CPColorComponentsKey]];
return self;
}

/*!
Expand All @@ -850,6 +897,8 @@ var CPColorComponentsKey = @"CPColorComponentsKey",
[aCoder encodeObject:_patternImage forKey:CPColorPatternImageKey];
else
[aCoder encodeObject:_components forKey:CPColorComponentsKey];

[self _encodeThemeObjectsWithCoder:aCoder];
}

@end
Expand Down
21 changes: 10 additions & 11 deletions AppKit/CPTheme.j
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,19 @@ var CPThemesByName = { },

if (!className)
{
if ([aClass isKindOfClass:[CPView class]])
if ([aClass respondsToSelector:@selector(defaultThemeClass)])
{
if ([aClass respondsToSelector:@selector(defaultThemeClass)])
className = [aClass defaultThemeClass];
else if ([aClass respondsToSelector:@selector(themeClass)])
{
CPLog.warn(@"%@ themeClass is deprecated in favor of defaultThemeClass", CPStringFromClass(aClass));
className = [aClass themeClass];
}
else
return nil;
className = [aClass defaultThemeClass];
}
else if ([aClass respondsToSelector:@selector(themeClass)])
{
CPLog.warn(@"%@ themeClass is deprecated in favor of defaultThemeClass", CPStringFromClass(aClass));
className = [aClass themeClass];
}
else
[CPException raise:CPInvalidArgumentException reason:@"aClass must be a class object or a string."];
{
return nil;
}
}

return [_attributes objectForKey:className];
Expand Down
Loading

0 comments on commit 1fe1e6f

Please sign in to comment.