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