| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/wrench_menu_controller.h" | 5 #import "chrome/browser/cocoa/wrench_menu_controller.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/menus/menu_model.h" | 8 #include "app/menus/menu_model.h" |
| 9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
| 10 #include "chrome/app/chrome_dll_resource.h" | 10 #include "chrome/app/chrome_dll_resource.h" |
| 11 #include "chrome/browser/browser.h" | 11 #include "chrome/browser/browser.h" |
| 12 #include "chrome/browser/browser_window.h" | 12 #include "chrome/browser/browser_window.h" |
| 13 #import "chrome/browser/cocoa/menu_tracked_root_view.h" | 13 #import "chrome/browser/cocoa/menu_tracked_root_view.h" |
| 14 #import "chrome/browser/cocoa/toolbar_controller.h" | 14 #import "chrome/browser/cocoa/toolbar_controller.h" |
| 15 #include "chrome/browser/wrench_menu_model.h" | 15 #include "chrome/browser/wrench_menu_model.h" |
| 16 #include "chrome/common/notification_observer.h" |
| 17 #include "chrome/common/notification_service.h" |
| 18 #include "chrome/common/notification_source.h" |
| 19 #include "chrome/common/notification_type.h" |
| 16 #include "grit/chromium_strings.h" | 20 #include "grit/chromium_strings.h" |
| 17 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 18 | 22 |
| 19 @interface WrenchMenuController (Private) | 23 @interface WrenchMenuController (Private) |
| 20 - (void)adjustPositioning; | 24 - (void)adjustPositioning; |
| 21 - (void)performCommandDispatch:(NSNumber*)tag; | 25 - (void)performCommandDispatch:(NSNumber*)tag; |
| 26 - (NSButton*)zoomDisplay; |
| 22 @end | 27 @end |
| 23 | 28 |
| 29 namespace WrenchMenuControllerInternal { |
| 30 |
| 31 class ZoomLevelObserver : public NotificationObserver { |
| 32 public: |
| 33 explicit ZoomLevelObserver(WrenchMenuController* controller) |
| 34 : controller_(controller) { |
| 35 registrar_.Add(this, NotificationType::ZOOM_LEVEL_CHANGED, |
| 36 NotificationService::AllSources()); |
| 37 } |
| 38 |
| 39 void Observe(NotificationType type, |
| 40 const NotificationSource& source, |
| 41 const NotificationDetails& details) { |
| 42 DCHECK_EQ(type.value, NotificationType::ZOOM_LEVEL_CHANGED); |
| 43 WrenchMenuModel* wrenchMenuModel = [controller_ wrenchMenuModel]; |
| 44 wrenchMenuModel->UpdateZoomControls(); |
| 45 const string16 level = |
| 46 wrenchMenuModel->GetLabelForCommandId(IDC_ZOOM_PERCENT_DISPLAY); |
| 47 [[controller_ zoomDisplay] setTitle:SysUTF16ToNSString(level)]; |
| 48 } |
| 49 |
| 50 private: |
| 51 NotificationRegistrar registrar_; |
| 52 WrenchMenuController* controller_; // Weak; owns this. |
| 53 }; |
| 54 |
| 55 } // namespace WrenchMenuControllerInternal |
| 56 |
| 24 @implementation WrenchMenuController | 57 @implementation WrenchMenuController |
| 25 | 58 |
| 26 - (id)init { | 59 - (id)init { |
| 27 self = [super init]; | 60 if ((self = [super init])) { |
| 61 observer_.reset(new WrenchMenuControllerInternal::ZoomLevelObserver(self)); |
| 62 } |
| 28 return self; | 63 return self; |
| 29 } | 64 } |
| 30 | 65 |
| 31 - (void)addItemToMenu:(NSMenu*)menu | 66 - (void)addItemToMenu:(NSMenu*)menu |
| 32 atIndex:(NSInteger)index | 67 atIndex:(NSInteger)index |
| 33 fromModel:(menus::MenuModel*)model | 68 fromModel:(menus::MenuModel*)model |
| 34 modelIndex:(int)modelIndex { | 69 modelIndex:(int)modelIndex { |
| 35 // Non-button item types should be built as normal items. | 70 // Non-button item types should be built as normal items. |
| 36 menus::MenuModel::ItemType type = model->GetTypeAt(modelIndex); | 71 menus::MenuModel::ItemType type = model->GetTypeAt(modelIndex); |
| 37 if (type != menus::MenuModel::TYPE_BUTTON_ITEM) { | 72 if (type != menus::MenuModel::TYPE_BUTTON_ITEM) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 [NSImage imageNamed:NSImageNameEnterFullScreenTemplate]; | 126 [NSImage imageNamed:NSImageNameEnterFullScreenTemplate]; |
| 92 [zoomFullScreen_ setImage:icon]; | 127 [zoomFullScreen_ setImage:icon]; |
| 93 } | 128 } |
| 94 | 129 |
| 95 // Used to dispatch commands from the Wrench menu. The custom items within the | 130 // Used to dispatch commands from the Wrench menu. The custom items within the |
| 96 // menu cannot be hooked up directly to First Responder because the window in | 131 // menu cannot be hooked up directly to First Responder because the window in |
| 97 // which the controls reside is not the BrowserWindowController, but a | 132 // which the controls reside is not the BrowserWindowController, but a |
| 98 // NSCarbonMenuWindow; this screws up the typical |-commandDispatch:| system. | 133 // NSCarbonMenuWindow; this screws up the typical |-commandDispatch:| system. |
| 99 - (IBAction)dispatchWrenchMenuCommand:(id)sender { | 134 - (IBAction)dispatchWrenchMenuCommand:(id)sender { |
| 100 NSInteger tag = [sender tag]; | 135 NSInteger tag = [sender tag]; |
| 101 // The custom views within the Wrench menu are abnormal and keep the menu open | 136 if (sender == zoomPlus_ || sender == zoomMinus_) { |
| 102 // after a target-action. Close the menu manually. | 137 // Do a direct dispatch rather than scheduling on the outermost run loop, |
| 103 // TODO(rsesek): It'd be great if the zoom buttons didn't have to close the | 138 // which would not get hit until after the menu had closed. |
| 104 // menu. See http://crbug.com/48679 for more info. | 139 [self performCommandDispatch:[NSNumber numberWithInt:tag]]; |
| 105 [menu_ cancelTracking]; | 140 |
| 106 [self dispatchCommandInternal:tag]; | 141 // The zoom buttons should not close the menu if opened sticky. |
| 142 if ([sender respondsToSelector:@selector(isTracking)] && |
| 143 [sender performSelector:@selector(isTracking)]) { |
| 144 [menu_ cancelTracking]; |
| 145 } |
| 146 } else { |
| 147 // The custom views within the Wrench menu are abnormal and keep the menu |
| 148 // open after a target-action. Close the menu manually. |
| 149 [menu_ cancelTracking]; |
| 150 [self dispatchCommandInternal:tag]; |
| 151 } |
| 107 } | 152 } |
| 108 | 153 |
| 109 - (void)dispatchCommandInternal:(NSInteger)tag { | 154 - (void)dispatchCommandInternal:(NSInteger)tag { |
| 110 // Executing certain commands from the nested run loop of the menu can lead | 155 // Executing certain commands from the nested run loop of the menu can lead |
| 111 // to wonky behavior (e.g. http://crbug.com/49716). To avoid this, schedule | 156 // to wonky behavior (e.g. http://crbug.com/49716). To avoid this, schedule |
| 112 // the dispatch on the outermost run loop. | 157 // the dispatch on the outermost run loop. |
| 113 [self performSelector:@selector(performCommandDispatch:) | 158 [self performSelector:@selector(performCommandDispatch:) |
| 114 withObject:[NSNumber numberWithInt:tag] | 159 withObject:[NSNumber numberWithInt:tag] |
| 115 afterDelay:0.0]; | 160 afterDelay:0.0]; |
| 116 } | 161 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 [editItem_ setFrame:itemFrame]; | 235 [editItem_ setFrame:itemFrame]; |
| 191 | 236 |
| 192 // Also resize the superview of the buttons, which is an NSView used to slide | 237 // Also resize the superview of the buttons, which is an NSView used to slide |
| 193 // when the item title is too big and GTM resizes it. | 238 // when the item title is too big and GTM resizes it. |
| 194 NSRect parentFrame = [[editCut_ superview] frame]; | 239 NSRect parentFrame = [[editCut_ superview] frame]; |
| 195 parentFrame.size.width += delta; | 240 parentFrame.size.width += delta; |
| 196 parentFrame.origin.x -= delta; | 241 parentFrame.origin.x -= delta; |
| 197 [[editCut_ superview] setFrame:parentFrame]; | 242 [[editCut_ superview] setFrame:parentFrame]; |
| 198 } | 243 } |
| 199 | 244 |
| 245 - (NSButton*)zoomDisplay { |
| 246 return zoomDisplay_; |
| 247 } |
| 248 |
| 200 @end // @implementation WrenchMenuController | 249 @end // @implementation WrenchMenuController |
| OLD | NEW |