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

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 3 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;
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!
21
22 // Returns the TextDirection associated associated with the given
23 // BiDi |command_id|.
24 base::i18n::TextDirection GetTextDirectionFromCommandId(int command_id) {
25 switch (command_id) {
26 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT:
27 return base::i18n::TextDirection::UNKNOWN_DIRECTION;
28 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR:
29 return base::i18n::TextDirection::LEFT_TO_RIGHT;
30 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL:
31 return base::i18n::TextDirection::RIGHT_TO_LEFT;
32 default:
33 NOTREACHED();
34 return base::i18n::TextDirection::UNKNOWN_DIRECTION;
35 }
36 }
37
38 } // namespace
39
40 namespace ui {
41
42 //////////////////////////////////////////////////////////////////
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.
43 // TextServicesContextMenu, public:
44
45 TextServicesContextMenu::TextServicesContextMenu(Delegate* delegate)
46 : speech_submenu_model_(this),
47 bidi_submenu_model_(this),
48 delegate_(delegate) {
49 DCHECK(delegate);
50
51 // 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.
52 speech_submenu_model_.AddItemWithStringId(IDS_SPEECH_START_SPEAKING_MAC,
53 IDS_SPEECH_START_SPEAKING_MAC);
54 speech_submenu_model_.AddItemWithStringId(IDS_SPEECH_STOP_SPEAKING_MAC,
55 IDS_SPEECH_STOP_SPEAKING_MAC);
56
57 // Add items to the BiDi submenu.
58 bidi_submenu_model_.AddCheckItem(
tapted 2016/12/21 11:20:26 AddCheckItemWithStringId ?
spqchan 2016/12/21 22:00:13 Done.
59 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT,
60 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT));
61 bidi_submenu_model_.AddCheckItem(
62 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR,
63 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR));
64 bidi_submenu_model_.AddCheckItem(
65 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL,
66 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL));
67 }
68
69 void TextServicesContextMenu::SpeakText(const base::string16& text) {
70 if (IsSpeaking())
71 StopSpeaking();
72
73 NewSpeechChannel(nullptr, &speechChannel);
74 SpeakCFString(speechChannel, SysUTF16ToCFStringRef(text), nullptr);
75 }
76
77 void TextServicesContextMenu::StopSpeaking() {
78 StopSpeechAt(speechChannel, kImmediate);
79 DisposeSpeechChannel(speechChannel);
80 }
81
82 bool TextServicesContextMenu::IsSpeaking() {
83 return SpeechBusy();
84 }
85
86 void TextServicesContextMenu::AppendToContextMenu(SimpleMenuModel* model) {
87 base::string16 printable_selection_text = delegate_->GetSelectedText();
88 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 :)
89 return;
90
91 model->AddSeparator(NORMAL_SEPARATOR);
92
93 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.
94 &speech_submenu_model_);
95 }
96
97 void TextServicesContextMenu::AppendEditableItems(SimpleMenuModel* model) {
98 model->AddSubMenu(
tapted 2016/12/21 11:20:26 AddSubMenuWithStringId ?
spqchan 2016/12/21 22:00:13 Done.
99 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_MENU,
100 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_MENU),
101 &bidi_submenu_model_);
102 }
103
104 bool TextServicesContextMenu::IsTextServicesCommandId(int command_id) const {
105 switch (command_id) {
106 case IDS_SPEECH_START_SPEAKING_MAC:
107 case IDS_SPEECH_STOP_SPEAKING_MAC:
108 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT:
109 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR:
110 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL:
111 return true;
112 }
113
114 return false;
115 }
116
117 //////////////////////////////////////////////////////////////////
118 // TextServicesContextMenu::SimpleMenuModel::Delegate:
119
120 void TextServicesContextMenu::ExecuteCommand(int command_id, int event_flags) {
121 switch (command_id) {
122 case IDS_SPEECH_START_SPEAKING_MAC:
123 delegate_->OnSpeakRequested();
124 break;
125 case IDS_SPEECH_STOP_SPEAKING_MAC:
126 StopSpeaking();
127 break;
128 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT:
129 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR:
130 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL:
131 delegate_->UpdateTextDirection(GetTextDirectionFromCommandId(command_id));
132 break;
133 default:
134 NOTREACHED();
135 }
136 }
137
138 bool TextServicesContextMenu::IsCommandIdChecked(int command_id) const {
139 switch (command_id) {
140 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT:
141 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR:
142 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL:
143 return delegate_->IsTextDirectionChecked(
144 GetTextDirectionFromCommandId(command_id));
145 case IDS_SPEECH_START_SPEAKING_MAC:
146 case IDS_SPEECH_STOP_SPEAKING_MAC:
147 return false;
148 }
149
150 NOTREACHED();
151 return false;
152 }
153
154 bool TextServicesContextMenu::IsCommandIdEnabled(int command_id) const {
155 switch (command_id) {
156 case IDS_SPEECH_START_SPEAKING_MAC:
157 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT:
158 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR:
159 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL:
160 return delegate_->IsTextDirectionEnabled(
161 GetTextDirectionFromCommandId(command_id));
162 case IDS_SPEECH_STOP_SPEAKING_MAC:
163 return IsSpeaking();
164 }
165
166 NOTREACHED();
167 return false;
168 }
169
170 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698