Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Unified Diff: ui/base/cocoa/text_services_context_menu.cc

Issue 2164483006: [MacViews] Implemented text context menu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix for tapted Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..c43a956418208d172cdfe36910deccd992b3824e
--- /dev/null
+++ b/ui/base/cocoa/text_services_context_menu.cc
@@ -0,0 +1,179 @@
+// 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;
+
+// The menu index of the look up item.
+int kLookUpMenuIndex = 0;
+
+// 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 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(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(
+ 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())
+ return;
+
+ if (delegate_->IsLookUpAvailable()) {
+ model->InsertItemAt(kLookUpMenuIndex, IDS_CONTENT_CONTEXT_LOOK_UP,
+ l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_LOOK_UP,
+ printable_selection_text));
+ model->InsertSeparatorAt(kLookUpMenuIndex + 1, NORMAL_SEPARATOR);
+ }
+
+ model->AddSeparator(NORMAL_SEPARATOR);
+
+ model->AddSubMenu(IDS_SPEECH_MAC, l10n_util::GetStringUTF16(IDS_SPEECH_MAC),
+ &speech_submenu_model_);
+ model->AddSubMenu(
+ 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_LOOK_UP:
+ 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_LOOK_UP:
+ delegate_->LookUpInDictionary();
+ 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));
+ }
+ return false;
+}
+
+bool TextServicesContextMenu::IsCommandIdEnabled(int command_id) const {
+ switch (command_id) {
+ case IDS_SPEECH_START_SPEAKING_MAC:
+ case IDS_CONTENT_CONTEXT_LOOK_UP:
+ // This is OK because the menu is not shown when it isn't
+ // appropriate.
+ return true;
+ 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

Powered by Google App Engine
This is Rietveld 408576698