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..32d1957004a95140a70d4ce0d0b389b10c9de870 |
--- /dev/null |
+++ b/ui/base/cocoa/text_services_context_menu.cc |
@@ -0,0 +1,170 @@ |
+// 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 "ui/base/l10n/l10n_util.h" |
+#include "ui/strings/grit/ui_strings.h" |
+ |
+namespace { |
+ |
+// The speech channel used for speaking. |
+SpeechChannel speechChannel; |
tapted
2016/12/21 11:20:26
speechChannel -> g_speech_channel
spqchan
2016/12/21 22:00:13
Done. Quick question, why the g_?
tapted
2016/12/21 22:13:16
This is to make it clear that we're declaring/refe
spqchan
2017/01/13 20:13:19
Gotcha, thanks!
|
+ |
+// Returns the TextDirection associated associated with the given |
+// BiDi |command_id|. |
+base::i18n::TextDirection GetTextDirectionFromCommandId(int command_id) { |
+ switch (command_id) { |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT: |
+ return base::i18n::TextDirection::UNKNOWN_DIRECTION; |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR: |
+ return base::i18n::TextDirection::LEFT_TO_RIGHT; |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL: |
+ return base::i18n::TextDirection::RIGHT_TO_LEFT; |
+ default: |
+ NOTREACHED(); |
+ return base::i18n::TextDirection::UNKNOWN_DIRECTION; |
+ } |
+} |
+ |
+} // namespace |
+ |
+namespace ui { |
+ |
+////////////////////////////////////////////////////////////////// |
tapted
2016/12/21 11:20:26
nit: I wouldn't worry about this style of comments
spqchan
2016/12/21 22:00:13
Done.
|
+// TextServicesContextMenu, public: |
+ |
+TextServicesContextMenu::TextServicesContextMenu(Delegate* delegate) |
+ : speech_submenu_model_(this), |
+ bidi_submenu_model_(this), |
+ delegate_(delegate) { |
+ DCHECK(delegate); |
+ |
+ // Add items to the speech submenu. |
tapted
2016/12/21 11:20:26
nit: submenu -> submenu model. Same below. Or I th
spqchan
2016/12/21 22:00:13
Done.
|
+ speech_submenu_model_.AddItemWithStringId(IDS_SPEECH_START_SPEAKING_MAC, |
+ IDS_SPEECH_START_SPEAKING_MAC); |
+ speech_submenu_model_.AddItemWithStringId(IDS_SPEECH_STOP_SPEAKING_MAC, |
+ IDS_SPEECH_STOP_SPEAKING_MAC); |
+ |
+ // Add items to the BiDi submenu. |
+ bidi_submenu_model_.AddCheckItem( |
tapted
2016/12/21 11:20:26
AddCheckItemWithStringId ?
spqchan
2016/12/21 22:00:13
Done.
|
+ IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT, |
+ l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT)); |
+ bidi_submenu_model_.AddCheckItem( |
+ IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR, |
+ l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR)); |
+ bidi_submenu_model_.AddCheckItem( |
+ IDS_CONTENT_CONTEXT_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) { |
+ base::string16 printable_selection_text = delegate_->GetSelectedText(); |
+ if (printable_selection_text.empty()) |
tapted
2016/12/21 11:20:26
Hmmm - it looks like we can still speak when there
spqchan
2016/12/21 22:00:13
We still need OnSpeakRequested() because if nothin
tapted
2016/12/21 22:13:15
But the logic here will not add the menu item if n
spqchan
2017/01/13 20:13:19
Doh, I see what you mean. Removed it :)
|
+ return; |
+ |
+ model->AddSeparator(NORMAL_SEPARATOR); |
+ |
+ model->AddSubMenu(IDS_SPEECH_MAC, l10n_util::GetStringUTF16(IDS_SPEECH_MAC), |
tapted
2016/12/21 11:20:26
AddSubMenuWithStringId ?
spqchan
2016/12/21 22:00:13
Done.
|
+ &speech_submenu_model_); |
+} |
+ |
+void TextServicesContextMenu::AppendEditableItems(SimpleMenuModel* model) { |
+ model->AddSubMenu( |
tapted
2016/12/21 11:20:26
AddSubMenuWithStringId ?
spqchan
2016/12/21 22:00:13
Done.
|
+ IDS_CONTENT_CONTEXT_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 IDS_SPEECH_START_SPEAKING_MAC: |
+ case IDS_SPEECH_STOP_SPEAKING_MAC: |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT: |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR: |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL: |
+ return true; |
+ } |
+ |
+ return false; |
+} |
+ |
+////////////////////////////////////////////////////////////////// |
+// TextServicesContextMenu::SimpleMenuModel::Delegate: |
+ |
+void TextServicesContextMenu::ExecuteCommand(int command_id, int event_flags) { |
+ switch (command_id) { |
+ case IDS_SPEECH_START_SPEAKING_MAC: |
+ delegate_->OnSpeakRequested(); |
+ break; |
+ case IDS_SPEECH_STOP_SPEAKING_MAC: |
+ StopSpeaking(); |
+ break; |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT: |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR: |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL: |
+ delegate_->UpdateTextDirection(GetTextDirectionFromCommandId(command_id)); |
+ break; |
+ default: |
+ NOTREACHED(); |
+ } |
+} |
+ |
+bool TextServicesContextMenu::IsCommandIdChecked(int command_id) const { |
+ switch (command_id) { |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT: |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR: |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL: |
+ return delegate_->IsTextDirectionChecked( |
+ GetTextDirectionFromCommandId(command_id)); |
+ case IDS_SPEECH_START_SPEAKING_MAC: |
+ case IDS_SPEECH_STOP_SPEAKING_MAC: |
+ return false; |
+ } |
+ |
+ NOTREACHED(); |
+ return false; |
+} |
+ |
+bool TextServicesContextMenu::IsCommandIdEnabled(int command_id) const { |
+ switch (command_id) { |
+ case IDS_SPEECH_START_SPEAKING_MAC: |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT: |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR: |
+ case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL: |
+ return delegate_->IsTextDirectionEnabled( |
+ GetTextDirectionFromCommandId(command_id)); |
+ case IDS_SPEECH_STOP_SPEAKING_MAC: |
+ return IsSpeaking(); |
+ } |
+ |
+ NOTREACHED(); |
+ return false; |
+} |
+ |
+} // namespace ui |