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.
5 #include "content/browser/web_contents/touch_editable_impl_aura.h"
7 #include "content/browser/renderer_host/render_widget_host_impl.h"
8 #include "content/browser/renderer_host/render_widget_host_view_aura.h"
9 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/common/view_messages.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/render_widget_host.h"
13 #include "ui/aura/client/screen_position_client.h"
14 #include "ui/aura/window.h"
15 #include "ui/aura/window_tree_host.h"
16 #include "ui/base/clipboard/clipboard.h"
17 #include "ui/base/touch/selection_bound.h"
18 #include "ui/base/ui_base_switches_util.h"
19 #include "ui/gfx/range/range.h"
20 #include "ui/strings/grit/ui_strings.h"
21 #include "ui/wm/public/activation_client.h"
25 ////////////////////////////////////////////////////////////////////////////////
26 // TouchEditableImplAura, public:
28 TouchEditableImplAura::~TouchEditableImplAura() {
33 TouchEditableImplAura
* TouchEditableImplAura::Create() {
34 if (switches::IsTouchEditingEnabled())
35 return new TouchEditableImplAura();
39 void TouchEditableImplAura::AttachToView(RenderWidgetHostViewAura
* view
) {
48 rwhva_
->set_touch_editing_client(this);
51 void TouchEditableImplAura::UpdateEditingController() {
52 if (!rwhva_
|| !rwhva_
->HasFocus())
55 if (text_input_type_
!= ui::TEXT_INPUT_TYPE_NONE
||
56 selection_anchor_
!= selection_focus_
) {
57 if (touch_selection_controller_
)
58 touch_selection_controller_
->SelectionChanged();
60 EndTouchEditing(false);
64 void TouchEditableImplAura::OverscrollStarted() {
65 scrolls_in_progress_
++;
68 void TouchEditableImplAura::OverscrollCompleted() {
72 ////////////////////////////////////////////////////////////////////////////////
73 // TouchEditableImplAura, RenderWidgetHostViewAura::TouchEditingClient
76 void TouchEditableImplAura::StartTouchEditing() {
77 if (!rwhva_
|| !rwhva_
->HasFocus())
80 if (!touch_selection_controller_
) {
81 touch_selection_controller_
.reset(
82 ui::TouchSelectionController::create(this));
84 if (touch_selection_controller_
)
85 touch_selection_controller_
->SelectionChanged();
88 void TouchEditableImplAura::EndTouchEditing(bool quick
) {
89 if (touch_selection_controller_
) {
90 if (touch_selection_controller_
->IsHandleDragInProgress()) {
91 touch_selection_controller_
->SelectionChanged();
93 selection_gesture_in_process_
= false;
94 touch_selection_controller_
->HideHandles(quick
);
95 touch_selection_controller_
.reset();
100 void TouchEditableImplAura::OnSelectionOrCursorChanged(
101 const ui::SelectionBound
& anchor
,
102 const ui::SelectionBound
& focus
) {
103 selection_anchor_
= anchor
;
104 selection_focus_
= focus
;
106 // If touch editing handles were not visible, we bring them up only if the
107 // current event is a gesture event, no scroll/fling/overscoll is in progress,
108 // and there is non-zero selection on the page
109 if (selection_gesture_in_process_
&& !scrolls_in_progress_
&&
110 selection_anchor_
!= selection_focus_
) {
112 selection_gesture_in_process_
= false;
115 UpdateEditingController();
118 void TouchEditableImplAura::OnTextInputTypeChanged(ui::TextInputType type
) {
119 text_input_type_
= type
;
122 bool TouchEditableImplAura::HandleInputEvent(const ui::Event
* event
) {
124 if (!event
->IsGestureEvent()) {
125 // Ignore all non-gesture events. Non-gesture events that can deactivate
126 // touch editing are handled in TouchSelectionControllerImpl.
130 const ui::GestureEvent
* gesture_event
=
131 static_cast<const ui::GestureEvent
*>(event
);
132 switch (event
->type()) {
133 case ui::ET_GESTURE_TAP
:
134 // When the user taps, we want to show touch editing handles if user
135 // tapped on selected text.
136 if (gesture_event
->details().tap_count() == 1 &&
137 selection_anchor_
!= selection_focus_
) {
138 gfx::Rect selection_rect
=
139 ui::RectBetweenSelectionBounds(selection_anchor_
, selection_focus_
);
140 if (selection_rect
.Contains(gesture_event
->location())) {
145 // For single taps, not inside selected region, we want to show handles
146 // only when the tap is on an already focused textfield.
147 textfield_was_focused_on_tap_
=
148 gesture_event
->details().tap_count() == 1 &&
149 text_input_type_
!= ui::TEXT_INPUT_TYPE_NONE
;
151 case ui::ET_GESTURE_LONG_PRESS
:
152 selection_gesture_in_process_
= true;
154 case ui::ET_GESTURE_SCROLL_BEGIN
:
155 scrolls_in_progress_
++;
156 // We need to hide selection handles during scroll (including fling and
157 // overscroll), but they should be re-activated after scrolling if:
158 // - an existing scroll decided that handles should be shown after
160 // - the gesture in progress is going to end in selection; or
161 // - selection handles are currently active.
162 handles_hidden_due_to_scroll_
= handles_hidden_due_to_scroll_
||
163 selection_gesture_in_process_
||
164 touch_selection_controller_
!= NULL
;
165 selection_gesture_in_process_
= false;
166 EndTouchEditing(true);
168 case ui::ET_GESTURE_SCROLL_END
:
177 void TouchEditableImplAura::GestureEventAck(int gesture_event_type
) {
179 if (gesture_event_type
== blink::WebInputEvent::GestureTap
&&
180 text_input_type_
!= ui::TEXT_INPUT_TYPE_NONE
&&
181 textfield_was_focused_on_tap_
) {
183 UpdateEditingController();
187 void TouchEditableImplAura::DidStopFlinging() {
191 void TouchEditableImplAura::OnViewDestroyed() {
195 ////////////////////////////////////////////////////////////////////////////////
196 // TouchEditableImplAura, ui::TouchEditable implementation:
198 void TouchEditableImplAura::SelectRect(const gfx::Point
& start
,
199 const gfx::Point
& end
) {
200 RenderWidgetHost
* host
= rwhva_
->GetRenderWidgetHost();
201 RenderViewHost
* rvh
= RenderViewHost::From(host
);
202 WebContentsImpl
* wc
=
203 static_cast<WebContentsImpl
*>(WebContents::FromRenderViewHost(rvh
));
204 wc
->SelectRange(start
, end
);
207 void TouchEditableImplAura::MoveCaretTo(const gfx::Point
& point
) {
211 RenderWidgetHostImpl
* host
= RenderWidgetHostImpl::From(
212 rwhva_
->GetRenderWidgetHost());
213 host
->MoveCaret(point
);
216 void TouchEditableImplAura::GetSelectionEndPoints(ui::SelectionBound
* anchor
,
217 ui::SelectionBound
* focus
) {
218 *anchor
= selection_anchor_
;
219 *focus
= selection_focus_
;
222 gfx::Rect
TouchEditableImplAura::GetBounds() {
223 return rwhva_
? gfx::Rect(rwhva_
->GetNativeView()->bounds().size()) :
227 gfx::NativeView
TouchEditableImplAura::GetNativeView() const {
228 return rwhva_
? rwhva_
->GetNativeView()->GetToplevelWindow() : NULL
;
231 void TouchEditableImplAura::ConvertPointToScreen(gfx::Point
* point
) {
234 aura::Window
* window
= rwhva_
->GetNativeView();
235 aura::client::ScreenPositionClient
* screen_position_client
=
236 aura::client::GetScreenPositionClient(window
->GetRootWindow());
237 if (screen_position_client
)
238 screen_position_client
->ConvertPointToScreen(window
, point
);
241 void TouchEditableImplAura::ConvertPointFromScreen(gfx::Point
* point
) {
244 aura::Window
* window
= rwhva_
->GetNativeView();
245 aura::client::ScreenPositionClient
* screen_position_client
=
246 aura::client::GetScreenPositionClient(window
->GetRootWindow());
247 if (screen_position_client
)
248 screen_position_client
->ConvertPointFromScreen(window
, point
);
251 bool TouchEditableImplAura::DrawsHandles() {
255 void TouchEditableImplAura::OpenContextMenu(const gfx::Point
& anchor
) {
258 gfx::Point point
= anchor
;
259 ConvertPointFromScreen(&point
);
260 RenderWidgetHost
* host
= rwhva_
->GetRenderWidgetHost();
261 host
->Send(new ViewMsg_ShowContextMenu(
262 host
->GetRoutingID(), ui::MENU_SOURCE_TOUCH_EDIT_MENU
, point
));
263 EndTouchEditing(false);
266 bool TouchEditableImplAura::IsCommandIdChecked(int command_id
) const {
271 bool TouchEditableImplAura::IsCommandIdEnabled(int command_id
) const {
274 bool editable
= rwhva_
->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE
;
275 bool readable
= rwhva_
->GetTextInputType() != ui::TEXT_INPUT_TYPE_PASSWORD
;
276 gfx::Range selection_range
;
277 rwhva_
->GetSelectionRange(&selection_range
);
278 bool has_selection
= !selection_range
.is_empty();
279 switch (command_id
) {
281 return editable
&& readable
&& has_selection
;
283 return readable
&& has_selection
;
284 case IDS_APP_PASTE
: {
285 base::string16 result
;
286 ui::Clipboard::GetForCurrentThread()->ReadText(
287 ui::CLIPBOARD_TYPE_COPY_PASTE
, &result
);
288 return editable
&& !result
.empty();
291 return editable
&& has_selection
;
292 case IDS_APP_SELECT_ALL
:
299 bool TouchEditableImplAura::GetAcceleratorForCommandId(
301 ui::Accelerator
* accelerator
) {
305 void TouchEditableImplAura::ExecuteCommand(int command_id
, int event_flags
) {
306 RenderWidgetHost
* host
= rwhva_
->GetRenderWidgetHost();
307 RenderViewHost
* rvh
= RenderViewHost::From(host
);
308 WebContents
* wc
= WebContents::FromRenderViewHost(rvh
);
310 switch (command_id
) {
323 case IDS_APP_SELECT_ALL
:
330 EndTouchEditing(false);
333 void TouchEditableImplAura::DestroyTouchSelection() {
334 EndTouchEditing(false);
337 ////////////////////////////////////////////////////////////////////////////////
338 // TouchEditableImplAura, private:
340 TouchEditableImplAura::TouchEditableImplAura()
341 : text_input_type_(ui::TEXT_INPUT_TYPE_NONE
),
343 selection_gesture_in_process_(false),
344 handles_hidden_due_to_scroll_(false),
345 scrolls_in_progress_(0),
346 textfield_was_focused_on_tap_(false) {
349 void TouchEditableImplAura::ScrollEnded() {
350 scrolls_in_progress_
--;
351 // If there is no scrolling left in progress, show selection handles if they
352 // were hidden due to scroll and there is a selection.
353 if (!scrolls_in_progress_
&& handles_hidden_due_to_scroll_
&&
354 (selection_anchor_
!= selection_focus_
||
355 text_input_type_
!= ui::TEXT_INPUT_TYPE_NONE
)) {
357 UpdateEditingController();
358 handles_hidden_due_to_scroll_
= false;
362 void TouchEditableImplAura::Cleanup() {
364 rwhva_
->set_touch_editing_client(NULL
);
367 text_input_type_
= ui::TEXT_INPUT_TYPE_NONE
;
368 EndTouchEditing(true);
369 selection_gesture_in_process_
= false;
370 handles_hidden_due_to_scroll_
= false;
371 scrolls_in_progress_
= 0;
374 } // namespace content