Chromium Code Reviews| Index: ui/aura/mus/mus_mouse_location_updater.cc |
| diff --git a/ui/aura/mus/mus_mouse_location_updater.cc b/ui/aura/mus/mus_mouse_location_updater.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3d6382d55e3c938e1d605b4a688252700b48e1c3 |
| --- /dev/null |
| +++ b/ui/aura/mus/mus_mouse_location_updater.cc |
| @@ -0,0 +1,72 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ui/aura/mus/mus_mouse_location_updater.h" |
| + |
| +#include "ui/aura/env.h" |
| +#include "ui/events/event.h" |
| + |
| +namespace aura { |
| +namespace { |
| + |
| +bool IsMouseEventWithLocation(const ui::Event& event) { |
| + // All mouse events except exited, capture, and wheel which mus doesn't |
| + // include locations for. |
| + switch (event.type()) { |
| + case ui::ET_MOUSE_PRESSED: |
| + case ui::ET_MOUSE_DRAGGED: |
| + case ui::ET_MOUSE_RELEASED: |
| + case ui::ET_MOUSE_MOVED: |
| + case ui::ET_MOUSE_ENTERED: |
| + return true; |
| + default: |
| + break; |
| + } |
| + return false; |
| +} |
| + |
| +} // namespace |
| + |
| +MusMouseLocationUpdater::MusMouseLocationUpdater() {} |
| + |
| +MusMouseLocationUpdater::~MusMouseLocationUpdater() { |
| + // Should never be destroyed while in the middle of processing an event. |
| + DCHECK(!observing_message_loop_); |
| +} |
| + |
| +void MusMouseLocationUpdater::OnEventProcessingStarted(const ui::Event& event) { |
| + // Should never process an event while processing another event, unless |
| + // a nested message loop is spun, which sets |observing_message_loop_| to |
| + // false. |
| + DCHECK(!observing_message_loop_); |
| + |
| + if (!IsMouseEventWithLocation(event)) |
| + return; |
| + |
| + Env::GetInstance()->set_last_mouse_location( |
| + event.AsMouseEvent()->root_location()); |
| + Env::GetInstance()->get_last_mouse_location_from_mus_ = false; |
|
sadrul
2017/01/27 21:08:36
Maybe call this |last_mouse_location_is_valid_| (a
|
| + observing_message_loop_ = true; |
| + base::MessageLoop::current()->AddNestingObserver(this); |
|
sadrul
2017/01/27 21:08:36
Is it better to just leave it always added as a ne
|
| +} |
| + |
| +void MusMouseLocationUpdater::OnEventProcessingFinished() { |
| + if (!observing_message_loop_) |
| + return; |
| + |
| + StopObserving(); |
| +} |
| + |
| +void MusMouseLocationUpdater::StopObserving() { |
| + DCHECK(observing_message_loop_); |
| + observing_message_loop_ = false; |
| + base::MessageLoop::current()->RemoveNestingObserver(this); |
| + Env::GetInstance()->get_last_mouse_location_from_mus_ = true; |
| +} |
| + |
| +void MusMouseLocationUpdater::OnBeginNestedMessageLoop() { |
| + StopObserving(); |
| +} |
| + |
| +} // namespace aura |