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; | |
David Tseng
2016/11/11 19:18:30
Should you set state to inactive for all other key
dmazzoni
2016/11/14 22:11:41
Great idea. Done, and added a new test for this ca
| |
40 } | |
41 } | |
42 } | |
43 } | |
44 | |
45 void SelectToSpeakEventHandler::OnMouseEvent(ui::MouseEvent* event) { | |
46 if (state_ == INACTIVE) | |
47 return; | |
48 | |
49 if ((state_ == SEARCH_DOWN || state_ == MOUSE_RELEASED) && | |
David Tseng
2016/11/11 19:18:30
How do we go from mouse released -> mouse pressed
dmazzoni
2016/11/14 22:11:42
This is to support the user holding down search an
| |
50 event->type() == ui::ET_MOUSE_PRESSED) { | |
51 state_ = CAPTURING; | |
52 } | |
53 | |
54 if (state_ == WAIT_FOR_MOUSE_RELEASE && | |
55 event->type() == ui::ET_MOUSE_RELEASED) { | |
56 CancelEvent(event); | |
57 state_ = INACTIVE; | |
58 return; | |
59 } | |
60 | |
61 if (state_ != CAPTURING) | |
David Tseng
2016/11/11 19:18:30
Rather than one state, I think it would be a lot c
dmazzoni
2016/11/14 22:11:41
That doesn't handle all of the current states. For
| |
62 return; | |
63 | |
64 // If we're in the capturing state, send accessibility events to | |
65 // the Select-to-speak extension based on the mouse event. | |
66 // First, figure out what event to send. | |
67 ui::AXEvent ax_event = ui::AX_EVENT_NONE; | |
68 switch (event->type()) { | |
69 case ui::ET_MOUSE_PRESSED: | |
70 ax_event = ui::AX_EVENT_MOUSE_PRESSED; | |
71 break; | |
72 case ui::ET_MOUSE_DRAGGED: | |
73 ax_event = ui::AX_EVENT_MOUSE_DRAGGED; | |
74 break; | |
75 case ui::ET_MOUSE_RELEASED: | |
76 state_ = MOUSE_RELEASED; | |
77 ax_event = ui::AX_EVENT_MOUSE_RELEASED; | |
78 break; | |
79 case ui::ET_MOUSE_MOVED: | |
80 case ui::ET_MOUSE_ENTERED: | |
81 case ui::ET_MOUSE_EXITED: | |
82 ax_event = ui::AX_EVENT_MOUSE_MOVED; | |
83 break; | |
84 default: | |
85 return; | |
86 } | |
87 | |
88 CancelEvent(event); | |
89 | |
90 // Find the View to post the accessibility event on. | |
91 aura::Window* event_target = static_cast<aura::Window*>(event->target()); | |
92 aura::Window* hit_window = event_target; | |
93 if (!hit_window) | |
94 return; | |
95 | |
96 views::Widget* hit_widget = views::Widget::GetWidgetForNativeView(hit_window); | |
97 while (!hit_widget) { | |
98 hit_window = hit_window->parent(); | |
99 if (!hit_window) | |
100 break; | |
101 | |
102 hit_widget = views::Widget::GetWidgetForNativeView(hit_window); | |
103 } | |
104 | |
105 if (!hit_window || !hit_widget) | |
106 return; | |
107 | |
108 gfx::Point window_location = event->location(); | |
109 aura::Window::ConvertPointToTarget(event_target, hit_window, | |
110 &window_location); | |
111 | |
112 views::View* root_view = hit_widget->GetRootView(); | |
113 views::View* hit_view = root_view->GetEventHandlerForPoint(window_location); | |
114 | |
115 if (hit_view) { | |
116 // Send the accessibility event, then save the view so we can post the | |
117 // cancel event on the same view if possible. | |
118 hit_view->NotifyAccessibilityEvent(ax_event, true); | |
119 if (!last_view_storage_id_) { | |
120 last_view_storage_id_ = | |
121 views::ViewStorage::GetInstance()->CreateStorageID(); | |
122 } | |
123 views::ViewStorage::GetInstance()->RemoveView(last_view_storage_id_); | |
124 views::ViewStorage::GetInstance()->StoreView(last_view_storage_id_, | |
125 hit_view); | |
126 } | |
127 } | |
128 | |
129 void SelectToSpeakEventHandler::CancelEvent(ui::Event* event) { | |
130 if (event->cancelable()) { | |
131 event->SetHandled(); | |
132 event->StopPropagation(); | |
133 } | |
134 } | |
135 | |
136 void SelectToSpeakEventHandler::SendCancelAXEvent() { | |
137 // If the user releases Search while the button is still down, cancel | |
138 // the Select-to-speak gesture. Try to post it on the same View that | |
139 // was last targeted, but if that's impossible, post it on the desktop. | |
140 views::View* last_view = | |
141 last_view_storage_id_ | |
142 ? views::ViewStorage::GetInstance()->RetrieveView( | |
143 last_view_storage_id_) | |
144 : nullptr; | |
145 if (last_view) { | |
146 last_view->NotifyAccessibilityEvent(ui::AX_EVENT_MOUSE_CANCELED, true); | |
147 } else { | |
148 AutomationManagerAura::GetInstance()->HandleEvent( | |
149 nullptr, nullptr, ui::AX_EVENT_MOUSE_CANCELED); | |
150 } | |
151 } | |
152 | |
153 } // namespace chromeos | |
OLD | NEW |