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.
5 #import "chrome/browser/ui/cocoa/browser_window_command_handler.h"
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"
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:)])
34 Browser* browser = chrome::FindBrowserWithWindow(window);
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];
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))
55 Profile* profile = browser->profile();
57 content::WebContents* current_tab =
58 browser->tab_strip_model()->GetActiveWebContents();
62 const std::string encoding = current_tab->GetEncoding();
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];
71 NSString* GetTitleForViewsFullscreenMenuItem(Browser* browser) {
72 return l10n_util::GetNSString(browser->window()->IsFullscreen()
73 ? IDS_EXIT_FULLSCREEN_MAC
74 : IDS_ENTER_FULLSCREEN_MAC);
77 // Get the text for the "Enter/Exit Fullscreen" menu item.
78 // TODO(jackhou): Remove the dependency on BrowserWindowController(Private).
79 NSString* GetTitleForFullscreenMenuItem(Browser* browser) {
80 NSWindow* ns_window = browser->window()->GetNativeWindow();
81 if (BrowserWindowController* controller = [ns_window windowController]) {
82 DCHECK(chrome::mac::SupportsSystemFullscreen());
83 return l10n_util::GetNSString([controller isInAppKitFullscreen]
84 ? IDS_EXIT_FULLSCREEN_MAC
85 : IDS_ENTER_FULLSCREEN_MAC);
88 return GetTitleForViewsFullscreenMenuItem(browser);
91 // Get the text for the "Enter/Exit Presentation Mode" menu item.
92 // TODO(jackhou): Remove the dependency on BrowserWindowController(Private).
93 NSString* GetTitleForPresentationModeMenuItem(Browser* browser) {
94 NSWindow* ns_window = browser->window()->GetNativeWindow();
95 if (BrowserWindowController* controller = [ns_window windowController]) {
96 return l10n_util::GetNSString([controller inPresentationMode]
97 ? IDS_EXIT_PRESENTATION_MAC
98 : IDS_ENTER_PRESENTATION_MAC);
101 return GetTitleForViewsFullscreenMenuItem(browser);
104 // Identify the actual Browser to which the command should be dispatched. It
105 // might belong to a background window, yet another dispatcher gets it because
106 // it is the foreground window's dispatcher and thus in the responder chain.
107 // Some senders don't have this problem (for example, menus only operate on the
108 // foreground window), so this is only an issue for senders that are part of
110 Browser* FindBrowserForSender(id sender, NSWindow* window) {
111 NSWindow* targetWindow = window;
112 if ([sender respondsToSelector:@selector(window)])
113 targetWindow = [sender window];
114 Browser* browser = chrome::FindBrowserWithWindow(targetWindow);
121 @implementation BrowserWindowCommandHandler
123 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item
124 window:(NSWindow*)window {
125 if ([item action] != @selector(commandDispatch:) &&
126 [item action] != @selector(commandDispatchUsingKeyModifiers:)) {
127 // NSWindow should only forward the above selectors here. All other
128 // selectors must be handled by the default -[NSWindow
129 // validateUserInterfaceItem:window:].
131 // By default, interface items are enabled if the object in the responder
132 // chain that implements the action does not implement
133 // -validateUserInterfaceItem. Since we only care about -commandDispatch,
134 // return YES for all other actions.
138 Browser* browser = chrome::FindBrowserWithWindow(window);
140 NSInteger tag = [item tag];
141 if (!chrome::SupportsCommand(browser, tag))
144 // Generate return value (enabled state).
145 BOOL enable = chrome::IsCommandEnabled(browser, tag);
148 // Disable "close tab" if the receiving window is not tabbed.
149 // We simply check whether the item has a keyboard shortcut set here;
150 // app_controller_mac.mm actually determines whether the item should
152 if (NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item))
153 enable &= !![[menuItem keyEquivalent] length];
155 case IDC_FULLSCREEN: {
156 if (NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item)) {
157 if (chrome::mac::SupportsSystemFullscreen())
158 [menuItem setTitle:GetTitleForFullscreenMenuItem(browser)];
160 [menuItem setHidden:YES];
164 case IDC_PRESENTATION_MODE: {
165 if (NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item)) {
166 [menuItem setTitle:GetTitleForPresentationModeMenuItem(browser)];
170 case IDC_SHOW_SIGNIN: {
171 Profile* original_profile = browser->profile()->GetOriginalProfile();
172 [AppController updateSigninItem:item
174 currentProfile:original_profile];
177 case IDC_BOOKMARK_PAGE: {
178 // Extensions have the ability to hide the bookmark page menu item.
179 // This only affects the bookmark page menu item under the main menu.
180 // The bookmark page menu item under the wrench menu has its
181 // visibility controlled by WrenchMenuModel.
183 chrome::ShouldRemoveBookmarkThisPageUI(browser->profile());
184 NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item);
185 [menuItem setHidden:shouldHide];
188 case IDC_BOOKMARK_ALL_TABS: {
189 // Extensions have the ability to hide the bookmark all tabs menu
190 // item. This only affects the bookmark page menu item under the main
191 // menu. The bookmark page menu item under the wrench menu has its
192 // visibility controlled by WrenchMenuModel.
194 chrome::ShouldRemoveBookmarkOpenPagesUI(browser->profile());
195 NSMenuItem* menuItem = base::mac::ObjCCast<NSMenuItem>(item);
196 [menuItem setHidden:shouldHide];
200 // Special handling for the contents of the Text Encoding submenu. On
201 // Mac OS, instead of enabling/disabling the top-level menu item, we
202 // enable/disable the submenu's contents (per Apple's HIG).
203 EncodingMenuController encoding_controller;
204 if (encoding_controller.DoesCommandBelongToEncodingMenu(tag))
205 enable &= chrome::IsCommandEnabled(browser, IDC_ENCODING_MENU);
208 // If the item is toggleable, find its toggle state and
209 // try to update it. This is a little awkward, but the alternative is
210 // to check after a commandDispatch, which seems worse.
211 UpdateToggleStateWithTag(tag, item, window);
216 - (void)commandDispatch:(id)sender window:(NSWindow*)window {
218 int command = [sender tag];
219 chrome::ExecuteCommand(FindBrowserForSender(sender, window), command);
222 - (void)commandDispatchUsingKeyModifiers:(id)sender window:(NSWindow*)window {
225 if (![sender isEnabled]) {
226 // This code is reachable e.g. if the user mashes the back button, queuing
227 // up a bunch of events before the button's enabled state is updated:
228 // http://crbug.com/63254
232 NSInteger command = [sender tag];
233 NSUInteger modifierFlags = [[NSApp currentEvent] modifierFlags];
234 if ((command == IDC_RELOAD) &&
235 (modifierFlags & (NSShiftKeyMask | NSControlKeyMask))) {
236 command = IDC_RELOAD_IGNORING_CACHE;
237 // Mask off Shift and Control so they don't affect the disposition below.
238 modifierFlags &= ~(NSShiftKeyMask | NSControlKeyMask);
240 if (![[sender window] isMainWindow]) {
241 // Remove the command key from the flags, it means "keep the window in
242 // the background" in this case.
243 modifierFlags &= ~NSCommandKeyMask;
245 chrome::ExecuteCommandWithDisposition(
246 FindBrowserForSender(sender, window), command,
247 ui::WindowOpenDispositionFromNSEventWithFlags([NSApp currentEvent],