Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "chrome/browser/ui/cocoa/browser_window_command_handler.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #import "base/mac/foundation_util.h" | |
| 9 #include "chrome/app/chrome_command_ids.h" | |
| 10 #import "chrome/browser/app_controller_mac.h" | |
| 11 #include "chrome/browser/fullscreen.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/ui/bookmarks/bookmark_utils.h" | |
| 14 #include "chrome/browser/ui/browser_commands.h" | |
| 15 #include "chrome/browser/ui/browser_finder.h" | |
| 16 #include "chrome/browser/ui/browser_window.h" | |
| 17 #import "chrome/browser/ui/cocoa/browser_window_controller_private.h" | |
| 18 #include "chrome/browser/ui/toolbar/encoding_menu_controller.h" | |
| 19 #include "chrome/grit/generated_resources.h" | |
| 20 #include "content/public/browser/web_contents.h" | |
| 21 #import "ui/base/cocoa/cocoa_base_utils.h" | |
| 22 #include "ui/base/l10n/l10n_util.h" | |
| 23 #include "ui/base/l10n/l10n_util_mac.h" | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // Update a toggle state for an item if modified. The item may be an NSMenuItem | |
| 28 // or NSButton. Called by -validateUserInterfaceItem:. | |
| 29 void UpdateToggleStateWithTag(NSInteger tag, id item, NSWindow* window) { | |
| 30 if (![item respondsToSelector:@selector(state)] || | |
| 31 ![item respondsToSelector:@selector(setState:)]) | |
| 32 return; | |
| 33 | |
| 34 Browser* browser = chrome::FindBrowserWithWindow(window); | |
| 35 DCHECK(browser); | |
| 36 | |
| 37 // On Windows this logic happens in bookmark_bar_view.cc. This simply updates | |
| 38 // the menu item; it does not display the bookmark bar itself. | |
| 39 if (tag == IDC_SHOW_BOOKMARK_BAR) { | |
| 40 bool toggled = browser->window()->IsBookmarkBarVisible(); | |
| 41 NSInteger oldState = [item state]; | |
| 42 NSInteger newState = toggled ? NSOnState : NSOffState; | |
| 43 if (oldState != newState) | |
| 44 [item setState:newState]; | |
| 45 return; | |
| 46 } | |
| 47 | |
| 48 // Update the checked/unchecked state of items in the encoding menu. | |
| 49 // On Windows, this logic is part of |EncodingMenuModel| in | |
| 50 // browser/ui/views/toolbar_view.h. | |
| 51 EncodingMenuController encoding_controller; | |
| 52 if (!encoding_controller.DoesCommandBelongToEncodingMenu(tag)) | |
| 53 return; | |
| 54 | |
| 55 Profile* profile = browser->profile(); | |
| 56 DCHECK(profile); | |
| 57 content::WebContents* current_tab = | |
| 58 browser->tab_strip_model()->GetActiveWebContents(); | |
| 59 if (!current_tab) | |
| 60 return; | |
| 61 | |
| 62 const std::string encoding = current_tab->GetEncoding(); | |
| 63 | |
| 64 bool toggled = encoding_controller.IsItemChecked(profile, encoding, tag); | |
| 65 NSInteger oldState = [item state]; | |
| 66 NSInteger newState = toggled ? NSOnState : NSOffState; | |
| 67 if (oldState != newState) | |
| 68 [item setState:newState]; | |
| 69 } | |
| 70 | |
| 71 // Get the text for the "Enter/Exit Fullscreen/Presentation Mode" menu item. | |
|
tapted
2015/09/04 02:56:24
I think this still needs a TODO, to remove the dep
jackhou1
2015/09/04 06:13:58
Done.
| |
| 72 NSString* GetTitleForFullscreenMenuItem(Browser* browser) { | |
| 73 NSWindow* ns_window = browser->window()->GetNativeWindow(); | |
| 74 if (BrowserWindowController* controller = [ns_window windowController]) { | |
| 75 if (!chrome::mac::SupportsSystemFullscreen()) { | |
| 76 return l10n_util::GetNSString([controller inPresentationMode] | |
| 77 ? IDS_EXIT_PRESENTATION_MAC | |
| 78 : IDS_ENTER_PRESENTATION_MAC); | |
| 79 } | |
| 80 | |
| 81 return l10n_util::GetNSString([controller isInAppKitFullscreen] | |
| 82 ? IDS_EXIT_FULLSCREEN_MAC | |
| 83 : IDS_ENTER_FULLSCREEN_MAC); | |
| 84 } | |
| 85 | |
| 86 return l10n_util::GetNSString(browser->window()->IsFullscreen() | |
| 87 ? IDS_EXIT_FULLSCREEN_MAC | |
| 88 : IDS_ENTER_FULLSCREEN_MAC); | |
| 89 } | |
| 90 | |
| 91 // Identify the actual Browser to which the command should be dispatched. It | |
| 92 // might belong to a background window, yet another dispatcher gets it because | |
| 93 // it is the foreground window's dispatcher and thus in the responder chain. | |
| 94 // Some senders don't have this problem (for example, menus only operate on the | |
| 95 // foreground window), so this is only an issue for senders that are part of | |
| 96 // windows. | |
| 97 Browser* FindBrowserForSender(id sender, NSWindow* window) { | |
| 98 NSWindow* targetWindow = window; | |
| 99 if ([sender respondsToSelector:@selector(window)]) | |
| 100 targetWindow = [sender window]; | |
| 101 Browser* browser = chrome::FindBrowserWithWindow(targetWindow); | |
| 102 DCHECK(browser); | |
| 103 return browser; | |
| 104 } | |
| 105 | |
| 106 } // namespace | |
| 107 | |
| 108 @implementation BrowserWindowCommandHandler | |
| 109 | |
| 110 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item | |
| 111 window:(NSWindow*)window { | |
| 112 if ([item action] != @selector(commandDispatch:) && | |
| 113 [item action] != @selector(commandDispatchUsingKeyModifiers:)) { | |
| 114 // Since the NSWindow implements these selectors, it needs to check if they | |
| 115 // should be handled by AppController. | |
|
tapted
2015/09/04 02:56:24
From the comment, It's not too clear without conte
jackhou1
2015/09/04 06:13:58
Updated comment.
| |
| 116 NOTREACHED(); | |
| 117 // By default, interface items are enabled if the object in the responder | |
| 118 // chain that implements the action does not implement | |
| 119 // -validateUserInterfaceItem. Since we only care about -commandDispatch, | |
| 120 // return YES for all other actions. | |
| 121 return YES; | |
| 122 } | |
| 123 | |
| 124 Browser* browser = chrome::FindBrowserWithWindow(window); | |
| 125 DCHECK(browser); | |
| 126 NSInteger tag = [item tag]; | |
| 127 if (!chrome::SupportsCommand(browser, tag)) | |
| 128 return NO; | |
| 129 | |
| 130 // Generate return value (enabled state). | |
| 131 BOOL enable = chrome::IsCommandEnabled(browser, tag); | |
| 132 switch (tag) { | |
| 133 case IDC_CLOSE_TAB: | |
| 134 // Disable "close tab" if the receiving window is not tabbed. | |
| 135 // We simply check whether the item has a keyboard shortcut set here; | |
| 136 // app_controller_mac.mm actually determines whether the item should | |
| 137 // be enabled. | |
| 138 if (NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item)) | |
| 139 enable &= !![[menuItem keyEquivalent] length]; | |
| 140 break; | |
| 141 case IDC_FULLSCREEN: { | |
| 142 if (NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item)) { | |
| 143 if (chrome::mac::SupportsSystemFullscreen()) | |
| 144 [menuItem setTitle:GetTitleForFullscreenMenuItem(browser)]; | |
| 145 else | |
| 146 [menuItem setHidden:YES]; | |
| 147 } | |
| 148 break; | |
| 149 } | |
| 150 case IDC_PRESENTATION_MODE: { | |
| 151 if (NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item)) { | |
| 152 [menuItem setTitle:GetTitleForFullscreenMenuItem(browser)]; | |
| 153 | |
| 154 if (chrome::mac::SupportsSystemFullscreen()) | |
| 155 [menuItem setAlternate:YES]; | |
| 156 } | |
| 157 break; | |
| 158 } | |
| 159 case IDC_SHOW_SIGNIN: { | |
| 160 Profile* original_profile = browser->profile()->GetOriginalProfile(); | |
| 161 [AppController updateSigninItem:item | |
| 162 shouldShow:enable | |
| 163 currentProfile:original_profile]; | |
| 164 break; | |
| 165 } | |
| 166 case IDC_BOOKMARK_PAGE: { | |
| 167 // Extensions have the ability to hide the bookmark page menu item. | |
| 168 // This only affects the bookmark page menu item under the main menu. | |
| 169 // The bookmark page menu item under the wrench menu has its | |
| 170 // visibility controlled by WrenchMenuModel. | |
| 171 bool shouldHide = | |
| 172 chrome::ShouldRemoveBookmarkThisPageUI(browser->profile()); | |
| 173 NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item); | |
| 174 [menuItem setHidden:shouldHide]; | |
| 175 break; | |
| 176 } | |
| 177 case IDC_BOOKMARK_ALL_TABS: { | |
| 178 // Extensions have the ability to hide the bookmark all tabs menu | |
| 179 // item. This only affects the bookmark page menu item under the main | |
| 180 // menu. The bookmark page menu item under the wrench menu has its | |
| 181 // visibility controlled by WrenchMenuModel. | |
| 182 bool shouldHide = | |
| 183 chrome::ShouldRemoveBookmarkOpenPagesUI(browser->profile()); | |
| 184 NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item); | |
| 185 [menuItem setHidden:shouldHide]; | |
| 186 break; | |
| 187 } | |
| 188 default: | |
| 189 // Special handling for the contents of the Text Encoding submenu. On | |
| 190 // Mac OS, instead of enabling/disabling the top-level menu item, we | |
| 191 // enable/disable the submenu's contents (per Apple's HIG). | |
| 192 EncodingMenuController encoding_controller; | |
| 193 if (encoding_controller.DoesCommandBelongToEncodingMenu(tag)) | |
| 194 enable &= chrome::IsCommandEnabled(browser, IDC_ENCODING_MENU); | |
| 195 } | |
| 196 | |
| 197 // If the item is toggleable, find its toggle state and | |
| 198 // try to update it. This is a little awkward, but the alternative is | |
| 199 // to check after a commandDispatch, which seems worse. | |
| 200 UpdateToggleStateWithTag(tag, item, window); | |
| 201 | |
| 202 return enable; | |
| 203 } | |
| 204 | |
| 205 - (void)commandDispatch:(id)sender window:(NSWindow*)window { | |
| 206 DCHECK(sender); | |
| 207 | |
| 208 // When system fullscreen is available, it supercedes presentation mode. | |
| 209 int command = [sender tag]; | |
| 210 if (command == IDC_PRESENTATION_MODE && | |
| 211 chrome::mac::SupportsSystemFullscreen()) | |
| 212 command = IDC_FULLSCREEN; | |
| 213 | |
| 214 chrome::ExecuteCommand(FindBrowserForSender(sender, window), command); | |
| 215 } | |
| 216 | |
| 217 - (void)commandDispatchUsingKeyModifiers:(id)sender window:(NSWindow*)window { | |
| 218 DCHECK(sender); | |
| 219 | |
| 220 if (![sender isEnabled]) { | |
| 221 // This code is reachable e.g. if the user mashes the back button, queuing | |
| 222 // up a bunch of events before the button's enabled state is updated: | |
| 223 // http://crbug.com/63254 | |
| 224 return; | |
| 225 } | |
| 226 | |
| 227 NSInteger command = [sender tag]; | |
| 228 NSUInteger modifierFlags = [[NSApp currentEvent] modifierFlags]; | |
| 229 if ((command == IDC_RELOAD) && | |
| 230 (modifierFlags & (NSShiftKeyMask | NSControlKeyMask))) { | |
| 231 command = IDC_RELOAD_IGNORING_CACHE; | |
| 232 // Mask off Shift and Control so they don't affect the disposition below. | |
| 233 modifierFlags &= ~(NSShiftKeyMask | NSControlKeyMask); | |
| 234 } | |
| 235 if (![[sender window] isMainWindow]) { | |
| 236 // Remove the command key from the flags, it means "keep the window in | |
| 237 // the background" in this case. | |
| 238 modifierFlags &= ~NSCommandKeyMask; | |
| 239 } | |
| 240 chrome::ExecuteCommandWithDisposition( | |
| 241 FindBrowserForSender(sender, window), command, | |
| 242 ui::WindowOpenDispositionFromNSEventWithFlags([NSApp currentEvent], | |
| 243 modifierFlags)); | |
| 244 } | |
| 245 | |
| 246 @end // BrowserWindowCommandHandler | |
|
tapted
2015/09/04 02:56:24
nit: doesn't need comment
jackhou1
2015/09/04 06:13:58
Done.
| |
| OLD | NEW |