Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / ash / accelerators / accelerator_dispatcher.cc
blob7d29e68d4379784fc7a0eccc462889adbd34f362
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/events/event.h"
23 #include "ui/events/event_constants.h"
24 #include "ui/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 #elif defined(USE_OZONE)
44 bool IsKeyEvent(const base::NativeEvent& native_event) {
45 const ui::KeyEvent* event = static_cast<const ui::KeyEvent*>(native_event);
46 return event->IsKeyEvent();
48 #endif
50 bool IsPossibleAcceleratorNotForMenu(const ui::KeyEvent& key_event) {
51 // For shortcuts generated by Ctrl or Alt plus a letter, number or
52 // the tab key, we want to exit the context menu first and then
53 // repost the event. That allows for the shortcut execution after
54 // the context menu has exited.
55 if (key_event.type() == ui::ET_KEY_PRESSED &&
56 (key_event.flags() & (ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN))) {
57 const ui::KeyboardCode key_code = key_event.key_code();
58 if ((key_code >= ui::VKEY_A && key_code <= ui::VKEY_Z) ||
59 (key_code >= ui::VKEY_0 && key_code <= ui::VKEY_9) ||
60 (key_code == ui::VKEY_TAB)) {
61 return true;
64 return false;
67 } // namespace
69 AcceleratorDispatcher::AcceleratorDispatcher(
70 base::MessagePumpDispatcher* nested_dispatcher,
71 aura::Window* associated_window)
72 : nested_dispatcher_(nested_dispatcher),
73 associated_window_(associated_window) {
74 DCHECK(nested_dispatcher_);
75 associated_window_->AddObserver(this);
78 AcceleratorDispatcher::~AcceleratorDispatcher() {
79 if (associated_window_)
80 associated_window_->RemoveObserver(this);
83 void AcceleratorDispatcher::OnWindowDestroying(aura::Window* window) {
84 if (associated_window_ == window)
85 associated_window_ = NULL;
88 bool AcceleratorDispatcher::Dispatch(const base::NativeEvent& event) {
89 if (!associated_window_)
90 return false;
91 if (!ui::IsNoopEvent(event) && !associated_window_->CanReceiveEvents())
92 return aura::Env::GetInstance()->GetDispatcher()->Dispatch(event);
94 if (IsKeyEvent(event)) {
95 // Modifiers can be changed by the user preference, so we need to rewrite
96 // the event explicitly.
97 ui::KeyEvent key_event(event, false);
98 ui::EventHandler* event_rewriter =
99 ash::Shell::GetInstance()->event_rewriter_filter();
100 DCHECK(event_rewriter);
101 event_rewriter->OnKeyEvent(&key_event);
102 if (key_event.stopped_propagation())
103 return true;
105 if (IsPossibleAcceleratorNotForMenu(key_event)) {
106 if (views::MenuController* menu_controller =
107 views::MenuController::GetActiveInstance()) {
108 menu_controller->CancelAll();
109 #if defined(USE_X11)
110 XPutBackEvent(event->xany.display, event);
111 #else
112 NOTIMPLEMENTED() << " Repost NativeEvent here.";
113 #endif
114 return false;
118 ash::AcceleratorController* accelerator_controller =
119 ash::Shell::GetInstance()->accelerator_controller();
120 if (accelerator_controller) {
121 ui::Accelerator accelerator(key_event.key_code(),
122 key_event.flags() & kModifierMask);
123 if (key_event.type() == ui::ET_KEY_RELEASED)
124 accelerator.set_type(ui::ET_KEY_RELEASED);
125 // Fill out context object so AcceleratorController will know what
126 // was the previous accelerator or if the current accelerator is repeated.
127 Shell::GetInstance()->accelerator_controller()->context()->
128 UpdateContext(accelerator);
129 if (accelerator_controller->Process(accelerator))
130 return true;
133 return nested_dispatcher_->Dispatch(key_event.native_event());
136 return nested_dispatcher_->Dispatch(event);
139 } // namespace ash