Repository navigation
Expand file tree
/
Copy pathWDMenu.m
More file actions
217 lines (174 loc) · 6.39 KB
/
Copy pathWDMenu.m
File metadata and controls
217 lines (174 loc) · 6.39 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
//
// WDMenu.m
// Inkpad
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2010-2013 Steve Sprang
//
#import "WDMenu.h"
#import "WDUtilities.h"
#import "WDMenuItem.h"
#define kMenuHeight 40
#define kSeparatorHeight 5
#define kSeparatorInset 4
#define kInset 20
#define kFontSize 17
#define kImageBuffer 15
#define kMinimumMenuWidth 160
@interface WDMenu (Private)
- (void) createLabels_;
@end
@implementation WDMenu
@synthesize indexOfSelectedItem = selectedIndex_;
@synthesize items = items_;
@synthesize visible = visible_;
@synthesize origin = origin_;
@synthesize popover = popover_;
@synthesize delegate = delegate_;
- (id) initWithItems:(NSArray *)items
{
float maxWidth = 0;
float height = 0;
for (WDMenuItem *item in items) {
int imageWidth = [item imageWidth] ? [item imageWidth] + kImageBuffer : 0;
NSDictionary *attrs = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:kFontSize]};
maxWidth = MAX(maxWidth, [item.title sizeWithAttributes:attrs].width + imageWidth);
height += (item.separator ? kSeparatorHeight : kMenuHeight);
}
maxWidth = MAX(kMinimumMenuWidth, maxWidth);
CGRect frame = CGRectMake(0, 0, floor(maxWidth + kInset * 2), height);
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
selectedIndex_ = -1;
self.opaque = NO;
self.backgroundColor = nil;
self.items = [items mutableCopy];
return self;
}
- (void) setItems:(NSMutableArray *)array
{
items_ = array;
[self createLabels_];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
if (selectedIndex_ >= 0) {
[[UIColor colorWithRed:(193.0f / 255) green:(220.0f / 255) blue:1.0f alpha:1.0f] set];
CGContextFillRect(ctx, [rects_[selectedIndex_] CGRectValue]);
}
}
- (void) setIndexOfSelectedItem:(int)index
{
selectedIndex_ = index;
[self setNeedsDisplay];
}
- (void) handlePoint:(CGPoint)pt
{
int currentIndex = selectedIndex_;
// stop tracking if the touch leaves the view entirely
if (!CGRectContainsPoint(self.bounds, pt)) {
selectedIndex_ = -1;
} else {
int ix = 0;
for (NSValue *rect in rects_) {
if (CGRectContainsPoint([rect CGRectValue], pt)) {
WDMenuItem *item = items_[ix];
selectedIndex_ = (item.separator || !item.enabled) ? -1 : [rects_ indexOfObject:rect];
break;
}
ix++;
}
}
if (selectedIndex_ != currentIndex) {
[self setNeedsDisplay];
}
}
- (BOOL) beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint pt = [touch locationInView:self];
[self handlePoint:pt];
return [super beginTrackingWithTouch:touch withEvent:event];
}
- (BOOL) continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint pt = [touch locationInView:self];
[self handlePoint:pt];
return [super continueTrackingWithTouch:touch withEvent:event];
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint pt = [touch locationInView:self];
[self handlePoint:pt];
if (selectedIndex_ >= 0) {
WDMenuItem *item = items_[selectedIndex_];
if (!item.separator && item.enabled) {
[[UIApplication sharedApplication] sendAction:item.action to:item.target from:item forEvent:event];
}
}
[self dismiss];
[super endTrackingWithTouch:touch withEvent:event];
}
- (void) dismiss
{
[self setIndexOfSelectedItem:(-1)];
[popover_ dismissPopoverAnimated:YES];
[delegate_ popoverControllerDidDismissPopover:popover_];
}
@end
@implementation WDMenu (Private)
- (void) createLabels_
{
rects_ = [[NSMutableArray alloc] init];
CGRect frame = self.bounds;
frame.size.height = kMenuHeight;
for (WDMenuItem *item in items_) {
frame.size.height = (item.separator ? kSeparatorHeight : kMenuHeight);
frame = CGRectIntegral(frame);
if (!item.separator) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectOffset(frame, kInset, 0.0f)];
label.opaque = NO;
label.backgroundColor = nil;
label.textColor = [UIColor colorWithRed:0.0f green:(118.0f / 255.0f) blue:1.0f alpha:1.0f];
label.textAlignment = NSTextAlignmentLeft;
label.font = [UIFont systemFontOfSize:kFontSize];
label.text = item.title;
[self addSubview:label];
item.label = label;
} else {
float scale = [UIScreen mainScreen].scale;
CGRect lineRect = frame;
lineRect.origin.x = 0.0f;
lineRect.origin.y += lineRect.size.height / 2;
lineRect = CGRectIntegral(lineRect);
lineRect = CGRectInset(lineRect, kSeparatorInset, 0.0f);
lineRect.size.height = 1.0f / scale;
UIView *line = [[UIView alloc] initWithFrame:lineRect];
line.opaque = NO;
line.layer.borderColor = [UIColor colorWithWhite:0.25f alpha:0.25f].CGColor;
line.layer.borderWidth = 1.0f / scale;
[self addSubview:line];
}
CGRect highlightRect = frame;
[rects_ addObject:[NSValue valueWithCGRect:highlightRect]];
frame.origin.y += frame.size.height;
if (item.image) {
UIImage *templateImage = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithImage:templateImage];
CGRect imageFrame = imageView.frame;
imageFrame.origin.x = CGRectGetMaxX(highlightRect) - CGRectGetWidth(imageFrame) - kImageBuffer;
imageFrame.origin.y = CGRectGetMidY(highlightRect) - CGRectGetHeight(imageFrame) / 2;
imageFrame.origin = WDRoundPoint(imageFrame.origin);
imageView.frame = imageFrame;
[self addSubview:imageView];
item.imageView = imageView;
imageView.tintColor = [UIColor colorWithRed:0.0f green:(118.0f / 255.0f) blue:1.0f alpha:1.0f];
}
}
}
@end