[Mac] Remove NSApplication from the renderer.
[chromium-blink-merge.git] / ash / accelerators / accelerator_dispatcher.cc
blob0395ee7aabd0c1c4b78acd49501020b98fbba5d8
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 "ui/aura/env.h"
19 #include "ui/aura/window_event_dispatcher.h"
20 #include "ui/base/accelerators/accelerator.h"
21 #include "ui/events/event.h"
22 #include "ui/events/event_constants.h"
23 #include "ui/events/event_utils.h"
24 #include "ui/views/controls/menu/menu_controller.h"
26 namespace ash {
27 namespace {
29 const int kModifierMask = (ui::EF_SHIFT_DOWN |
30 ui::EF_CONTROL_DOWN |
31 ui::EF_ALT_DOWN);
32 #if defined(OS_WIN)
33 bool IsKeyEvent(const MSG& msg) {
34 return
35 msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN ||
36 msg.message == WM_KEYUP || msg.message == WM_SYSKEYUP;
38 #elif defined(USE_X11)
39 bool IsKeyEvent(const XEvent* xev) {
40 return xev->type == KeyPress || xev->type == KeyRelease;
42 #elif defined(USE_OZONE)
43 bool IsKeyEvent(const base::NativeEvent& native_event) {
44 const ui::KeyEvent* event = static_cast<const ui::KeyEvent*>(native_event);
45 return event->IsKeyEvent();
47 #endif
49 bool IsPossibleAcceleratorNotForMenu(const ui::KeyEvent& key_event) {
50 // For shortcuts generated by Ctrl or Alt plus a letter, number or
51 // the tab key, we want to exit the context menu first and then
52 // repost the event. That allows for the shortcut execution after
53 // the context menu has exited.
54 if (key_event.type() == ui::ET_KEY_PRESSED &&
55 (key_event.flags() & (ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN))) {
56 const ui::KeyboardCode key_code = key_event.key_code();
57 if ((key_code >= ui::VKEY_A && key_code <= ui::VKEY_Z) ||
58 (key_code >= ui::VKEY_0 && key_code <= ui::VKEY_9) ||
59 (key_code == ui::VKEY_TAB)) {
60 return true;
63 return false;
66 } // namespace
68 AcceleratorDispatcher::AcceleratorDispatcher(
69 base::MessagePumpDispatcher* nested_dispatcher,
70 aura::Window* associated_window)
71 : nested_dispatcher_(nested_dispatcher),
72 associated_window_(associated_window) {
73 DCHECK(nested_dispatcher_);
74 associated_window_->AddObserver(this);
77 AcceleratorDispatcher::~AcceleratorDispatcher() {
78 if (associated_window_)
79 associated_window_->RemoveObserver(this);
82 void AcceleratorDispatcher::OnWindowDestroying(aura::Window* window) {
83 if (associated_window_ == window)
84 associated_window_ = NULL;
87 uint32_t AcceleratorDispatcher::Dispatch(const base::NativeEvent& event) {
88 if (!associated_window_)
89 return POST_DISPATCH_QUIT_LOOP;
91 if (!associated_window_->CanReceiveEvents())
92 return POST_DISPATCH_PERFORM_DEFAULT;
94 if (IsKeyEvent(event)) {
95 ui::KeyEvent key_event(event, false);
96 if (IsPossibleAcceleratorNotForMenu(key_event)) {
97 if (views::MenuController* menu_controller =
98 views::MenuController::GetActiveInstance()) {
99 menu_controller->CancelAll();
100 #if defined(USE_X11)
101 XPutBackEvent(event->xany.display, event);
102 #else
103 NOTIMPLEMENTED() << " Repost NativeEvent here.";
104 #endif
105 return POST_DISPATCH_QUIT_LOOP;
109 ash::AcceleratorController* accelerator_controller =
110 ash::Shell::GetInstance()->accelerator_controller();
111 if (accelerator_controller) {
112 ui::Accelerator accelerator(key_event.key_code(),
113 key_event.flags() & kModifierMask);
114 if (key_event.type() == ui::ET_KEY_RELEASED)
115 accelerator.set_type(ui::ET_KEY_RELEASED);
116 // Fill out context object so AcceleratorController will know what
117 // was the previous accelerator or if the current accelerator is repeated.
118 Shell::GetInstance()->accelerator_controller()->context()->
119 UpdateContext(accelerator);
120 if (accelerator_controller->Process(accelerator))
121 return POST_DISPATCH_NONE;
124 return nested_dispatcher_->Dispatch(key_event.native_event());
127 return nested_dispatcher_->Dispatch(event);
130 } // namespace ash