1 // Copyright 2014 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 #include "chrome/browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac.h"
7 #include "base/compiler_specific.h"
8 #import "base/mac/scoped_sending_event.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/render_widget_host_view.h"
14 #include "grit/generated_resources.h"
15 #import "ui/base/cocoa/menu_controller.h"
16 #include "ui/base/l10n/l10n_util.h"
18 using content::WebContents;
22 // Retrieves an NSMenuItem which has the specified command_id. This function
23 // traverses the given |model| in the depth-first order. When this function
24 // finds an item whose command_id is the same as the given |command_id|, it
25 // returns the NSMenuItem associated with the item. This function emulates
26 // views::MenuItemViews::GetMenuItemByID() for Mac.
27 NSMenuItem* GetMenuItemByID(ui::MenuModel* model,
30 for (int i = 0; i < model->GetItemCount(); ++i) {
31 NSMenuItem* item = [menu itemAtIndex:i];
32 if (model->GetCommandIdAt(i) == command_id)
35 ui::MenuModel* submenu = model->GetSubmenuModelAt(i);
36 if (submenu && [item hasSubmenu]) {
37 NSMenuItem* subitem = GetMenuItemByID(submenu,
49 // Obj-C bridge class that is the target of all items in the context menu.
50 // Relies on the tag being set to the command id.
52 RenderViewContextMenuMac::RenderViewContextMenuMac(
53 content::RenderFrameHost* render_frame_host,
54 const content::ContextMenuParams& params,
56 : RenderViewContextMenu(render_frame_host, params),
57 speech_submenu_model_(this),
58 bidi_submenu_model_(this),
59 parent_view_(parent_view) {
62 RenderViewContextMenuMac::~RenderViewContextMenuMac() {
65 void RenderViewContextMenuMac::PlatformInit() {
69 void RenderViewContextMenuMac::Show() {
70 menu_controller_.reset(
71 [[MenuController alloc] initWithModel:&menu_model_
72 useWithPopUpButtonCell:NO]);
74 // Synthesize an event for the click, as there is no certainty that
75 // [NSApp currentEvent] will return a valid event.
76 NSEvent* currentEvent = [NSApp currentEvent];
77 NSWindow* window = [parent_view_ window];
78 NSPoint position = [window mouseLocationOutsideOfEventStream];
79 NSTimeInterval eventTime = [currentEvent timestamp];
80 NSEvent* clickEvent = [NSEvent mouseEventWithType:NSRightMouseDown
82 modifierFlags:NSRightMouseDownMask
84 windowNumber:[window windowNumber]
91 // Make sure events can be pumped while the menu is up.
92 base::MessageLoop::ScopedNestableTaskAllower allow(
93 base::MessageLoop::current());
95 // One of the events that could be pumped is |window.close()|.
96 // User-initiated event-tracking loops protect against this by
97 // setting flags in -[CrApplication sendEvent:], but since
98 // web-content menus are initiated by IPC message the setup has to
100 base::mac::ScopedSendingEvent sendingEventScoper;
103 [NSMenu popUpContextMenu:[menu_controller_ menu]
105 forView:parent_view_];
109 void RenderViewContextMenuMac::PlatformCancel() {
110 [menu_controller_ cancel];
113 void RenderViewContextMenuMac::ExecuteCommand(int command_id, int event_flags) {
114 switch (command_id) {
115 case IDC_CONTENT_CONTEXT_LOOK_UP_IN_DICTIONARY:
116 LookUpInDictionary();
119 case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING:
123 case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING:
127 case IDC_WRITING_DIRECTION_DEFAULT:
128 // WebKit's current behavior is for this menu item to always be disabled.
132 case IDC_WRITING_DIRECTION_RTL:
133 case IDC_WRITING_DIRECTION_LTR: {
134 content::RenderViewHost* view_host = GetRenderViewHost();
135 blink::WebTextDirection dir = blink::WebTextDirectionLeftToRight;
136 if (command_id == IDC_WRITING_DIRECTION_RTL)
137 dir = blink::WebTextDirectionRightToLeft;
138 view_host->UpdateTextDirection(dir);
139 view_host->NotifyTextDirection();
144 RenderViewContextMenu::ExecuteCommand(command_id, event_flags);
149 bool RenderViewContextMenuMac::IsCommandIdChecked(int command_id) const {
150 switch (command_id) {
151 case IDC_WRITING_DIRECTION_DEFAULT:
152 return params_.writing_direction_default &
153 blink::WebContextMenuData::CheckableMenuItemChecked;
154 case IDC_WRITING_DIRECTION_RTL:
155 return params_.writing_direction_right_to_left &
156 blink::WebContextMenuData::CheckableMenuItemChecked;
157 case IDC_WRITING_DIRECTION_LTR:
158 return params_.writing_direction_left_to_right &
159 blink::WebContextMenuData::CheckableMenuItemChecked;
162 return RenderViewContextMenu::IsCommandIdChecked(command_id);
166 bool RenderViewContextMenuMac::IsCommandIdEnabled(int command_id) const {
167 switch (command_id) {
168 case IDC_CONTENT_CONTEXT_LOOK_UP_IN_DICTIONARY:
169 // This is OK because the menu is not shown when it isn't
173 case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING:
174 // This is OK because the menu is not shown when it isn't
178 case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING: {
179 content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
180 return view && view->IsSpeaking();
183 case IDC_WRITING_DIRECTION_DEFAULT: // Provided to match OS defaults.
184 return params_.writing_direction_default &
185 blink::WebContextMenuData::CheckableMenuItemEnabled;
186 case IDC_WRITING_DIRECTION_RTL:
187 return params_.writing_direction_right_to_left &
188 blink::WebContextMenuData::CheckableMenuItemEnabled;
189 case IDC_WRITING_DIRECTION_LTR:
190 return params_.writing_direction_left_to_right &
191 blink::WebContextMenuData::CheckableMenuItemEnabled;
194 return RenderViewContextMenu::IsCommandIdEnabled(command_id);
198 bool RenderViewContextMenuMac::GetAcceleratorForCommandId(
200 ui::Accelerator* accelerator) {
204 void RenderViewContextMenuMac::AppendPlatformEditableItems() {
205 // OS X provides a contextual menu to set writing direction for BiDi
207 // This functionality is exposed as a keyboard shortcut on Windows & Linux.
211 void RenderViewContextMenuMac::InitPlatformMenu() {
212 bool has_selection = !params_.selection_text.empty();
215 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
216 menu_model_.AddItemWithStringId(
217 IDC_CONTENT_CONTEXT_LOOK_UP_IN_DICTIONARY,
218 IDS_CONTENT_CONTEXT_LOOK_UP_IN_DICTIONARY);
220 content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
221 if (view && view->SupportsSpeech()) {
222 speech_submenu_model_.AddItemWithStringId(
223 IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING,
224 IDS_SPEECH_START_SPEAKING_MAC);
225 speech_submenu_model_.AddItemWithStringId(
226 IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING,
227 IDS_SPEECH_STOP_SPEAKING_MAC);
228 menu_model_.AddSubMenu(
229 IDC_CONTENT_CONTEXT_SPEECH_MENU,
230 l10n_util::GetStringUTF16(IDS_SPEECH_MAC),
231 &speech_submenu_model_);
236 void RenderViewContextMenuMac::AppendBidiSubMenu() {
237 bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_DEFAULT,
238 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT));
239 bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_LTR,
240 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR));
241 bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_RTL,
242 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL));
244 menu_model_.AddSubMenu(
245 IDC_WRITING_DIRECTION_MENU,
246 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_MENU),
247 &bidi_submenu_model_);
250 void RenderViewContextMenuMac::LookUpInDictionary() {
251 content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
253 view->ShowDefinitionForSelection();
256 void RenderViewContextMenuMac::StartSpeaking() {
257 content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
259 view->SpeakSelection();
262 void RenderViewContextMenuMac::StopSpeaking() {
263 content::RenderWidgetHostView* view = GetRenderViewHost()->GetView();
265 view->StopSpeaking();
268 void RenderViewContextMenuMac::UpdateMenuItem(int command_id,
271 const base::string16& title) {
272 NSMenuItem* item = GetMenuItemByID(&menu_model_, [menu_controller_ menu],
277 // Update the returned NSMenuItem directly so we can update it immediately.
278 [item setEnabled:enabled];
279 [item setTitle:SysUTF16ToNSString(title)];
280 [item setHidden:hidden];
281 [[item menu] itemChanged:item];