forked from KOed/welly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWLEffectView.m
399 lines (330 loc) · 11.6 KB
/
WLEffectView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//
// WLEffectView.m
// Welly
//
// Created by K.O.ed on 08-8-15.
// Copyright 2008 Welly Group. All rights reserved.
//
#import "WLEffectView.h"
#import "WLGlobalConfig.h"
#import <Quartz/Quartz.h>
#import <ScreenSaver/ScreenSaver.h>
#import <CoreText/CTFont.h>
#define OMIT_IMPLIED_ANIM_BEGIN \
[CATransaction begin]; \
[CATransaction setValue:[NSNumber numberWithFloat:0.0f] \
forKey:kCATransactionAnimationDuration]
#define OMIT_IMPLIED_ANIM_END \
[CATransaction commit]
@implementation WLEffectView
- (id)initWithView:(WLTerminalView *)view {
self = [self initWithFrame:[view frame]];
if (self) {
_mainView = [view retain];
[self setWantsLayer:YES];
}
return self;
}
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
[self setFrame:frame];
[self setWantsLayer:YES];
}
return self;
}
- (void)dealloc {
[_mainLayer release];
[_ipAddrLayer release];
if (_popUpLayer) {
[_popUpLayer release];
}
if (_buttonLayer) {
[[[_buttonLayer sublayers] lastObject] removeFromSuperlayer];
[_buttonLayer release];
}
CGColorRelease(_popUpLayerTextColor);
CGFontRelease(_popUpLayerTextFont);
[super dealloc];
}
- (void)setupLayer {
NSRect contentFrame = [_mainView frame];
[self setFrame:contentFrame];
// mainLayer is the layer that gets scaled. All of its sublayers
// are automatically scaled with it.
if (!_mainLayer) {
_mainLayer = [CALayer layer];
// Make the background color to be a dark gray with a 50% alpha similar to
// the real Dashbaord.
CGColorRef bgColor = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 0.0);
[_mainLayer setBackgroundColor:bgColor];
CGColorRelease(bgColor);
[self setLayer:_mainLayer];
}
[_mainLayer setFrame:NSRectToCGRect(contentFrame)];
}
- (void)clear {
[self clearIPAddrBox];
[self clearClickEntry];
[self clearButton];
}
- (void)drawRect:(NSRect)rect {
// Drawing code here.
}
- (void)resize {
[self setFrameSize:[_mainView frame].size];
[self setFrameOrigin:NSMakePoint(0, 0)];
}
- (void)awakeFromNib {
[self setupLayer];
}
- (void)setIPAddrBox {
_ipAddrLayer = [CALayer layer];
// Set up the box
CGColorRef ipAddrLayerBGColor = CGColorCreateGenericRGB(0.0, 0.95, 0.95, 0.1f);
CGColorRef ipAddrLayerBorderColor = CGColorCreateGenericRGB(1.0, 1.0, 1.0, 1.0f);
[_ipAddrLayer setBackgroundColor:ipAddrLayerBGColor];
[_ipAddrLayer setBorderColor:ipAddrLayerBorderColor];
CGColorRelease(ipAddrLayerBGColor);
CGColorRelease(ipAddrLayerBorderColor);
[_ipAddrLayer setBorderWidth:1.4];
[_ipAddrLayer setCornerRadius:6.0];
// Insert the layer into the root layer
[_mainLayer addSublayer:[_ipAddrLayer retain]];
}
- (void)drawIPAddrBox:(NSRect)rect {
if (!_ipAddrLayer)
[self setIPAddrBox];
rect.origin.x -= 1.0;
rect.origin.y -= 0.0;
rect.size.width += 2.0;
rect.size.height += 0.0;
// Set the layer frame to the rect
[_ipAddrLayer setFrame:NSRectToCGRect(rect)];
// Set the opacity to make the layer appear
[_ipAddrLayer setOpacity:1.0f];
}
- (void)clearIPAddrBox {
[_ipAddrLayer setOpacity:0.0f];
}
#pragma mark Click Entry
- (void)setupClickEntry {
_clickEntryLayer = [CALayer layer];
CGColorRef clickEntryLayerBGColor = CGColorCreateGenericRGB(0.0, 0.95, 0.95, 0.17f);
[_clickEntryLayer setBackgroundColor:clickEntryLayerBGColor];
CGColorRelease(clickEntryLayerBGColor);
[_clickEntryLayer setBorderWidth:0];
[_clickEntryLayer setCornerRadius:6.0];
// Insert the layer into the root layer
[_mainLayer addSublayer:[_clickEntryLayer retain]];
}
- (void)drawClickEntry:(NSRect)rect {
if (!_clickEntryLayer)
[self setupClickEntry];
rect.origin.x -= 1.0;
rect.origin.y -= 0.0;
rect.size.width += 2.0;
rect.size.height += 0.0;
// Set the layer frame to the rect
[_clickEntryLayer setFrame:NSRectToCGRect(rect)];
// Set the opacity to make the layer appear
[_clickEntryLayer setOpacity:1.0f];
}
- (void)clearClickEntry {
[_clickEntryLayer setOpacity:0.0f];
}
#pragma mark Welly Buttons
- (void)setupButtonLayer {
if (_buttonLayer)
[_buttonLayer release];
_buttonLayer = [CALayer layer];
// Set the colors of the pop-up layer
CGColorRef myColor = CGColorCreateGenericRGB(0.05, 0.05, 0.05, 0.9f);
[_buttonLayer setBackgroundColor:myColor];
CGColorRelease(myColor);
myColor = CGColorCreateGenericRGB(1.0, 1.0, 1.0, 0.9f);
[_buttonLayer setBorderColor:myColor];
CGColorRelease(myColor);
[_buttonLayer setBorderWidth:2.0];
[_buttonLayer setCornerRadius:10.0];
// Create a text layer to add so we can see the messages.
CATextLayer *textLayer = [CATextLayer layer];
[textLayer autorelease];
// Set its foreground color
myColor = CGColorCreateGenericRGB(1, 1, 1, 1.0f);
[textLayer setForegroundColor:myColor];
CGColorRelease(myColor);
[_buttonLayer addSublayer:[textLayer retain]];
CATransition *buttonTrans = [CATransition new];
[buttonTrans setType:kCATransitionFade];
[_buttonLayer addAnimation:buttonTrans forKey:kCATransition];
[buttonTrans autorelease];
[_buttonLayer setHidden:YES];
// Insert the layer into the root layer
[_mainLayer addSublayer:[_buttonLayer retain]];
}
- (void)drawButton:(NSRect)rect
withMessage:(NSString *)message {
//Initiallize a new CALayer
[self clearButton];
if (!_buttonLayer)
[self setupButtonLayer];
CATextLayer *textLayer = [[_buttonLayer sublayers] lastObject];
// Set the message to the text layer
[textLayer setString:message];
// Modify its styles
[textLayer setTruncationMode:kCATruncationEnd];
CGFontRef font = CGFontCreateWithFontName((CFStringRef)[[WLGlobalConfig sharedInstance] englishFontName]);
[textLayer setFont:font];
[textLayer setFontSize:[[WLGlobalConfig sharedInstance] englishFontSize] - 2];
// Here, calculate the size of the text layer
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont fontWithName:[[WLGlobalConfig sharedInstance] englishFontName]
size:[textLayer fontSize]],
NSFontAttributeName,
nil];
NSSize messageSize = [message sizeWithAttributes:attributes];
// Change the size of text layer automatically
NSRect textRect = NSZeroRect;
textRect.size.width = messageSize.width;
textRect.size.height = messageSize.height;
CGFontRelease(font);
// Create a new rectangle with a suitable size for the inner texts.
// Set it to an appropriate position of the whole view
NSRect finalRect = rect;
if (finalRect.size.width < textRect.size.width + 8)
finalRect.size.width = textRect.size.width + 8;
finalRect.size.height = textRect.size.height + 4;
// Move the origin point of the message layer, so the message can be
// displayed in the center of the background rect
textRect.origin.x += (finalRect.size.width - textRect.size.width) / 2.0;
textRect.origin.y += (finalRect.size.height - textRect.size.height) / 2.0;
// We don't want the implied animation for moving
OMIT_IMPLIED_ANIM_BEGIN;
// Set the layer frame to our rectangle.
[textLayer setFrame:NSRectToCGRect(textRect)];
[_buttonLayer setFrame:NSRectToCGRect(finalRect)];
// Now commit the animation transaction, omit all animations
OMIT_IMPLIED_ANIM_END;
// Now we reveal the button layer at new position
[_buttonLayer setHidden:NO];
}
- (void)clearButton {
if (_buttonLayer == nil)
return;
[_buttonLayer setHidden:YES];
}
#pragma mark -
#pragma mark URL drawing
- (CGImageRef)indicatorImage {
if (_urlIndicatorImage == NULL) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"indicator"
ofType:@"png"];
NSURL *imageURL = [NSURL fileURLWithPath:path];
CGImageSourceRef src = CGImageSourceCreateWithURL((CFURLRef)imageURL, NULL);
if (NULL != src) {
_urlIndicatorImage = CGImageSourceCreateImageAtIndex(src, 0, NULL);
CFRelease(src);
}
}
return _urlIndicatorImage;
}
- (void)setupURLIndicatorLayer {
_urlIndicatorLayer = [CALayer layer];
[_urlIndicatorLayer setContents:(id)[self indicatorImage]];
[_urlIndicatorLayer setFrame:CGRectMake(0, 0, 79, 90)];
[_mainLayer addSublayer:_urlIndicatorLayer];
}
- (void)showIndicatorAtPoint:(NSPoint)point {
if (!_urlIndicatorLayer)
[self setupURLIndicatorLayer];
[_urlIndicatorLayer setOpacity:0.9];
CGRect rect = [_urlIndicatorLayer frame];
rect.origin = NSPointToCGPoint(point);
[_urlIndicatorLayer setFrame:rect];
}
- (void)removeIndicator {
if (_urlIndicatorLayer)
[_urlIndicatorLayer setOpacity:0.0f];
}
#pragma mark Pop-Up Message
- (void)setupPopUpLayer {
NSAssert(_popUpLayer == nil, @"Setup pop-up layer when there exists one already!");
_popUpLayer = [CALayer layer];
// Set the colors of the pop-up layer
CGColorRef popUpLayerBGColor = CGColorCreateGenericRGB(0.1, 0.1, 0.1, 0.5f);
CGColorRef popUpLayerBorderColor = CGColorCreateGenericRGB(1.0, 1.0, 1.0, 0.75f);
[_popUpLayer setBackgroundColor:popUpLayerBGColor];
[_popUpLayer setBorderColor:popUpLayerBorderColor];
CGColorRelease(popUpLayerBGColor);
CGColorRelease(popUpLayerBorderColor);
[_popUpLayer setBorderWidth:2.0];
// Move to proper position before shows up, avoiding moving on screen
NSRect rect = [self frame];
rect.origin.x = rect.size.width / 2;
rect.origin.y = rect.size.height / 5;
rect.size.width = 0;
rect.size.height = 0;
[_popUpLayer setFrame:NSRectToCGRect(rect)];
// Set up text color/font, which would be used many times
_popUpLayerTextColor = CGColorCreateGenericRGB(1.0, 1.0, 1.0, 1.0f);
_popUpLayerTextFont = CGFontCreateWithFontName((CFStringRef)DEFAULT_POPUP_BOX_FONT);
// Create a text layer to add so we can see the message.
CATextLayer *textLayer = [CATextLayer layer];
[textLayer autorelease];
// Set its foreground color
[textLayer setForegroundColor:_popUpLayerTextColor];
// Modify its styles
[textLayer setTruncationMode:kCATruncationEnd];
[textLayer setFont:_popUpLayerTextFont];
[_popUpLayer addSublayer:[textLayer retain]];
// Insert the layer into the root layer
[_mainLayer addSublayer:[_popUpLayer retain]];
}
// Just similiar to the code of "addNewLayer"...
// by gtCarrera @ 9#
- (void)drawPopUpMessage:(NSString *)message {
// Remove previous message
[self removePopUpMessage];
//Initiallize a new CALayer
if (!_popUpLayer) {
[self setupPopUpLayer];
}
CATextLayer *textLayer = [[_popUpLayer sublayers] lastObject];
// Set the message to the text layer
[textLayer setString:message];
// Here, calculate the size of the text layer
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont fontWithName:DEFAULT_POPUP_BOX_FONT
size:textLayer.fontSize],
NSFontAttributeName,
nil];
NSSize messageSize = [message sizeWithAttributes:attributes];
// Change the size of text layer automatically
NSRect textRect = NSZeroRect;
textRect.size.width = messageSize.width;
textRect.size.height = messageSize.height;
// Create a new rectangle with a suitable size for the inner texts.
// Set it to an appropriate position of the whole view
NSRect rect = textRect;
NSRect screenRect = [self frame];
rect.size.height += 10;
rect.size.width += 50;
rect.origin.x = screenRect.size.width / 2 - rect.size.width / 2;
rect.origin.y = screenRect.size.height / 5;
// Move the origin point of the message layer, so the message can be
// displayed in the center of the background layer
textRect.origin.x += (rect.size.width - textRect.size.width) / 2.0;
[textLayer setFrame:NSRectToCGRect(textRect)];
// Set the layer frame to our rectangle.
[_popUpLayer setFrame:NSRectToCGRect(rect)];
[_popUpLayer setCornerRadius:rect.size.height/5];
[_popUpLayer setHidden:NO];
}
- (void)removePopUpMessage {
if(_popUpLayer) {
[_popUpLayer setHidden:YES];
}
}
@end