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 #import <Cocoa/Cocoa.h> | |
tapted
2016/12/21 11:20:27
nit: blank line before
spqchan
2016/12/21 22:00:14
Done.
| |
5 | |
6 #include "base/memory/ptr_util.h" | |
7 #include "ui/base/cocoa/text_services_context_menu.h" | |
8 #include "ui/base/l10n/l10n_util.h" | |
9 #include "ui/base/models/simple_menu_model.h" | |
10 #include "ui/gfx/decorated_text.h" | |
11 #include "ui/gfx/decorated_text_mac.h" | |
tapted
2016/12/21 11:20:27
nit: import
spqchan
2016/12/21 22:00:14
Done.
| |
12 #include "ui/strings/grit/ui_strings.h" | |
13 #include "ui/views/controls/textfield/textfield.h" | |
14 #include "ui/views/controls/views_text_services_context_menu.h" | |
tapted
2016/12/21 11:20:27
I think you can put this one first (before Cocoa)
spqchan
2016/12/21 22:00:14
Done.
| |
15 #include "ui/views/view.h" | |
16 #include "ui/views/widget/widget.h" | |
17 | |
18 namespace views { | |
19 | |
20 namespace { | |
21 | |
22 // The menu index for "Look Up". | |
23 int kLookupMenuIndex = 0; | |
tapted
2016/12/21 11:20:27
constexpr
spqchan
2016/12/21 22:00:14
Done.
| |
24 | |
25 // This class serves as a bridge to TextServicesContextMenu to add and handle | |
26 // text service items in the context menu. The items include Speech, Look Up | |
27 // and BiDi. | |
28 class ViewsTextServicesContextMenuMac | |
29 : public ViewsTextServicesContextMenu, | |
30 public ui::TextServicesContextMenu::Delegate { | |
31 public: | |
32 ViewsTextServicesContextMenuMac(ui::SimpleMenuModel* menu, Textfield* client); | |
33 ~ViewsTextServicesContextMenuMac() override; | |
34 | |
35 // Handler for the "Look Up" menu item. | |
36 void LookUpInDictionary(); | |
37 | |
38 // ViewTextServiceContextMenu: | |
tapted
2016/12/21 11:20:27
Service -> Services
spqchan
2016/12/21 22:00:14
Done.
| |
39 bool HandlesCommandId(int command_id) const override; | |
40 void ExecuteCommand(int command_id, int event_flags) override; | |
tapted
2016/12/21 11:20:27
nit: reorder
spqchan
2016/12/21 22:00:14
Done.
| |
41 bool IsCommandIdChecked(int command_id) const override; | |
42 bool IsCommandIdEnabled(int command_id) const override; | |
43 | |
44 // TextServicesContextMenu::Delegate: | |
45 base::string16 GetSelectedText() const override; | |
46 void OnSpeakRequested() override; | |
47 bool IsTextDirectionEnabled( | |
48 base::i18n::TextDirection direction) const override; | |
49 bool IsTextDirectionChecked( | |
50 base::i18n::TextDirection direction) const override; | |
51 void UpdateTextDirection(base::i18n::TextDirection direction) override; | |
52 | |
53 private: | |
54 // Appends and handles the text service menu. | |
55 ui::TextServicesContextMenu menu_; | |
56 | |
57 // The view associated with the menu. Weak. | |
tapted
2016/12/21 11:20:27
add "Owns |this|."
spqchan
2016/12/21 22:00:14
Done.
| |
58 Textfield* client_; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(ViewsTextServicesContextMenuMac); | |
61 }; | |
62 | |
63 ViewsTextServicesContextMenuMac::ViewsTextServicesContextMenuMac( | |
64 ui::SimpleMenuModel* menu, | |
65 Textfield* client) | |
66 : menu_(this), client_(client) { | |
67 menu->InsertItemAt(kLookupMenuIndex, IDS_CONTENT_CONTEXT_LOOK_UP, | |
68 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_LOOK_UP, | |
69 GetSelectedText())); | |
70 menu->InsertSeparatorAt(kLookupMenuIndex + 1, ui::NORMAL_SEPARATOR); | |
71 menu_.AppendToContextMenu(menu); | |
72 } | |
73 | |
74 ViewsTextServicesContextMenuMac::~ViewsTextServicesContextMenuMac() {} | |
75 | |
76 void ViewsTextServicesContextMenuMac::LookUpInDictionary() { | |
77 gfx::Point baseline_point; | |
78 gfx::DecoratedText text; | |
79 client_->GetDecoratedTextAndBaselineFromSelection(&text, &baseline_point); | |
tapted
2016/12/21 11:20:27
check return value?
spqchan
2016/12/21 22:00:14
Done.
| |
80 | |
81 Widget* widget = client_->GetWidget(); | |
82 gfx::NativeView view = widget->GetNativeView(); | |
83 views::View::ConvertPointToTarget(client_, widget->GetRootView(), | |
84 &baseline_point); | |
85 | |
86 NSPoint lookup_point = NSMakePoint( | |
87 baseline_point.x(), NSHeight([view frame]) - baseline_point.y()); | |
88 [view showDefinitionForAttributedString: | |
89 gfx::GetAttributedStringFromDecoratedText(text) | |
90 atPoint:lookup_point]; | |
91 } | |
92 | |
93 bool ViewsTextServicesContextMenuMac::HandlesCommandId(int command_id) const { | |
94 return menu_.IsTextServicesCommandId(command_id) || | |
95 command_id == IDS_CONTENT_CONTEXT_LOOK_UP; | |
96 } | |
97 | |
98 void ViewsTextServicesContextMenuMac::ExecuteCommand(int command_id, | |
99 int event_flags) { | |
100 if (command_id == IDS_CONTENT_CONTEXT_LOOK_UP) | |
101 LookUpInDictionary(); | |
102 else | |
103 menu_.ExecuteCommand(command_id, event_flags); | |
104 } | |
105 | |
106 bool ViewsTextServicesContextMenuMac::IsCommandIdChecked(int command_id) const { | |
107 if (command_id == IDS_CONTENT_CONTEXT_LOOK_UP) | |
108 return false; | |
109 | |
110 return menu_.IsCommandIdChecked(command_id); | |
111 } | |
112 | |
113 bool ViewsTextServicesContextMenuMac::IsCommandIdEnabled(int command_id) const { | |
114 if (command_id == IDS_CONTENT_CONTEXT_LOOK_UP) | |
115 return true; | |
116 | |
117 return menu_.IsCommandIdEnabled(command_id); | |
118 } | |
119 | |
120 base::string16 ViewsTextServicesContextMenuMac::GetSelectedText() const { | |
121 gfx::Range range; | |
tapted
2016/12/21 11:20:27
I think client_->GetSelectedText() will work now
spqchan
2016/12/21 22:00:14
Done.
| |
122 base::string16 text; | |
123 client_->GetSelectionRange(&range); | |
124 client_->GetTextFromRange(range, &text); | |
125 return text; | |
126 } | |
127 | |
128 void ViewsTextServicesContextMenuMac::OnSpeakRequested() { | |
129 menu_.SpeakText(GetSelectedText()); | |
130 } | |
131 | |
132 bool ViewsTextServicesContextMenuMac::IsTextDirectionEnabled( | |
133 base::i18n::TextDirection direction) const { | |
134 return direction != base::i18n::TextDirection::UNKNOWN_DIRECTION; | |
135 } | |
136 | |
137 bool ViewsTextServicesContextMenuMac::IsTextDirectionChecked( | |
138 base::i18n::TextDirection direction) const { | |
139 switch (direction) { | |
140 case base::i18n::TextDirection::UNKNOWN_DIRECTION: | |
141 return false; | |
142 case base::i18n::TextDirection::RIGHT_TO_LEFT: | |
143 return client_->GetTextDirection() == base::i18n::RIGHT_TO_LEFT; | |
144 case base::i18n::TextDirection::LEFT_TO_RIGHT: | |
145 return client_->GetTextDirection() == base::i18n::LEFT_TO_RIGHT; | |
146 case base::i18n::TextDirection::TEXT_DIRECTION_NUM_DIRECTIONS: | |
147 NOTREACHED(); | |
148 return false; | |
149 } | |
150 } | |
151 | |
152 void ViewsTextServicesContextMenuMac::UpdateTextDirection( | |
153 base::i18n::TextDirection direction) { | |
154 DCHECK_NE(direction, base::i18n::TextDirection::UNKNOWN_DIRECTION); | |
155 | |
156 base::i18n::TextDirection text_direction = | |
157 direction == base::i18n::TextDirection::LEFT_TO_RIGHT | |
tapted
2016/12/21 11:20:27
I think the `TextDirection::` part of this is redu
spqchan
2016/12/21 22:00:14
Done.
| |
158 ? base::i18n::LEFT_TO_RIGHT | |
159 : base::i18n::RIGHT_TO_LEFT; | |
160 client_->ChangeTextDirectionAndLayoutAlignment(text_direction); | |
161 } | |
162 | |
163 } // namespace | |
164 | |
165 // static | |
166 std::unique_ptr<ViewsTextServicesContextMenu> | |
167 ViewsTextServicesContextMenu::Create(ui::SimpleMenuModel* menu, | |
168 Textfield* client) { | |
169 return base::MakeUnique<ViewsTextServicesContextMenuMac>(menu, client); | |
170 } | |
171 | |
172 } // namespace views | |
OLD | NEW |