Commit b027b38a authored by Jun Mukai's avatar Jun Mukai Committed by Commit Bot

Refactor TopmostWindowObserver to be EventObserver

It has been an window observer + pre-target event handler to
conduct the watching of topmost windows. During debugging of
a test of tab-dragging, I've noticed that this can't deal with
the destruction of the root window itself.

Actually, it doesn't have to be a pre-target event handler
anymore since there's EventObserver now.

BUG=890071
TEST=existing ash_unittests / services_unittests pass

Change-Id: Ic6750700e73fd3694be5aa00f080f3019e4fbd95
Reviewed-on: https://chromium-review.googlesource.com/c/1303034
Commit-Queue: Jun Mukai <mukai@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarMichael Wasserman <msw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#603286}
parent c2c723d0
......@@ -16,17 +16,6 @@
#include "ui/gfx/geometry/point_f.h"
#include "ui/wm/core/coordinate_conversion.h"
namespace {
gfx::Point GetLocationInScreen(const ui::LocatedEvent& event) {
gfx::Point location_in_screen = event.location();
::wm::ConvertPointToScreen(static_cast<aura::Window*>(event.target()),
&location_in_screen);
return location_in_screen;
}
} // namespace
namespace ws {
TopmostWindowObserver::TopmostWindowObserver(WindowTree* window_tree,
......@@ -35,50 +24,37 @@ TopmostWindowObserver::TopmostWindowObserver(WindowTree* window_tree,
: window_tree_(window_tree),
source_(source),
last_target_(initial_target),
root_(initial_target->GetRootWindow()) {
root_->AddPreTargetHandler(this, ui::EventTarget::Priority::kSystem);
env_(initial_target->env()) {
std::set<ui::EventType> types;
if (source == mojom::MoveLoopSource::MOUSE) {
last_location_ =
root_->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
::wm::ConvertPointToScreen(root_, &last_location_);
types.insert(ui::ET_MOUSE_MOVED);
types.insert(ui::ET_MOUSE_DRAGGED);
last_location_ = env_->last_mouse_location();
} else {
types.insert(ui::ET_TOUCH_MOVED);
gfx::PointF point;
ui::GestureRecognizer* gesture_recognizer =
initial_target->env()->gesture_recognizer();
ui::GestureRecognizer* gesture_recognizer = env_->gesture_recognizer();
if (gesture_recognizer->GetLastTouchPointForTarget(last_target_, &point))
last_location_ = gfx::Point(point.x(), point.y());
::wm::ConvertPointToScreen(last_target_, &last_location_);
}
env_->AddEventObserver(this, env_, types);
UpdateTopmostWindows();
}
TopmostWindowObserver::~TopmostWindowObserver() {
root_->RemovePreTargetHandler(this);
env_->RemoveEventObserver(this);
if (topmost_)
topmost_->RemoveObserver(this);
if (real_topmost_ && topmost_ != real_topmost_)
real_topmost_->RemoveObserver(this);
}
void TopmostWindowObserver::OnMouseEvent(ui::MouseEvent* event) {
CHECK_EQ(ui::EP_PRETARGET, event->phase());
if (source_ != mojom::MoveLoopSource::MOUSE)
return;
// The event target can change when the dragged browser tab is detached into a
// new window.
last_target_ = static_cast<aura::Window*>(event->target());
last_location_ = GetLocationInScreen(*event);
UpdateTopmostWindows();
}
void TopmostWindowObserver::OnTouchEvent(ui::TouchEvent* event) {
CHECK_EQ(ui::EP_PRETARGET, event->phase());
if (source_ != mojom::MoveLoopSource::TOUCH)
return;
// The event target can change when the dragged browser tab is detached into a
// new window.
last_target_ = static_cast<aura::Window*>(event->target());
last_location_ = GetLocationInScreen(*event);
void TopmostWindowObserver::OnEvent(const ui::Event& event) {
CHECK(event.IsLocatedEvent());
last_target_ = static_cast<aura::Window*>(event.target());
last_location_ = event.AsLocatedEvent()->location();
::wm::ConvertPointToScreen(last_target_, &last_location_);
UpdateTopmostWindows();
}
......
......@@ -7,10 +7,11 @@
#include "services/ws/public/mojom/window_tree.mojom.h"
#include "ui/aura/window_observer.h"
#include "ui/events/event_handler.h"
#include "ui/events/event_observer.h"
#include "ui/gfx/geometry/point.h"
namespace aura {
class Env;
class Window;
}
......@@ -23,7 +24,7 @@ class WindowTree;
// TODO(mukai): support multiple displays.
class COMPONENT_EXPORT(WINDOW_SERVICE) TopmostWindowObserver
: public ui::EventHandler,
: public ui::EventObserver,
public aura::WindowObserver {
public:
// |source| determines the type of the event, and |initial_target| is the
......@@ -37,9 +38,8 @@ class COMPONENT_EXPORT(WINDOW_SERVICE) TopmostWindowObserver
private:
friend class TopmostWindowObserverTest;
// ui::EventHandler:
void OnMouseEvent(ui::MouseEvent* event) override;
void OnTouchEvent(ui::TouchEvent* event) override;
// ui::EventObserver:
void OnEvent(const ui::Event& event) override;
// aura::WindowObserver:
void OnWindowVisibilityChanged(aura::Window* window, bool visible) override;
......@@ -72,8 +72,7 @@ class COMPONENT_EXPORT(WINDOW_SERVICE) TopmostWindowObserver
// The topmost window (including |last_target_|) under the cursor/touch.
aura::Window* real_topmost_ = nullptr;
// The attached root window.
aura::Window* root_;
aura::Env* env_;
DISALLOW_COPY_AND_ASSIGN(TopmostWindowObserver);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment