| 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_cocoa.h" | 5 #import "chrome/browser/ui/cocoa/toolbar/reload_button_cocoa.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
| 11 #include "chrome/browser/command_updater.h" | 11 #include "chrome/browser/command_updater.h" |
| 12 #import "chrome/browser/ui/cocoa/accelerators_cocoa.h" | 12 #import "chrome/browser/ui/cocoa/accelerators_cocoa.h" |
| 13 #import "chrome/browser/ui/cocoa/themed_window.h" |
| 13 #import "chrome/browser/ui/cocoa/view_id_util.h" | 14 #import "chrome/browser/ui/cocoa/view_id_util.h" |
| 14 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 15 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
| 16 #include "ui/base/accelerators/platform_accelerator_cocoa.h" | 17 #include "ui/base/accelerators/platform_accelerator_cocoa.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/l10n/l10n_util_mac.h" | 19 #include "ui/base/l10n/l10n_util_mac.h" |
| 20 #include "ui/base/material_design/material_design_controller.h" |
| 19 #import "ui/events/event_utils.h" | 21 #import "ui/events/event_utils.h" |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 // Constant matches Windows. | 25 // Constant matches Windows. |
| 24 NSTimeInterval kPendingReloadTimeout = 1.35; | 26 NSTimeInterval kPendingReloadTimeout = 1.35; |
| 25 | 27 |
| 26 // Contents of the Reload drop-down menu. | 28 // Contents of the Reload drop-down menu. |
| 27 const int kReloadMenuItems[] = { | 29 const int kReloadMenuItems[] = { |
| 28 IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM, | 30 IDS_RELOAD_MENU_NORMAL_RELOAD_ITEM, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 } | 84 } |
| 83 | 85 |
| 84 - (void)updateTag:(NSInteger)anInt { | 86 - (void)updateTag:(NSInteger)anInt { |
| 85 if ([self tag] == anInt) | 87 if ([self tag] == anInt) |
| 86 return; | 88 return; |
| 87 | 89 |
| 88 // Forcibly remove any stale tooltip which is being displayed. | 90 // Forcibly remove any stale tooltip which is being displayed. |
| 89 [self removeAllToolTips]; | 91 [self removeAllToolTips]; |
| 90 id cell = [self cell]; | 92 id cell = [self cell]; |
| 91 [self setTag:anInt]; | 93 [self setTag:anInt]; |
| 94 |
| 95 // The old way of doing things. |
| 96 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 97 if (anInt == IDC_RELOAD) { |
| 98 [cell setImageID:IDR_RELOAD |
| 99 forButtonState:image_button_cell::kDefaultState]; |
| 100 [cell setImageID:IDR_RELOAD_H |
| 101 forButtonState:image_button_cell::kHoverState]; |
| 102 [cell setImageID:IDR_RELOAD_P |
| 103 forButtonState:image_button_cell::kPressedState]; |
| 104 // The stop button has a disabled image but the reload button doesn't. To |
| 105 // unset it we have to explicilty change the image ID to 0. |
| 106 [cell setImageID:0 |
| 107 forButtonState:image_button_cell::kDisabledState]; |
| 108 [self setToolTip:l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_RELOAD)]; |
| 109 } else if (anInt == IDC_STOP) { |
| 110 [cell setImageID:IDR_STOP |
| 111 forButtonState:image_button_cell::kDefaultState]; |
| 112 [cell setImageID:IDR_STOP_H |
| 113 forButtonState:image_button_cell::kHoverState]; |
| 114 [cell setImageID:IDR_STOP_P |
| 115 forButtonState:image_button_cell::kPressedState]; |
| 116 [cell setImageID:IDR_STOP_D |
| 117 forButtonState:image_button_cell::kDisabledState]; |
| 118 [self setToolTip:l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_STOP)]; |
| 119 } else { |
| 120 NOTREACHED(); |
| 121 } |
| 122 return; |
| 123 } |
| 124 |
| 125 [self resetIcons]; |
| 92 if (anInt == IDC_RELOAD) { | 126 if (anInt == IDC_RELOAD) { |
| 93 [cell setImageID:IDR_RELOAD | |
| 94 forButtonState:image_button_cell::kDefaultState]; | |
| 95 [cell setImageID:IDR_RELOAD_H | |
| 96 forButtonState:image_button_cell::kHoverState]; | |
| 97 [cell setImageID:IDR_RELOAD_P | |
| 98 forButtonState:image_button_cell::kPressedState]; | |
| 99 // The stop button has a disabled image but the reload button doesn't. To | |
| 100 // unset it we have to explicilty change the image ID to 0. | |
| 101 [cell setImageID:0 | |
| 102 forButtonState:image_button_cell::kDisabledState]; | |
| 103 [self setToolTip:l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_RELOAD)]; | 127 [self setToolTip:l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_RELOAD)]; |
| 104 } else if (anInt == IDC_STOP) { | 128 } else if (anInt == IDC_STOP) { |
| 105 [cell setImageID:IDR_STOP | |
| 106 forButtonState:image_button_cell::kDefaultState]; | |
| 107 [cell setImageID:IDR_STOP_H | |
| 108 forButtonState:image_button_cell::kHoverState]; | |
| 109 [cell setImageID:IDR_STOP_P | |
| 110 forButtonState:image_button_cell::kPressedState]; | |
| 111 [cell setImageID:IDR_STOP_D | |
| 112 forButtonState:image_button_cell::kDisabledState]; | |
| 113 [self setToolTip:l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_STOP)]; | 129 [self setToolTip:l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_STOP)]; |
| 114 } else { | 130 } else { |
| 115 NOTREACHED(); | 131 NOTREACHED(); |
| 116 } | 132 } |
| 117 } | 133 } |
| 118 | 134 |
| 119 - (id)accessibilityAttributeValue:(NSString *)attribute { | 135 - (id)accessibilityAttributeValue:(NSString *)attribute { |
| 120 if ([attribute isEqualToString:NSAccessibilityEnabledAttribute] && | 136 if ([attribute isEqualToString:NSAccessibilityEnabledAttribute] && |
| 121 pendingReloadTimer_) { | 137 pendingReloadTimer_) { |
| 122 return [NSNumber numberWithBool:NO]; | 138 return [NSNumber numberWithBool:NO]; |
| 123 } else { | 139 } else { |
| 124 return [super accessibilityAttributeValue:attribute]; | 140 return [super accessibilityAttributeValue:attribute]; |
| 125 } | 141 } |
| 126 } | 142 } |
| 127 | 143 |
| 128 - (void)setIsLoading:(BOOL)isLoading force:(BOOL)force { | 144 - (void)setIsLoading:(BOOL)isLoading force:(BOOL)force { |
| 129 // Can always transition to stop mode. Only transition to reload | 145 // Can always transition to stop mode. Only transition to reload |
| 130 // mode if forced or if the mouse isn't hovering. Otherwise, note | 146 // mode if forced or if the mouse isn't hovering. Otherwise, note |
| 131 // that reload mode is desired and disable the button. | 147 // that reload mode is desired and disable the button. |
| 132 if (isLoading) { | 148 if (isLoading) { |
| 133 [self invalidatePendingReloadTimer]; | 149 [self invalidatePendingReloadTimer]; |
| 134 [self updateTag:IDC_STOP]; | 150 [self updateTag:IDC_STOP]; |
| 135 } else if (force) { | 151 } else if (force) { |
| 136 [self invalidatePendingReloadTimer]; | 152 [self invalidatePendingReloadTimer]; |
| 137 [self updateTag:IDC_RELOAD]; | 153 [self updateTag:IDC_RELOAD]; |
| 138 } else if ([self tag] == IDC_STOP && | 154 } else if ([self tag] == IDC_STOP && |
| 139 !pendingReloadTimer_ && | 155 !pendingReloadTimer_ && |
| 140 [[self cell] isMouseInside]) { | 156 [[self cell] isMouseInside]) { |
| 141 id cell = [self cell]; | 157 id cell = [self cell]; |
| 142 [cell setImageID:IDR_STOP_D | 158 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 143 forButtonState:image_button_cell::kDefaultState]; | 159 [self setImagesFromIconId:gfx::VectorIconId::NAVIGATE_STOP]; |
| 144 [cell setImageID:IDR_STOP_D | 160 NSImage* disabledStopImage = |
| 145 forButtonState:image_button_cell::kDisabledState]; | 161 [[self cell] imageForState:image_button_cell::kDisabledState |
| 146 [cell setImageID:IDR_STOP_D | 162 view:self]; |
| 147 forButtonState:image_button_cell::kHoverState]; | 163 [cell setImage:disabledStopImage |
| 148 [cell setImageID:IDR_STOP_D | 164 forButtonState:image_button_cell::kDefaultState]; |
| 149 forButtonState:image_button_cell::kPressedState]; | 165 [cell setImage:disabledStopImage |
| 166 forButtonState:image_button_cell::kHoverState]; |
| 167 [cell setImage:disabledStopImage |
| 168 forButtonState:image_button_cell::kPressedState]; |
| 169 } else { |
| 170 [cell setImageID:IDR_STOP_D |
| 171 forButtonState:image_button_cell::kDefaultState]; |
| 172 [cell setImageID:IDR_STOP_D |
| 173 forButtonState:image_button_cell::kDisabledState]; |
| 174 [cell setImageID:IDR_STOP_D |
| 175 forButtonState:image_button_cell::kHoverState]; |
| 176 [cell setImageID:IDR_STOP_D |
| 177 forButtonState:image_button_cell::kPressedState]; |
| 178 } |
| 150 pendingReloadTimer_ = | 179 pendingReloadTimer_ = |
| 151 [NSTimer timerWithTimeInterval:kPendingReloadTimeout | 180 [NSTimer timerWithTimeInterval:kPendingReloadTimeout |
| 152 target:self | 181 target:self |
| 153 selector:@selector(forceReloadState:) | 182 selector:@selector(forceReloadState:) |
| 154 userInfo:nil | 183 userInfo:nil |
| 155 repeats:NO]; | 184 repeats:NO]; |
| 156 // Must add the timer to |NSRunLoopCommonModes| because | 185 // Must add the timer to |NSRunLoopCommonModes| because |
| 157 // it should run in |NSEventTrackingRunLoopMode| as well as | 186 // it should run in |NSEventTrackingRunLoopMode| as well as |
| 158 // |NSDefaultRunLoopMode|. | 187 // |NSDefaultRunLoopMode|. |
| 159 [[NSRunLoop currentRunLoop] addTimer:pendingReloadTimer_ | 188 [[NSRunLoop currentRunLoop] addTimer:pendingReloadTimer_ |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 - (void)executeMenuItem:(id)sender { | 279 - (void)executeMenuItem:(id)sender { |
| 251 if (!commandUpdater_) | 280 if (!commandUpdater_) |
| 252 return; | 281 return; |
| 253 DCHECK([sender isKindOfClass:[NSMenuItem class]]); | 282 DCHECK([sender isKindOfClass:[NSMenuItem class]]); |
| 254 int command = [sender tag]; | 283 int command = [sender tag]; |
| 255 int event_flags = ui::EventFlagsFromNative([NSApp currentEvent]); | 284 int event_flags = ui::EventFlagsFromNative([NSApp currentEvent]); |
| 256 commandUpdater_->ExecuteCommandWithDisposition( | 285 commandUpdater_->ExecuteCommandWithDisposition( |
| 257 command, ui::DispositionFromEventFlags(event_flags)); | 286 command, ui::DispositionFromEventFlags(event_flags)); |
| 258 } | 287 } |
| 259 | 288 |
| 289 - (void)resetIcons { |
| 290 if ([self tag] == IDC_RELOAD) { |
| 291 [self setImagesFromIconId:gfx::VectorIconId::NAVIGATE_RELOAD]; |
| 292 } else if ([self tag] == IDC_STOP) { |
| 293 [self setImagesFromIconId:gfx::VectorIconId::NAVIGATE_STOP]; |
| 294 } else { |
| 295 NOTREACHED(); |
| 296 } |
| 297 } |
| 298 |
| 299 - (void)viewDidMoveToWindow { |
| 300 // If a dark theme in Material Design, make sure the icon is the correct |
| 301 // color. |
| 302 if ([self window] && ui::MaterialDesignController::IsModeMaterial() && |
| 303 [[self window] hasDarkTheme]) { |
| 304 [self resetIcons]; |
| 305 } |
| 306 } |
| 307 |
| 260 @end // ReloadButton | 308 @end // ReloadButton |
| 261 | 309 |
| 262 @implementation ReloadButton (Testing) | 310 @implementation ReloadButton (Testing) |
| 263 | 311 |
| 264 + (void)setPendingReloadTimeout:(NSTimeInterval)seconds { | 312 + (void)setPendingReloadTimeout:(NSTimeInterval)seconds { |
| 265 kPendingReloadTimeout = seconds; | 313 kPendingReloadTimeout = seconds; |
| 266 } | 314 } |
| 267 | 315 |
| 268 @end | 316 @end |
| OLD | NEW |