Chromium Code Reviews| 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_context_menu.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #import <Cocoa/Cocoa.h> | |
| 10 | |
| 11 #include "chrome/app/chrome_command_ids.h" | |
|
tapted
2016/07/22 03:06:42
we can't add a chrome dependency here, but I don't
spqchan
2016/12/12 19:32:27
Done.
| |
| 12 #include "ui/base/l10n/l10n_util.h" | |
| 13 #include "ui/strings/grit/ui_strings.h" | |
| 14 | |
| 15 // These are not documented, so use only after checking -respondsToSelector:. | |
|
tapted
2016/07/22 03:06:42
huh.. we should probably use the public API, Speak
spqchan
2016/12/12 19:32:27
Done.
| |
| 16 @interface NSApplication (UndocumentedSpeechMethods) | |
| 17 - (void)speakString:(NSString*)string; | |
| 18 - (void)stopSpeaking:(id)sender; | |
| 19 - (BOOL)isSpeaking; | |
| 20 @end | |
| 21 | |
| 22 TextContextMenu::TextContextMenu(Delegate* delegate) | |
| 23 : speech_submenu_model_(this), delegate_(delegate) {} | |
| 24 | |
| 25 void TextContextMenu::SpeakText(const base::string16& text) { | |
|
tapted
2016/07/22 03:06:42
(ensure function order matches header
spqchan
2016/12/12 19:32:27
Done.
| |
| 26 [NSApp speakString:base::SysUTF16ToNSString(text)]; | |
| 27 } | |
| 28 | |
| 29 bool TextContextMenu::SupportsSpeech() { | |
| 30 return [NSApp respondsToSelector:@selector(speakString:)] && | |
| 31 [NSApp respondsToSelector:@selector(stopSpeaking:)]; | |
| 32 } | |
| 33 | |
| 34 bool TextContextMenu::IsSpeaking() { | |
| 35 return [NSApp respondsToSelector:@selector(isSpeaking)] && [NSApp isSpeaking]; | |
| 36 } | |
| 37 | |
| 38 void TextContextMenu::StopSpeaking() { | |
| 39 if ([NSApp respondsToSelector:@selector(stopSpeaking:)]) | |
| 40 [NSApp stopSpeaking:nil]; | |
| 41 } | |
| 42 | |
| 43 void TextContextMenu::AppendToContextMenu(ui::SimpleMenuModel* model) { | |
| 44 if (SupportsSpeech()) { | |
| 45 model->AddSeparator(ui::NORMAL_SEPARATOR); | |
| 46 speech_submenu_model_.AddItemWithStringId( | |
| 47 IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING, | |
| 48 IDS_SPEECH_START_SPEAKING_MAC); | |
| 49 speech_submenu_model_.AddItemWithStringId( | |
| 50 IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING, IDS_SPEECH_STOP_SPEAKING_MAC); | |
| 51 model->AddSubMenu(IDC_CONTENT_CONTEXT_SPEECH_MENU, | |
| 52 l10n_util::GetStringUTF16(IDS_SPEECH_MAC), | |
| 53 &speech_submenu_model_); | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 void TextContextMenu::ExecuteCommand(int command_id, int event_flags) { | |
| 58 switch (command_id) { | |
| 59 case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING: | |
| 60 delegate_->StartSpeaking(); | |
| 61 break; | |
| 62 case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING: | |
| 63 StopSpeaking(); | |
| 64 break; | |
| 65 | |
| 66 default: | |
|
tapted
2016/07/22 03:06:42
default not needed
spqchan
2016/12/12 19:32:27
Done.
| |
| 67 break; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 bool TextContextMenu::IsCommandIdChecked(int command_id) const { | |
| 72 return false; | |
| 73 } | |
| 74 | |
| 75 bool TextContextMenu::IsCommandIdEnabled(int command_id) const { | |
| 76 switch (command_id) { | |
| 77 case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING: | |
| 78 // This is OK because the menu is not shown when it isn't | |
| 79 // appropriate. | |
| 80 return true; | |
| 81 case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING: | |
| 82 return IsSpeaking(); | |
| 83 | |
| 84 default: | |
|
tapted
2016/07/22 03:06:42
default case not needed
spqchan
2016/12/12 19:32:27
Done.
| |
| 85 return false; | |
| 86 } | |
| 87 | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 bool TextContextMenu::GetAcceleratorForCommandId(int command_id, | |
| 92 ui::Accelerator* accelerator) { | |
| 93 return false; | |
| 94 } | |
| OLD | NEW |