| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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/views/mus/pointer_watcher_event_router2.h" | |
| 6 | |
| 7 #include "ui/aura/client/capture_client.h" | |
| 8 #include "ui/aura/mus/window_tree_client.h" | |
| 9 #include "ui/aura/window.h" | |
| 10 #include "ui/display/screen.h" | |
| 11 #include "ui/events/base_event_utils.h" | |
| 12 #include "ui/events/event.h" | |
| 13 #include "ui/views/pointer_watcher.h" | |
| 14 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h" | |
| 15 | |
| 16 namespace views { | |
| 17 namespace { | |
| 18 | |
| 19 bool HasPointerWatcher( | |
| 20 base::ObserverList<views::PointerWatcher, true>* observer_list) { | |
| 21 return observer_list->begin() != observer_list->end(); | |
| 22 } | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 PointerWatcherEventRouter2::PointerWatcherEventRouter2( | |
| 27 aura::WindowTreeClient* window_tree_client) | |
| 28 : window_tree_client_(window_tree_client) { | |
| 29 window_tree_client->AddObserver(this); | |
| 30 window_tree_client_->GetCaptureClient()->AddObserver(this); | |
| 31 } | |
| 32 | |
| 33 PointerWatcherEventRouter2::~PointerWatcherEventRouter2() { | |
| 34 if (window_tree_client_) { | |
| 35 window_tree_client_->RemoveObserver(this); | |
| 36 window_tree_client_->GetCaptureClient()->RemoveObserver(this); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 void PointerWatcherEventRouter2::AddPointerWatcher(PointerWatcher* watcher, | |
| 41 bool wants_moves) { | |
| 42 // Pointer watchers cannot be added multiple times. | |
| 43 DCHECK(!move_watchers_.HasObserver(watcher)); | |
| 44 DCHECK(!non_move_watchers_.HasObserver(watcher)); | |
| 45 if (wants_moves) { | |
| 46 move_watchers_.AddObserver(watcher); | |
| 47 if (event_types_ != EventTypes::MOVE_EVENTS) { | |
| 48 event_types_ = EventTypes::MOVE_EVENTS; | |
| 49 const bool wants_moves = true; | |
| 50 window_tree_client_->StartPointerWatcher(wants_moves); | |
| 51 } | |
| 52 } else { | |
| 53 non_move_watchers_.AddObserver(watcher); | |
| 54 if (event_types_ == EventTypes::NONE) { | |
| 55 event_types_ = EventTypes::NON_MOVE_EVENTS; | |
| 56 const bool wants_moves = false; | |
| 57 window_tree_client_->StartPointerWatcher(wants_moves); | |
| 58 } | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 void PointerWatcherEventRouter2::RemovePointerWatcher(PointerWatcher* watcher) { | |
| 63 if (non_move_watchers_.HasObserver(watcher)) { | |
| 64 non_move_watchers_.RemoveObserver(watcher); | |
| 65 } else { | |
| 66 DCHECK(move_watchers_.HasObserver(watcher)); | |
| 67 move_watchers_.RemoveObserver(watcher); | |
| 68 } | |
| 69 const EventTypes types = DetermineEventTypes(); | |
| 70 if (types == event_types_) | |
| 71 return; | |
| 72 | |
| 73 event_types_ = types; | |
| 74 switch (types) { | |
| 75 case EventTypes::NONE: | |
| 76 window_tree_client_->StopPointerWatcher(); | |
| 77 break; | |
| 78 case EventTypes::NON_MOVE_EVENTS: | |
| 79 window_tree_client_->StartPointerWatcher(false); | |
| 80 break; | |
| 81 case EventTypes::MOVE_EVENTS: | |
| 82 // It isn't possible to remove an observer and transition to wanting move | |
| 83 // events. This could only happen if there is a bug in the add logic. | |
| 84 NOTREACHED(); | |
| 85 break; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void PointerWatcherEventRouter2::OnPointerEventObserved( | |
| 90 const ui::PointerEvent& event, | |
| 91 aura::Window* target) { | |
| 92 Widget* target_widget = nullptr; | |
| 93 ui::PointerEvent updated_event(event); | |
| 94 if (target) { | |
| 95 aura::Window* window = target; | |
| 96 while (window && !target_widget) { | |
| 97 target_widget = Widget::GetWidgetForNativeView(window); | |
| 98 if (!target_widget) { | |
| 99 // Widget::GetWidgetForNativeView() uses NativeWidgetAura. Views with | |
| 100 // aura-mus may also create DesktopNativeWidgetAura. | |
| 101 DesktopNativeWidgetAura* desktop_native_widget_aura = | |
| 102 DesktopNativeWidgetAura::ForWindow(target); | |
| 103 if (desktop_native_widget_aura) { | |
| 104 target_widget = static_cast<internal::NativeWidgetPrivate*>( | |
| 105 desktop_native_widget_aura) | |
| 106 ->GetWidget(); | |
| 107 } | |
| 108 } | |
| 109 window = window->parent(); | |
| 110 } | |
| 111 if (target_widget) { | |
| 112 gfx::Point widget_relative_location(event.location()); | |
| 113 aura::Window::ConvertPointToTarget(target, target_widget->GetNativeView(), | |
| 114 &widget_relative_location); | |
| 115 updated_event.set_location(widget_relative_location); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 // The mojo input events type converter uses the event root_location field | |
| 120 // to store screen coordinates. Screen coordinates really should be returned | |
| 121 // separately. See http://crbug.com/608547 | |
| 122 gfx::Point location_in_screen = event.root_location(); | |
| 123 for (PointerWatcher& observer : move_watchers_) { | |
| 124 observer.OnPointerEventObserved(updated_event, location_in_screen, | |
| 125 target_widget); | |
| 126 } | |
| 127 if (event.type() != ui::ET_POINTER_MOVED) { | |
| 128 for (PointerWatcher& observer : non_move_watchers_) { | |
| 129 observer.OnPointerEventObserved(updated_event, location_in_screen, | |
| 130 target_widget); | |
| 131 } | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 PointerWatcherEventRouter2::EventTypes | |
| 136 PointerWatcherEventRouter2::DetermineEventTypes() { | |
| 137 if (HasPointerWatcher(&move_watchers_)) | |
| 138 return EventTypes::MOVE_EVENTS; | |
| 139 | |
| 140 if (HasPointerWatcher(&non_move_watchers_)) | |
| 141 return EventTypes::NON_MOVE_EVENTS; | |
| 142 | |
| 143 return EventTypes::NONE; | |
| 144 } | |
| 145 | |
| 146 void PointerWatcherEventRouter2::OnCaptureChanged( | |
| 147 aura::Window* lost_capture, | |
| 148 aura::Window* gained_capture) { | |
| 149 const ui::MouseEvent mouse_event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(), | |
| 150 gfx::Point(), ui::EventTimeForNow(), 0, 0); | |
| 151 const ui::PointerEvent event(mouse_event); | |
| 152 gfx::Point location_in_screen = | |
| 153 display::Screen::GetScreen()->GetCursorScreenPoint(); | |
| 154 for (PointerWatcher& observer : move_watchers_) | |
| 155 observer.OnPointerEventObserved(event, location_in_screen, nullptr); | |
| 156 for (PointerWatcher& observer : non_move_watchers_) | |
| 157 observer.OnPointerEventObserved(event, location_in_screen, nullptr); | |
| 158 } | |
| 159 | |
| 160 void PointerWatcherEventRouter2::OnDidDestroyClient( | |
| 161 aura::WindowTreeClient* client) { | |
| 162 // We expect that all observers have been removed by this time. | |
| 163 DCHECK_EQ(event_types_, EventTypes::NONE); | |
| 164 DCHECK_EQ(client, window_tree_client_); | |
| 165 window_tree_client_->GetCaptureClient()->RemoveObserver(this); | |
| 166 window_tree_client_->RemoveObserver(this); | |
| 167 window_tree_client_ = nullptr; | |
| 168 } | |
| 169 | |
| 170 } // namespace views | |
| OLD | NEW |