OLD | NEW |
---|---|
(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 #import <Cocoa/Cocoa.h> | |
tapted
2016/12/13 05:11:23
We might not need this now that we're not calling
spqchan
2016/12/15 23:29:01
Done.
| |
10 | |
11 #include "base/strings/sys_string_conversions.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "ui/base/l10n/l10n_util.h" | |
14 #include "ui/strings/grit/ui_strings.h" | |
15 | |
16 namespace ui { | |
17 | |
18 SpeechChannel TextServicesContextMenu::speechChannel; | |
tapted
2016/12/13 05:11:23
i.e.
namespace {
SpeechChannel g_speech_channel;
spqchan
2016/12/15 23:29:01
Done.
| |
19 | |
20 ////////////////////////////////////////////////////////////////// | |
21 // TextServicesContextMenu, public: | |
22 | |
23 TextServicesContextMenu::TextServicesContextMenu(Delegate* delegate) | |
24 : speech_submenu_model_(this), | |
25 bidi_submenu_model_(this), | |
26 delegate_(delegate) { | |
27 DCHECK(delegate); | |
28 } | |
29 | |
30 void TextServicesContextMenu::SpeakText(const base::string16& text) { | |
31 if (IsSpeaking()) | |
32 StopSpeaking(); | |
33 | |
34 NewSpeechChannel(NULL, &speechChannel); | |
tapted
2016/12/13 05:11:23
NULL -> nullptr. Same below.
spqchan
2016/12/15 23:29:01
Done.
| |
35 SpeakCFString(speechChannel, SysUTF16ToCFStringRef(text), NULL); | |
36 } | |
37 | |
38 void TextServicesContextMenu::StopSpeaking() { | |
39 StopSpeechAt(speechChannel, kImmediate); | |
40 DisposeSpeechChannel(speechChannel); | |
41 } | |
42 | |
43 bool TextServicesContextMenu::IsSpeaking() { | |
44 return SpeechBusy(); | |
45 } | |
46 | |
47 void TextServicesContextMenu::AppendToContextMenu(ui::SimpleMenuModel* model) { | |
tapted
2016/12/13 05:11:23
nit: no ui::
spqchan
2016/12/15 23:29:01
Done.
| |
48 base::string16 printable_selection_text = delegate_->GetSelectedText(); | |
49 if (printable_selection_text.empty()) | |
50 return; | |
51 | |
52 if (delegate_->IsLookUpAvailable()) { | |
53 model->InsertItemAt(0, IDS_CONTENT_CONTEXT_LOOK_UP, | |
tapted
2016/12/13 05:11:23
I guess this `0` was previously the calculated ind
spqchan
2016/12/15 23:29:01
Yes, we need it so that the LookUp item actually a
| |
54 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_LOOK_UP, | |
55 printable_selection_text)); | |
56 } | |
57 | |
58 model->AddSeparator(ui::NORMAL_SEPARATOR); | |
tapted
2016/12/13 05:11:23
nit: no ui::
spqchan
2016/12/15 23:29:01
Done.
| |
59 speech_submenu_model_.AddItemWithStringId(IDS_SPEECH_START_SPEAKING_MAC, | |
tapted
2016/12/13 05:11:23
Can we do these `AddItemWithStringId` calls on the
spqchan
2016/12/15 23:29:01
Done
| |
60 IDS_SPEECH_START_SPEAKING_MAC); | |
61 speech_submenu_model_.AddItemWithStringId(IDS_SPEECH_STOP_SPEAKING_MAC, | |
62 IDS_SPEECH_STOP_SPEAKING_MAC); | |
63 model->AddSubMenu(IDS_SPEECH_MAC, l10n_util::GetStringUTF16(IDS_SPEECH_MAC), | |
64 &speech_submenu_model_); | |
65 } | |
66 | |
67 void TextServicesContextMenu::AppendPlatformEditableItems( | |
68 ui::SimpleMenuModel* model) { | |
tapted
2016/12/13 05:11:23
nit: no ui::
spqchan
2016/12/15 23:29:01
Done.
| |
69 bidi_submenu_model_.AddCheckItem( | |
70 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT, | |
tapted
2016/12/13 05:11:23
Same here - I think these calls can be made in the
spqchan
2016/12/15 23:29:01
Done.
| |
71 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT)); | |
72 bidi_submenu_model_.AddCheckItem( | |
73 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR, | |
74 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR)); | |
75 bidi_submenu_model_.AddCheckItem( | |
76 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL, | |
77 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL)); | |
78 | |
79 model->AddSubMenu( | |
80 IDS_CONTENT_CONTEXT_WRITING_DIRECTION_MENU, | |
81 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_WRITING_DIRECTION_MENU), | |
82 &bidi_submenu_model_); | |
83 } | |
84 | |
85 bool TextServicesContextMenu::IsTextServicesCommandId(int command_id) const { | |
86 switch (command_id) { | |
87 case IDS_SPEECH_START_SPEAKING_MAC: | |
88 case IDS_SPEECH_STOP_SPEAKING_MAC: | |
89 case IDS_CONTENT_CONTEXT_LOOK_UP: | |
tapted
2016/12/13 05:11:23
For the others it might be OK, but do we risk a co
spqchan
2016/12/15 23:29:01
That's true. AFAIK. there's no conflict now, but I
tapted
2016/12/16 07:01:19
So one way to avoid it could be to use MenuModel r
| |
90 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT: | |
91 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR: | |
92 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL: | |
93 return true; | |
94 } | |
95 | |
96 return false; | |
97 } | |
98 | |
99 ////////////////////////////////////////////////////////////////// | |
100 // TextServicesContextMenu::SimpleMenuModel::Delegate: | |
101 | |
102 void TextServicesContextMenu::ExecuteCommand(int command_id, int event_flags) { | |
103 switch (command_id) { | |
104 case IDS_SPEECH_START_SPEAKING_MAC: | |
105 delegate_->OnSpeakRequested(); | |
106 break; | |
107 case IDS_SPEECH_STOP_SPEAKING_MAC: | |
108 StopSpeaking(); | |
109 break; | |
110 case IDS_CONTENT_CONTEXT_LOOK_UP: | |
111 delegate_->LookUpInDictionary(); | |
112 break; | |
113 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT: | |
114 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR: | |
115 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL: | |
116 delegate_->UpdateTextDirection( | |
117 GetWritingDirectionFromCommandId(command_id)); | |
118 break; | |
119 } | |
tapted
2016/12/13 05:11:23
default: NOTREACHED()?
spqchan
2016/12/15 23:29:01
Done.
| |
120 } | |
121 | |
122 bool TextServicesContextMenu::IsCommandIdChecked(int command_id) const { | |
123 switch (command_id) { | |
124 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT: | |
125 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR: | |
126 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL: | |
127 return delegate_->IsWritingDirectionChecked( | |
128 GetWritingDirectionFromCommandId(command_id)); | |
129 } | |
130 return false; | |
131 } | |
132 | |
133 bool TextServicesContextMenu::IsCommandIdEnabled(int command_id) const { | |
134 switch (command_id) { | |
135 case IDS_SPEECH_START_SPEAKING_MAC: | |
136 case IDS_CONTENT_CONTEXT_LOOK_UP: | |
137 // This is OK because the menu is not shown when it isn't | |
138 // appropriate. | |
139 return true; | |
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_->IsWritingDirectionEnabled( | |
144 GetWritingDirectionFromCommandId(command_id)); | |
145 case IDS_SPEECH_STOP_SPEAKING_MAC: | |
146 return IsSpeaking(); | |
147 } | |
148 | |
149 return false; | |
tapted
2016/12/13 05:11:23
NOTREACHED(); before this?
spqchan
2016/12/15 23:29:01
Done.
| |
150 } | |
151 | |
152 bool TextServicesContextMenu::GetAcceleratorForCommandId( | |
153 int command_id, | |
154 ui::Accelerator* accelerator) const { | |
155 return false; | |
tapted
2016/12/13 05:11:23
this matches the default implementation, so I thin
spqchan
2016/12/15 23:29:01
Done.
| |
156 } | |
157 | |
158 ////////////////////////////////////////////////////////////////// | |
159 // TextServicesContextMenu, private: | |
160 | |
161 WritingDirection TextServicesContextMenu::GetWritingDirectionFromCommandId( | |
162 int command_id) const { | |
163 switch (command_id) { | |
164 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_DEFAULT: | |
165 return WritingDirection::DEFAULT; | |
166 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_LTR: | |
167 return WritingDirection::LTR; | |
168 case IDS_CONTENT_CONTEXT_WRITING_DIRECTION_RTL: | |
169 return WritingDirection::RTL; | |
170 default: | |
171 NOTREACHED(); | |
172 return WritingDirection::DEFAULT; | |
173 } | |
174 } | |
175 | |
176 } // namespace ui | |
OLD | NEW |