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

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

Issue 155493002: Add key based scrolling on magnified screen for kiosk mode. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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, aura::WindowTracker* tracker) {
47 // The target window may be gone.
48 if (tracker->windows().empty())
49 return;
sadrul 2014/02/08 16:34:03 |tracker| leaks here, right? Why not use a scoped
oshima 2014/02/09 02:06:17 good idea. done.
50 aura::Window* target = *(tracker->windows().begin());
51 delete tracker;
52
53 ui::KeyEvent event(&native_event, false);
54 event.set_flags(event.flags() | ui::EF_IS_SYNTHESIZED);
55 ui::Event::DispatcherApi dispatcher(&event);
56 dispatcher.set_target(target);
sadrul 2014/02/08 16:34:03 You shouldn't need to use DispatcherApi here (and
oshima 2014/02/09 02:06:17 Thanks, done.
57 ui::EventDispatchDetails result ALLOW_UNUSED =
58 target->GetDispatcher()->OnEventFromSource(&event);
59 }
60
61 void PostPressedEvent(ui::KeyEvent* event) {
62 // Modify RELEASED event to PRESSED event.
63 XEvent xkey = *(event->native_event());
64 xkey.xkey.type = KeyPress;
65 xkey.xkey.state |= ShiftMask;
66 aura::WindowTracker* tracker = new aura::WindowTracker();
67 tracker->Add(static_cast<aura::Window*>(event->target()));
68
69 base::MessageLoopForUI::current()->PostTask(
70 FROM_HERE,
71 base::Bind(&DispatchPressedEvent, xkey, tracker));
72 }
73
74 } // namespace
75
76 // static
77 bool MagnifierKeyScroller::IsEnabled() {
78 return (magnifier_key_scroller_enabled ||
79 CommandLine::ForCurrentProcess()->HasSwitch(
80 switches::kAshEnableMagnifierKeyScroller)) &&
81 ash::Shell::GetInstance()->magnification_controller()->IsEnabled();
82 }
83
84 // static
85 void MagnifierKeyScroller::SetEnabled(bool enabled) {
86 magnifier_key_scroller_enabled = enabled;
87 }
88
89 MagnifierKeyScroller::MagnifierKeyScroller()
90 : state_(INITIAL) {}
91
92 MagnifierKeyScroller::~MagnifierKeyScroller() {}
93
94 void MagnifierKeyScroller::OnKeyEvent(
95 ui::KeyEvent* event) {
96 if (!IsEnabled())
97 return;
98
99 if (event->key_code() != ui::VKEY_UP &&
100 event->key_code() != ui::VKEY_DOWN &&
101 event->key_code() != ui::VKEY_LEFT &&
102 event->key_code() != ui::VKEY_RIGHT) {
103 return;
104 }
105
106 if (event->type() == ui::ET_KEY_PRESSED &&
107 event->flags() & ui::EF_SHIFT_DOWN) {
108 switch (state_) {
109 case INITIAL:
110 // Pass through posted event.
111 if (event->flags() & ui::EF_IS_SYNTHESIZED) {
112 event->set_flags(event->flags() & ~ui::EF_IS_SYNTHESIZED);
113 return;
114 }
115 state_ = PRESSED;
116 // Don't process ET_KEY_PRESSED event yet. The ET_KEY_PRESSED
117 // event will be generated upon ET_KEY_RELEASEED event below.
118 event->StopPropagation();
119 break;
120 case PRESSED:
121 state_ = HOLD;
122 // pass through
123 case HOLD:
124 ScrollScreen(event);
125 event->StopPropagation();
126 break;
127 }
128 } else if (event->type() == ui::ET_KEY_RELEASED) {
129 switch (state_) {
130 case INITIAL:
131 break;
132 case PRESSED: {
133 PostPressedEvent(event);
134 event->StopPropagation();
135 break;
136 }
137 case HOLD: {
138 MagnificationController* controller =
139 Shell::GetInstance()->magnification_controller();
140 controller->SetScrollDirection(MagnificationController::SCROLL_NONE);
141 event->StopPropagation();
142 break;
143 }
144 }
145 state_ = INITIAL;
146 }
147 }
148
149 } // 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