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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/website_settings/split_block_button.h"
#include <cmath>
#include "base/logging.h"
#include "base/mac/scoped_nsobject.h"
#include "chrome/grit/generated_resources.h"
#include "skia/ext/skia_utils_mac.h"
#import "ui/base/cocoa/menu_controller.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/base/models/simple_menu_model.h"
namespace {
enum MouseLocation {
kInsideLeftCell,
kInsideRightCell,
kNotInside,
};
enum CornerType {
kRounded,
kAngled,
};
NSBezierPath* PathWithCornerStyles(NSRect frame,
CornerType leftCornerStyle,
CornerType rightCornerStyle) {
base::scoped_nsobject<NSBezierPath> path([[NSBezierPath bezierPath] retain]);
const CGFloat x0 = NSMinX(frame);
const CGFloat x1 = NSMaxX(frame);
const CGFloat y0 = NSMinY(frame);
const CGFloat y1 = NSMaxY(frame);
const CGFloat radius = 2;
// Start at the center bottom. Draw left and up, including both left corners.
[path moveToPoint:NSMakePoint(std::floor((x1 - x0) * .5), y0)];
if (leftCornerStyle == kAngled) {
[path lineToPoint:NSMakePoint(x0, y0)];
[path lineToPoint:NSMakePoint(x0, y1)];
} else {
[path appendBezierPathWithArcFromPoint:NSMakePoint(x0, y0)
toPoint:NSMakePoint(x0, y0 + radius)
radius:radius];
[path appendBezierPathWithArcFromPoint:NSMakePoint(x0, y1)
toPoint:NSMakePoint(x0 + radius, y1)
radius:radius];
}
// Draw through the upper right-hand and lower-right-hand corners.
if (rightCornerStyle == kAngled) {
[path lineToPoint:NSMakePoint(x1, y1)];
[path lineToPoint:NSMakePoint(x1, y0)];
} else {
[path appendBezierPathWithArcFromPoint:NSMakePoint(x1, y1)
toPoint:NSMakePoint(x1, y1 - radius)
radius:radius];
[path appendBezierPathWithArcFromPoint:NSMakePoint(x1, y0)
toPoint:NSMakePoint(x1 - radius, y0)
radius:radius];
}
return path.autorelease();
}
void DrawBezel(id<ConstrainedWindowButtonDrawableCell>cell,
CornerType leftCorners,
CornerType rightCorners,
NSRect frame,
NSView* view) {
if ([cell isMouseInside]) {
base::scoped_nsobject<NSBezierPath> path(
[PathWithCornerStyles(frame, leftCorners, rightCorners) retain]);
[ConstrainedWindowButton DrawBackgroundAndShadowForPath:path
withCell:cell
inView:view];
[ConstrainedWindowButton DrawInnerHighlightForPath:path
withCell:cell
inView:view];
}
}
} // namespace
// A button cell used by SplitBlockButton, containing the title.
@interface SplitButtonTitleCell : ConstrainedWindowButtonCell
- (NSRect)rect;
@end
// A button cell used by SplitBlockButton, containing the popup menu.
@interface SplitButtonPopUpCell :
NSPopUpButtonCell<ConstrainedWindowButtonDrawableCell> {
@private
BOOL isMouseInside_;
base::scoped_nsobject<MenuController> menuController_;
scoped_ptr<ui::SimpleMenuModel> menuModel_;
}
// Designated initializer.
- (id)initWithMenuDelegate:(ui::SimpleMenuModel::Delegate*)menuDelegate;
- (NSRect)rect;
@end
@implementation SplitBlockButton
- (id)initWithMenuDelegate:(ui::SimpleMenuModel::Delegate*)menuDelegate {
if (self = [super initWithFrame:NSZeroRect]) {
leftCell_.reset([[SplitButtonTitleCell alloc] init]);
rightCell_.reset([[SplitButtonPopUpCell alloc]
initWithMenuDelegate:menuDelegate]);
[leftCell_ setTitle:l10n_util::GetNSString(IDS_PERMISSION_DENY)];
[leftCell_ setEnabled:YES];
[rightCell_ setEnabled:YES];
}
return self;
}
+ (Class)cellClass {
return nil;
}
- (NSString*)title {
return [leftCell_ title];
}
- (void)setAction:(SEL)action {
[leftCell_ setAction:action];
}
- (void)setTarget:(id)target {
[leftCell_ setTarget:target];
}
- (void)drawRect:(NSRect)rect {
// Copy the base class: inset to leave room for the shadow.
--rect.size.height;
// This function assumes that |rect| is always the same as [self frame].
// If that changes, the drawing functions will need to be adjusted.
const CGFloat radius = 2;
NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:rect
xRadius:radius
yRadius:radius];
[ConstrainedWindowButton DrawBackgroundAndShadowForPath:path
withCell:nil
inView:self];
// Use intersection rects for the cell drawing, to ensure the height
// adjustment is honored.
[leftCell_ setControlView:self];
[leftCell_ drawWithFrame:NSIntersectionRect(rect, [self leftCellRect])
inView:self];
[rightCell_ setControlView:self];
[rightCell_ drawWithFrame:NSIntersectionRect(rect, [self rightCellRect])
inView:self];
// Draw the border.
path = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(rect, 0.5, 0.5)
xRadius:radius
yRadius:radius];
[ConstrainedWindowButton DrawBorderForPath:path
withCell:nil
inView:self];
}
- (void)updateTrackingAreas {
[self updateTrackingArea:&leftTrackingArea_
forCell:leftCell_
withRect:[self leftCellRect]];
[self updateTrackingArea:&rightTrackingArea_
forCell:rightCell_
withRect:[self rightCellRect]];
}
- (void)updateTrackingArea:(ui::ScopedCrTrackingArea*)trackingArea
forCell:(id<ConstrainedWindowButtonDrawableCell>)cell
withRect:(NSRect)rect {
DCHECK(trackingArea);
NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited |
NSTrackingActiveInActiveApp;
[self removeTrackingArea:trackingArea->get()];
trackingArea->reset([[CrTrackingArea alloc] initWithRect:rect
options:options
owner:self
userInfo:nil]);
[self addTrackingArea:trackingArea->get()];
}
- (void)mouseEntered:(NSEvent*)theEvent {
[self mouseMoved:theEvent];
}
- (void)mouseExited:(NSEvent*)theEvent {
[self mouseMoved:theEvent];
}
- (void)mouseMoved:(NSEvent*)theEvent {
MouseLocation location = [self mouseLocationForEvent:theEvent];
[rightCell_ setIsMouseInside:NO];
[leftCell_ setIsMouseInside:NO];
if (location == kInsideLeftCell)
[leftCell_ setIsMouseInside:YES];
else if (location == kInsideRightCell)
[rightCell_ setIsMouseInside:YES];
[self setNeedsDisplay:YES];
}
- (void)mouseDown:(NSEvent*)theEvent {
MouseLocation downLocation = [self mouseLocationForEvent:theEvent];
NSCell* focusCell = nil;
NSRect rect;
if (downLocation == kInsideLeftCell) {
focusCell = leftCell_.get();
rect = [self leftCellRect];
} else if (downLocation == kInsideRightCell) {
focusCell = rightCell_.get();
rect = [self rightCellRect];
}
do {
MouseLocation location = [self mouseLocationForEvent:theEvent];
if (location != kNotInside) {
[focusCell setHighlighted:YES];
[self setNeedsDisplay:YES];
if ([focusCell trackMouse:theEvent
inRect:rect
ofView:self
untilMouseUp:NO]) {
[focusCell setState:![focusCell state]];
[self setNeedsDisplay:YES];
break;
} else {
// The above -trackMouse call returned NO, so we know that
// the mouse left the cell before a mouse up event occurred.
[focusCell setHighlighted:NO];
[self setNeedsDisplay:YES];
}
}
const NSUInteger mask = NSLeftMouseUpMask | NSLeftMouseDraggedMask;
theEvent = [[self window] nextEventMatchingMask:mask];
} while ([theEvent type] != NSLeftMouseUp);
}
- (MouseLocation)mouseLocationForEvent:(NSEvent*)theEvent {
MouseLocation location = kNotInside;
NSPoint mousePoint = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
if ([self mouse:mousePoint inRect:[leftCell_ rect]])
location = kInsideLeftCell;
else if ([self mouse:mousePoint inRect:[self rightCellRect]])
location = kInsideRightCell;
return location;
}
- (void)sizeToFit {
NSSize leftSize = [leftCell_ cellSize];
NSSize rightSize = [rightCell_ cellSize];
NSSize size = NSMakeSize(
std::ceil(std::max(leftSize.width + rightSize.width,
constrained_window_button::kButtonMinWidth)),
std::ceil(std::max(leftSize.height, rightSize.height)));
[self setFrameSize:size];
}
- (NSRect)leftCellRect {
return [leftCell_ rect];
}
- (NSRect)rightCellRect {
NSRect leftFrame, rightFrame;
NSDivideRect([self bounds], &leftFrame, &rightFrame,
NSWidth([self leftCellRect]), NSMinXEdge);
return rightFrame;
}
// Accessor for Testing.
- (NSMenu*)menu {
return [rightCell_ menu];
}
@end
@implementation SplitButtonTitleCell
- (void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView {
DrawBezel(self, kRounded, kAngled, frame, controlView);
}
- (NSRect)rect {
NSSize size = [self cellSize];
return NSMakeRect(0, 0, std::ceil(size.width), std::ceil(size.height));
}
@end
@implementation SplitButtonPopUpCell
@synthesize isMouseInside = isMouseInside_;
- (id)initWithMenuDelegate:(ui::SimpleMenuModel::Delegate*)menuDelegate {
if (self = [super initTextCell:@"" pullsDown:YES]) {
[self setControlSize:NSSmallControlSize];
[self setArrowPosition:NSPopUpArrowAtCenter];
[self setBordered:NO];
[self setBackgroundColor:[NSColor clearColor]];
menuModel_.reset(new ui::SimpleMenuModel(menuDelegate));
menuModel_->AddItemWithStringId(0, IDS_PERMISSION_CUSTOMIZE);
menuController_.reset(
[[MenuController alloc] initWithModel:menuModel_.get()
useWithPopUpButtonCell:NO]);
[self setMenu:[menuController_ menu]];
[self setUsesItemFromMenu:NO];
}
return self;
}
- (void)drawBorderAndBackgroundWithFrame:(NSRect)frame
inView:(NSView*)controlView {
// The arrow, which is what should be drawn by the base class, is drawn
// during -drawBezelWithFrame. The only way to draw our own border with
// the default arrow is to make the cell unbordered, and draw the border
// from -drawBorderAndBackgroundWithFrame, rather than simply overriding
// -drawBezelWithFrame.
DrawBezel(self, kAngled, kRounded, frame, controlView);
[super drawBorderAndBackgroundWithFrame:NSOffsetRect(frame, -4, 0)
inView:controlView];
}
- (NSRect)rect {
NSSize size = [self cellSize];
return NSMakeRect(0, 0, std::ceil(size.width), std::ceil(size.height));
}
@end
|