| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "ui/aura_shell/root_window_event_filter.h" | |
| 6 | |
| 7 #include "ash/wm/activation_controller.h" | |
| 8 #include "ui/aura/event.h" | |
| 9 #include "ui/aura/focus_manager.h" | |
| 10 #include "ui/aura/root_window.h" | |
| 11 #include "ui/aura/window_delegate.h" | |
| 12 #include "ui/aura_shell/shell.h" | |
| 13 #include "ui/aura_shell/window_util.h" | |
| 14 #include "ui/base/hit_test.h" | |
| 15 | |
| 16 namespace aura_shell { | |
| 17 namespace internal { | |
| 18 | |
| 19 // Returns the default cursor for a window component. | |
| 20 gfx::NativeCursor CursorForWindowComponent(int window_component) { | |
| 21 switch (window_component) { | |
| 22 case HTBOTTOM: | |
| 23 return aura::kCursorSouthResize; | |
| 24 case HTBOTTOMLEFT: | |
| 25 return aura::kCursorSouthWestResize; | |
| 26 case HTBOTTOMRIGHT: | |
| 27 return aura::kCursorSouthEastResize; | |
| 28 case HTLEFT: | |
| 29 return aura::kCursorWestResize; | |
| 30 case HTRIGHT: | |
| 31 return aura::kCursorEastResize; | |
| 32 case HTTOP: | |
| 33 return aura::kCursorNorthResize; | |
| 34 case HTTOPLEFT: | |
| 35 return aura::kCursorNorthWestResize; | |
| 36 case HTTOPRIGHT: | |
| 37 return aura::kCursorNorthEastResize; | |
| 38 default: | |
| 39 return aura::kCursorNull; | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 //////////////////////////////////////////////////////////////////////////////// | |
| 44 // RootWindowEventFilter, public: | |
| 45 | |
| 46 RootWindowEventFilter::RootWindowEventFilter() | |
| 47 : EventFilter(aura::RootWindow::GetInstance()) { | |
| 48 } | |
| 49 | |
| 50 RootWindowEventFilter::~RootWindowEventFilter() { | |
| 51 // Additional filters are not owned by RootWindowEventFilter and they | |
| 52 // should all be removed when running here. |filters_| has | |
| 53 // check_empty == true and will DCHECK failure if it is not empty. | |
| 54 } | |
| 55 | |
| 56 void RootWindowEventFilter::AddFilter(aura::EventFilter* filter) { | |
| 57 filters_.AddObserver(filter); | |
| 58 } | |
| 59 | |
| 60 void RootWindowEventFilter::RemoveFilter(aura::EventFilter* filter) { | |
| 61 filters_.RemoveObserver(filter); | |
| 62 } | |
| 63 | |
| 64 //////////////////////////////////////////////////////////////////////////////// | |
| 65 // RootWindowEventFilter, EventFilter implementation: | |
| 66 | |
| 67 bool RootWindowEventFilter::PreHandleKeyEvent(aura::Window* target, | |
| 68 aura::KeyEvent* event) { | |
| 69 return FilterKeyEvent(target, event); | |
| 70 } | |
| 71 | |
| 72 bool RootWindowEventFilter::PreHandleMouseEvent(aura::Window* target, | |
| 73 aura::MouseEvent* event) { | |
| 74 // We must always update the cursor, otherwise the cursor can get stuck if an | |
| 75 // event filter registered with us consumes the event. | |
| 76 if (event->type() == ui::ET_MOUSE_MOVED) { | |
| 77 // Shows the cursor when mouse moved. | |
| 78 SetCursorVisible(target, event, true); | |
| 79 | |
| 80 UpdateCursor(target, event); | |
| 81 } | |
| 82 | |
| 83 if (FilterMouseEvent(target, event)) | |
| 84 return true; | |
| 85 | |
| 86 if (event->type() == ui::ET_MOUSE_PRESSED) | |
| 87 target->GetFocusManager()->SetFocusedWindow(target); | |
| 88 | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 ui::TouchStatus RootWindowEventFilter::PreHandleTouchEvent( | |
| 93 aura::Window* target, | |
| 94 aura::TouchEvent* event) { | |
| 95 ui::TouchStatus status = FilterTouchEvent(target, event); | |
| 96 if (status != ui::TOUCH_STATUS_UNKNOWN) | |
| 97 return status; | |
| 98 | |
| 99 if (event->type() == ui::ET_TOUCH_PRESSED) { | |
| 100 // Hides the cursor when touch pressed. | |
| 101 SetCursorVisible(target, event, false); | |
| 102 | |
| 103 target->GetFocusManager()->SetFocusedWindow(target); | |
| 104 } | |
| 105 return ui::TOUCH_STATUS_UNKNOWN; | |
| 106 } | |
| 107 | |
| 108 //////////////////////////////////////////////////////////////////////////////// | |
| 109 // RootWindowEventFilter, private: | |
| 110 | |
| 111 void RootWindowEventFilter::UpdateCursor(aura::Window* target, | |
| 112 aura::MouseEvent* event) { | |
| 113 gfx::NativeCursor cursor = target->GetCursor(event->location()); | |
| 114 if (event->flags() & ui::EF_IS_NON_CLIENT) { | |
| 115 int window_component = | |
| 116 target->delegate()->GetNonClientComponent(event->location()); | |
| 117 cursor = CursorForWindowComponent(window_component); | |
| 118 } | |
| 119 aura::RootWindow::GetInstance()->SetCursor(cursor); | |
| 120 } | |
| 121 | |
| 122 void RootWindowEventFilter::SetCursorVisible(aura::Window* target, | |
| 123 aura::LocatedEvent* event, | |
| 124 bool show) { | |
| 125 aura::RootWindow::GetInstance()->ShowCursor(show); | |
| 126 } | |
| 127 | |
| 128 bool RootWindowEventFilter::FilterKeyEvent(aura::Window* target, | |
| 129 aura::KeyEvent* event) { | |
| 130 bool handled = false; | |
| 131 if (filters_.might_have_observers()) { | |
| 132 ObserverListBase<aura::EventFilter>::Iterator it(filters_); | |
| 133 aura::EventFilter* filter; | |
| 134 while (!handled && (filter = it.GetNext()) != NULL) | |
| 135 handled = filter->PreHandleKeyEvent(target, event); | |
| 136 } | |
| 137 return handled; | |
| 138 } | |
| 139 | |
| 140 bool RootWindowEventFilter::FilterMouseEvent(aura::Window* target, | |
| 141 aura::MouseEvent* event) { | |
| 142 bool handled = false; | |
| 143 if (filters_.might_have_observers()) { | |
| 144 ObserverListBase<aura::EventFilter>::Iterator it(filters_); | |
| 145 aura::EventFilter* filter; | |
| 146 while (!handled && (filter = it.GetNext()) != NULL) | |
| 147 handled = filter->PreHandleMouseEvent(target, event); | |
| 148 } | |
| 149 return handled; | |
| 150 } | |
| 151 | |
| 152 ui::TouchStatus RootWindowEventFilter::FilterTouchEvent( | |
| 153 aura::Window* target, | |
| 154 aura::TouchEvent* event) { | |
| 155 ui::TouchStatus status = ui::TOUCH_STATUS_UNKNOWN; | |
| 156 if (filters_.might_have_observers()) { | |
| 157 ObserverListBase<aura::EventFilter>::Iterator it(filters_); | |
| 158 aura::EventFilter* filter; | |
| 159 while (status == ui::TOUCH_STATUS_UNKNOWN && | |
| 160 (filter = it.GetNext()) != NULL) { | |
| 161 status = filter->PreHandleTouchEvent(target, event); | |
| 162 } | |
| 163 } | |
| 164 return status; | |
| 165 } | |
| 166 | |
| 167 } // namespace internal | |
| 168 } // namespace aura_shell | |
| OLD | NEW |