API  0.9.10
CPVisualEffectView.j
Go to the documentation of this file.
1 /*
2  * CPVisualEffectView.j
3  * AppKit
4  *
5  * Created by Antoine Mercadal.
6  * Copyright 2015, 280 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 @typedef CPVisualEffectMaterial
29 
30 @typedef CPVisualEffectBlendingMode
33 
34 @typedef CPVisualEffectState
38 
39 
48 @implementation CPVisualEffectView : CPView
49 {
50  CPImage _maskImage;
51  CPVisualEffectBlendingMode _blendingMode;
52  CPVisualEffectMaterial _material;
53  CPVisualEffectState _state;
54 }
55 
56 
57 #pragma mark -
58 #pragma mark Initialization
59 
60 - (id)initWithFrame:(CGRect)aFrame
61 {
62  if (self = [super initWithFrame:aFrame])
63  {
67  _appearance = [CPAppearance appearanceNamed:CPAppearanceNameVibrantDark];
68  }
69 
70  return self;
71 }
72 
73 
74 #pragma mark -
75 #pragma mark CPVisualEffectView API
76 
83 - (void)setAppearance:(CPAppearance)anAppearance
84 {
85  if (![self _validAppearance:anAppearance])
86  [CPException raise:CPInvalidArgumentException reason:"Appearance can only be CPAppearanceNameVibrantDark or CPAppearanceNameVibrantLight in CPVisualEffectView, but is " + anAppearance];
87 
88  [super setAppearance:anAppearance];
89 
90  [self setNeedsLayout:YES];
91 }
92 
101 - (void)setState:(CPVisualEffectState)aState
102 {
103  if (_state == aState)
104  return;
105 
106  [self willChangeValueForKey:"state"];
107  _state = aState;
108  [self didChangeValueForKey:"state"];
109 
110  [self setNeedsLayout:YES];
111 }
112 
113 
114 #pragma mark -
115 #pragma mark Utilities
116 
117 - (void)_setEffectEnabled:(BOOL)shouldEnable
118 {
119  var dark = [[self appearance] isEqual:[CPAppearance appearanceNamed:CPAppearanceNameVibrantDark]],
120  prop = CPBrowserStyleProperty("backdrop-filter"),
121  color = (dark ? [CPColor colorWithHexString:@"1e1e1e"] : [CPColor whiteColor]),
122  finalColor = shouldEnable ? [color colorWithAlphaComponent:0.6] : color;
123 
124  [self setBackgroundColor:finalColor];
125 
126 #if PLATFORM(DOM)
127  self._DOMElement.style[prop] = shouldEnable ? "blur(30px)" : nil;
128 #endif
129 
130 }
131 
133 {
134  switch (_state)
135  {
137  [self _setEffectEnabled:[self hasThemeState:CPThemeStateKeyWindow]];
138  break;
139 
141  [self _setEffectEnabled:YES];
142  break;
143 
145  [self _setEffectEnabled:NO];
146  break;
147  }
148 }
149 
150 - (BOOL)_validAppearance:(CPAppearance)anAppearance
151 {
152  return [anAppearance isEqual:[CPAppearance appearanceNamed:CPAppearanceNameVibrantDark]] || [anAppearance isEqual:[CPAppearance appearanceNamed:CPAppearanceNameVibrantLight]];
153 }
154 
155 
156 #pragma mark -
157 #pragma mark CPCoding
158 
159 - (id)initWithCoder:(CPCoder)aCoder
160 {
161  if (self = [super initWithCoder:aCoder])
162  {
163  _blendingMode = [aCoder decodeIntForKey:@"_blendingMode"] || CPVisualEffectBlendingModeWithinWindow;
164  _maskImage = [aCoder decodeObjectForKey:@"_maskImage"];
165  _material = [aCoder decodeIntForKey:@"_material"] || CPVisualEffectMaterialAppearanceBased;
166  _state = [aCoder decodeIntForKey:@"_state"] || CPVisualEffectStateFollowsWindowActiveState;
167  }
168 
169  return self;
170 }
171 
172 - (void)encodeWithCoder:(CPCoder)aCoder
173 {
174  [super encodeWithCoder:aCoder];
175 
176  [aCoder encodeObject:_maskImage forKey:@"_maskImage"];
177  [aCoder encodeInt:_blendingMode forKey:@"_blendingMode"];
178  [aCoder encodeInt:_material forKey:@"_material"];
179  [aCoder encodeInt:_state forKey:@"_state"];
180 }
181 
182 
183 @end
184 
186 
191 {
192  return _maskImage;
193 }
194 
198 - (void)setMaskImage:(CPImage)aValue
199 {
200  _maskImage = aValue;
201 }
202 
206 - (CPVisualEffectBlendingMode)blendingMode
207 {
208  return _blendingMode;
209 }
210 
214 - (void)setBlendingMode:(CPVisualEffectBlendingMode)aValue
215 {
216  _blendingMode = aValue;
217 }
218 
222 - (CPVisualEffectMaterial)material
223 {
224  return _material;
225 }
226 
230 - (void)setMaterial:(CPVisualEffectMaterial)aValue
231 {
232  _material = aValue;
233 }
234 
238 - (CPVisualEffectState)state
239 {
240  return _state;
241 }
242 
246 - (void)setState:(CPVisualEffectState)aValue
247 {
248  _state = aValue;
249 }
250 
251 @end
Used to implement exception handling (creating & raising).
Definition: CPException.h:2
void setAppearance:(CPAppearance anAppearance)
Definition: CPView.j:3427
CPColor colorWithHexString:(string hex)
Definition: CPColor.j:263
CPVisualEffectMaterialDark
CPVisualEffectMaterialTitlebar
CPColor whiteColor()
Definition: CPColor.j:360
void raise:reason:(CPString aName, [reason] CPString aReason)
Definition: CPException.j:66
BOOL isEqual:(id anObject)
Definition: CPAppearance.j:126
CPVisualEffectMaterial CPVisualEffectMaterialAppearanceBased
void setNeedsLayout:(BOOL needLayout)
Definition: CPView.j:2712
CPVisualEffectMaterialLight
Definition: CPImage.h:2
CPAppearance appearanceNamed:(CPString aName)
Definition: CPAppearance.j:88
CPVisualEffectBlendingMode CPVisualEffectBlendingModeBehindWindow
void encodeWithCoder:(CPCoder aCoder)
Definition: CPView.j:3770
CPVisualEffectStateInactive
CPVisualEffectBlendingModeWithinWindow
CPVisualEffectState CPVisualEffectStateFollowsWindowActiveState
function CPBrowserStyleProperty(aProperty)
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
void setBackgroundColor:(CPColor aColor)
Definition: CPView.j:1923
CPVisualEffectStateActive
Definition: CPView.j:136