Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: ash/magnifier/magnifier_key_scroller.cc

Issue 164823005: Long press menu (shift+f6) to toggle spoken feedback. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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 "ash/magnifier/magnifier_key_scroller.h"
6
7 #include <X11/Xlib.h>
8
9 #undef RootWindow
10 #undef Status
11
12 #include "ash/ash_switches.h"
13 #include "ash/magnifier/magnification_controller.h"
14 #include "ash/shell.h"
15 #include "base/command_line.h"
16 #include "base/message_loop/message_loop.h"
17 #include "ui/aura/root_window.h"
18 #include "ui/aura/window_tracker.h"
19
20 namespace ash {
21 namespace {
22
23 bool magnifier_key_scroller_enabled = false;
24
25 void ScrollScreen(ui::KeyEvent* event) {
26 MagnificationController* controller =
27 Shell::GetInstance()->magnification_controller();
28 switch (event->key_code()) {
29 case ui::VKEY_UP:
30 controller->SetScrollDirection(MagnificationController::SCROLL_UP);
31 break;
32 case ui::VKEY_DOWN:
33 controller->SetScrollDirection(MagnificationController::SCROLL_DOWN);
34 break;
35 case ui::VKEY_LEFT:
36 controller->SetScrollDirection(MagnificationController::SCROLL_LEFT);
37 break;
38 case ui::VKEY_RIGHT:
39 controller->SetScrollDirection(MagnificationController::SCROLL_RIGHT);
40 break;
41 default:
42 NOTREACHED() << "Unknown keyboard_code:" << event->key_code();
43 }
44 }
45
46 void DispatchPressedEvent(XEvent native_event,
47 scoped_ptr<aura::WindowTracker> tracker) {
48 // The target window may be gone.
49 if (tracker->windows().empty())
50 return;
51 aura::Window* target = *(tracker->windows().begin());
52 ui::KeyEvent event(&native_event, false);
53 event.set_flags(event.flags() | ui::EF_IS_SYNTHESIZED);
54 ui::EventDispatchDetails result ALLOW_UNUSED =
55 target->GetDispatcher()->OnEventFromSource(&event);
56 }
57
58 void PostPressedEvent(ui::KeyEvent* event) {
59 // Modify RELEASED event to PRESSED event.
60 XEvent xkey = *(event->native_event());
61 xkey.xkey.type = KeyPress;
62 xkey.xkey.state |= ShiftMask;
63 scoped_ptr<aura::WindowTracker> tracker(new aura::WindowTracker);
64 tracker->Add(static_cast<aura::Window*>(event->target()));
65
66 base::MessageLoopForUI::current()->PostTask(
67 FROM_HERE,
68 base::Bind(&DispatchPressedEvent, xkey, base::Passed(&tracker)));
69 }
70
71 } // namespace
72
73 // static
74 bool MagnifierKeyScroller::IsEnabled() {
75 return (magnifier_key_scroller_enabled ||
76 CommandLine::ForCurrentProcess()->HasSwitch(
77 switches::kAshEnableMagnifierKeyScroller)) &&
78 ash::Shell::GetInstance()->magnification_controller()->IsEnabled();
79 }
80
81 // static
82 void MagnifierKeyScroller::SetEnabled(bool enabled) {
83 magnifier_key_scroller_enabled = enabled;
84 }
85
86 MagnifierKeyScroller::MagnifierKeyScroller()
87 : state_(INITIAL) {}
88
89 MagnifierKeyScroller::~MagnifierKeyScroller() {}
90
91 void MagnifierKeyScroller::OnKeyEvent(
92 ui::KeyEvent* event) {
93 if (!IsEnabled())
94 return;
95
96 if (event->key_code() != ui::VKEY_UP &&
97 event->key_code() != ui::VKEY_DOWN &&
98 event->key_code() != ui::VKEY_LEFT &&
99 event->key_code() != ui::VKEY_RIGHT) {
100 return;
101 }
102
103 if (event->type() == ui::ET_KEY_PRESSED &&
104 event->flags() & ui::EF_SHIFT_DOWN) {
105 switch (state_) {
106 case INITIAL:
107 // Pass through posted event.
108 if (event->flags() & ui::EF_IS_SYNTHESIZED) {
109 event->set_flags(event->flags() & ~ui::EF_IS_SYNTHESIZED);
110 return;
111 }
112 state_ = PRESSED;
113 // Don't process ET_KEY_PRESSED event yet. The ET_KEY_PRESSED
114 // event will be generated upon ET_KEY_RELEASEED event below.
115 event->StopPropagation();
116 break;
117 case PRESSED:
118 state_ = HOLD;
119 // pass through
120 case HOLD:
121 ScrollScreen(event);
122 event->StopPropagation();
123 break;
124 }
125 } else if (event->type() == ui::ET_KEY_RELEASED) {
126 switch (state_) {
127 case INITIAL:
128 break;
129 case PRESSED: {
130 PostPressedEvent(event);
131 event->StopPropagation();
132 break;
133 }
134 case HOLD: {
135 MagnificationController* controller =
136 Shell::GetInstance()->magnification_controller();
137 controller->SetScrollDirection(MagnificationController::SCROLL_NONE);
138 event->StopPropagation();
139 break;
140 }
141 }
142 state_ = INITIAL;
143 }
144 }
145
146 } // namespace ash
OLDNEW
« no previous file with comments | « ash/magnifier/magnifier_key_scroller.h ('k') | ash/magnifier/magnifier_key_scroller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698