Chromium Code Reviews| Index: ui/base/cocoa/text_context_menu.mm |
| diff --git a/ui/base/cocoa/text_context_menu.mm b/ui/base/cocoa/text_context_menu.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b8563b8a55936b4c5ef663c853e3f83c29a1fa1f |
| --- /dev/null |
| +++ b/ui/base/cocoa/text_context_menu.mm |
| @@ -0,0 +1,94 @@ |
| +// 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_context_menu.h" |
| + |
| +#include <utility> |
| + |
| +#import <Cocoa/Cocoa.h> |
| + |
| +#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.
|
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/strings/grit/ui_strings.h" |
| + |
| +// 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.
|
| +@interface NSApplication (UndocumentedSpeechMethods) |
| +- (void)speakString:(NSString*)string; |
| +- (void)stopSpeaking:(id)sender; |
| +- (BOOL)isSpeaking; |
| +@end |
| + |
| +TextContextMenu::TextContextMenu(Delegate* delegate) |
| + : speech_submenu_model_(this), delegate_(delegate) {} |
| + |
| +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.
|
| + [NSApp speakString:base::SysUTF16ToNSString(text)]; |
| +} |
| + |
| +bool TextContextMenu::SupportsSpeech() { |
| + return [NSApp respondsToSelector:@selector(speakString:)] && |
| + [NSApp respondsToSelector:@selector(stopSpeaking:)]; |
| +} |
| + |
| +bool TextContextMenu::IsSpeaking() { |
| + return [NSApp respondsToSelector:@selector(isSpeaking)] && [NSApp isSpeaking]; |
| +} |
| + |
| +void TextContextMenu::StopSpeaking() { |
| + if ([NSApp respondsToSelector:@selector(stopSpeaking:)]) |
| + [NSApp stopSpeaking:nil]; |
| +} |
| + |
| +void TextContextMenu::AppendToContextMenu(ui::SimpleMenuModel* model) { |
| + if (SupportsSpeech()) { |
| + model->AddSeparator(ui::NORMAL_SEPARATOR); |
| + speech_submenu_model_.AddItemWithStringId( |
| + IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING, |
| + IDS_SPEECH_START_SPEAKING_MAC); |
| + speech_submenu_model_.AddItemWithStringId( |
| + IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING, IDS_SPEECH_STOP_SPEAKING_MAC); |
| + model->AddSubMenu(IDC_CONTENT_CONTEXT_SPEECH_MENU, |
| + l10n_util::GetStringUTF16(IDS_SPEECH_MAC), |
| + &speech_submenu_model_); |
| + } |
| +} |
| + |
| +void TextContextMenu::ExecuteCommand(int command_id, int event_flags) { |
| + switch (command_id) { |
| + case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING: |
| + delegate_->StartSpeaking(); |
| + break; |
| + case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING: |
| + StopSpeaking(); |
| + break; |
| + |
| + default: |
|
tapted
2016/07/22 03:06:42
default not needed
spqchan
2016/12/12 19:32:27
Done.
|
| + break; |
| + } |
| +} |
| + |
| +bool TextContextMenu::IsCommandIdChecked(int command_id) const { |
| + return false; |
| +} |
| + |
| +bool TextContextMenu::IsCommandIdEnabled(int command_id) const { |
| + switch (command_id) { |
| + case IDC_CONTENT_CONTEXT_SPEECH_START_SPEAKING: |
| + // This is OK because the menu is not shown when it isn't |
| + // appropriate. |
| + return true; |
| + case IDC_CONTENT_CONTEXT_SPEECH_STOP_SPEAKING: |
| + return IsSpeaking(); |
| + |
| + default: |
|
tapted
2016/07/22 03:06:42
default case not needed
spqchan
2016/12/12 19:32:27
Done.
|
| + return false; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +bool TextContextMenu::GetAcceleratorForCommandId(int command_id, |
| + ui::Accelerator* accelerator) { |
| + return false; |
| +} |