Separate projection mode from rest of touch HUD
[chromium-blink-merge.git] / ash / accelerators / accelerator_dispatcher.cc
blob91f01540f63e8c1a638b06d8a0f0a472160acae6
1 // Copyright (c) 2012 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.
5 #include "ash/accelerators/accelerator_dispatcher.h"
7 #if defined(USE_X11)
8 #include <X11/Xlib.h>
10 // Xlib defines RootWindow
11 #ifdef RootWindow
12 #undef RootWindow
13 #endif
14 #endif // defined(USE_X11)
16 #include "ash/accelerators/accelerator_controller.h"
17 #include "ash/shell.h"
18 #include "ash/wm/event_rewriter_event_filter.h"
19 #include "ui/aura/env.h"
20 #include "ui/aura/root_window.h"
21 #include "ui/base/accelerators/accelerator.h"
22 #include "ui/base/events/event.h"
23 #include "ui/base/events/event_constants.h"
24 #include "ui/base/events/event_utils.h"
25 #include "ui/views/controls/menu/menu_controller.h"
27 namespace ash {
28 namespace {
30 const int kModifierMask = (ui::EF_SHIFT_DOWN |
31 ui::EF_CONTROL_DOWN |
32 ui::EF_ALT_DOWN);
33 #if defined(OS_WIN)
34 bool IsKeyEvent(const MSG& msg) {
35 return
36 msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN ||
37 msg.message == WM_KEYUP || msg.message == WM_SYSKEYUP;
39 #elif defined(USE_X11)
40 bool IsKeyEvent(const XEvent* xev) {
41 return xev->type == KeyPress || xev->type == KeyRelease;
43 #endif
45 bool IsPossibleAcceleratorNotForMenu(const ui::KeyEvent& key_event) {
46 // For shortcuts generated by Ctrl or Alt plus a letter, number or
47 // the tab key, we want to exit the context menu first and then
48 // repost the event. That allows for the shortcut execution after
49 // the context menu has exited.
50 if (key_event.type() == ui::ET_KEY_PRESSED &&
51 (key_event.flags() & (ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN))) {
52 const ui::KeyboardCode key_code = key_event.key_code();
53 if ((key_code >= ui::VKEY_A && key_code <= ui::VKEY_Z) ||
54 (key_code >= ui::VKEY_0 && key_code <= ui::VKEY_9) ||
55 (key_code == ui::VKEY_TAB)) {
56 return true;
59 return false;
62 } // namespace
64 AcceleratorDispatcher::AcceleratorDispatcher(
65 base::MessageLoop::Dispatcher* nested_dispatcher,
66 aura::Window* associated_window)
67 : nested_dispatcher_(nested_dispatcher),
68 associated_window_(associated_window) {
69 DCHECK(nested_dispatcher_);
70 associated_window_->AddObserver(this);
73 AcceleratorDispatcher::~AcceleratorDispatcher() {
74 if (associated_window_)
75 associated_window_->RemoveObserver(this);
78 void AcceleratorDispatcher::OnWindowDestroying(aura::Window* window) {
79 if (associated_window_ == window)
80 associated_window_ = NULL;
83 bool AcceleratorDispatcher::Dispatch(const base::NativeEvent& event) {
84 if (!associated_window_)
85 return false;
86 if (!ui::IsNoopEvent(event) && !associated_window_->CanReceiveEvents())
87 return aura::Env::GetInstance()->GetDispatcher()->Dispatch(event);
89 if (IsKeyEvent(event)) {
90 // Modifiers can be changed by the user preference, so we need to rewrite
91 // the event explicitly.
92 ui::KeyEvent key_event(event, false);
93 ui::EventHandler* event_rewriter =
94 ash::Shell::GetInstance()->event_rewriter_filter();
95 DCHECK(event_rewriter);
96 event_rewriter->OnKeyEvent(&key_event);
97 if (key_event.stopped_propagation())
98 return true;
100 if (IsPossibleAcceleratorNotForMenu(key_event)) {
101 if (views::MenuController* menu_controller =
102 views::MenuController::GetActiveInstance()) {
103 menu_controller->CancelAll();
104 #if defined(USE_X11)
105 XPutBackEvent(event->xany.display, event);
106 #else
107 NOTIMPLEMENTED() << " Repost NativeEvent here.";
108 #endif
109 return false;
113 ash::AcceleratorController* accelerator_controller =
114 ash::Shell::GetInstance()->accelerator_controller();
115 if (accelerator_controller) {
116 ui::Accelerator accelerator(key_event.key_code(),
117 key_event.flags() & kModifierMask);
118 if (key_event.type() == ui::ET_KEY_RELEASED)
119 accelerator.set_type(ui::ET_KEY_RELEASED);
120 // Fill out context object so AcceleratorController will know what
121 // was the previous accelerator or if the current accelerator is repeated.
122 Shell::GetInstance()->accelerator_controller()->context()->
123 UpdateContext(accelerator);
124 if (accelerator_controller->Process(accelerator))
125 return true;
128 return nested_dispatcher_->Dispatch(key_event.native_event());
131 return nested_dispatcher_->Dispatch(event);
134 } // namespace ash