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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include "ui/base/cocoa/text_services_context_menu.h"
6
7 #include <utility>
8
9 #include <ApplicationServices/ApplicationServices.h>
10 #include <CoreAudio/CoreAudio.h>
11
12 #include "base/strings/sys_string_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/strings/grit/ui_strings.h"
16
17 namespace {
18
19 // The speech channel used for speaking.
20 SpeechChannel speechChannel;
21
22 // The menu index of the look up item.
23 int kLookUpMenuIndex = 0;
24
25 // Returns the TextDirection associated associated with the given
26 // BiDi |command_id|.
27 base::i18n::TextDirection GetTextDirectionFromCommandId(int command_id) {
28 switch (command_id) {
29 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT:
30 return base::i18n::TextDirection::UNKNOWN_DIRECTION;
31 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR:
32 return base::i18n::TextDirection::LEFT_TO_RIGHT;
33 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL:
34 return base::i18n::TextDirection::RIGHT_TO_LEFT;
35 default:
36 NOTREACHED();
37 return base::i18n::TextDirection::UNKNOWN_DIRECTION;
38 }
39 }
40 }
41
42 namespace ui {
43
44 //////////////////////////////////////////////////////////////////
45 // TextServicesContextMenu, public:
46
47 TextServicesContextMenu::TextServicesContextMenu(Delegate* delegate)
48 : speech_submenu_model_(this),
49 bidi_submenu_model_(this),
50 delegate_(delegate) {
51 DCHECK(delegate);
52
53 // Add items to the speech submenu.
54 speech_submenu_model_.AddItemWithStringId(IDS_SPEECH_START_SPEAKING_MAC,
55 IDS_SPEECH_START_SPEAKING_MAC);
56 speech_submenu_model_.AddItemWithStringId(IDS_SPEECH_STOP_SPEAKING_MAC,
57 IDS_SPEECH_STOP_SPEAKING_MAC);
58
59 // Add items to the BiDi submenu.
60 bidi_submenu_model_.AddCheckItem(
61 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT,
62 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT));
63 bidi_submenu_model_.AddCheckItem(
64 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR,
65 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR));
66 bidi_submenu_model_.AddCheckItem(
67 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL,
68 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL));
69 }
70
71 void TextServicesContextMenu::SpeakText(const base::string16& text) {
72 if (IsSpeaking())
73 StopSpeaking();
74
75 NewSpeechChannel(nullptr, &speechChannel);
76 SpeakCFString(speechChannel, SysUTF16ToCFStringRef(text), nullptr);
77 }
78
79 void TextServicesContextMenu::StopSpeaking() {
80 StopSpeechAt(speechChannel, kImmediate);
81 DisposeSpeechChannel(speechChannel);
82 }
83
84 bool TextServicesContextMenu::IsSpeaking() {
85 return SpeechBusy();
86 }
87
88 void TextServicesContextMenu::AppendToContextMenu(SimpleMenuModel* model) {
89 base::string16 printable_selection_text = delegate_->GetSelectedText();
90 if (printable_selection_text.empty())
91 return;
92
93 if (delegate_->IsLookUpAvailable()) {
94 model->InsertItemAt(kLookUpMenuIndex, IDS_CONTENT_CONTEXT_LOOK_UP,
95 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_LOOK_UP,
96 printable_selection_text));
97 model->InsertSeparatorAt(kLookUpMenuIndex + 1, NORMAL_SEPARATOR);
98 }
99
100 model->AddSeparator(NORMAL_SEPARATOR);
101
102 model->AddSubMenu(IDS_SPEECH_MAC, l10n_util::GetStringUTF16(IDS_SPEECH_MAC),
103 &speech_submenu_model_);
104 model->AddSubMenu(
105 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_MENU,
106 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_MENU),
107 &bidi_submenu_model_);
108 }
109
110 bool TextServicesContextMenu::IsTextServicesCommandId(int command_id) const {
111 switch (command_id) {
112 case IDS_SPEECH_START_SPEAKING_MAC:
113 case IDS_SPEECH_STOP_SPEAKING_MAC:
114 case IDS_CONTENT_CONTEXT_LOOK_UP:
115 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT:
116 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR:
117 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL:
118 return true;
119 }
120
121 return false;
122 }
123
124 //////////////////////////////////////////////////////////////////
125 // TextServicesContextMenu::SimpleMenuModel::Delegate:
126
127 void TextServicesContextMenu::ExecuteCommand(int command_id, int event_flags) {
128 switch (command_id) {
129 case IDS_SPEECH_START_SPEAKING_MAC:
130 delegate_->OnSpeakRequested();
131 break;
132 case IDS_SPEECH_STOP_SPEAKING_MAC:
133 StopSpeaking();
134 break;
135 case IDS_CONTENT_CONTEXT_LOOK_UP:
136 delegate_->LookUpInDictionary();
137 break;
138 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT:
139 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR:
140 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL:
141 delegate_->UpdateTextDirection(GetTextDirectionFromCommandId(command_id));
142 break;
143 default:
144 NOTREACHED();
145 }
146 }
147
148 bool TextServicesContextMenu::IsCommandIdChecked(int command_id) const {
149 switch (command_id) {
150 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT:
151 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR:
152 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL:
153 return delegate_->IsTextDirectionChecked(
154 GetTextDirectionFromCommandId(command_id));
155 }
156 return false;
157 }
158
159 bool TextServicesContextMenu::IsCommandIdEnabled(int command_id) const {
160 switch (command_id) {
161 case IDS_SPEECH_START_SPEAKING_MAC:
162 case IDS_CONTENT_CONTEXT_LOOK_UP:
163 // This is OK because the menu is not shown when it isn't
164 // appropriate.
165 return true;
166 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT:
167 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR:
168 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL:
169 return delegate_->IsTextDirectionEnabled(
170 GetTextDirectionFromCommandId(command_id));
171 case IDS_SPEECH_STOP_SPEAKING_MAC:
172 return IsSpeaking();
173 }
174
175 NOTREACHED();
176 return false;
177 }
178
179 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698