Chromium Code Reviews| Index: ui/base/cocoa/text_services_context_menu.cc |
| diff --git a/ui/base/cocoa/text_services_context_menu.cc b/ui/base/cocoa/text_services_context_menu.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..407137ed4f3ffb9aaaa0cd23b9f568753aa5bbbd |
| --- /dev/null |
| +++ b/ui/base/cocoa/text_services_context_menu.cc |
| @@ -0,0 +1,194 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/base/cocoa/text_services_context_menu.h" |
| + |
| +#include <utility> |
| + |
| +#include <ApplicationServices/ApplicationServices.h> |
| +#include <CoreAudio/CoreAudio.h> |
| + |
| +#include "base/strings/sys_string_conversions.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/app/chrome_command_ids.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/strings/grit/ui_strings.h" |
| + |
| +namespace { |
| + |
| +// The speech channel used for speaking. |
| +SpeechChannel speechChannel; |
| + |
| +// Returns the TextDirection associated associated with the given |
| +// BiDi |command_id|. |
| +base::i18n::TextDirection GetTextDirectionFromCommandId(int command_id) { |
| + switch (command_id) { |
| + case IDC_WRITING_DIRECTION_DEFAULT: |
| + return base::i18n::TextDirection::UNKNOWN_DIRECTION; |
| + case IDC_WRITING_DIRECTION_LTR: |
| + return base::i18n::TextDirection::LEFT_TO_RIGHT; |
| + case IDC_WRITING_DIRECTION_RTL: |
| + return base::i18n::TextDirection::RIGHT_TO_LEFT; |
| + default: |
| + NOTREACHED(); |
| + return base::i18n::TextDirection::UNKNOWN_DIRECTION; |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +namespace ui { |
| + |
| +////////////////////////////////////////////////////////////////// |
| +// TextServicesContextMenu, public: |
| + |
| +TextServicesContextMenu::TextServicesContextMenu(Delegate* delegate) |
| + : speech_submenu_model_(this), |
| + bidi_submenu_model_(this), |
| + delegate_(delegate) { |
| + DCHECK(delegate); |
| + |
| + // Add items to the speech submenu. |
| + 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); |
| + |
| + // Add items to the BiDi submenu. |
| + 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)); |
| +} |
| + |
| +void TextServicesContextMenu::SpeakText(const base::string16& text) { |
| + if (IsSpeaking()) |
| + StopSpeaking(); |
| + |
| + NewSpeechChannel(nullptr, &speechChannel); |
| + SpeakCFString(speechChannel, SysUTF16ToCFStringRef(text), nullptr); |
| +} |
| + |
| +void TextServicesContextMenu::StopSpeaking() { |
| + StopSpeechAt(speechChannel, kImmediate); |
| + DisposeSpeechChannel(speechChannel); |
| +} |
| + |
| +bool TextServicesContextMenu::IsSpeaking() { |
| + return SpeechBusy(); |
| +} |
| + |
| +void TextServicesContextMenu::AppendToContextMenu(SimpleMenuModel* model) { |
|
tapted
2016/12/19 07:41:21
we can perhaps just pass in |index|? (see below)
spqchan
2016/12/21 01:03:02
I think it's better to just get rid of look up fro
|
| + base::string16 printable_selection_text = delegate_->GetSelectedText(); |
| + if (printable_selection_text.empty()) |
| + return; |
| + |
| + if (delegate_->IsLookUpAvailable()) { |
| + // 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 = model->GetIndexOfCommandId(IDC_SPELLCHECK_ADD_TO_DICTIONARY); |
|
tapted
2016/12/19 07:41:21
which means this is hard to fix (i.e. if we can't
spqchan
2016/12/21 01:03:02
Sounds good, let's just make it add only submenus
|
| + if (index < 0) { |
| + index = 0; |
| + } else { |
| + while (model->GetTypeAt(index) != MenuModel::TYPE_SEPARATOR) |
| + index++; |
| + index += 1; // Place it below the separator. |
| + } |
| + |
| + model->InsertItemAt(index++, IDC_CONTENT_CONTEXT_LOOK_UP, |
| + l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_LOOK_UP, |
| + printable_selection_text)); |
| + model->InsertSeparatorAt(index++, NORMAL_SEPARATOR); |
| + } |
| + |
| + model->AddSeparator(NORMAL_SEPARATOR); |
| + |
| + model->AddSubMenu(IDC_CONTENT_CONTEXT_SPEECH_MENU, |
| + l10n_util::GetStringUTF16(IDS_SPEECH_MAC), |
| + &speech_submenu_model_); |
| +} |
| + |
| +void TextServicesContextMenu::AppendEditableItems(SimpleMenuModel* model) { |
| + model->AddSubMenu( |
| + IDC_WRITING_DIRECTION_MENU, |
| + l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_MENU), |
| + &bidi_submenu_model_); |
| +} |
| + |
| +bool TextServicesContextMenu::IsTextServicesCommandId(int command_id) const { |
| + switch (command_id) { |
| + case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING: |
| + case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING: |
| + case IDC_CONTENT_CONTEXT_LOOK_UP: |
| + case IDC_WRITING_DIRECTION_DEFAULT: |
| + case IDC_WRITING_DIRECTION_LTR: |
| + case IDC_WRITING_DIRECTION_RTL: |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +////////////////////////////////////////////////////////////////// |
| +// TextServicesContextMenu::SimpleMenuModel::Delegate: |
| + |
| +void TextServicesContextMenu::ExecuteCommand(int command_id, int event_flags) { |
| + switch (command_id) { |
| + case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING: |
| + delegate_->OnSpeakRequested(); |
| + break; |
| + case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING: |
| + StopSpeaking(); |
| + break; |
| + case IDC_CONTENT_CONTEXT_LOOK_UP: |
| + delegate_->LookUpInDictionary(); |
| + break; |
| + case IDC_WRITING_DIRECTION_DEFAULT: |
| + case IDC_WRITING_DIRECTION_LTR: |
| + case IDC_WRITING_DIRECTION_RTL: |
| + delegate_->UpdateTextDirection(GetTextDirectionFromCommandId(command_id)); |
| + break; |
| + default: |
| + NOTREACHED(); |
| + } |
| +} |
| + |
| +bool TextServicesContextMenu::IsCommandIdChecked(int command_id) const { |
| + switch (command_id) { |
| + case IDC_WRITING_DIRECTION_DEFAULT: |
| + case IDC_WRITING_DIRECTION_LTR: |
| + case IDC_WRITING_DIRECTION_RTL: |
| + return delegate_->IsTextDirectionChecked( |
| + GetTextDirectionFromCommandId(command_id)); |
| + } |
| + return false; |
| +} |
| + |
| +bool TextServicesContextMenu::IsCommandIdEnabled(int command_id) const { |
| + switch (command_id) { |
| + case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING: |
| + case IDC_CONTENT_CONTEXT_LOOK_UP: |
| + // This is OK because the menu is not shown when it isn't |
| + // appropriate. |
| + return true; |
| + case IDC_WRITING_DIRECTION_DEFAULT: |
| + case IDC_WRITING_DIRECTION_LTR: |
| + case IDC_WRITING_DIRECTION_RTL: |
| + return delegate_->IsTextDirectionEnabled( |
| + GetTextDirectionFromCommandId(command_id)); |
| + case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING: |
| + return IsSpeaking(); |
| + } |
| + |
| + NOTREACHED(); |
| + return false; |
| +} |
| + |
| +} // namespace ui |