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 RenderWidgetHostImpl* host = RenderWidgetHostImpl::From( |
| 169 rwhva_->GetRenderWidgetHost()); |
| 170 host->SelectRange(start, end); |
| 171 } |
| 172 |
| 173 void TouchEditableImplAura::MoveCaretTo(const gfx::Point& point) { |
| 174 RenderWidgetHostImpl* host = RenderWidgetHostImpl::From( |
| 175 rwhva_->GetRenderWidgetHost()); |
| 176 host->MoveCaret(point); |
| 177 } |
| 178 |
| 179 void TouchEditableImplAura::GetSelectionEndPoints(gfx::Rect* p1, |
| 180 gfx::Rect* p2) { |
| 181 *p1 = selection_anchor_rect_; |
| 182 *p2 = selection_focus_rect_; |
| 183 } |
| 184 |
| 185 gfx::Rect TouchEditableImplAura::GetBounds() { |
| 186 return rwhva_ ? rwhva_->GetNativeView()->bounds() : gfx::Rect(); |
| 187 } |
| 188 |
| 189 gfx::NativeView TouchEditableImplAura::GetNativeView() { |
| 190 return rwhva_ ? rwhva_->GetNativeView()->GetRootWindow() : NULL; |
| 191 } |
| 192 |
| 193 void TouchEditableImplAura::ConvertPointToScreen(gfx::Point* point) { |
| 194 if (!rwhva_) |
| 195 return; |
| 196 aura::Window* window = rwhva_->GetNativeView(); |
| 197 aura::client::ScreenPositionClient* screen_position_client = |
| 198 aura::client::GetScreenPositionClient(window->GetRootWindow()); |
| 199 if (screen_position_client) |
| 200 screen_position_client->ConvertPointToScreen(window, point); |
| 201 } |
| 202 |
| 203 void TouchEditableImplAura::ConvertPointFromScreen(gfx::Point* point) { |
| 204 if (!rwhva_) |
| 205 return; |
| 206 aura::Window* window = rwhva_->GetNativeView(); |
| 207 aura::client::ScreenPositionClient* screen_position_client = |
| 208 aura::client::GetScreenPositionClient(window->GetRootWindow()); |
| 209 if (screen_position_client) |
| 210 screen_position_client->ConvertPointFromScreen(window, point); |
| 211 } |
| 212 |
| 213 bool TouchEditableImplAura::DrawsHandles() { |
| 214 return false; |
| 215 } |
| 216 |
| 217 void TouchEditableImplAura::OpenContextMenu(const gfx::Point anchor) { |
| 218 if (!rwhva_) |
| 219 return; |
| 220 RenderWidgetHost* host = rwhva_->GetRenderWidgetHost(); |
| 221 host->Send(new ViewMsg_ShowContextMenu(host->GetRoutingID())); |
| 222 EndTouchEditing(); |
| 223 } |
| 224 |
| 225 bool TouchEditableImplAura::IsCommandIdChecked(int command_id) const { |
| 226 NOTREACHED(); |
| 227 return false; |
| 228 } |
| 229 |
| 230 bool TouchEditableImplAura::IsCommandIdEnabled(int command_id) const { |
| 231 if (!rwhva_) |
| 232 return false; |
| 233 bool editable = rwhva_->GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE; |
| 234 ui::Range selection_range; |
| 235 rwhva_->GetSelectionRange(&selection_range); |
| 236 bool has_selection = !selection_range.is_empty(); |
| 237 switch (command_id) { |
| 238 case IDS_APP_CUT: |
| 239 return editable && has_selection; |
| 240 case IDS_APP_COPY: |
| 241 return has_selection; |
| 242 case IDS_APP_PASTE: { |
| 243 string16 result; |
| 244 ui::Clipboard::GetForCurrentThread()->ReadText( |
| 245 ui::Clipboard::BUFFER_STANDARD, &result); |
| 246 return editable && !result.empty(); |
| 247 } |
| 248 case IDS_APP_DELETE: |
| 249 return editable && has_selection; |
| 250 case IDS_APP_SELECT_ALL: |
| 251 return true; |
| 252 default: |
| 253 return false; |
| 254 } |
| 255 } |
| 256 |
| 257 bool TouchEditableImplAura::GetAcceleratorForCommandId( |
| 258 int command_id, |
| 259 ui::Accelerator* accelerator) { |
| 260 return false; |
| 261 } |
| 262 |
| 263 void TouchEditableImplAura::ExecuteCommand(int command_id, int event_flags) { |
| 264 if (!rwhva_) |
| 265 return; |
| 266 RenderWidgetHost* host = rwhva_->GetRenderWidgetHost(); |
| 267 switch (command_id) { |
| 268 case IDS_APP_CUT: |
| 269 host->Cut(); |
| 270 break; |
| 271 case IDS_APP_COPY: |
| 272 host->Copy(); |
| 273 break; |
| 274 case IDS_APP_PASTE: |
| 275 host->Paste(); |
| 276 break; |
| 277 case IDS_APP_DELETE: |
| 278 host->Delete(); |
| 279 break; |
| 280 case IDS_APP_SELECT_ALL: |
| 281 host->SelectAll(); |
| 282 break; |
| 283 default: |
| 284 NOTREACHED(); |
| 285 break; |
| 286 } |
| 287 EndTouchEditing(); |
| 288 } |
| 289 |
| 290 void TouchEditableImplAura::OnWindowParentChanged(aura::Window* window, |
| 291 aura::Window* parent) { |
| 292 if (window == rwhva_->GetNativeView()) { |
| 293 if (top_level_window_) |
| 294 top_level_window_->RemoveObserver(this); |
| 295 |
| 296 top_level_window_ = GetToplevelWindowForWindow(window); |
| 297 if (top_level_window_) |
| 298 top_level_window_->AddObserver(this); |
| 299 } |
| 300 } |
| 301 |
| 302 void TouchEditableImplAura::OnWindowBoundsChanged(aura::Window* window, |
| 303 const gfx::Rect& old_bounds, |
| 304 const gfx::Rect& new_bounds) { |
| 305 if (top_level_window_ == window && touch_selection_controller_) |
| 306 touch_selection_controller_->SelectionChanged(); |
| 307 } |
| 308 |
| 309 void TouchEditableImplAura::OnWindowDestroying(aura::Window* window) { |
| 310 if (top_level_window_ == window) { |
| 311 top_level_window_->RemoveObserver(this); |
| 312 top_level_window_ = NULL; |
| 313 } else if (rwhva_->GetNativeView() == window) |
| 314 rwhva_->GetNativeView()->RemoveObserver(this); |
| 315 } |
| 316 |
| 317 //////////////////////////////////////////////////////////////////////////////// |
| 318 // TouchEditableImplAura, private: |
| 319 |
| 320 TouchEditableImplAura::TouchEditableImplAura() |
| 321 : text_input_type_(ui::TEXT_INPUT_TYPE_NONE), |
| 322 rwhva_(NULL), |
| 323 top_level_window_(NULL), |
| 324 selection_gesture_in_process_(false) { |
| 325 } |
| 326 |
| 327 void TouchEditableImplAura::UpdateController() { |
| 328 DCHECK(rwhva_); |
| 329 |
| 330 // If touch editing handles were not visible, we bring them up only if |
| 331 // there is non-zero selection on the page. And the current event is a |
| 332 // gesture event (we dont want to show handles if the user is selecting |
| 333 // using mouse or keyboard). |
| 334 if (selection_gesture_in_process_ && |
| 335 selection_anchor_rect_ != selection_focus_rect_) |
| 336 StartTouchEditing(); |
| 337 |
| 338 if (text_input_type_ != ui::TEXT_INPUT_TYPE_NONE || |
| 339 selection_anchor_rect_ != selection_focus_rect_) { |
| 340 if (touch_selection_controller_) |
| 341 touch_selection_controller_->SelectionChanged(); |
| 342 } else |
| 343 EndTouchEditing(); |
| 344 } |
| 345 |
| 346 void TouchEditableImplAura::Cleanup() { |
| 347 if (top_level_window_) |
| 348 top_level_window_->RemoveObserver(this); |
| 349 if (rwhva_) { |
| 350 if (rwhva_->GetNativeView()) |
| 351 rwhva_->GetNativeView()->RemoveObserver(this); |
| 352 rwhva_->set_touch_editing_client(NULL); |
| 353 rwhva_ = NULL; |
| 354 } |
| 355 touch_selection_controller_.reset(); |
| 356 } |
| 357 |
| 358 } // namespace content |
OLD | NEW |