forked from facebookarchive/AsyncDisplayKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASButtonNode.mm
More file actions
222 lines (179 loc) · 5.16 KB
/
ASButtonNode.mm
File metadata and controls
222 lines (179 loc) · 5.16 KB
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
/* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "ASButtonNode.h"
#import <AsyncDisplayKit/ASThread.h>
@interface ASButtonNode ()
{
ASDN::RecursiveMutex _propertyLock;
NSAttributedString *_normalAttributedTitle;
NSAttributedString *_highlightedAttributedTitle;
NSAttributedString *_disabledAttributedTitle;
UIImage *_normalImage;
UIImage *_highlightedImage;
UIImage *_disabledImage;
}
@end
@implementation ASButtonNode
@synthesize contentSpacing = _contentSpacing;
@synthesize laysOutHorizontally = _laysOutHorizontally;
- (instancetype)init
{
if (self = [super init]) {
_contentSpacing = 8.0;
_laysOutHorizontally = YES;
_titleNode = [[ASTextNode alloc] init];
_imageNode = [[ASImageNode alloc] init];
[self addSubnode:_titleNode];
[self addSubnode:_imageNode];
[self addTarget:self action:@selector(controlEventUpdated:) forControlEvents:ASControlNodeEventAllEvents];
}
return self;
}
- (void)controlEventUpdated:(ASControlNode *)node
{
[self updateImage];
[self updateTitle];
}
- (void)updateImage
{
ASDN::MutexLocker l(_propertyLock);
UIImage *newImage;
if (self.enabled == NO && _disabledImage) {
newImage = _disabledImage;
} else if (self.highlighted && _highlightedImage) {
newImage = _highlightedImage;
} else {
newImage = _normalImage;
}
if (newImage != self.imageNode.image) {
self.imageNode.image = newImage;
[self setNeedsLayout];
}
}
- (void)updateTitle
{
ASDN::MutexLocker l(_propertyLock);
NSAttributedString *newTitle;
if (self.enabled == NO && _disabledAttributedTitle) {
newTitle = _disabledAttributedTitle;
} else if (self.highlighted && _highlightedAttributedTitle) {
newTitle = _highlightedAttributedTitle;
} else {
newTitle = _normalAttributedTitle;
}
if (newTitle != self.titleNode.attributedString) {
self.titleNode.attributedString = newTitle;
[self setNeedsLayout];
}
}
- (CGFloat)contentSpacing
{
ASDN::MutexLocker l(_propertyLock);
return _contentSpacing;
}
- (void)setContentSpacing:(CGFloat)contentSpacing
{
ASDN::MutexLocker l(_propertyLock);
if (contentSpacing == _contentSpacing)
return;
_contentSpacing = contentSpacing;
[self setNeedsLayout];
}
- (BOOL)laysOutHorizontally
{
ASDN::MutexLocker l(_propertyLock);
return _laysOutHorizontally;
}
- (void)setLaysOutHorizontally:(BOOL)laysOutHorizontally
{
ASDN::MutexLocker l(_propertyLock);
if (laysOutHorizontally == _laysOutHorizontally)
return;
_laysOutHorizontally = laysOutHorizontally;
[self setNeedsLayout];
}
- (NSAttributedString *)attributedTitleForState:(ASButtonState)state
{
ASDN::MutexLocker l(_propertyLock);
switch (state) {
case ASButtonStateNormal:
return _normalAttributedTitle;
case ASButtonStateHighlighted:
return _highlightedAttributedTitle;
case ASButtonStateDisabled:
return _disabledAttributedTitle;
}
}
- (void)setAttributedTitle:(NSAttributedString *)title forState:(ASButtonState)state
{
ASDN::MutexLocker l(_propertyLock);
switch (state) {
case ASButtonStateNormal:
_normalAttributedTitle = [title copy];
break;
case ASButtonStateHighlighted:
_highlightedAttributedTitle = [title copy];
break;
case ASButtonStateDisabled:
_disabledAttributedTitle = [title copy];
break;
}
[self updateTitle];
}
- (UIImage *)imageForState:(ASButtonState)state
{
ASDN::MutexLocker l(_propertyLock);
switch (state) {
case ASButtonStateNormal:
return _normalImage;
case ASButtonStateHighlighted:
return _highlightedImage;
case ASButtonStateDisabled:
return _disabledImage;
}
}
- (void)setImage:(UIImage *)image forState:(ASButtonState)state
{
ASDN::MutexLocker l(_propertyLock);
switch (state) {
case ASButtonStateNormal:
_normalImage = image;
break;
case ASButtonStateHighlighted:
_highlightedImage = image;
break;
case ASButtonStateDisabled:
_disabledImage = image;
break;
}
[self updateImage];
}
- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize
{
ASStackLayoutSpec *stack = [[ASStackLayoutSpec alloc] init];
stack.direction = self.laysOutHorizontally ? ASStackLayoutDirectionHorizontal : ASStackLayoutDirectionVertical;
stack.spacing = self.contentSpacing;
stack.justifyContent = ASStackLayoutJustifyContentCenter;
stack.alignItems = ASStackLayoutAlignItemsCenter;
NSMutableArray *children = [[NSMutableArray alloc] initWithCapacity:2];
if (self.imageNode.image) {
[children addObject:self.imageNode];
}
if (self.titleNode.attributedString.length > 0) {
[children addObject:self.titleNode];
}
stack.children = children;
return stack;
}
- (void)layout
{
[super layout];
self.imageNode.hidden = self.imageNode.image == nil;
self.titleNode.hidden = self.titleNode.attributedString.length > 0 == NO;
}
@end