Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/toolbar/reload_button.h" | 5 #import "chrome/browser/ui/cocoa/toolbar/reload_button.h" |
| 6 | 6 |
| 7 #include "chrome/app/chrome_command_ids.h" | 7 #include "chrome/app/chrome_command_ids.h" |
| 8 #import "chrome/browser/ui/cocoa/accelerators_cocoa.h" | |
| 8 #import "chrome/browser/ui/cocoa/view_id_util.h" | 9 #import "chrome/browser/ui/cocoa/view_id_util.h" |
| 10 #include "chrome/browser/command_updater.h" | |
| 9 #include "chrome/grit/generated_resources.h" | 11 #include "chrome/grit/generated_resources.h" |
| 10 #include "grit/theme_resources.h" | 12 #include "grit/theme_resources.h" |
| 13 #include "ui/base/accelerators/platform_accelerator_cocoa.h" | |
| 11 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 12 #include "ui/base/l10n/l10n_util_mac.h" | 15 #include "ui/base/l10n/l10n_util_mac.h" |
| 16 #import "ui/events/event_utils.h" | |
| 13 | 17 |
| 14 namespace { | 18 namespace { |
| 15 | 19 |
| 16 // Constant matches Windows. | 20 // Constant matches Windows. |
| 17 NSTimeInterval kPendingReloadTimeout = 1.35; | 21 NSTimeInterval kPendingReloadTimeout = 1.35; |
| 18 | 22 |
| 23 // Contents of the Reload drop-down menu. | |
| 24 const int kReloadMenuItems[] = { | |
| 25 IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM, | |
| 26 IDS_RELOAD_MENU_HARD_RELOAD_ITEM, | |
| 27 IDS_RELOAD_MENU_EMPTY_AND_HARD_RELOAD_ITEM, | |
| 28 }; | |
| 29 // Note: must have the same size as |kReloadMenuItems|. | |
| 30 const int kReloadMenuCommands[] = { | |
| 31 IDC_RELOAD, | |
| 32 IDC_RELOAD_IGNORING_CACHE, | |
| 33 IDC_RELOAD_CLEARING_CACHE, | |
| 34 }; | |
| 35 | |
| 19 } // namespace | 36 } // namespace |
| 20 | 37 |
| 21 @interface ReloadButton () | 38 @interface ReloadButton () |
| 22 - (void)invalidatePendingReloadTimer; | 39 - (void)invalidatePendingReloadTimer; |
| 23 - (void)forceReloadState:(NSTimer *)timer; | 40 - (void)forceReloadState:(NSTimer *)timer; |
| 41 - (void)populateMenu; | |
| 24 @end | 42 @end |
| 25 | 43 |
| 26 @implementation ReloadButton | 44 @implementation ReloadButton |
| 27 | 45 |
| 28 + (Class)cellClass { | 46 + (Class)cellClass { |
| 29 return [ImageButtonCell class]; | 47 return [ClickHoldButtonCell class]; |
| 30 } | 48 } |
| 31 | 49 |
| 32 - (id)initWithFrame:(NSRect)frameRect { | 50 - (id)initWithFrame:(NSRect)frameRect { |
| 33 if ((self = [super initWithFrame:frameRect])) { | 51 if ((self = [super initWithFrame:frameRect])) { |
| 34 // Since this is not a custom view, -awakeFromNib won't be called twice. | 52 // Since this is not a custom view, -awakeFromNib won't be called twice. |
| 35 [self awakeFromNib]; | 53 [self awakeFromNib]; |
| 36 } | 54 } |
| 37 return self; | 55 return self; |
| 38 } | 56 } |
| 39 | 57 |
| 40 - (void)viewWillMoveToWindow:(NSWindow *)newWindow { | 58 - (void)viewWillMoveToWindow:(NSWindow *)newWindow { |
| 41 // If this view is moved to a new window, reset its state. | 59 // If this view is moved to a new window, reset its state. |
| 42 [self setIsLoading:NO force:YES]; | 60 [self setIsLoading:NO force:YES]; |
| 43 [super viewWillMoveToWindow:newWindow]; | 61 [super viewWillMoveToWindow:newWindow]; |
| 44 } | 62 } |
| 45 | 63 |
| 46 - (void)awakeFromNib { | 64 - (void)awakeFromNib { |
| 47 // Don't allow multi-clicks, because the user probably wouldn't ever | 65 // Don't allow multi-clicks, because the user probably wouldn't ever |
| 48 // want to stop+reload or reload+stop. | 66 // want to stop+reload or reload+stop. |
| 49 [self setIgnoresMultiClick:YES]; | 67 [self setIgnoresMultiClick:YES]; |
| 68 | |
| 69 [self setOpenMenuOnRightClick:YES]; | |
| 70 [self setOpenMenuOnClick:NO]; | |
| 71 | |
| 72 menu_.reset([[NSMenu alloc] initWithTitle:@""]); | |
| 73 [self populateMenu]; | |
| 50 } | 74 } |
| 51 | 75 |
| 52 - (void)invalidatePendingReloadTimer { | 76 - (void)invalidatePendingReloadTimer { |
| 53 [pendingReloadTimer_ invalidate]; | 77 [pendingReloadTimer_ invalidate]; |
| 54 pendingReloadTimer_ = nil; | 78 pendingReloadTimer_ = nil; |
| 55 } | 79 } |
| 56 | 80 |
| 57 - (void)updateTag:(NSInteger)anInt { | 81 - (void)updateTag:(NSInteger)anInt { |
| 58 if ([self tag] == anInt) | 82 if ([self tag] == anInt) |
| 59 return; | 83 return; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 // |NSDefaultRunLoopMode|. | 155 // |NSDefaultRunLoopMode|. |
| 132 [[NSRunLoop currentRunLoop] addTimer:pendingReloadTimer_ | 156 [[NSRunLoop currentRunLoop] addTimer:pendingReloadTimer_ |
| 133 forMode:NSRunLoopCommonModes]; | 157 forMode:NSRunLoopCommonModes]; |
| 134 } else { | 158 } else { |
| 135 [self invalidatePendingReloadTimer]; | 159 [self invalidatePendingReloadTimer]; |
| 136 [self updateTag:IDC_RELOAD]; | 160 [self updateTag:IDC_RELOAD]; |
| 137 } | 161 } |
| 138 [self setEnabled:pendingReloadTimer_ == nil]; | 162 [self setEnabled:pendingReloadTimer_ == nil]; |
| 139 } | 163 } |
| 140 | 164 |
| 165 - (void)setMenuEnabled:(BOOL)enabled { | |
| 166 [self setAttachedMenu:(enabled ? menu_ : nil)]; | |
| 167 } | |
| 168 | |
| 169 - (void)setCommandUpdater:(CommandUpdater*)commandUpdater { | |
| 170 commandUpdater_ = commandUpdater; | |
| 171 } | |
| 172 | |
| 141 - (void)forceReloadState:(NSTimer *)timer { | 173 - (void)forceReloadState:(NSTimer *)timer { |
| 142 DCHECK_EQ(timer, pendingReloadTimer_); | 174 DCHECK_EQ(timer, pendingReloadTimer_); |
| 143 [self setIsLoading:NO force:YES]; | 175 [self setIsLoading:NO force:YES]; |
| 144 // Verify that |pendingReloadTimer_| is nil so it is not left dangling. | 176 // Verify that |pendingReloadTimer_| is nil so it is not left dangling. |
| 145 DCHECK(!pendingReloadTimer_); | 177 DCHECK(!pendingReloadTimer_); |
| 146 } | 178 } |
| 147 | 179 |
| 148 - (BOOL)sendAction:(SEL)theAction to:(id)theTarget { | 180 - (BOOL)sendAction:(SEL)theAction to:(id)theTarget { |
| 149 if ([self tag] == IDC_STOP) { | 181 if ([self tag] == IDC_STOP) { |
| 150 if (pendingReloadTimer_) { | 182 if (pendingReloadTimer_) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 169 } | 201 } |
| 170 | 202 |
| 171 - (ViewID)viewID { | 203 - (ViewID)viewID { |
| 172 return VIEW_ID_RELOAD_BUTTON; | 204 return VIEW_ID_RELOAD_BUTTON; |
| 173 } | 205 } |
| 174 | 206 |
| 175 - (void)mouseInsideStateDidChange:(BOOL)isInside { | 207 - (void)mouseInsideStateDidChange:(BOOL)isInside { |
| 176 [pendingReloadTimer_ fire]; | 208 [pendingReloadTimer_ fire]; |
| 177 } | 209 } |
| 178 | 210 |
| 211 - (void)populateMenu { | |
| 212 [menu_ setAutoenablesItems:NO]; | |
| 213 // 0-th item must be blank. (This is because we use a pulldown list, for which | |
| 214 // Cocoa uses the 0-th item as "title" in the button.) | |
| 215 [menu_ addItemWithTitle:@"" | |
| 216 action:nil | |
| 217 keyEquivalent:@""]; | |
| 218 AcceleratorsCocoa* keymap = AcceleratorsCocoa::GetInstance(); | |
| 219 for (size_t i = 0; i < arraysize(kReloadMenuItems); ++i) { | |
| 220 base::string16 title16 = l10n_util::GetStringUTF16(kReloadMenuItems[i]); | |
| 221 NSString* title = l10n_util::FixUpWindowsStyleLabel(title16); | |
|
Avi (use Gerrit)
2014/10/14 16:42:57
Replace these two lines with
NSString* title = l1
dgozman
2014/10/15 10:20:56
Done.
| |
| 222 base::scoped_nsobject<NSMenuItem> item( | |
| 223 [[NSMenuItem alloc] initWithTitle:title | |
| 224 action:@selector(executeMenuItem:) | |
| 225 keyEquivalent:@""]); | |
| 226 | |
| 227 const ui::Accelerator* accelerator = | |
| 228 keymap->GetAcceleratorForCommand(kReloadMenuCommands[i]); | |
| 229 if (accelerator) { | |
| 230 const ui::PlatformAcceleratorCocoa* platform = | |
| 231 static_cast<const ui::PlatformAcceleratorCocoa*>( | |
| 232 accelerator->platform_accelerator()); | |
| 233 if (platform) { | |
| 234 [item setKeyEquivalent:platform->characters()]; | |
| 235 [item setKeyEquivalentModifierMask:platform->modifier_mask()]; | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 [item setTag:kReloadMenuCommands[i]]; | |
| 240 [item setTarget:self]; | |
| 241 [item setEnabled:YES]; | |
| 242 | |
| 243 [menu_ addItem:item]; | |
| 244 } | |
| 245 } | |
| 246 | |
| 247 // Action for menu items. | |
| 248 - (void)executeMenuItem:(id)sender { | |
| 249 if (!commandUpdater_) | |
| 250 return; | |
| 251 DCHECK([sender isKindOfClass:[NSMenuItem class]]); | |
| 252 int command = [sender tag]; | |
| 253 int event_flags = ui::EventFlagsFromNative([NSApp currentEvent]); | |
| 254 commandUpdater_->ExecuteCommandWithDisposition( | |
| 255 command, ui::DispositionFromEventFlags(event_flags)); | |
| 256 } | |
| 257 | |
| 179 @end // ReloadButton | 258 @end // ReloadButton |
| 180 | 259 |
| 181 @implementation ReloadButton (Testing) | 260 @implementation ReloadButton (Testing) |
| 182 | 261 |
| 183 + (void)setPendingReloadTimeout:(NSTimeInterval)seconds { | 262 + (void)setPendingReloadTimeout:(NSTimeInterval)seconds { |
| 184 kPendingReloadTimeout = seconds; | 263 kPendingReloadTimeout = seconds; |
| 185 } | 264 } |
| 186 | 265 |
| 187 @end | 266 @end |
| OLD | NEW |