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 //////////////////////////////////////////////////////////////////////////////// |
| 24 // TouchEditableImplAura, public: |
| 25 |
| 26 TouchEditableImplAura::~TouchEditableImplAura() { |
| 27 Cleanup(); |
| 28 } |
| 29 |
| 30 // static |
| 31 TouchEditableImplAura* TouchEditableImplAura::Create() { |
| 32 #if defined(OS_CHROMEOS) |
| 33 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 34 switches::kEnableTouchEditing)) |
| 35 return new TouchEditableImplAura(); |
| 36 #endif |
| 37 return NULL; |
| 38 } |
| 39 |
| 40 void TouchEditableImplAura::AttachToView(RenderWidgetHostViewAura* view) { |
| 41 if (rwhva_ == view) |
| 42 return; |
| 43 |
| 44 Cleanup(); |
| 45 if (!view) |
| 46 return; |
| 47 |
| 48 rwhva_ = view; |
| 49 rwhva_->set_touch_editing_client(this); |
| 50 } |
| 51 |
| 52 void TouchEditableImplAura::UpdateEditingController() { |
| 53 if (!rwhva_) |
| 54 return; |
| 55 |
| 56 // If touch editing handles were not visible, we bring them up only if |
| 57 // there is non-zero selection on the page. And the current event is a |
| 58 // gesture event (we dont want to show handles if the user is selecting |
| 59 // using mouse or keyboard). |
| 60 if (selection_gesture_in_process_ && |
| 61 selection_anchor_rect_ != selection_focus_rect_) |
| 62 StartTouchEditing(); |
| 63 |
| 64 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE || |
| 65 selection_anchor_rect_ != selection_focus_rect_) { |
| 66 if (touch_selection_controller_) |
| 67 touch_selection_controller_->SelectionChanged(); |
| 68 } else { |
| 69 EndTouchEditing(); |
| 70 } |
| 71 } |
| 72 |
| 73 //////////////////////////////////////////////////////////////////////////////// |
| 74 // TouchEditableImplAura, RenderWidgetHostViewAura::TouchEditingClient |
| 75 // implementation: |
| 76 |
| 77 void TouchEditableImplAura::StartTouchEditing() { |
| 78 if (!touch_selection_controller_) { |
| 79 touch_selection_controller_.reset( |
| 80 ui::TouchSelectionController::create(this)); |
| 81 } |
| 82 if (touch_selection_controller_) |
| 83 touch_selection_controller_->SelectionChanged(); |
| 84 } |
| 85 |
| 86 void TouchEditableImplAura::EndTouchEditing() { |
| 87 if (touch_selection_controller_) { |
| 88 if (touch_selection_controller_->IsHandleDragInProgress()) |
| 89 touch_selection_controller_->SelectionChanged(); |
| 90 else |
| 91 touch_selection_controller_.reset(); |
| 92 } |
| 93 } |
| 94 |
| 95 void TouchEditableImplAura::OnSelectionOrCursorChanged(const gfx::Rect& anchor, |
| 96 const gfx::Rect& focus) { |
| 97 selection_anchor_rect_ = anchor; |
| 98 selection_focus_rect_ = focus; |
| 99 UpdateEditingController(); |
| 100 } |
| 101 |
| 102 void TouchEditableImplAura::OnTextInputTypeChanged(ui::TextInputType type) { |
| 103 text_input_type_ = type; |
| 104 } |
| 105 |
| 106 bool TouchEditableImplAura::HandleInputEvent(const ui::Event* event) { |
| 107 DCHECK(rwhva_); |
| 108 if (event->IsTouchEvent()) |
| 109 return false; |
| 110 |
| 111 if (!event->IsGestureEvent()) { |
| 112 EndTouchEditing(); |
| 113 return false; |
| 114 } |
| 115 |
| 116 const ui::GestureEvent* gesture_event = |
| 117 static_cast<const ui::GestureEvent*>(event); |
| 118 switch (event->type()) { |
| 119 case ui::ET_GESTURE_TAP: |
| 120 if (gesture_event->details().tap_count() > 1) |
| 121 selection_gesture_in_process_ = true; |
| 122 // When the user taps, we want to show touch editing handles if user |
| 123 // tapped on selected text. |
| 124 if (selection_anchor_rect_ != selection_focus_rect_) { |
| 125 // UnionRects only works for rects with non-zero width. |
| 126 gfx::Rect anchor(selection_anchor_rect_.origin(), |
| 127 gfx::Size(1, selection_anchor_rect_.height())); |
| 128 gfx::Rect focus(selection_focus_rect_.origin(), |
| 129 gfx::Size(1, selection_focus_rect_.height())); |
| 130 gfx::Rect selection_rect = gfx::UnionRects(anchor, focus); |
| 131 if (selection_rect.Contains(gesture_event->location())) { |
| 132 StartTouchEditing(); |
| 133 return true; |
| 134 } |
| 135 } |
| 136 break; |
| 137 case ui::ET_GESTURE_LONG_PRESS: |
| 138 selection_gesture_in_process_ = true; |
| 139 break; |
| 140 default: |
| 141 break; |
| 142 } |
| 143 return false; |
| 144 } |
| 145 |
| 146 void TouchEditableImplAura::GestureEventAck(int gesture_event_type) { |
| 147 DCHECK(rwhva_); |
| 148 if (gesture_event_type == WebKit::WebInputEvent::GestureTap && |
| 149 text_input_type_ != ui::TEXT_INPUT_TYPE_NONE) { |
| 150 StartTouchEditing(); |
| 151 if (touch_selection_controller_) |
| 152 touch_selection_controller_->SelectionChanged(); |
| 153 } |
| 154 |
| 155 if (gesture_event_type == WebKit::WebInputEvent::GestureLongPress || |
| 156 gesture_event_type == WebKit::WebInputEvent::GestureTap) |
| 157 selection_gesture_in_process_ = false; |
| 158 } |
| 159 |
| 160 void TouchEditableImplAura::OnViewDestroyed() { |
| 161 Cleanup(); |
| 162 } |
| 163 |
| 164 //////////////////////////////////////////////////////////////////////////////// |
| 165 // TouchEditableImplAura, ui::TouchEditable implementation: |
| 166 |
| 167 void TouchEditableImplAura::SelectRect(const gfx::Point& start, |
| 168 const gfx::Point& end) { |
| 169 if (!rwhva_) |
| 170 return; |
| 171 |
| 172 RenderWidgetHostImpl* host = RenderWidgetHostImpl::From( |
| 173 rwhva_->GetRenderWidgetHost()); |
| 174 host->SelectRange(start, end); |
| 175 } |
| 176 |
| 177 void TouchEditableImplAura::MoveCaretTo(const gfx::Point& point) { |
| 178 if (!rwhva_) |
| 179 return; |
| 180 |
| 181 RenderWidgetHostImpl* host = RenderWidgetHostImpl::From( |
| 182 rwhva_->GetRenderWidgetHost()); |
| 183 host->MoveCaret(point); |
| 184 } |
| 185 |
| 186 void TouchEditableImplAura::GetSelectionEndPoints(gfx::Rect* p1, |
| 187 gfx::Rect* p2) { |
| 188 *p1 = selection_anchor_rect_; |
| 189 *p2 = selection_focus_rect_; |
| 190 } |
| 191 |
| 192 gfx::Rect TouchEditableImplAura::GetBounds() { |
| 193 return rwhva_ ? rwhva_->GetNativeView()->bounds() : gfx::Rect(); |
| 194 } |
| 195 |
| 196 gfx::NativeView TouchEditableImplAura::GetNativeView() { |
| 197 return rwhva_ ? rwhva_->GetNativeView()->GetRootWindow() : NULL; |
| 198 } |
| 199 |
| 200 void TouchEditableImplAura::ConvertPointToScreen(gfx::Point* point) { |
| 201 if (!rwhva_) |
| 202 return; |
| 203 aura::Window* window = rwhva_->GetNativeView(); |
| 204 aura::client::ScreenPositionClient* screen_position_client = |
| 205 aura::client::GetScreenPositionClient(window->GetRootWindow()); |
| 206 if (screen_position_client) |
| 207 screen_position_client->ConvertPointToScreen(window, point); |
| 208 } |
| 209 |
| 210 void TouchEditableImplAura::ConvertPointFromScreen(gfx::Point* point) { |
| 211 if (!rwhva_) |
| 212 return; |
| 213 aura::Window* window = rwhva_->GetNativeView(); |
| 214 aura::client::ScreenPositionClient* screen_position_client = |
| 215 aura::client::GetScreenPositionClient(window->GetRootWindow()); |
| 216 if (screen_position_client) |
| 217 screen_position_client->ConvertPointFromScreen(window, point); |
| 218 } |
| 219 |
| 220 bool TouchEditableImplAura::DrawsHandles() { |
| 221 return false; |
| 222 } |
| 223 |
| 224 void TouchEditableImplAura::OpenContextMenu(const gfx::Point anchor) { |
| 225 if (!rwhva_) |
| 226 return; |
| 227 RenderWidgetHost* host = rwhva_->GetRenderWidgetHost(); |
| 228 host->Send(new ViewMsg_ShowContextMenu(host->GetRoutingID())); |
| 229 EndTouchEditing(); |
| 230 } |
| 231 |
| 232 bool TouchEditableImplAura::IsCommandIdChecked(int command_id) const { |
| 233 NOTREACHED(); |
| 234 return false; |
| 235 } |
| 236 |
| 237 bool TouchEditableImplAura::IsCommandIdEnabled(int command_id) const { |
| 238 if (!rwhva_) |
| 239 return false; |
| 240 bool editable = rwhva_->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE; |
| 241 ui::Range selection_range; |
| 242 rwhva_->GetSelectionRange(&selection_range); |
| 243 bool has_selection = !selection_range.is_empty(); |
| 244 switch (command_id) { |
| 245 case IDS_APP_CUT: |
| 246 return editable && has_selection; |
| 247 case IDS_APP_COPY: |
| 248 return has_selection; |
| 249 case IDS_APP_PASTE: { |
| 250 string16 result; |
| 251 ui::Clipboard::GetForCurrentThread()->ReadText( |
| 252 ui::Clipboard::BUFFER_STANDARD, &result); |
| 253 return editable && !result.empty(); |
| 254 } |
| 255 case IDS_APP_DELETE: |
| 256 return editable && has_selection; |
| 257 case IDS_APP_SELECT_ALL: |
| 258 return true; |
| 259 default: |
| 260 return false; |
| 261 } |
| 262 } |
| 263 |
| 264 bool TouchEditableImplAura::GetAcceleratorForCommandId( |
| 265 int command_id, |
| 266 ui::Accelerator* accelerator) { |
| 267 return false; |
| 268 } |
| 269 |
| 270 void TouchEditableImplAura::ExecuteCommand(int command_id, int event_flags) { |
| 271 if (!rwhva_) |
| 272 return; |
| 273 RenderWidgetHost* host = rwhva_->GetRenderWidgetHost(); |
| 274 switch (command_id) { |
| 275 case IDS_APP_CUT: |
| 276 host->Cut(); |
| 277 break; |
| 278 case IDS_APP_COPY: |
| 279 host->Copy(); |
| 280 break; |
| 281 case IDS_APP_PASTE: |
| 282 host->Paste(); |
| 283 break; |
| 284 case IDS_APP_DELETE: |
| 285 host->Delete(); |
| 286 break; |
| 287 case IDS_APP_SELECT_ALL: |
| 288 host->SelectAll(); |
| 289 break; |
| 290 default: |
| 291 NOTREACHED(); |
| 292 break; |
| 293 } |
| 294 EndTouchEditing(); |
| 295 } |
| 296 |
| 297 //////////////////////////////////////////////////////////////////////////////// |
| 298 // TouchEditableImplAura, private: |
| 299 |
| 300 TouchEditableImplAura::TouchEditableImplAura() |
| 301 : text_input_type_(ui::TEXT_INPUT_TYPE_NONE), |
| 302 rwhva_(NULL), |
| 303 selection_gesture_in_process_(false) { |
| 304 } |
| 305 |
| 306 void TouchEditableImplAura::Cleanup() { |
| 307 if (rwhva_) { |
| 308 rwhva_->set_touch_editing_client(NULL); |
| 309 rwhva_ = NULL; |
| 310 } |
| 311 touch_selection_controller_.reset(); |
| 312 } |
| 313 |
| 314 } // namespace content |
OLD | NEW |