-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppDelegate.m
111 lines (93 loc) · 4.9 KB
/
AppDelegate.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
//
// AppDelegate.m
// HeightForWidth
//
// Created by Richard Hult on 2009-10-14.
// Copyright 2009 Richard Hult. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "AppDelegate.h"
#import "HeightForWidthLayoutManager.h"
#define MARGIN 20
#define PADDING 10
@implementation AppDelegate
@synthesize window;
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)application
{
return YES;
}
- (NSDictionary *)nullActions
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNull null], @"bounds",
[NSNull null], @"position",
[NSNull null], @"contents",
nil];
}
- (void)awakeFromNib
{
// A black background layer.
CALayer *backgroundLayer = [CALayer layer];
backgroundLayer.backgroundColor = CGColorGetConstantColor(kCGColorWhite);
backgroundLayer.layoutManager = [HeightForWidthLayoutManager layoutManager];
// A blue gradient as background for the text.
CAGradientLayer *textBackgroundLayer = [CAGradientLayer layer];
textBackgroundLayer.colors = [NSArray arrayWithObjects:
NSMakeCollectable(CGColorCreateGenericRGB(0.0, 0.0, 0.3, 1)),
NSMakeCollectable(CGColorCreateGenericRGB(0.1, 0.1, 0.4, 1)),
nil];
textBackgroundLayer.cornerRadius = 6;
textBackgroundLayer.shadowOpacity = 1;
// We don't want animated changes of the bounds, position and contents, as it
// looks weird when the contents of the box doesn't follow the box's bounds as
// it changes size.
textBackgroundLayer.actions = [self nullActions];
// Make the text background follow the width of the background with some margin,
// positioned towards the bottom.
[textBackgroundLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX
relativeTo:@"text"
attribute:kCAConstraintMidX]];
[textBackgroundLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintWidth
relativeTo:@"text"
attribute:kCAConstraintWidth
offset:2 * PADDING]];
[textBackgroundLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidY
relativeTo:@"text"
attribute:kCAConstraintMidY]];
[textBackgroundLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintHeight
relativeTo:@"text"
attribute:kCAConstraintHeight
offset:2 * PADDING]];
[backgroundLayer addSublayer:textBackgroundLayer];
// Add our custom text layer.
CATextLayer *textLayer = [CATextLayer layer];
textLayer.name = @"text";
textLayer.font = @"Lucida Grande";
textLayer.fontSize = 18;
NSString *string = (@"This is a text layer with some text in it, "
@"hopefully long enough to be wrapped if the "
@"window is not too large.");
textLayer.string = string;
textLayer.foregroundColor = CGColorGetConstantColor(kCGColorWhite);
textLayer.wrapped = YES;
// Again, we want no animations.
textLayer.actions = [self nullActions];
// Center the text on the background layer, at the bottom. Make it follow the width
// of the background with some margin.
[textLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidX
relativeTo:@"superlayer"
attribute:kCAConstraintMidX]];
[textLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMinY
relativeTo:@"superlayer"
attribute:kCAConstraintMinY
offset:MARGIN + PADDING]];
[textLayer addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintWidth
relativeTo:@"superlayer"
attribute:kCAConstraintWidth
offset:-2 * (PADDING + MARGIN)]];
[backgroundLayer addSublayer:textLayer];
// Set up the view for layer-hosting.
[[window contentView] setLayer:backgroundLayer];
[[window contentView] setWantsLayer:YES];
}
@end