Chromium Code Reviews| Index: ui/views/controls/views_text_services_context_menu_mac.mm |
| diff --git a/ui/views/controls/views_text_services_context_menu_mac.mm b/ui/views/controls/views_text_services_context_menu_mac.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d96b533cfc5d0daf585abcf037b49cb985d03ff7 |
| --- /dev/null |
| +++ b/ui/views/controls/views_text_services_context_menu_mac.mm |
| @@ -0,0 +1,130 @@ |
| +// 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/views/controls/views_text_services_context_menu_mac.h" |
| + |
| +#import <Cocoa/Cocoa.h> |
| + |
| +#include "ui/base/ime/text_input_client.h" |
| +#include "ui/base/models/simple_menu_model.h" |
| +#include "ui/gfx/decorated_text.h" |
| +#include "ui/gfx/decorated_text_mac.h" |
| +#include "ui/views/view.h" |
| +#include "ui/views/widget/widget.h" |
| +#include "ui/views/word_lookup_client.h" |
| + |
| +namespace views { |
| + |
| +// static |
| +ViewsTextServicesContextMenu* ViewsTextServicesContextMenu::Create( |
| + ui::TextInputClient* text_client, |
| + WordLookupClient* lookup_client, |
| + ui::SimpleMenuModel* menu, |
| + View* view) { |
| + return new ViewsTextServicesContextMenuMac(text_client, lookup_client, menu, |
|
tapted
2016/12/13 05:11:24
return base::MakeUnique<ViewsTextServicesContextMe
spqchan
2016/12/15 23:29:02
Done.
|
| + view); |
| +} |
| + |
| +ViewsTextServicesContextMenuMac::ViewsTextServicesContextMenuMac( |
| + ui::TextInputClient* text_client, |
| + WordLookupClient* lookup_client, |
| + ui::SimpleMenuModel* menu, |
| + View* view) |
| + : text_client_(text_client), |
| + lookup_client_(lookup_client), |
| + menu_(this), |
| + view_(view) { |
| + menu_.AppendToContextMenu(menu); |
| + menu_.AppendPlatformEditableItems(menu); |
| + selection_text_ = GetSelectedText(); |
| +} |
| + |
| +ViewsTextServicesContextMenuMac::~ViewsTextServicesContextMenuMac() {} |
| + |
| +bool ViewsTextServicesContextMenuMac::ShouldUpdateMenu() const { |
|
tapted
2016/12/13 05:11:24
You might be able to remove this delegate method (
spqchan
2016/12/15 23:29:02
Done.
|
| + return selection_text_ != GetSelectedText(); |
| +} |
| + |
| +bool ViewsTextServicesContextMenuMac::IsTextServicesCommandId( |
| + int command_id) const { |
| + return menu_.IsTextServicesCommandId(command_id); |
| +} |
| + |
| +void ViewsTextServicesContextMenuMac::ExecuteCommand(int command_id, |
| + int event_flags) { |
| + menu_.ExecuteCommand(command_id, event_flags); |
|
tapted
2016/12/13 05:11:24
maybe DCHECK(IsTextServicesCommandId(..))?
spqchan
2016/12/15 23:29:02
Done.
|
| +} |
| + |
| +bool ViewsTextServicesContextMenuMac::IsCommandIdChecked(int command_id) const { |
| + return menu_.IsCommandIdChecked(command_id); |
| +} |
| + |
| +bool ViewsTextServicesContextMenuMac::IsCommandIdEnabled(int command_id) const { |
| + return menu_.IsCommandIdEnabled(command_id); |
| +} |
| + |
| +base::string16 ViewsTextServicesContextMenuMac::GetSelectedText() const { |
| + gfx::Range range; |
| + base::string16 text; |
| + text_client_->GetSelectionRange(&range); |
| + text_client_->GetTextFromRange(range, &text); |
| + return text; |
| +} |
| + |
| +void ViewsTextServicesContextMenuMac::OnSpeakRequested() { |
| + menu_.SpeakText(GetSelectedText()); |
| +} |
| + |
| +bool ViewsTextServicesContextMenuMac::IsLookUpAvailable() const { |
| + gfx::Range range; |
|
tapted
2016/12/13 05:11:24
If client_ is a TextField, we can now just call so
spqchan
2016/12/15 23:29:02
Done.
|
| + text_client_->GetSelectionRange(&range); |
| + return !range.is_empty(); |
| +} |
| + |
| +void ViewsTextServicesContextMenuMac::LookUpInDictionary() { |
| + gfx::Point baselinePoint; |
|
tapted
2016/12/13 05:11:24
nit: baseline_point
spqchan
2016/12/15 23:29:02
Done.
|
| + gfx::DecoratedText text; |
| + lookup_client_->GetDecoratedTextFromSelection(&text, &baselinePoint); |
| + |
| + Widget* widget = view_->GetWidget(); |
| + gfx::NativeView view = widget->GetNativeView(); |
| + views::View::ConvertPointToTarget(view_, widget->GetRootView(), |
| + &baselinePoint); |
| + |
| + NSPoint lookUpPoint = NSMakePoint(baselinePoint.x(), |
| + NSHeight([view frame]) - baselinePoint.y()); |
| + [view showDefinitionForAttributedString: |
| + gfx::GetAttributedStringFromDecoratedText(text) |
| + atPoint:lookUpPoint]; |
| +} |
| + |
| +bool ViewsTextServicesContextMenuMac::IsWritingDirectionEnabled( |
| + ui::WritingDirection direction) const { |
| + return direction != ui::WritingDirection::DEFAULT; |
| +} |
| + |
| +bool ViewsTextServicesContextMenuMac::IsWritingDirectionChecked( |
| + ui::WritingDirection direction) const { |
| + switch (direction) { |
| + case ui::WritingDirection::DEFAULT: |
| + return false; |
| + case ui::WritingDirection::RTL: |
| + return text_client_->GetTextDirection() == base::i18n::RIGHT_TO_LEFT; |
| + case ui::WritingDirection::LTR: |
| + return text_client_->GetTextDirection() == base::i18n::LEFT_TO_RIGHT; |
|
tapted
2016/12/13 05:11:24
we should add some test coverage for this using vi
spqchan
2016/12/15 23:29:02
I added some test coverage in textfield_unittest.
|
| + } |
| +} |
| + |
| +void ViewsTextServicesContextMenuMac::UpdateTextDirection( |
| + ui::WritingDirection direction) { |
| + if (direction == ui::WritingDirection::DEFAULT) |
|
tapted
2016/12/13 05:11:24
DCHECK_NE
spqchan
2016/12/15 23:29:02
Done.
|
| + NOTREACHED(); |
| + |
| + base::i18n::TextDirection text_direction = |
| + direction == ui::WritingDirection::LTR ? base::i18n::LEFT_TO_RIGHT |
| + : base::i18n::RIGHT_TO_LEFT; |
| + text_client_->ChangeTextDirectionAndLayoutAlignment(text_direction); |
| +} |
| + |
| +} // namespace views |