Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(198)

Side by Side Diff: chrome/browser/cocoa/delayedmenu_button.mm

Issue 179043: (Mac) Added unit tests for ClickHoldButtonCell and DelayedMenuButton. (Closed)
Patch Set: Read gtest docs; using constructors instead of SetUp() is okay. Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "chrome/browser/cocoa/delayedmenu_button.h" 5 #import "chrome/browser/cocoa/delayedmenu_button.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/scoped_nsobject.h" 8 #include "base/scoped_nsobject.h"
9 #import "chrome/browser/cocoa/clickhold_button_cell.h" 9 #import "chrome/browser/cocoa/clickhold_button_cell.h"
10 10
11 @interface DelayedMenuButton (Private) 11 @interface DelayedMenuButton (Private)
12 12
13 - (void)resetToDefaults; 13 - (void)setupCell;
14 - (void)menuAction:(id)sender; 14 - (void)menuAction:(id)sender;
15 15
16 @end // @interface DelayedMenuButton (Private) 16 @end // @interface DelayedMenuButton (Private)
17 17
18 @implementation DelayedMenuButton 18 @implementation DelayedMenuButton
19 19
20 // Overrides: 20 // Overrides:
21 21
22 + (Class)cellClass { 22 + (Class)cellClass {
23 return [ClickHoldButtonCell class]; 23 return [ClickHoldButtonCell class];
24 } 24 }
25 25
26 - (id)init { 26 - (id)init {
27 if ((self = [super init])) 27 if ((self = [super init]))
28 [self resetToDefaults]; 28 [self setupCell];
29 return self; 29 return self;
30 } 30 }
31 31
32 - (id)initWithCoder:(NSCoder*)decoder { 32 - (id)initWithCoder:(NSCoder*)decoder {
33 if ((self = [super initWithCoder:decoder])) 33 if ((self = [super initWithCoder:decoder]))
34 [self resetToDefaults]; 34 [self setupCell];
35 return self; 35 return self;
36 } 36 }
37 37
38 - (id)initWithFrame:(NSRect)frameRect { 38 - (id)initWithFrame:(NSRect)frameRect {
39 if ((self = [super initWithFrame:frameRect])) 39 if ((self = [super initWithFrame:frameRect]))
40 [self resetToDefaults]; 40 [self setupCell];
41 return self; 41 return self;
42 } 42 }
43 43
44 - (void)dealloc { 44 - (void)dealloc {
45 [menu_ release]; 45 [menu_ release];
46 [super dealloc]; 46 [super dealloc];
47 } 47 }
48 48
49 - (void)awakeFromNib { 49 - (void)awakeFromNib {
50 [self resetToDefaults]; 50 [self setupCell];
51 }
52
53 - (void)setCell:(NSCell*)cell {
54 [super setCell:cell];
55 [self setupCell];
51 } 56 }
52 57
53 // Accessors and mutators: 58 // Accessors and mutators:
54 59
55 @synthesize menu = menu_; 60 @synthesize menu = menu_;
56 61
57 // Don't synthesize for menuEnabled_; its mutator must do other things. 62 // Don't synthesize for menuEnabled_; its mutator must do other things.
58 - (void)setMenuEnabled:(BOOL)enabled { 63 - (void)setMenuEnabled:(BOOL)enabled {
59 menuEnabled_ = enabled; 64 menuEnabled_ = enabled;
60 [[self cell] setEnableClickHold:menuEnabled_]; 65 [[self cell] setEnableClickHold:menuEnabled_];
61 } 66 }
62 67
63 - (BOOL)menuEnabled { 68 - (BOOL)menuEnabled {
64 return menuEnabled_; 69 return menuEnabled_;
65 } 70 }
66 71
67 @end // @implementation DelayedMenuButton 72 @end // @implementation DelayedMenuButton
68 73
69 @implementation DelayedMenuButton (Private) 74 @implementation DelayedMenuButton (Private)
70 75
71 - (void)resetToDefaults { 76 // Set up the button's cell if we've reached a point where it's been set.
72 id cell = [self cell]; 77 - (void)setupCell {
73 DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]); 78 ClickHoldButtonCell* cell = [self cell];
74 [self setEnabled:NO]; // Make the controller put in a menu and 79 if (cell) {
80 DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]);
81 [self setEnabled:NO]; // Make the controller put in a menu and
75 // enable it explicitly. This also takes 82 // enable it explicitly. This also takes
76 // care of |[cell setEnableClickHold:]|. 83 // care of |[cell setEnableClickHold:]|.
77 [cell setClickHoldTimeout:0.25]; // Random guess at Cocoa-ish value. 84 [cell setClickHoldAction:@selector(menuAction:)];
78 [cell setTrackOnlyInRect:NO]; 85 [cell setClickHoldTarget:self];
79 [cell setActivateOnDrag:YES]; 86 }
80 [cell setClickHoldAction:@selector(menuAction:)];
81 [cell setClickHoldTarget:self];
82 } 87 }
83 88
89 // Display the menu.
84 - (void)menuAction:(id)sender { 90 - (void)menuAction:(id)sender {
85 // We shouldn't get here unless the menu is enabled. 91 // We shouldn't get here unless the menu is enabled.
86 DCHECK(menuEnabled_); 92 DCHECK(menuEnabled_);
87 93
88 // If we don't have a menu (in which case the person using this control is 94 // If we don't have a menu (in which case the person using this control is
89 // being bad), just wait for a mouse up. 95 // being bad), just wait for a mouse up.
90 if (!menu_) { 96 if (!menu_) {
91 LOG(WARNING) << "No menu available."; 97 LOG(WARNING) << "No menu available.";
92 [NSApp nextEventMatchingMask:NSLeftMouseUpMask 98 [NSApp nextEventMatchingMask:NSLeftMouseUpMask
93 untilDate:[NSDate distantFuture] 99 untilDate:[NSDate distantFuture]
94 inMode:NSEventTrackingRunLoopMode 100 inMode:NSEventTrackingRunLoopMode
95 dequeue:YES]; 101 dequeue:YES];
96 return; 102 return;
97 } 103 }
98 104
99 // FIXME(viettrungluu@gmail.com): We have some fudge factors below to make 105 // FIXME(viettrungluu): We have some fudge factors below to make things line
100 // things line up (approximately). I wish I knew how to get rid of them. (Note 106 // up (approximately). I wish I knew how to get rid of them. (Note that our
101 // that our view is flipped, and that frame should be in our coordinates.) 107 // view is flipped, and that frame should be in our coordinates.) The y/height
102 // The y/height is very odd, since it doesn't seem to respond to changes the 108 // is very odd, since it doesn't seem to respond to changes the way that it
103 // way that it should. I don't understand it. 109 // should. I don't understand it.
104 NSRect frame = [self convertRect:[self frame] 110 NSRect frame = [self convertRect:[self frame]
105 fromView:[self superview]]; 111 fromView:[self superview]];
106 frame.origin.x -= 2.0; 112 frame.origin.x -= 2.0;
107 frame.size.height += 10.0; 113 frame.size.height += 10.0;
108 114
109 // Make our pop-up button cell and set things up. This is, as of 10.5, the 115 // Make our pop-up button cell and set things up. This is, as of 10.5, the
110 // official Apple-recommended hack. Later, perhaps |-[NSMenu 116 // official Apple-recommended hack. Later, perhaps |-[NSMenu
111 // popUpMenuPositioningItem:atLocation:inView:]| may be a better option. 117 // popUpMenuPositioningItem:atLocation:inView:]| may be a better option.
112 // However, using a pulldown has the benefit that Cocoa automatically places 118 // However, using a pulldown has the benefit that Cocoa automatically places
113 // the menu correctly even when we're at the edge of the screen (including 119 // the menu correctly even when we're at the edge of the screen (including
114 // "dragging upwards" when the button is close to the bottom of the screen). 120 // "dragging upwards" when the button is close to the bottom of the screen).
115 scoped_nsobject<NSPopUpButtonCell> popUpCell( 121 scoped_nsobject<NSPopUpButtonCell> popUpCell(
116 [[NSPopUpButtonCell alloc] initTextCell:@"" 122 [[NSPopUpButtonCell alloc] initTextCell:@""
117 pullsDown:YES]); 123 pullsDown:YES]);
118 DCHECK(popUpCell.get()); 124 DCHECK(popUpCell.get());
119 [popUpCell setMenu:menu_]; 125 [popUpCell setMenu:menu_];
120 [popUpCell selectItem:nil]; 126 [popUpCell selectItem:nil];
121 [popUpCell attachPopUpWithFrame:frame 127 [popUpCell attachPopUpWithFrame:frame
122 inView:self]; 128 inView:self];
123 [popUpCell performClickWithFrame:frame 129 [popUpCell performClickWithFrame:frame
124 inView:self]; 130 inView:self];
125 } 131 }
126 132
127 @end // @implementation DelayedMenuButton (Private) 133 @end // @implementation DelayedMenuButton (Private)
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/clickhold_button_cell_unittest.mm ('k') | chrome/browser/cocoa/delayedmenu_button_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698