| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/cocoa/menu_button.h" | 5 #import "chrome/browser/ui/cocoa/menu_button.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #import "chrome/browser/ui/cocoa/clickhold_button_cell.h" | 8 #import "chrome/browser/ui/cocoa/clickhold_button_cell.h" |
| 9 #import "ui/base/cocoa/nsview_additions.h" | 9 #import "ui/base/cocoa/nsview_additions.h" |
| 10 | 10 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Accessors and mutators: | 69 // Accessors and mutators: |
| 70 | 70 |
| 71 - (NSMenu*)attachedMenu { | 71 - (NSMenu*)attachedMenu { |
| 72 return attachedMenu_.get(); | 72 return attachedMenu_.get(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 - (void)setAttachedMenu:(NSMenu*)menu { | 75 - (void)setAttachedMenu:(NSMenu*)menu { |
| 76 attachedMenu_.reset([menu retain]); | 76 attachedMenu_.reset([menu retain]); |
| 77 [[self cell] setEnableClickHold:(menu != nil)]; | 77 [self configureCell]; |
| 78 } | 78 } |
| 79 | 79 |
| 80 - (void)setOpenMenuOnClick:(BOOL)enabled { | 80 - (void)setOpenMenuOnClick:(BOOL)enabled { |
| 81 openMenuOnClick_ = enabled; | 81 openMenuOnClick_ = enabled; |
| 82 if (enabled) { | 82 [self configureCell]; |
| 83 [[self cell] setClickHoldTimeout:0.0]; // Make menu trigger immediately. | |
| 84 [[self cell] setAction:@selector(clickShowMenu:)]; | |
| 85 [[self cell] setTarget:self]; | |
| 86 } else { | |
| 87 [[self cell] setClickHoldTimeout:0.25]; // Default value. | |
| 88 } | |
| 89 } | 83 } |
| 90 | 84 |
| 91 - (void)setOpenMenuOnRightClick:(BOOL)enabled { | 85 - (void)setOpenMenuOnRightClick:(BOOL)enabled { |
| 92 openMenuOnRightClick_ = enabled; | 86 openMenuOnRightClick_ = enabled; |
| 93 [[self cell] setEnableRightClick:enabled]; | 87 [self configureCell]; |
| 94 } | 88 } |
| 95 | 89 |
| 96 - (NSRect)menuRect { | 90 - (NSRect)menuRect { |
| 97 return [self bounds]; | 91 return [self bounds]; |
| 98 } | 92 } |
| 99 | 93 |
| 100 @end // @implementation MenuButton | 94 @end // @implementation MenuButton |
| 101 | 95 |
| 102 @implementation MenuButton (Private) | 96 @implementation MenuButton (Private) |
| 103 | 97 |
| 104 // Reset various settings of the button and its associated |ClickHoldButtonCell| | 98 // Synchronize the state of this class with its ClickHoldButtonCell. |
| 105 // to the standard state which provides reasonable defaults. | |
| 106 - (void)configureCell { | 99 - (void)configureCell { |
| 107 ClickHoldButtonCell* cell = [self cell]; | 100 ClickHoldButtonCell* cell = [self cell]; |
| 108 DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]); | 101 DCHECK([cell isKindOfClass:[ClickHoldButtonCell class]]); |
| 109 [cell setClickHoldAction:@selector(dragShowMenu:)]; | 102 |
| 110 [cell setClickHoldTarget:self]; | 103 if (![self attachedMenu]) { |
| 111 [cell setEnableClickHold:([self attachedMenu] != nil)]; | 104 [cell setEnableClickHold:NO]; |
| 105 [cell setEnableRightClick:NO]; |
| 106 [cell setClickHoldAction:nil]; |
| 107 [cell setClickHoldTarget:nil]; |
| 108 [cell setAccessibilityShowMenuAction:nil]; |
| 109 [cell setAccessibilityShowMenuTarget:nil]; |
| 110 return; |
| 111 } |
| 112 |
| 113 if (openMenuOnClick_) { |
| 114 [cell setEnableClickHold:NO]; |
| 115 [cell setClickHoldTimeout:0.0]; // Make menu trigger immediately. |
| 116 [cell setAction:@selector(clickShowMenu:)]; |
| 117 [cell setTarget:self]; |
| 118 [cell setClickHoldAction:nil]; |
| 119 [cell setClickHoldTarget:nil]; |
| 120 } else { |
| 121 [cell setEnableClickHold:YES]; |
| 122 [cell setClickHoldTimeout:0.25]; // Default value. |
| 123 [cell setClickHoldAction:@selector(dragShowMenu:)]; |
| 124 [cell setClickHoldTarget:self]; |
| 125 } |
| 126 |
| 127 [cell setEnableRightClick:openMenuOnRightClick_]; |
| 128 if (!openMenuOnClick_ || openMenuOnRightClick_) { |
| 129 [cell setAccessibilityShowMenuAction:@selector(clickShowMenu:)]; |
| 130 [cell setAccessibilityShowMenuTarget:self]; |
| 131 } else { |
| 132 [cell setAccessibilityShowMenuAction:nil]; |
| 133 [cell setAccessibilityShowMenuTarget:nil]; |
| 134 } |
| 112 } | 135 } |
| 113 | 136 |
| 114 // Actually show the menu (in the correct location). |isDragging| indicates | 137 // Actually show the menu (in the correct location). |isDragging| indicates |
| 115 // whether the mouse button is still down or not. | 138 // whether the mouse button is still down or not. |
| 116 - (void)showMenu:(BOOL)isDragging { | 139 - (void)showMenu:(BOOL)isDragging { |
| 117 if (![self attachedMenu]) { | 140 if (![self attachedMenu]) { |
| 118 LOG(WARNING) << "No menu available."; | 141 LOG(WARNING) << "No menu available."; |
| 119 if (isDragging) { | 142 if (isDragging) { |
| 120 // If we're dragging, wait for mouse up. | 143 // If we're dragging, wait for mouse up. |
| 121 [NSApp nextEventMatchingMask:NSLeftMouseUpMask | 144 [NSApp nextEventMatchingMask:NSLeftMouseUpMask |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 // See http://crbug.com/82456 | 183 // See http://crbug.com/82456 |
| 161 if (![self cr_isMouseInView]) { | 184 if (![self cr_isMouseInView]) { |
| 162 if ([[self cell] respondsToSelector:@selector(mouseExited:)]) | 185 if ([[self cell] respondsToSelector:@selector(mouseExited:)]) |
| 163 [[self cell] mouseExited:nil]; | 186 [[self cell] mouseExited:nil]; |
| 164 } | 187 } |
| 165 } | 188 } |
| 166 | 189 |
| 167 // Called when the button is clicked and released. (Shouldn't happen with | 190 // Called when the button is clicked and released. (Shouldn't happen with |
| 168 // timeout of 0, though there may be some strange pointing devices out there.) | 191 // timeout of 0, though there may be some strange pointing devices out there.) |
| 169 - (void)clickShowMenu:(id)sender { | 192 - (void)clickShowMenu:(id)sender { |
| 170 // This should only be called if openMenuOnClick has been set (which hooks | |
| 171 // up this target-action). | |
| 172 DCHECK(openMenuOnClick_ || openMenuOnRightClick_); | |
| 173 [self showMenu:NO]; | 193 [self showMenu:NO]; |
| 174 } | 194 } |
| 175 | 195 |
| 176 // Called when the button is clicked and dragged/held. | 196 // Called when the button is clicked and dragged/held. |
| 177 - (void)dragShowMenu:(id)sender { | 197 - (void)dragShowMenu:(id)sender { |
| 178 // We shouldn't get here unless the menu is enabled. | 198 // We shouldn't get here unless the menu is enabled. |
| 179 DCHECK([self attachedMenu]); | 199 DCHECK([self attachedMenu]); |
| 180 [self showMenu:YES]; | 200 [self showMenu:YES]; |
| 181 } | 201 } |
| 182 | 202 |
| 183 @end // @implementation MenuButton (Private) | 203 @end // @implementation MenuButton (Private) |
| OLD | NEW |