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/views/controls/views_text_services_context_menu_mac.h" | |
| 6 | |
| 7 #import <Cocoa/Cocoa.h> | |
| 8 | |
| 9 #include "ui/base/ime/text_input_client.h" | |
| 10 #include "ui/base/models/simple_menu_model.h" | |
| 11 #include "ui/gfx/decorated_text.h" | |
| 12 #include "ui/gfx/decorated_text_mac.h" | |
| 13 #include "ui/views/view.h" | |
| 14 #include "ui/views/widget/widget.h" | |
| 15 #include "ui/views/word_lookup_client.h" | |
| 16 | |
| 17 namespace views { | |
| 18 | |
| 19 // static | |
| 20 ViewsTextServicesContextMenu* ViewsTextServicesContextMenu::Create( | |
| 21 ui::TextInputClient* text_client, | |
| 22 WordLookupClient* lookup_client, | |
| 23 ui::SimpleMenuModel* menu, | |
| 24 View* view) { | |
| 25 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.
| |
| 26 view); | |
| 27 } | |
| 28 | |
| 29 ViewsTextServicesContextMenuMac::ViewsTextServicesContextMenuMac( | |
| 30 ui::TextInputClient* text_client, | |
| 31 WordLookupClient* lookup_client, | |
| 32 ui::SimpleMenuModel* menu, | |
| 33 View* view) | |
| 34 : text_client_(text_client), | |
| 35 lookup_client_(lookup_client), | |
| 36 menu_(this), | |
| 37 view_(view) { | |
| 38 menu_.AppendToContextMenu(menu); | |
| 39 menu_.AppendPlatformEditableItems(menu); | |
| 40 selection_text_ = GetSelectedText(); | |
| 41 } | |
| 42 | |
| 43 ViewsTextServicesContextMenuMac::~ViewsTextServicesContextMenuMac() {} | |
| 44 | |
| 45 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.
| |
| 46 return selection_text_ != GetSelectedText(); | |
| 47 } | |
| 48 | |
| 49 bool ViewsTextServicesContextMenuMac::IsTextServicesCommandId( | |
| 50 int command_id) const { | |
| 51 return menu_.IsTextServicesCommandId(command_id); | |
| 52 } | |
| 53 | |
| 54 void ViewsTextServicesContextMenuMac::ExecuteCommand(int command_id, | |
| 55 int event_flags) { | |
| 56 menu_.ExecuteCommand(command_id, event_flags); | |
|
tapted
2016/12/13 05:11:24
maybe DCHECK(IsTextServicesCommandId(..))?
spqchan
2016/12/15 23:29:02
Done.
| |
| 57 } | |
| 58 | |
| 59 bool ViewsTextServicesContextMenuMac::IsCommandIdChecked(int command_id) const { | |
| 60 return menu_.IsCommandIdChecked(command_id); | |
| 61 } | |
| 62 | |
| 63 bool ViewsTextServicesContextMenuMac::IsCommandIdEnabled(int command_id) const { | |
| 64 return menu_.IsCommandIdEnabled(command_id); | |
| 65 } | |
| 66 | |
| 67 base::string16 ViewsTextServicesContextMenuMac::GetSelectedText() const { | |
| 68 gfx::Range range; | |
| 69 base::string16 text; | |
| 70 text_client_->GetSelectionRange(&range); | |
| 71 text_client_->GetTextFromRange(range, &text); | |
| 72 return text; | |
| 73 } | |
| 74 | |
| 75 void ViewsTextServicesContextMenuMac::OnSpeakRequested() { | |
| 76 menu_.SpeakText(GetSelectedText()); | |
| 77 } | |
| 78 | |
| 79 bool ViewsTextServicesContextMenuMac::IsLookUpAvailable() const { | |
| 80 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.
| |
| 81 text_client_->GetSelectionRange(&range); | |
| 82 return !range.is_empty(); | |
| 83 } | |
| 84 | |
| 85 void ViewsTextServicesContextMenuMac::LookUpInDictionary() { | |
| 86 gfx::Point baselinePoint; | |
|
tapted
2016/12/13 05:11:24
nit: baseline_point
spqchan
2016/12/15 23:29:02
Done.
| |
| 87 gfx::DecoratedText text; | |
| 88 lookup_client_->GetDecoratedTextFromSelection(&text, &baselinePoint); | |
| 89 | |
| 90 Widget* widget = view_->GetWidget(); | |
| 91 gfx::NativeView view = widget->GetNativeView(); | |
| 92 views::View::ConvertPointToTarget(view_, widget->GetRootView(), | |
| 93 &baselinePoint); | |
| 94 | |
| 95 NSPoint lookUpPoint = NSMakePoint(baselinePoint.x(), | |
| 96 NSHeight([view frame]) - baselinePoint.y()); | |
| 97 [view showDefinitionForAttributedString: | |
| 98 gfx::GetAttributedStringFromDecoratedText(text) | |
| 99 atPoint:lookUpPoint]; | |
| 100 } | |
| 101 | |
| 102 bool ViewsTextServicesContextMenuMac::IsWritingDirectionEnabled( | |
| 103 ui::WritingDirection direction) const { | |
| 104 return direction != ui::WritingDirection::DEFAULT; | |
| 105 } | |
| 106 | |
| 107 bool ViewsTextServicesContextMenuMac::IsWritingDirectionChecked( | |
| 108 ui::WritingDirection direction) const { | |
| 109 switch (direction) { | |
| 110 case ui::WritingDirection::DEFAULT: | |
| 111 return false; | |
| 112 case ui::WritingDirection::RTL: | |
| 113 return text_client_->GetTextDirection() == base::i18n::RIGHT_TO_LEFT; | |
| 114 case ui::WritingDirection::LTR: | |
| 115 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.
| |
| 116 } | |
| 117 } | |
| 118 | |
| 119 void ViewsTextServicesContextMenuMac::UpdateTextDirection( | |
| 120 ui::WritingDirection direction) { | |
| 121 if (direction == ui::WritingDirection::DEFAULT) | |
|
tapted
2016/12/13 05:11:24
DCHECK_NE
spqchan
2016/12/15 23:29:02
Done.
| |
| 122 NOTREACHED(); | |
| 123 | |
| 124 base::i18n::TextDirection text_direction = | |
| 125 direction == ui::WritingDirection::LTR ? base::i18n::LEFT_TO_RIGHT | |
| 126 : base::i18n::RIGHT_TO_LEFT; | |
| 127 text_client_->ChangeTextDirectionAndLayoutAlignment(text_direction); | |
| 128 } | |
| 129 | |
| 130 } // namespace views | |
| OLD | NEW |