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 } | |
| 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 | |
|
sky
2013/04/22 14:20:51
nit: {}
varunjain
2013/04/22 14:25:23
Done.
| |
| 69 EndTouchEditing(); | |
| 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 UpdateEditingController(); | |
| 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 //////////////////////////////////////////////////////////////////////////////// | |
| 297 // TouchEditableImplAura, private: | |
| 298 | |
| 299 TouchEditableImplAura::TouchEditableImplAura() | |
| 300 : text_input_type_(ui::TEXT_INPUT_TYPE_NONE), | |
| 301 rwhva_(NULL), | |
| 302 selection_gesture_in_process_(false) { | |
| 303 } | |
| 304 | |
| 305 void TouchEditableImplAura::Cleanup() { | |
| 306 if (rwhva_) { | |
| 307 rwhva_->set_touch_editing_client(NULL); | |
| 308 rwhva_ = NULL; | |
| 309 } | |
| 310 touch_selection_controller_.reset(); | |
| 311 } | |
| 312 | |
| 313 } // namespace content | |
| OLD | NEW |