| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/mus/ws/event_dispatcher.h" | 5 #include "components/mus/ws/event_dispatcher.h" |
| 6 | 6 |
| 7 #include "cc/surfaces/surface_hittest.h" | 7 #include "cc/surfaces/surface_hittest.h" |
| 8 #include "components/mus/surfaces/surfaces_state.h" | 8 #include "components/mus/surfaces/surfaces_state.h" |
| 9 #include "components/mus/ws/event_dispatcher_delegate.h" | 9 #include "components/mus/ws/event_dispatcher_delegate.h" |
| 10 #include "components/mus/ws/move_loop.h" | |
| 11 #include "components/mus/ws/server_window.h" | 10 #include "components/mus/ws/server_window.h" |
| 12 #include "components/mus/ws/server_window_delegate.h" | 11 #include "components/mus/ws/server_window_delegate.h" |
| 13 #include "components/mus/ws/window_coordinate_conversions.h" | 12 #include "components/mus/ws/window_coordinate_conversions.h" |
| 14 #include "components/mus/ws/window_finder.h" | 13 #include "components/mus/ws/window_finder.h" |
| 15 #include "components/mus/ws/window_tree_host_impl.h" | 14 #include "components/mus/ws/window_tree_host_impl.h" |
| 16 #include "mojo/converters/geometry/geometry_type_converters.h" | 15 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 17 #include "ui/gfx/geometry/point.h" | 16 #include "ui/gfx/geometry/point.h" |
| 17 #include "ui/gfx/geometry/point_conversions.h" |
| 18 | 18 |
| 19 namespace mus { | 19 namespace mus { |
| 20 namespace ws { | 20 namespace ws { |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 bool IsPointerEvent(const mojo::Event& event) { | 23 bool IsPointerEvent(const mojo::Event& event) { |
| 24 return event.action == mojo::EVENT_TYPE_POINTER_CANCEL || | 24 return event.action == mojo::EVENT_TYPE_POINTER_CANCEL || |
| 25 event.action == mojo::EVENT_TYPE_POINTER_DOWN || | 25 event.action == mojo::EVENT_TYPE_POINTER_DOWN || |
| 26 event.action == mojo::EVENT_TYPE_POINTER_MOVE || | 26 event.action == mojo::EVENT_TYPE_POINTER_MOVE || |
| 27 event.action == mojo::EVENT_TYPE_POINTER_UP; | 27 event.action == mojo::EVENT_TYPE_POINTER_UP; |
| 28 } | 28 } |
| 29 | 29 |
| 30 } // namespace | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 bool IsMouseEventFlag(int32_t event_flags) { | 30 bool IsMouseEventFlag(int32_t event_flags) { |
| 35 return !!(event_flags & (mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON | | 31 return !!(event_flags & (mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON | |
| 36 mojo::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON | | 32 mojo::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON | |
| 37 mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON)); | 33 mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON)); |
| 38 } | 34 } |
| 39 | 35 |
| 40 bool IsOnlyOneMouseButtonDown(mojo::EventFlags flags) { | 36 bool IsOnlyOneMouseButtonDown(mojo::EventFlags flags) { |
| 41 const uint32_t mouse_only_flags = | 37 const uint32_t mouse_only_flags = |
| 42 flags & (mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON | | 38 flags & (mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON | |
| 43 mojo::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON | | 39 mojo::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON | |
| 44 mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON); | 40 mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON); |
| 45 return mouse_only_flags == mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON || | 41 return mouse_only_flags == mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON || |
| 46 mouse_only_flags == mojo::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON || | 42 mouse_only_flags == mojo::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON || |
| 47 mouse_only_flags == mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON; | 43 mouse_only_flags == mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON; |
| 48 } | 44 } |
| 49 | 45 |
| 46 bool IsLocationInNonclientArea(const ServerWindow* target, |
| 47 const gfx::Point& location) { |
| 48 return target->parent() && |
| 49 !target->client_area().Contains(location); |
| 50 } |
| 51 |
| 52 gfx::Point EventLocationToPoint(const mojo::Event& event) { |
| 53 return gfx::ToFlooredPoint(gfx::PointF(event.pointer_data->location->x, |
| 54 event.pointer_data->location->y)); |
| 55 } |
| 56 |
| 50 } // namespace | 57 } // namespace |
| 51 | 58 |
| 52 class EventMatcher { | 59 class EventMatcher { |
| 53 public: | 60 public: |
| 54 explicit EventMatcher(const mojo::EventMatcher& matcher) | 61 explicit EventMatcher(const mojo::EventMatcher& matcher) |
| 55 : fields_to_match_(NONE), | 62 : fields_to_match_(NONE), |
| 56 event_type_(mojo::EVENT_TYPE_UNKNOWN), | 63 event_type_(mojo::EVENT_TYPE_UNKNOWN), |
| 57 event_flags_(mojo::EVENT_FLAGS_NONE), | 64 event_flags_(mojo::EVENT_FLAGS_NONE), |
| 58 keyboard_code_(mojo::KEYBOARD_CODE_UNKNOWN), | 65 keyboard_code_(mojo::KEYBOARD_CODE_UNKNOWN), |
| 59 pointer_kind_(mojo::POINTER_KIND_MOUSE) { | 66 pointer_kind_(mojo::POINTER_KIND_MOUSE) { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 mojo::EventType event_type_; | 141 mojo::EventType event_type_; |
| 135 mojo::EventFlags event_flags_; | 142 mojo::EventFlags event_flags_; |
| 136 mojo::KeyboardCode keyboard_code_; | 143 mojo::KeyboardCode keyboard_code_; |
| 137 mojo::PointerKind pointer_kind_; | 144 mojo::PointerKind pointer_kind_; |
| 138 gfx::RectF pointer_region_; | 145 gfx::RectF pointer_region_; |
| 139 }; | 146 }; |
| 140 | 147 |
| 141 //////////////////////////////////////////////////////////////////////////////// | 148 //////////////////////////////////////////////////////////////////////////////// |
| 142 | 149 |
| 143 EventDispatcher::EventDispatcher(EventDispatcherDelegate* delegate) | 150 EventDispatcher::EventDispatcher(EventDispatcherDelegate* delegate) |
| 144 : delegate_(delegate), root_(nullptr), capture_window_(nullptr) {} | 151 : delegate_(delegate), |
| 152 root_(nullptr), |
| 153 capture_window_(nullptr), |
| 154 capture_in_nonclient_area_(false) {} |
| 145 | 155 |
| 146 EventDispatcher::~EventDispatcher() {} | 156 EventDispatcher::~EventDispatcher() {} |
| 147 | 157 |
| 148 void EventDispatcher::AddAccelerator(uint32_t id, | 158 void EventDispatcher::AddAccelerator(uint32_t id, |
| 149 mojo::EventMatcherPtr event_matcher) { | 159 mojo::EventMatcherPtr event_matcher) { |
| 150 EventMatcher matcher(*event_matcher); | 160 EventMatcher matcher(*event_matcher); |
| 151 #if !defined(NDEBUG) | 161 #if !defined(NDEBUG) |
| 152 for (const auto& pair : accelerators_) { | 162 for (const auto& pair : accelerators_) { |
| 153 DCHECK_NE(pair.first, id); | 163 DCHECK_NE(pair.first, id); |
| 154 DCHECK(!matcher.Equals(pair.second)); | 164 DCHECK(!matcher.Equals(pair.second)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 169 | 179 |
| 170 if (event->action == mojo::EVENT_TYPE_KEY_PRESSED && | 180 if (event->action == mojo::EVENT_TYPE_KEY_PRESSED && |
| 171 !event->key_data->is_char) { | 181 !event->key_data->is_char) { |
| 172 uint32_t accelerator = 0u; | 182 uint32_t accelerator = 0u; |
| 173 if (FindAccelerator(*event, &accelerator)) { | 183 if (FindAccelerator(*event, &accelerator)) { |
| 174 delegate_->OnAccelerator(accelerator, event.Pass()); | 184 delegate_->OnAccelerator(accelerator, event.Pass()); |
| 175 return; | 185 return; |
| 176 } | 186 } |
| 177 } | 187 } |
| 178 | 188 |
| 179 // If there is a MoveLoop all pointer events are forwarded to it. | |
| 180 if (move_loop_ && IsPointerEvent(*event)) { | |
| 181 if (move_loop_->Move(*event) == MoveLoop::DONE) { | |
| 182 move_loop_.reset(); | |
| 183 ResetCaptureWindowIfPointerUp(*event); | |
| 184 } | |
| 185 | |
| 186 return; | |
| 187 } | |
| 188 | |
| 189 ServerWindow* target = FindEventTarget(event.get()); | 189 ServerWindow* target = FindEventTarget(event.get()); |
| 190 bool in_nonclient_area = false; |
| 190 | 191 |
| 191 if (IsMouseEventFlag(event->flags)) { | 192 if (IsMouseEventFlag(event->flags)) { |
| 192 if (!capture_window_ && (event->action == mojo::EVENT_TYPE_POINTER_DOWN)) | 193 if (!capture_window_ && target && |
| 194 (event->action == mojo::EVENT_TYPE_POINTER_DOWN)) { |
| 195 // TODO(sky): |capture_window_| needs to be reset when window removed |
| 196 // from hierarchy. |
| 193 capture_window_ = target; | 197 capture_window_ = target; |
| 194 else | 198 // TODO(sky): this needs to happen for pointer down events too. |
| 195 ResetCaptureWindowIfPointerUp(*event); | 199 capture_in_nonclient_area_ = |
| 200 IsLocationInNonclientArea(target, EventLocationToPoint(*event)); |
| 201 in_nonclient_area = capture_in_nonclient_area_; |
| 202 } else if (event->action == mojo::EVENT_TYPE_POINTER_UP && |
| 203 IsOnlyOneMouseButtonDown(event->flags)) { |
| 204 capture_window_ = nullptr; |
| 205 } |
| 206 in_nonclient_area = capture_in_nonclient_area_; |
| 196 } | 207 } |
| 197 | 208 |
| 198 if (target) { | 209 if (target) { |
| 199 if (event->action == mojo::EVENT_TYPE_POINTER_DOWN) { | 210 if (event->action == mojo::EVENT_TYPE_POINTER_DOWN) |
| 200 delegate_->SetFocusedWindowFromEventDispatcher(target); | 211 delegate_->SetFocusedWindowFromEventDispatcher(target); |
| 201 | 212 |
| 202 if (!move_loop_) { | 213 delegate_->DispatchInputEventToWindow(target, in_nonclient_area, |
| 203 move_loop_ = MoveLoop::Create(target, *event); | 214 event.Pass()); |
| 204 if (move_loop_) | |
| 205 return; | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 delegate_->DispatchInputEventToWindow(target, event.Pass()); | |
| 210 } | 215 } |
| 211 } | 216 } |
| 212 | 217 |
| 213 bool EventDispatcher::FindAccelerator(const mojo::Event& event, | 218 bool EventDispatcher::FindAccelerator(const mojo::Event& event, |
| 214 uint32_t* accelerator_id) { | 219 uint32_t* accelerator_id) { |
| 215 DCHECK(event.key_data); | 220 DCHECK(event.key_data); |
| 216 for (const auto& pair : accelerators_) { | 221 for (const auto& pair : accelerators_) { |
| 217 if (pair.second.MatchesEvent(event)) { | 222 if (pair.second.MatchesEvent(event)) { |
| 218 *accelerator_id = pair.first; | 223 *accelerator_id = pair.first; |
| 219 return true; | 224 return true; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 244 | 249 |
| 245 if (!target) { | 250 if (!target) { |
| 246 target = FindDeepestVisibleWindow(root_, surface_id_, &location); | 251 target = FindDeepestVisibleWindow(root_, surface_id_, &location); |
| 247 } else { | 252 } else { |
| 248 gfx::Transform transform(GetTransformToWindow(surface_id_, target)); | 253 gfx::Transform transform(GetTransformToWindow(surface_id_, target)); |
| 249 transform.TransformPoint(&location); | 254 transform.TransformPoint(&location); |
| 250 } | 255 } |
| 251 | 256 |
| 252 event_location->x = location.x(); | 257 event_location->x = location.x(); |
| 253 event_location->y = location.y(); | 258 event_location->y = location.y(); |
| 259 |
| 254 return target; | 260 return target; |
| 255 } | 261 } |
| 256 | 262 |
| 257 void EventDispatcher::ResetCaptureWindowIfPointerUp(const mojo::Event& event) { | |
| 258 if (event.action == mojo::EVENT_TYPE_POINTER_UP && | |
| 259 IsOnlyOneMouseButtonDown(event.flags)) { | |
| 260 capture_window_ = nullptr; | |
| 261 } | |
| 262 } | |
| 263 | |
| 264 } // namespace ws | 263 } // namespace ws |
| 265 } // namespace mus | 264 } // namespace mus |
| OLD | NEW |