OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "chrome/browser/chromeos/accessibility/select_to_speak_event_handler.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h" |
| 9 #include "ui/aura/window.h" |
| 10 #include "ui/display/display.h" |
| 11 #include "ui/events/event.h" |
| 12 #include "ui/views/focus/view_storage.h" |
| 13 #include "ui/views/view.h" |
| 14 #include "ui/views/widget/widget.h" |
| 15 |
| 16 namespace chromeos { |
| 17 |
| 18 SelectToSpeakEventHandler::SelectToSpeakEventHandler() { |
| 19 if (ash::Shell::HasInstance()) |
| 20 ash::Shell::GetInstance()->AddPreTargetHandler(this); |
| 21 } |
| 22 |
| 23 SelectToSpeakEventHandler::~SelectToSpeakEventHandler() { |
| 24 if (ash::Shell::HasInstance()) |
| 25 ash::Shell::GetInstance()->RemovePreTargetHandler(this); |
| 26 } |
| 27 |
| 28 void SelectToSpeakEventHandler::OnKeyEvent(ui::KeyEvent* event) { |
| 29 if (event->key_code() == ui::VKEY_LWIN) { |
| 30 if (event->type() == ui::ET_KEY_PRESSED) { |
| 31 state_ = SEARCH_DOWN; |
| 32 } else if (event->type() == ui::ET_KEY_RELEASED) { |
| 33 if (state_ == CAPTURING) { |
| 34 SendCancelAXEvent(); |
| 35 CancelEvent(event); |
| 36 state_ = WAIT_FOR_MOUSE_RELEASE; |
| 37 } else if (state_ == MOUSE_RELEASED) { |
| 38 CancelEvent(event); |
| 39 state_ = INACTIVE; |
| 40 } |
| 41 } |
| 42 } else if (state_ == SEARCH_DOWN) { |
| 43 state_ = INACTIVE; |
| 44 } |
| 45 } |
| 46 |
| 47 void SelectToSpeakEventHandler::OnMouseEvent(ui::MouseEvent* event) { |
| 48 if (state_ == INACTIVE) |
| 49 return; |
| 50 |
| 51 if ((state_ == SEARCH_DOWN || state_ == MOUSE_RELEASED) && |
| 52 event->type() == ui::ET_MOUSE_PRESSED) { |
| 53 state_ = CAPTURING; |
| 54 } |
| 55 |
| 56 if (state_ == WAIT_FOR_MOUSE_RELEASE && |
| 57 event->type() == ui::ET_MOUSE_RELEASED) { |
| 58 CancelEvent(event); |
| 59 state_ = INACTIVE; |
| 60 return; |
| 61 } |
| 62 |
| 63 if (state_ != CAPTURING) |
| 64 return; |
| 65 |
| 66 // If we're in the capturing state, send accessibility events to |
| 67 // the Select-to-speak extension based on the mouse event. |
| 68 // First, figure out what event to send. |
| 69 ui::AXEvent ax_event = ui::AX_EVENT_NONE; |
| 70 switch (event->type()) { |
| 71 case ui::ET_MOUSE_PRESSED: |
| 72 ax_event = ui::AX_EVENT_MOUSE_PRESSED; |
| 73 break; |
| 74 case ui::ET_MOUSE_DRAGGED: |
| 75 ax_event = ui::AX_EVENT_MOUSE_DRAGGED; |
| 76 break; |
| 77 case ui::ET_MOUSE_RELEASED: |
| 78 state_ = MOUSE_RELEASED; |
| 79 ax_event = ui::AX_EVENT_MOUSE_RELEASED; |
| 80 break; |
| 81 case ui::ET_MOUSE_MOVED: |
| 82 case ui::ET_MOUSE_ENTERED: |
| 83 case ui::ET_MOUSE_EXITED: |
| 84 ax_event = ui::AX_EVENT_MOUSE_MOVED; |
| 85 break; |
| 86 default: |
| 87 return; |
| 88 } |
| 89 |
| 90 CancelEvent(event); |
| 91 |
| 92 // Find the View to post the accessibility event on. |
| 93 aura::Window* event_target = static_cast<aura::Window*>(event->target()); |
| 94 aura::Window* hit_window = event_target; |
| 95 if (!hit_window) |
| 96 return; |
| 97 |
| 98 views::Widget* hit_widget = views::Widget::GetWidgetForNativeView(hit_window); |
| 99 while (!hit_widget) { |
| 100 hit_window = hit_window->parent(); |
| 101 if (!hit_window) |
| 102 break; |
| 103 |
| 104 hit_widget = views::Widget::GetWidgetForNativeView(hit_window); |
| 105 } |
| 106 |
| 107 if (!hit_window || !hit_widget) |
| 108 return; |
| 109 |
| 110 gfx::Point window_location = event->location(); |
| 111 aura::Window::ConvertPointToTarget(event_target, hit_window, |
| 112 &window_location); |
| 113 |
| 114 views::View* root_view = hit_widget->GetRootView(); |
| 115 views::View* hit_view = root_view->GetEventHandlerForPoint(window_location); |
| 116 |
| 117 if (hit_view) { |
| 118 // Send the accessibility event, then save the view so we can post the |
| 119 // cancel event on the same view if possible. |
| 120 hit_view->NotifyAccessibilityEvent(ax_event, true); |
| 121 if (!last_view_storage_id_) { |
| 122 last_view_storage_id_ = |
| 123 views::ViewStorage::GetInstance()->CreateStorageID(); |
| 124 } |
| 125 views::ViewStorage::GetInstance()->RemoveView(last_view_storage_id_); |
| 126 views::ViewStorage::GetInstance()->StoreView(last_view_storage_id_, |
| 127 hit_view); |
| 128 } |
| 129 } |
| 130 |
| 131 void SelectToSpeakEventHandler::CancelEvent(ui::Event* event) { |
| 132 if (event->cancelable()) { |
| 133 event->SetHandled(); |
| 134 event->StopPropagation(); |
| 135 } |
| 136 } |
| 137 |
| 138 void SelectToSpeakEventHandler::SendCancelAXEvent() { |
| 139 // If the user releases Search while the button is still down, cancel |
| 140 // the Select-to-speak gesture. Try to post it on the same View that |
| 141 // was last targeted, but if that's impossible, post it on the desktop. |
| 142 views::View* last_view = |
| 143 last_view_storage_id_ |
| 144 ? views::ViewStorage::GetInstance()->RetrieveView( |
| 145 last_view_storage_id_) |
| 146 : nullptr; |
| 147 if (last_view) { |
| 148 last_view->NotifyAccessibilityEvent(ui::AX_EVENT_MOUSE_CANCELED, true); |
| 149 } else { |
| 150 AutomationManagerAura::GetInstance()->HandleEvent( |
| 151 nullptr, nullptr, ui::AX_EVENT_MOUSE_CANCELED); |
| 152 } |
| 153 } |
| 154 |
| 155 } // namespace chromeos |
OLD | NEW |