| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ui/views/controls/menu/menu_message_loop_aura.h" | 5 #include "ui/views/controls/menu/menu_message_loop_aura.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "ui/aura/client/screen_position_client.h" | 10 #include "ui/aura/client/screen_position_client.h" |
| 11 #include "ui/aura/window.h" | 11 #include "ui/aura/window.h" |
| 12 #include "ui/aura/window_event_dispatcher.h" | 12 #include "ui/aura/window_event_dispatcher.h" |
| 13 #include "ui/aura/window_tree_host.h" | 13 #include "ui/aura/window_tree_host.h" |
| 14 #include "ui/events/event.h" | 14 #include "ui/events/event.h" |
| 15 #include "ui/events/platform/platform_event_source.h" | 15 #include "ui/events/platform/platform_event_source.h" |
| 16 #include "ui/events/platform/scoped_event_dispatcher.h" | 16 #include "ui/events/platform/scoped_event_dispatcher.h" |
| 17 #include "ui/views/controls/menu/menu_controller.h" | 17 #include "ui/views/controls/menu/menu_controller.h" |
| 18 #include "ui/views/controls/menu/menu_key_event_handler.h" | |
| 19 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
| 20 #include "ui/wm/public/activation_change_observer.h" | 19 #include "ui/wm/public/activation_change_observer.h" |
| 21 #include "ui/wm/public/activation_client.h" | 20 #include "ui/wm/public/activation_client.h" |
| 22 #include "ui/wm/public/drag_drop_client.h" | 21 #include "ui/wm/public/drag_drop_client.h" |
| 23 | 22 |
| 24 | 23 |
| 25 using aura::client::ScreenPositionClient; | 24 using aura::client::ScreenPositionClient; |
| 26 | 25 |
| 27 namespace views { | 26 namespace views { |
| 28 | 27 |
| 29 namespace { | |
| 30 | |
| 31 aura::Window* GetOwnerRootWindow(views::Widget* owner) { | |
| 32 return owner ? owner->GetNativeWindow()->GetRootWindow() : NULL; | |
| 33 } | |
| 34 | |
| 35 // ActivationChangeObserverImpl is used to observe activation changes and close | |
| 36 // the menu. Additionally it listens for the root window to be destroyed and | |
| 37 // cancel the menu as well. | |
| 38 class ActivationChangeObserverImpl | |
| 39 : public aura::client::ActivationChangeObserver, | |
| 40 public aura::WindowObserver, | |
| 41 public ui::EventHandler { | |
| 42 public: | |
| 43 ActivationChangeObserverImpl(MenuController* controller, aura::Window* root) | |
| 44 : controller_(controller), root_(root) { | |
| 45 aura::client::GetActivationClient(root_)->AddObserver(this); | |
| 46 root_->AddObserver(this); | |
| 47 root_->AddPreTargetHandler(this); | |
| 48 } | |
| 49 | |
| 50 ~ActivationChangeObserverImpl() override { Cleanup(); } | |
| 51 | |
| 52 // aura::client::ActivationChangeObserver: | |
| 53 void OnWindowActivated( | |
| 54 aura::client::ActivationChangeObserver::ActivationReason reason, | |
| 55 aura::Window* gained_active, | |
| 56 aura::Window* lost_active) override { | |
| 57 if (!controller_->drag_in_progress()) | |
| 58 controller_->CancelAll(); | |
| 59 } | |
| 60 | |
| 61 // aura::WindowObserver: | |
| 62 void OnWindowDestroying(aura::Window* window) override { Cleanup(); } | |
| 63 | |
| 64 // ui::EventHandler: | |
| 65 void OnCancelMode(ui::CancelModeEvent* event) override { | |
| 66 controller_->CancelAll(); | |
| 67 } | |
| 68 | |
| 69 private: | |
| 70 void Cleanup() { | |
| 71 if (!root_) | |
| 72 return; | |
| 73 // The ActivationClient may have been destroyed by the time we get here. | |
| 74 aura::client::ActivationClient* client = | |
| 75 aura::client::GetActivationClient(root_); | |
| 76 if (client) | |
| 77 client->RemoveObserver(this); | |
| 78 root_->RemovePreTargetHandler(this); | |
| 79 root_->RemoveObserver(this); | |
| 80 root_ = NULL; | |
| 81 } | |
| 82 | |
| 83 MenuController* controller_; | |
| 84 aura::Window* root_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl); | |
| 87 }; | |
| 88 | |
| 89 } // namespace | |
| 90 | |
| 91 // static | 28 // static |
| 92 MenuMessageLoop* MenuMessageLoop::Create() { | 29 MenuMessageLoop* MenuMessageLoop::Create() { |
| 93 return new MenuMessageLoopAura; | 30 return new MenuMessageLoopAura; |
| 94 } | 31 } |
| 95 | 32 |
| 96 // static | 33 // static |
| 97 void MenuMessageLoop::RepostEventToWindow(const ui::LocatedEvent* event, | 34 void MenuMessageLoop::RepostEventToWindow(const ui::LocatedEvent* event, |
| 98 gfx::NativeWindow window, | 35 gfx::NativeWindow window, |
| 99 const gfx::Point& screen_loc) { | 36 const gfx::Point& screen_loc) { |
| 100 aura::Window* root = window->GetRootWindow(); | 37 aura::Window* root = window->GetRootWindow(); |
| 101 aura::client::ScreenPositionClient* spc = | 38 aura::client::ScreenPositionClient* spc = |
| 102 aura::client::GetScreenPositionClient(root); | 39 aura::client::GetScreenPositionClient(root); |
| 103 if (!spc) | 40 if (!spc) |
| 104 return; | 41 return; |
| 105 | 42 |
| 106 gfx::Point root_loc(screen_loc); | 43 gfx::Point root_loc(screen_loc); |
| 107 spc->ConvertPointFromScreen(root, &root_loc); | 44 spc->ConvertPointFromScreen(root, &root_loc); |
| 108 | 45 |
| 109 std::unique_ptr<ui::Event> clone = ui::Event::Clone(*event); | 46 std::unique_ptr<ui::Event> clone = ui::Event::Clone(*event); |
| 110 std::unique_ptr<ui::LocatedEvent> located_event( | 47 std::unique_ptr<ui::LocatedEvent> located_event( |
| 111 static_cast<ui::LocatedEvent*>(clone.release())); | 48 static_cast<ui::LocatedEvent*>(clone.release())); |
| 112 located_event->set_location(root_loc); | 49 located_event->set_location(root_loc); |
| 113 located_event->set_root_location(root_loc); | 50 located_event->set_root_location(root_loc); |
| 114 | 51 |
| 115 root->GetHost()->dispatcher()->RepostEvent(located_event.get()); | 52 root->GetHost()->dispatcher()->RepostEvent(located_event.get()); |
| 116 } | 53 } |
| 117 | 54 |
| 118 MenuMessageLoopAura::MenuMessageLoopAura() : owner_(nullptr) {} | 55 MenuMessageLoopAura::MenuMessageLoopAura() {} |
| 119 | 56 |
| 120 MenuMessageLoopAura::~MenuMessageLoopAura() {} | 57 MenuMessageLoopAura::~MenuMessageLoopAura() {} |
| 121 | 58 |
| 122 void MenuMessageLoopAura::Run(MenuController* controller, | 59 void MenuMessageLoopAura::Run() { |
| 123 Widget* owner, | |
| 124 bool nested_menu) { | |
| 125 // |owner_| may be NULL. | |
| 126 owner_ = owner; | |
| 127 aura::Window* root = GetOwnerRootWindow(owner_); | |
| 128 // It is possible for the same MenuMessageLoopAura to start a nested | 60 // It is possible for the same MenuMessageLoopAura to start a nested |
| 129 // message-loop while it is already running a nested loop. So make sure the | 61 // message-loop while it is already running a nested loop. So make sure the |
| 130 // quit-closure gets reset to the outer loop's quit-closure once the innermost | 62 // quit-closure gets reset to the outer loop's quit-closure once the innermost |
| 131 // loop terminates. | 63 // loop terminates. |
| 132 base::AutoReset<base::Closure> reset_quit_closure(&message_loop_quit_, | 64 base::AutoReset<base::Closure> reset_quit_closure(&message_loop_quit_, |
| 133 base::Closure()); | 65 base::Closure()); |
| 134 | 66 |
| 135 std::unique_ptr<ActivationChangeObserverImpl> observer; | |
| 136 if (root) { | |
| 137 if (!nested_menu) | |
| 138 observer.reset(new ActivationChangeObserverImpl(controller, root)); | |
| 139 } | |
| 140 | |
| 141 base::MessageLoop* loop = base::MessageLoop::current(); | 67 base::MessageLoop* loop = base::MessageLoop::current(); |
| 142 base::MessageLoop::ScopedNestableTaskAllower allow(loop); | 68 base::MessageLoop::ScopedNestableTaskAllower allow(loop); |
| 143 base::RunLoop run_loop; | 69 base::RunLoop run_loop; |
| 144 message_loop_quit_ = run_loop.QuitClosure(); | 70 message_loop_quit_ = run_loop.QuitClosure(); |
| 145 | 71 |
| 146 run_loop.Run(); | 72 run_loop.Run(); |
| 147 } | 73 } |
| 148 | 74 |
| 149 void MenuMessageLoopAura::QuitNow() { | 75 void MenuMessageLoopAura::QuitNow() { |
| 150 CHECK(!message_loop_quit_.is_null()); | 76 CHECK(!message_loop_quit_.is_null()); |
| 151 message_loop_quit_.Run(); | 77 message_loop_quit_.Run(); |
| 152 | 78 |
| 153 #if !defined(OS_WIN) | 79 #if !defined(OS_WIN) |
| 154 // Ask PlatformEventSource to stop dispatching events in this message loop | 80 // Ask PlatformEventSource to stop dispatching events in this message loop |
| 155 // iteration. We want our menu's loop to return before the next event. | 81 // iteration. We want our menu's loop to return before the next event. |
| 156 if (ui::PlatformEventSource::GetInstance()) | 82 if (ui::PlatformEventSource::GetInstance()) |
| 157 ui::PlatformEventSource::GetInstance()->StopCurrentEventStream(); | 83 ui::PlatformEventSource::GetInstance()->StopCurrentEventStream(); |
| 158 #endif | 84 #endif |
| 159 } | 85 } |
| 160 | 86 |
| 161 void MenuMessageLoopAura::ClearOwner() { | |
| 162 owner_ = NULL; | |
| 163 } | |
| 164 | |
| 165 } // namespace views | 87 } // namespace views |
| OLD | NEW |