API  0.9.10
CPAppearance.j
Go to the documentation of this file.
1 /*
2  * CPAppearance.j
3  * AppKit
4  *
5  * Created by Antoine Mercadal.
6  * Copyright 2015, Cappuccino Project.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 CPAppearanceNameAqua = @"CPAppearanceNameAqua";
25 CPAppearanceNameLightContent = @"CPAppearanceNameLightContent";
26 CPAppearanceNameVibrantDark = @"CPAppearanceNameVibrantDark";
27 CPAppearanceNameVibrantLight = @"CPAppearanceNameVibrantLight";
28 
29 var _CPAppearanceCurrent = nil,
30  _CPAppearancesRegistry = @{};
31 
32 
33 @protocol CPAppearanceCustomization <CPObject>
34 
35 @required
36 - (CPAppearance)appearance;
37 - (void)setAppearance:(CPAppearance)appearance;
38 - (CPAppearance)effectiveAppearance;
39 - (void)setEffectiveAppearance:(CPAppearance)appearance;
40 
41 @end
42 
43 CPThemeStateAppearanceAqua = CPThemeState("appearance-aqua");
44 CPThemeStateAppearanceLightContent = CPThemeState("appearance-light-content");
45 CPThemeStateAppearanceVibrantLight = CPThemeState("appearance-vibrant-light");
46 CPThemeStateAppearanceVibrantDark = CPThemeState("appearance-vibrant-dark");
47 
48 
56 @implementation CPAppearance : CPObject
57 {
58  BOOL _allowsVibrancy;
59 
60  CPString _name;
61 }
62 
63 
64 #pragma mark -
65 #pragma mark Class Methods
66 
70 {
71  if (!_CPAppearanceCurrent)
72  _CPAppearanceCurrent = [CPAppearance appearanceNamed:CPAppearanceNameAqua];
73 
74  return _CPAppearanceCurrent;
75 }
76 
80 + (void)setCurrentAppearance:(CPAppearance)anAppearance
81 {
82  _CPAppearanceCurrent = anAppearance;
83 }
84 
88 + (CPAppearance)appearanceNamed:(CPString)aName
89 {
90  if (![_CPAppearancesRegistry containsKey:aName])
91  {
92  [_CPAppearancesRegistry setObject:[[CPAppearance alloc] initWithAppearanceNamed:aName bundle:nil]
93  forKey:aName];
94  }
95 
96  return [_CPAppearancesRegistry objectForKey:aName];
97 }
98 
99 
100 #pragma mark -
101 #pragma mark Initialization
102 
106 - (id)initWithAppearanceNamed:(CPString)aName bundle:(CPBundle)bundle
107 {
108  if (self = [super init])
109  {
110  _name = aName;
111  _allowsVibrancy = YES;
112 
113  if ([_CPAppearancesRegistry containsKey:aName])
114  [CPException raise:CPInternalInconsistencyException reason:"Appearance with name '" + aName + "' is already declared."];
115 
116  [_CPAppearancesRegistry setObject:self forKey:aName];
117  }
118 
119  return self;
120 }
121 
122 
123 #pragma mark -
124 #pragma mark Implementation
125 
126 - (BOOL)isEqual:(id)anObject
127 {
128  if (![anObject isKindOfClass:CPAppearance])
129  return NO;
130 
131  return self._name == anObject._name;
132 }
133 
135 {
136  return @"<CPAppearance @" + [self UID] + @" name: " + _name + ">";
137 }
138 
139 
140 #pragma mark -
141 #pragma mark CPCoding
142 
143 - (id)initWithCoder:(CPCoder)aCoder
144 {
145  if (self = [super init])
146  {
147  _name = [aCoder decodeObjectForKey:@"_name"];
148  _allowsVibrancy = [aCoder decodeBoolForKey:@"_allowsVibrancy"];
149  }
150 
151  return self;
152 }
153 
154 - (void)encodeWithCoder:(CPCoder)aCoder
155 {
156  [aCoder encodeObject:_name forKey:@"_name"];
157  [aCoder encodeBool:_allowsVibrancy forKey:@"_allowsVibrancy"];
158 }
159 
160 @end
161 
163 
168 {
169  return _allowsVibrancy;
170 }
171 
175 - (void)setAllowsVibrancy:(BOOL)aValue
176 {
177  _allowsVibrancy = aValue;
178 }
179 
180 @end
Used to implement exception handling (creating & raising).
Definition: CPException.h:2
var isEqual
CPThemeStateAppearanceVibrantDark
Definition: CPAppearance.j:46
void raise:reason:(CPString aName, [reason] CPString aReason)
Definition: CPException.j:66
id initWithAppearanceNamed:bundle:(CPString aName, [bundle] CPBundle bundle)
Definition: CPAppearance.j:106
An immutable string (collection of characters).
Definition: CPString.h:2
CPAppearance appearanceNamed:(CPString aName)
Definition: CPAppearance.j:88
CPThemeStateAppearanceVibrantLight
Definition: CPAppearance.j:45
CPAppearanceNameAqua
Definition: CPAppearance.j:24
CPThemeStateAppearanceLightContent
Definition: CPAppearance.j:44
CPThemeStateAppearanceAqua
Definition: CPAppearance.j:43
CPAppearanceNameLightContent
Definition: CPAppearance.j:25
CPAppearanceNameVibrantDark
Definition: CPAppearance.j:26
CPString description()
Definition: CPAppearance.j:134
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
id init()
Definition: CPObject.j:145
CPAppearance currentAppearance()
Definition: CPAppearance.j:69
CPAppearanceNameVibrantLight
Definition: CPAppearance.j:27
CPString UID()
Definition: CPObject.j:552
id alloc()
Definition: CPObject.j:130