OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "content/browser/web_contents/touch_editable_impl_aura.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "content/browser/renderer_host/render_widget_host_impl.h" | |
9 #include "content/browser/renderer_host/render_widget_host_view_aura.h" | |
10 #include "content/common/view_messages.h" | |
11 #include "content/public/browser/render_widget_host.h" | |
12 #include "grit/ui_strings.h" | |
13 #include "ui/aura/client/activation_client.h" | |
14 #include "ui/aura/client/screen_position_client.h" | |
15 #include "ui/aura/root_window.h" | |
16 #include "ui/aura/window.h" | |
17 #include "ui/base/clipboard/clipboard.h" | |
18 #include "ui/base/range/range.h" | |
19 #include "ui/base/ui_base_switches.h" | |
20 | |
21 namespace content { | |
22 | |
23 namespace { | |
24 | |
25 aura::Window* GetToplevelWindowForWindow(aura::Window* window) { | |
26 aura::RootWindow* root = window->GetRootWindow(); | |
27 if (!root) | |
28 return NULL; | |
29 aura::client::ActivationClient* activation_client = | |
30 aura::client::GetActivationClient(root); | |
31 if (!activation_client) | |
32 return NULL; | |
33 return activation_client->GetToplevelWindow(window); | |
34 } | |
35 | |
36 } | |
37 | |
38 //////////////////////////////////////////////////////////////////////////////// | |
39 // TouchEditableImplAura, public: | |
40 | |
41 TouchEditableImplAura::~TouchEditableImplAura() { | |
42 Cleanup(); | |
43 } | |
44 | |
45 // static | |
46 TouchEditableImplAura* TouchEditableImplAura::Create() { | |
47 #if defined(OS_CHROMEOS) | |
48 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
49 switches::kEnableTouchEditing)) | |
50 return new TouchEditableImplAura(); | |
51 #endif | |
52 return NULL; | |
53 } | |
54 | |
55 void TouchEditableImplAura::AttachToView(RenderWidgetHostViewAura* view) { | |
56 if (rwhva_ == view) | |
57 return; | |
58 | |
59 Cleanup(); | |
60 if (!view) | |
61 return; | |
62 | |
63 rwhva_ = view; | |
64 rwhva_->set_touch_editing_client(this); | |
65 aura::Window* window = rwhva_->GetNativeView(); | |
66 window->AddObserver(this); | |
67 top_level_window_ = GetToplevelWindowForWindow(window); | |
68 if (top_level_window_) | |
69 top_level_window_->AddObserver(this); | |
70 } | |
71 | |
72 //////////////////////////////////////////////////////////////////////////////// | |
73 // TouchEditableImplAura, RenderWidgetHostViewAura::TouchEditingClient | |
74 // implementation: | |
75 | |
76 void TouchEditableImplAura::StartTouchEditing() { | |
77 if (!touch_selection_controller_) { | |
78 touch_selection_controller_.reset( | |
79 ui::TouchSelectionController::create(this)); | |
80 } | |
81 if (touch_selection_controller_) | |
82 touch_selection_controller_->SelectionChanged(); | |
83 } | |
84 | |
85 void TouchEditableImplAura::EndTouchEditing() { | |
86 if (touch_selection_controller_) { | |
87 if (touch_selection_controller_->IsHandleDragInProgress()) | |
88 touch_selection_controller_->SelectionChanged(); | |
89 else | |
90 touch_selection_controller_.reset(); | |
91 } | |
92 } | |
93 | |
94 void TouchEditableImplAura::OnSelectionOrCursorChanged(const gfx::Rect& anchor, | |
95 const gfx::Rect& focus) { | |
96 selection_anchor_rect_ = anchor; | |
97 selection_focus_rect_ = focus; | |
98 UpdateController(); | |
99 } | |
100 | |
101 void TouchEditableImplAura::OnTextInputTypeChanged(ui::TextInputType type) { | |
102 text_input_type_ = type; | |
103 } | |
104 | |
105 bool TouchEditableImplAura::HandleInputEvent(const ui::Event* event) { | |
106 DCHECK(rwhva_); | |
107 if (event->IsTouchEvent()) | |
108 return false; | |
109 | |
110 if (!event->IsGestureEvent()) { | |
111 EndTouchEditing(); | |
112 return false; | |
113 } | |
114 | |
115 const ui::GestureEvent* gesture_event = | |
116 static_cast<const ui::GestureEvent*>(event); | |
117 switch (event->type()) { | |
118 case ui::ET_GESTURE_TAP: | |
119 if (gesture_event->details().tap_count() > 1) | |
120 selection_gesture_in_process_ = true; | |
121 // When the user taps, we want to show touch editing handles if user | |
122 // tapped on selected text. | |
123 if (selection_anchor_rect_ != selection_focus_rect_) { | |
124 // UnionRects only works for rects with non-zero width. | |
125 gfx::Rect anchor(selection_anchor_rect_.origin(), | |
126 gfx::Size(1, selection_anchor_rect_.height())); | |
127 gfx::Rect focus(selection_focus_rect_.origin(), | |
128 gfx::Size(1, selection_focus_rect_.height())); | |
129 gfx::Rect selection_rect = gfx::UnionRects(anchor, focus); | |
130 if (selection_rect.Contains(gesture_event->location())) { | |
131 StartTouchEditing(); | |
132 return true; | |
133 } | |
134 } | |
135 break; | |
136 case ui::ET_GESTURE_LONG_PRESS: | |
137 selection_gesture_in_process_ = true; | |
138 break; | |
139 default: | |
140 break; | |
141 } | |
142 return false; | |
143 } | |
144 | |
145 void TouchEditableImplAura::GestureEventAck(int gesture_event_type) { | |
146 DCHECK(rwhva_); | |
147 if (gesture_event_type == WebKit::WebInputEvent::GestureTap && | |
148 text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) { | |
149 StartTouchEditing(); | |
150 if (touch_selection_controller_) | |
151 touch_selection_controller_->SelectionChanged(); | |
152 } | |
153 | |
154 if (gesture_event_type == WebKit::WebInputEvent::GestureLongPress || | |
155 gesture_event_type == WebKit::WebInputEvent::GestureTap) | |
156 selection_gesture_in_process_ = false; | |
157 } | |
158 | |
159 void TouchEditableImplAura::OnViewDestroyed() { | |
160 Cleanup(); | |
161 } | |
162 | |
163 //////////////////////////////////////////////////////////////////////////////// | |
164 // TouchEditableImplAura, ui::TouchEditable implementation: | |
165 | |
166 void TouchEditableImplAura::SelectRect(const gfx::Point& start, | |
167 const gfx::Point& end) { | |
168 if (!rwhva_) | |
169 return; | |
170 | |
171 RenderWidgetHostImpl* host = RenderWidgetHostImpl::From( | |
172 rwhva_->GetRenderWidgetHost()); | |
173 host->SelectRange(start, end); | |
174 } | |
175 | |
176 void TouchEditableImplAura::MoveCaretTo(const gfx::Point& point) { | |
177 if (!rwhva_) | |
178 return; | |
179 | |
180 RenderWidgetHostImpl* host = RenderWidgetHostImpl::From( | |
181 rwhva_->GetRenderWidgetHost()); | |
182 host->MoveCaret(point); | |
183 } | |
184 | |
185 void TouchEditableImplAura::GetSelectionEndPoints(gfx::Rect* p1, | |
186 gfx::Rect* p2) { | |
187 *p1 = selection_anchor_rect_; | |
188 *p2 = selection_focus_rect_; | |
189 } | |
190 | |
191 gfx::Rect TouchEditableImplAura::GetBounds() { | |
192 return rwhva_ ? rwhva_->GetNativeView()->bounds() : gfx::Rect(); | |
193 } | |
194 | |
195 gfx::NativeView TouchEditableImplAura::GetNativeView() { | |
196 return rwhva_ ? rwhva_->GetNativeView()->GetRootWindow() : NULL; | |
197 } | |
198 | |
199 void TouchEditableImplAura::ConvertPointToScreen(gfx::Point* point) { | |
200 if (!rwhva_) | |
201 return; | |
202 aura::Window* window = rwhva_->GetNativeView(); | |
203 aura::client::ScreenPositionClient* screen_position_client = | |
204 aura::client::GetScreenPositionClient(window->GetRootWindow()); | |
205 if (screen_position_client) | |
206 screen_position_client->ConvertPointToScreen(window, point); | |
207 } | |
208 | |
209 void TouchEditableImplAura::ConvertPointFromScreen(gfx::Point* point) { | |
210 if (!rwhva_) | |
211 return; | |
212 aura::Window* window = rwhva_->GetNativeView(); | |
213 aura::client::ScreenPositionClient* screen_position_client = | |
214 aura::client::GetScreenPositionClient(window->GetRootWindow()); | |
215 if (screen_position_client) | |
216 screen_position_client->ConvertPointFromScreen(window, point); | |
217 } | |
218 | |
219 bool TouchEditableImplAura::DrawsHandles() { | |
220 return false; | |
221 } | |
222 | |
223 void TouchEditableImplAura::OpenContextMenu(const gfx::Point anchor) { | |
224 if (!rwhva_) | |
225 return; | |
226 RenderWidgetHost* host = rwhva_->GetRenderWidgetHost(); | |
227 host->Send(new ViewMsg_ShowContextMenu(host->GetRoutingID())); | |
228 EndTouchEditing(); | |
229 } | |
230 | |
231 bool TouchEditableImplAura::IsCommandIdChecked(int command_id) const { | |
232 NOTREACHED(); | |
233 return false; | |
234 } | |
235 | |
236 bool TouchEditableImplAura::IsCommandIdEnabled(int command_id) const { | |
237 if (!rwhva_) | |
238 return false; | |
239 bool editable = rwhva_->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE; | |
240 ui::Range selection_range; | |
241 rwhva_->GetSelectionRange(&selection_range); | |
242 bool has_selection = !selection_range.is_empty(); | |
243 switch (command_id) { | |
244 case IDS_APP_CUT: | |
245 return editable && has_selection; | |
246 case IDS_APP_COPY: | |
247 return has_selection; | |
248 case IDS_APP_PASTE: { | |
249 string16 result; | |
250 ui::Clipboard::GetForCurrentThread()->ReadText( | |
251 ui::Clipboard::BUFFER_STANDARD, &result); | |
252 return editable && !result.empty(); | |
253 } | |
254 case IDS_APP_DELETE: | |
255 return editable && has_selection; | |
256 case IDS_APP_SELECT_ALL: | |
257 return true; | |
258 default: | |
259 return false; | |
260 } | |
261 } | |
262 | |
263 bool TouchEditableImplAura::GetAcceleratorForCommandId( | |
264 int command_id, | |
265 ui::Accelerator* accelerator) { | |
266 return false; | |
267 } | |
268 | |
269 void TouchEditableImplAura::ExecuteCommand(int command_id, int event_flags) { | |
270 if (!rwhva_) | |
271 return; | |
272 RenderWidgetHost* host = rwhva_->GetRenderWidgetHost(); | |
273 switch (command_id) { | |
274 case IDS_APP_CUT: | |
275 host->Cut(); | |
276 break; | |
277 case IDS_APP_COPY: | |
278 host->Copy(); | |
279 break; | |
280 case IDS_APP_PASTE: | |
281 host->Paste(); | |
282 break; | |
283 case IDS_APP_DELETE: | |
284 host->Delete(); | |
285 break; | |
286 case IDS_APP_SELECT_ALL: | |
287 host->SelectAll(); | |
288 break; | |
289 default: | |
290 NOTREACHED(); | |
291 break; | |
292 } | |
293 EndTouchEditing(); | |
294 } | |
295 | |
296 void TouchEditableImplAura::OnWindowParentChanged(aura::Window* window, | |
297 aura::Window* parent) { | |
piman
2013/04/18 22:28:42
I don't think this is enough. This only tells you
varunjain
2013/04/19 05:14:12
removed top_level_window_
| |
298 if (window == rwhva_->GetNativeView()) { | |
299 if (top_level_window_) | |
300 top_level_window_->RemoveObserver(this); | |
301 | |
302 top_level_window_ = GetToplevelWindowForWindow(window); | |
303 if (top_level_window_) | |
304 top_level_window_->AddObserver(this); | |
305 } | |
306 } | |
307 | |
308 void TouchEditableImplAura::OnWindowBoundsChanged(aura::Window* window, | |
309 const gfx::Rect& old_bounds, | |
310 const gfx::Rect& new_bounds) { | |
311 if (top_level_window_ == window && touch_selection_controller_) | |
312 touch_selection_controller_->SelectionChanged(); | |
piman
2013/04/18 22:28:42
I don't understand why this is needed, and further
varunjain
2013/04/19 05:14:12
You are correct. This should not be needed here. R
| |
313 } | |
314 | |
315 void TouchEditableImplAura::OnWindowDestroying(aura::Window* window) { | |
316 if (top_level_window_ == window) { | |
317 top_level_window_->RemoveObserver(this); | |
318 top_level_window_ = NULL; | |
319 } else if (rwhva_->GetNativeView() == window) { | |
320 rwhva_->GetNativeView()->RemoveObserver(this); | |
321 } | |
322 } | |
323 | |
324 //////////////////////////////////////////////////////////////////////////////// | |
325 // TouchEditableImplAura, private: | |
326 | |
327 TouchEditableImplAura::TouchEditableImplAura() | |
328 : text_input_type_(ui::TEXT_INPUT_TYPE_NONE), | |
329 rwhva_(NULL), | |
330 top_level_window_(NULL), | |
331 selection_gesture_in_process_(false) { | |
332 } | |
333 | |
334 void TouchEditableImplAura::UpdateController() { | |
335 DCHECK(rwhva_); | |
336 | |
337 // If touch editing handles were not visible, we bring them up only if | |
338 // there is non-zero selection on the page. And the current event is a | |
339 // gesture event (we dont want to show handles if the user is selecting | |
340 // using mouse or keyboard). | |
341 if (selection_gesture_in_process_ && | |
342 selection_anchor_rect_ != selection_focus_rect_) | |
343 StartTouchEditing(); | |
344 | |
345 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE || | |
346 selection_anchor_rect_ != selection_focus_rect_) { | |
347 if (touch_selection_controller_) | |
348 touch_selection_controller_->SelectionChanged(); | |
349 } else | |
350 EndTouchEditing(); | |
351 } | |
352 | |
353 void TouchEditableImplAura::Cleanup() { | |
354 if (top_level_window_) | |
355 top_level_window_->RemoveObserver(this); | |
356 if (rwhva_) { | |
357 if (rwhva_->GetNativeView()) | |
358 rwhva_->GetNativeView()->RemoveObserver(this); | |
359 rwhva_->set_touch_editing_client(NULL); | |
360 rwhva_ = NULL; | |
361 } | |
362 touch_selection_controller_.reset(); | |
363 } | |
364 | |
365 } // namespace content | |
OLD | NEW |