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. |
| 72 // TODO(jackhou): Remove the dependency on BrowserWindowController(Private). |
| 73 NSString* GetTitleForFullscreenMenuItem(Browser* browser) { |
| 74 NSWindow* ns_window = browser->window()->GetNativeWindow(); |
| 75 if (BrowserWindowController* controller = [ns_window windowController]) { |
| 76 if (!chrome::mac::SupportsSystemFullscreen()) { |
| 77 return l10n_util::GetNSString([controller inPresentationMode] |
| 78 ? IDS_EXIT_PRESENTATION_MAC |
| 79 : IDS_ENTER_PRESENTATION_MAC); |
| 80 } |
| 81 |
| 82 return l10n_util::GetNSString([controller isInAppKitFullscreen] |
| 83 ? IDS_EXIT_FULLSCREEN_MAC |
| 84 : IDS_ENTER_FULLSCREEN_MAC); |
| 85 } |
| 86 |
| 87 return l10n_util::GetNSString(browser->window()->IsFullscreen() |
| 88 ? IDS_EXIT_FULLSCREEN_MAC |
| 89 : IDS_ENTER_FULLSCREEN_MAC); |
| 90 } |
| 91 |
| 92 // Identify the actual Browser to which the command should be dispatched. It |
| 93 // might belong to a background window, yet another dispatcher gets it because |
| 94 // it is the foreground window's dispatcher and thus in the responder chain. |
| 95 // Some senders don't have this problem (for example, menus only operate on the |
| 96 // foreground window), so this is only an issue for senders that are part of |
| 97 // windows. |
| 98 Browser* FindBrowserForSender(id sender, NSWindow* window) { |
| 99 NSWindow* targetWindow = window; |
| 100 if ([sender respondsToSelector:@selector(window)]) |
| 101 targetWindow = [sender window]; |
| 102 Browser* browser = chrome::FindBrowserWithWindow(targetWindow); |
| 103 DCHECK(browser); |
| 104 return browser; |
| 105 } |
| 106 |
| 107 } // namespace |
| 108 |
| 109 @implementation BrowserWindowCommandHandler |
| 110 |
| 111 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item |
| 112 window:(NSWindow*)window { |
| 113 if ([item action] != @selector(commandDispatch:) && |
| 114 [item action] != @selector(commandDispatchUsingKeyModifiers:)) { |
| 115 // NSWindow should only forward the above selectors here. All other |
| 116 // selectors must be handled by the default -[NSWindow |
| 117 // validateUserInterfaceItem:window:]. |
| 118 NOTREACHED(); |
| 119 // By default, interface items are enabled if the object in the responder |
| 120 // chain that implements the action does not implement |
| 121 // -validateUserInterfaceItem. Since we only care about -commandDispatch, |
| 122 // return YES for all other actions. |
| 123 return YES; |
| 124 } |
| 125 |
| 126 Browser* browser = chrome::FindBrowserWithWindow(window); |
| 127 DCHECK(browser); |
| 128 NSInteger tag = [item tag]; |
| 129 if (!chrome::SupportsCommand(browser, tag)) |
| 130 return NO; |
| 131 |
| 132 // Generate return value (enabled state). |
| 133 BOOL enable = chrome::IsCommandEnabled(browser, tag); |
| 134 switch (tag) { |
| 135 case IDC_CLOSE_TAB: |
| 136 // Disable "close tab" if the receiving window is not tabbed. |
| 137 // We simply check whether the item has a keyboard shortcut set here; |
| 138 // app_controller_mac.mm actually determines whether the item should |
| 139 // be enabled. |
| 140 if (NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item)) |
| 141 enable &= !![[menuItem keyEquivalent] length]; |
| 142 break; |
| 143 case IDC_FULLSCREEN: { |
| 144 if (NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item)) { |
| 145 if (chrome::mac::SupportsSystemFullscreen()) |
| 146 [menuItem setTitle:GetTitleForFullscreenMenuItem(browser)]; |
| 147 else |
| 148 [menuItem setHidden:YES]; |
| 149 } |
| 150 break; |
| 151 } |
| 152 case IDC_PRESENTATION_MODE: { |
| 153 if (NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item)) { |
| 154 [menuItem setTitle:GetTitleForFullscreenMenuItem(browser)]; |
| 155 |
| 156 if (chrome::mac::SupportsSystemFullscreen()) |
| 157 [menuItem setAlternate:YES]; |
| 158 } |
| 159 break; |
| 160 } |
| 161 case IDC_SHOW_SIGNIN: { |
| 162 Profile* original_profile = browser->profile()->GetOriginalProfile(); |
| 163 [AppController updateSigninItem:item |
| 164 shouldShow:enable |
| 165 currentProfile:original_profile]; |
| 166 break; |
| 167 } |
| 168 case IDC_BOOKMARK_PAGE: { |
| 169 // Extensions have the ability to hide the bookmark page menu item. |
| 170 // This only affects the bookmark page menu item under the main menu. |
| 171 // The bookmark page menu item under the wrench menu has its |
| 172 // visibility controlled by WrenchMenuModel. |
| 173 bool shouldHide = |
| 174 chrome::ShouldRemoveBookmarkThisPageUI(browser->profile()); |
| 175 NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item); |
| 176 [menuItem setHidden:shouldHide]; |
| 177 break; |
| 178 } |
| 179 case IDC_BOOKMARK_ALL_TABS: { |
| 180 // Extensions have the ability to hide the bookmark all tabs menu |
| 181 // item. This only affects the bookmark page menu item under the main |
| 182 // menu. The bookmark page menu item under the wrench menu has its |
| 183 // visibility controlled by WrenchMenuModel. |
| 184 bool shouldHide = |
| 185 chrome::ShouldRemoveBookmarkOpenPagesUI(browser->profile()); |
| 186 NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item); |
| 187 [menuItem setHidden:shouldHide]; |
| 188 break; |
| 189 } |
| 190 default: |
| 191 // Special handling for the contents of the Text Encoding submenu. On |
| 192 // Mac OS, instead of enabling/disabling the top-level menu item, we |
| 193 // enable/disable the submenu's contents (per Apple's HIG). |
| 194 EncodingMenuController encoding_controller; |
| 195 if (encoding_controller.DoesCommandBelongToEncodingMenu(tag)) |
| 196 enable &= chrome::IsCommandEnabled(browser, IDC_ENCODING_MENU); |
| 197 } |
| 198 |
| 199 // If the item is toggleable, find its toggle state and |
| 200 // try to update it. This is a little awkward, but the alternative is |
| 201 // to check after a commandDispatch, which seems worse. |
| 202 UpdateToggleStateWithTag(tag, item, window); |
| 203 |
| 204 return enable; |
| 205 } |
| 206 |
| 207 - (void)commandDispatch:(id)sender window:(NSWindow*)window { |
| 208 DCHECK(sender); |
| 209 |
| 210 // When system fullscreen is available, it supercedes presentation mode. |
| 211 int command = [sender tag]; |
| 212 if (command == IDC_PRESENTATION_MODE && |
| 213 chrome::mac::SupportsSystemFullscreen()) |
| 214 command = IDC_FULLSCREEN; |
| 215 |
| 216 chrome::ExecuteCommand(FindBrowserForSender(sender, window), command); |
| 217 } |
| 218 |
| 219 - (void)commandDispatchUsingKeyModifiers:(id)sender window:(NSWindow*)window { |
| 220 DCHECK(sender); |
| 221 |
| 222 if (![sender isEnabled]) { |
| 223 // This code is reachable e.g. if the user mashes the back button, queuing |
| 224 // up a bunch of events before the button's enabled state is updated: |
| 225 // http://crbug.com/63254 |
| 226 return; |
| 227 } |
| 228 |
| 229 NSInteger command = [sender tag]; |
| 230 NSUInteger modifierFlags = [[NSApp currentEvent] modifierFlags]; |
| 231 if ((command == IDC_RELOAD) && |
| 232 (modifierFlags & (NSShiftKeyMask | NSControlKeyMask))) { |
| 233 command = IDC_RELOAD_IGNORING_CACHE; |
| 234 // Mask off Shift and Control so they don't affect the disposition below. |
| 235 modifierFlags &= ~(NSShiftKeyMask | NSControlKeyMask); |
| 236 } |
| 237 if (![[sender window] isMainWindow]) { |
| 238 // Remove the command key from the flags, it means "keep the window in |
| 239 // the background" in this case. |
| 240 modifierFlags &= ~NSCommandKeyMask; |
| 241 } |
| 242 chrome::ExecuteCommandWithDisposition( |
| 243 FindBrowserForSender(sender, window), command, |
| 244 ui::WindowOpenDispositionFromNSEventWithFlags([NSApp currentEvent], |
| 245 modifierFlags)); |
| 246 } |
| 247 |
| 248 @end |
OLD | NEW |