| Index: chrome/browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac.mm
|
| diff --git a/chrome/browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac.mm b/chrome/browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac.mm
|
| index 549d41b6ec2f32c6222b816402010e7e14a8a3e4..51010d4a354225f0e54825cc5a01aa6932c93906 100644
|
| --- a/chrome/browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac.mm
|
| +++ b/chrome/browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac.mm
|
| @@ -183,9 +183,8 @@ RenderViewContextMenuMac::RenderViewContextMenuMac(
|
| const content::ContextMenuParams& params,
|
| NSView* parent_view)
|
| : RenderViewContextMenu(render_frame_host, params),
|
| - speech_submenu_model_(this),
|
| - bidi_submenu_model_(this),
|
| - parent_view_(parent_view) {
|
| + parent_view_(parent_view),
|
| + text_services_context_menu_(this) {
|
| std::unique_ptr<ToolkitDelegate> delegate(new ToolkitDelegateMac(this));
|
| set_toolkit_delegate(std::move(delegate));
|
| }
|
| @@ -245,143 +244,28 @@ void RenderViewContextMenuMac::Show() {
|
| }
|
|
|
| void RenderViewContextMenuMac::ExecuteCommand(int command_id, int event_flags) {
|
| - switch (command_id) {
|
| - case IDC_CONTENT_CONTEXT_LOOK_UP:
|
| - LookUpInDictionary();
|
| - break;
|
| + if (text_services_context_menu_.IsTextServicesCommandId(command_id))
|
| + return text_services_context_menu_.ExecuteCommand(command_id, event_flags);
|
|
|
| - case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING:
|
| - StartSpeaking();
|
| - break;
|
| -
|
| - case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING:
|
| - StopSpeaking();
|
| - break;
|
| -
|
| - case IDC_WRITING_DIRECTION_DEFAULT:
|
| - // WebKit's current behavior is for this menu item to always be disabled.
|
| - NOTREACHED();
|
| - break;
|
| -
|
| - case IDC_WRITING_DIRECTION_RTL:
|
| - case IDC_WRITING_DIRECTION_LTR: {
|
| - content::RenderViewHost* view_host = GetRenderViewHost();
|
| - blink::WebTextDirection dir = blink::WebTextDirectionLeftToRight;
|
| - if (command_id == IDC_WRITING_DIRECTION_RTL)
|
| - dir = blink::WebTextDirectionRightToLeft;
|
| - view_host->GetWidget()->UpdateTextDirection(dir);
|
| - view_host->GetWidget()->NotifyTextDirection();
|
| - RenderViewContextMenu::RecordUsedItem(command_id);
|
| - break;
|
| - }
|
| -
|
| - default:
|
| - RenderViewContextMenu::ExecuteCommand(command_id, event_flags);
|
| - break;
|
| - }
|
| + RenderViewContextMenu::ExecuteCommand(command_id, event_flags);
|
| }
|
|
|
| bool RenderViewContextMenuMac::IsCommandIdChecked(int command_id) const {
|
| - switch (command_id) {
|
| - case IDC_WRITING_DIRECTION_DEFAULT:
|
| - return params_.writing_direction_default &
|
| - blink::WebContextMenuData::CheckableMenuItemChecked;
|
| - case IDC_WRITING_DIRECTION_RTL:
|
| - return params_.writing_direction_right_to_left &
|
| - blink::WebContextMenuData::CheckableMenuItemChecked;
|
| - case IDC_WRITING_DIRECTION_LTR:
|
| - return params_.writing_direction_left_to_right &
|
| - blink::WebContextMenuData::CheckableMenuItemChecked;
|
| + if (text_services_context_menu_.IsTextServicesCommandId(command_id))
|
| + return text_services_context_menu_.IsCommandIdChecked(command_id);
|
|
|
| - default:
|
| - return RenderViewContextMenu::IsCommandIdChecked(command_id);
|
| - }
|
| + return RenderViewContextMenu::IsCommandIdChecked(command_id);
|
| }
|
|
|
| bool RenderViewContextMenuMac::IsCommandIdEnabled(int command_id) const {
|
| - switch (command_id) {
|
| - case IDC_CONTENT_CONTEXT_LOOK_UP:
|
| - // This is OK because the menu is not shown when it isn't
|
| - // appropriate.
|
| - return true;
|
| -
|
| - case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING:
|
| - // This is OK because the menu is not shown when it isn't
|
| - // appropriate.
|
| - return true;
|
| -
|
| - case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING: {
|
| - content::RenderWidgetHostView* view =
|
| - GetRenderViewHost()->GetWidget()->GetView();
|
| - return view && view->IsSpeaking();
|
| - }
|
| -
|
| - case IDC_WRITING_DIRECTION_DEFAULT: // Provided to match OS defaults.
|
| - return params_.writing_direction_default &
|
| - blink::WebContextMenuData::CheckableMenuItemEnabled;
|
| - case IDC_WRITING_DIRECTION_RTL:
|
| - return params_.writing_direction_right_to_left &
|
| - blink::WebContextMenuData::CheckableMenuItemEnabled;
|
| - case IDC_WRITING_DIRECTION_LTR:
|
| - return params_.writing_direction_left_to_right &
|
| - blink::WebContextMenuData::CheckableMenuItemEnabled;
|
| + if (text_services_context_menu_.IsTextServicesCommandId(command_id))
|
| + return text_services_context_menu_.IsCommandIdEnabled(command_id);
|
|
|
| - default:
|
| - return RenderViewContextMenu::IsCommandIdEnabled(command_id);
|
| - }
|
| -}
|
| -
|
| -void RenderViewContextMenuMac::AppendPlatformEditableItems() {
|
| - // OS X provides a contextual menu to set writing direction for BiDi
|
| - // languages.
|
| - // This functionality is exposed as a keyboard shortcut on Windows & Linux.
|
| - AppendBidiSubMenu();
|
| + return RenderViewContextMenu::IsCommandIdEnabled(command_id);
|
| }
|
|
|
| void RenderViewContextMenuMac::InitToolkitMenu() {
|
| - if (params_.selection_text.empty())
|
| - return;
|
| -
|
| - if (params_.link_url.is_empty()) {
|
| - // In case the user has selected a word that triggers spelling suggestions,
|
| - // show the dictionary lookup under the group that contains the command to
|
| - // “Add to Dictionary.”
|
| - int index = menu_model_.GetIndexOfCommandId(
|
| - IDC_SPELLCHECK_ADD_TO_DICTIONARY);
|
| - if (index < 0) {
|
| - index = 0;
|
| - } else {
|
| - while (menu_model_.GetTypeAt(index) != ui::MenuModel::TYPE_SEPARATOR) {
|
| - index++;
|
| - }
|
| - index += 1; // Place it below the separator.
|
| - }
|
| -
|
| - base::string16 printable_selection_text = PrintableSelectionText();
|
| - EscapeAmpersands(&printable_selection_text);
|
| - menu_model_.InsertItemAt(
|
| - index++,
|
| - IDC_CONTENT_CONTEXT_LOOK_UP,
|
| - l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_LOOK_UP,
|
| - printable_selection_text));
|
| - menu_model_.InsertSeparatorAt(index++, ui::NORMAL_SEPARATOR);
|
| - }
|
| -
|
| - content::RenderWidgetHostView* view =
|
| - GetRenderViewHost()->GetWidget()->GetView();
|
| - if (view && view->SupportsSpeech()) {
|
| - menu_model_.AddSeparator(ui::NORMAL_SEPARATOR);
|
| - speech_submenu_model_.AddItemWithStringId(
|
| - IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING,
|
| - IDS_SPEECH_START_SPEAKING_MAC);
|
| - speech_submenu_model_.AddItemWithStringId(
|
| - IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING,
|
| - IDS_SPEECH_STOP_SPEAKING_MAC);
|
| - menu_model_.AddSubMenu(
|
| - IDC_CONTENT_CONTEXT_SPEECH_MENU,
|
| - l10n_util::GetStringUTF16(IDS_SPEECH_MAC),
|
| - &speech_submenu_model_);
|
| - }
|
| + text_services_context_menu_.AppendToContextMenu(&menu_model_);
|
| }
|
|
|
| void RenderViewContextMenuMac::CancelToolkitMenu() {
|
| @@ -405,37 +289,78 @@ void RenderViewContextMenuMac::UpdateToolkitMenuItem(
|
| [[item menu] itemChanged:item];
|
| }
|
|
|
| -void RenderViewContextMenuMac::AppendBidiSubMenu() {
|
| - bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_DEFAULT,
|
| - l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT));
|
| - bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_LTR,
|
| - l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR));
|
| - bidi_submenu_model_.AddCheckItem(IDC_WRITING_DIRECTION_RTL,
|
| - l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL));
|
| -
|
| - menu_model_.AddSubMenu(
|
| - IDC_WRITING_DIRECTION_MENU,
|
| - l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_MENU),
|
| - &bidi_submenu_model_);
|
| +base::string16 RenderViewContextMenuMac::GetSelectedText() const {
|
| + return params_.selection_text;
|
| }
|
|
|
| -void RenderViewContextMenuMac::LookUpInDictionary() {
|
| +void RenderViewContextMenuMac::OnSpeakRequested() {
|
| content::RenderWidgetHostView* view =
|
| GetRenderViewHost()->GetWidget()->GetView();
|
| if (view)
|
| - view->ShowDefinitionForSelection();
|
| + view->SpeakSelection();
|
| }
|
|
|
| -void RenderViewContextMenuMac::StartSpeaking() {
|
| - content::RenderWidgetHostView* view =
|
| - GetRenderViewHost()->GetWidget()->GetView();
|
| - if (view)
|
| - view->SpeakSelection();
|
| +bool RenderViewContextMenuMac::IsLookUpAvailable() const {
|
| + return params_.link_url.is_empty();
|
| }
|
|
|
| -void RenderViewContextMenuMac::StopSpeaking() {
|
| +void RenderViewContextMenuMac::LookUpInDictionary() {
|
| content::RenderWidgetHostView* view =
|
| GetRenderViewHost()->GetWidget()->GetView();
|
| if (view)
|
| - view->StopSpeaking();
|
| + view->ShowDefinitionForSelection();
|
| +}
|
| +
|
| +bool RenderViewContextMenuMac::IsTextDirectionEnabled(
|
| + base::i18n::TextDirection direction) const {
|
| + switch (direction) {
|
| + case base::i18n::TextDirection::UNKNOWN_DIRECTION:
|
| + return params_.writing_direction_default &
|
| + blink::WebContextMenuData::CheckableMenuItemEnabled;
|
| + case base::i18n::TextDirection::RIGHT_TO_LEFT:
|
| + return params_.writing_direction_right_to_left &
|
| + blink::WebContextMenuData::CheckableMenuItemEnabled;
|
| + case base::i18n::TextDirection::LEFT_TO_RIGHT:
|
| + return params_.writing_direction_left_to_right &
|
| + blink::WebContextMenuData::CheckableMenuItemEnabled;
|
| + case base::i18n::TextDirection::TEXT_DIRECTION_NUM_DIRECTIONS:
|
| + NOTREACHED();
|
| + return false;
|
| + }
|
| +}
|
| +
|
| +bool RenderViewContextMenuMac::IsTextDirectionChecked(
|
| + base::i18n::TextDirection direction) const {
|
| + switch (direction) {
|
| + case base::i18n::TextDirection::UNKNOWN_DIRECTION:
|
| + return params_.writing_direction_default &
|
| + blink::WebContextMenuData::CheckableMenuItemChecked;
|
| + case base::i18n::TextDirection::RIGHT_TO_LEFT:
|
| + return params_.writing_direction_right_to_left &
|
| + blink::WebContextMenuData::CheckableMenuItemChecked;
|
| + case base::i18n::TextDirection::LEFT_TO_RIGHT:
|
| + return params_.writing_direction_left_to_right &
|
| + blink::WebContextMenuData::CheckableMenuItemChecked;
|
| + case base::i18n::TextDirection::TEXT_DIRECTION_NUM_DIRECTIONS:
|
| + NOTREACHED();
|
| + return false;
|
| + }
|
| +}
|
| +
|
| +void RenderViewContextMenuMac::UpdateTextDirection(
|
| + base::i18n::TextDirection direction) {
|
| + DCHECK_NE(direction, base::i18n::TextDirection::UNKNOWN_DIRECTION);
|
| +
|
| + content::RenderViewHost* view_host = GetRenderViewHost();
|
| + blink::WebTextDirection dir = blink::WebTextDirectionLeftToRight;
|
| + int command_id = IDC_WRITING_DIRECTION_LTR;
|
| + if (direction == base::i18n::TextDirection::RIGHT_TO_LEFT) {
|
| + dir = blink::WebTextDirectionRightToLeft;
|
| + command_id = IDC_WRITING_DIRECTION_RTL;
|
| + }
|
| +
|
| + view_host->GetWidget()->UpdateTextDirection(dir);
|
| + view_host->GetWidget()->NotifyTextDirection();
|
| +
|
| + RenderViewContextMenu::RecordUsedItem(command_id);
|
| }
|
|
|