OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/accelerators/accelerator_dispatcher.h" | 5 #include "ash/accelerators/accelerator_dispatcher.h" |
6 | 6 |
7 #if defined(USE_X11) | |
8 #include <X11/Xlib.h> | |
9 | |
10 // Xlib defines RootWindow | |
11 #ifdef RootWindow | |
12 #undef RootWindow | |
13 #endif | |
14 #endif // defined(USE_X11) | |
15 | |
16 #include "ash/accelerators/accelerator_controller.h" | |
17 #include "ash/shell.h" | |
18 #include "ui/aura/env.h" | |
19 #include "ui/aura/event.h" | |
20 #include "ui/aura/root_window.h" | |
21 #include "ui/base/accelerators/accelerator.h" | |
22 #include "ui/base/events.h" | |
23 | |
7 namespace ash { | 24 namespace ash { |
25 namespace { | |
26 | |
27 const int kModifierMask = (ui::EF_SHIFT_DOWN | | |
28 ui::EF_CONTROL_DOWN | | |
29 ui::EF_ALT_DOWN); | |
30 #if defined(OS_WIN) | |
31 bool IsKeyEvent(const MSG& msg) { | |
32 return | |
33 msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN || | |
34 msg.message == WM_KEYUP || msg.message == WM_SYSKEYUP; | |
35 } | |
36 bool IsKeyRelease(const MSG& msg) { | |
37 return msg.message == WM_KEYUP || msg.message == WM_SYSKEYUP; | |
38 } | |
39 #endif | |
40 | |
41 #if defined(USE_X11) | |
sadrul
2012/04/06 00:16:57
#elif defined(USE_X11)?
oshima
2012/04/06 00:34:35
Done.
| |
42 bool IsKeyEvent(const XEvent* xev) { | |
43 return xev->type == KeyPress || xev->type == KeyRelease; | |
44 } | |
45 bool IsKeyRelease(const XEvent* xev) { | |
46 return xev->type == KeyRelease; | |
47 } | |
48 #endif | |
49 | |
50 } // namespace | |
8 | 51 |
9 AcceleratorDispatcher::AcceleratorDispatcher( | 52 AcceleratorDispatcher::AcceleratorDispatcher( |
10 MessageLoop::Dispatcher* nested_dispatcher, aura::Window* associated_window) | 53 MessageLoop::Dispatcher* nested_dispatcher, aura::Window* associated_window) |
11 : nested_dispatcher_(nested_dispatcher), | 54 : nested_dispatcher_(nested_dispatcher), |
12 associated_window_(associated_window) { | 55 associated_window_(associated_window) { |
13 DCHECK(nested_dispatcher_); | 56 DCHECK(nested_dispatcher_); |
14 associated_window_->AddObserver(this); | 57 associated_window_->AddObserver(this); |
15 } | 58 } |
16 | 59 |
17 AcceleratorDispatcher::~AcceleratorDispatcher() { | 60 AcceleratorDispatcher::~AcceleratorDispatcher() { |
18 if (associated_window_) | 61 if (associated_window_) |
19 associated_window_->RemoveObserver(this); | 62 associated_window_->RemoveObserver(this); |
20 } | 63 } |
21 | 64 |
22 void AcceleratorDispatcher::OnWindowDestroying(aura::Window* window) { | 65 void AcceleratorDispatcher::OnWindowDestroying(aura::Window* window) { |
23 if (associated_window_ == window) | 66 if (associated_window_ == window) |
24 associated_window_ = NULL; | 67 associated_window_ = NULL; |
25 } | 68 } |
26 | 69 |
70 bool AcceleratorDispatcher::Dispatch(const base::NativeEvent& event) { | |
71 if (!associated_window_) | |
72 return false; | |
73 if (!ui::IsNoopEvent(event) && !associated_window_->CanReceiveEvents()) | |
74 return aura::Env::GetInstance()->GetDispatcher()->Dispatch(event); | |
75 | |
76 if (IsKeyEvent(event)) { | |
77 ash::AcceleratorController* accelerator_controller = | |
78 ash::Shell::GetInstance()->accelerator_controller(); | |
79 if (accelerator_controller) { | |
80 ui::Accelerator accelerator(ui::KeyboardCodeFromNative(event), | |
81 ui::EventFlagsFromNative(event) & kModifierMask); | |
82 if (IsKeyRelease(event)) | |
83 accelerator.set_type(ui::ET_KEY_RELEASED); | |
84 if (accelerator_controller->Process(accelerator)) | |
85 return true; | |
86 accelerator.set_type(aura::TranslatedKeyEvent(event, false).type()); | |
87 if (accelerator_controller->Process(accelerator)) | |
88 return true; | |
89 } | |
90 } | |
91 | |
92 return nested_dispatcher_->Dispatch(event); | |
93 } | |
94 | |
27 } // namespace ash | 95 } // namespace ash |
OLD | NEW |