From 1c34932902e6462e70980112ee045907492602c8 Mon Sep 17 00:00:00 2001 From: "jar@chromium.org" Date: Tue, 30 Aug 2011 18:52:44 +0000 Subject: Revert 98850 - Proper MouseEvent targeting. Adds a Window method that locates a Window for a given point. Also adds an Aura test suite. http://crbug.com/93933 http://crbug.com/93943 TEST=see unittest Review URL: http://codereview.chromium.org/7791030 TBR=ben@chromium.org Review URL: http://codereview.chromium.org/7739002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@98852 0039d316-1c4b-4281-b951-d872f2087c98 --- aura/aura.gyp | 26 ---------- aura/demo/demo_main.cc | 6 +-- aura/desktop.cc | 16 ++---- aura/desktop.h | 11 ++-- aura/desktop_host.h | 3 +- aura/desktop_host_win.cc | 11 ---- aura/desktop_host_win.h | 1 - aura/event.cc | 15 ------ aura/event.h | 12 ----- aura/root_window.cc | 35 ------------- aura/root_window.h | 33 ------------ aura/run_all_unittests.cc | 9 ---- aura/test_suite.cc | 32 ------------ aura/test_suite.h | 22 -------- aura/window.cc | 32 +----------- aura/window.h | 18 ------- aura/window_delegate.h | 10 +--- aura/window_unittest.cc | 124 ---------------------------------------------- 18 files changed, 11 insertions(+), 405 deletions(-) delete mode 100644 aura/root_window.cc delete mode 100644 aura/root_window.h delete mode 100644 aura/run_all_unittests.cc delete mode 100644 aura/test_suite.cc delete mode 100644 aura/test_suite.h delete mode 100644 aura/window_unittest.cc (limited to 'aura') diff --git a/aura/aura.gyp b/aura/aura.gyp index a304860..fa76c99 100644 --- a/aura/aura.gyp +++ b/aura/aura.gyp @@ -29,8 +29,6 @@ 'event.cc', 'event.h', 'event_win.cc', - 'root_window.cc', - 'root_window.h', 'window.cc', 'window.h', 'window_delegate.h', @@ -60,29 +58,5 @@ '<(SHARED_INTERMEDIATE_DIR)/ui/ui_resources/ui_resources.rc', ], }, - { - 'target_name': 'aura_unittests', - 'type': 'executable', - 'dependencies': [ - '<(DEPTH)/base/base.gyp:test_support_base', - '<(DEPTH)/skia/skia.gyp:skia', - '<(DEPTH)/testing/gtest.gyp:gtest', - '<(DEPTH)/ui/ui.gyp:gfx_resources', - '<(DEPTH)/ui/ui.gyp:ui', - '<(DEPTH)/ui/ui.gyp:ui_resources', - 'aura', - ], - 'include_dirs': [ - '..', - ], - 'sources': [ - 'window_unittest.cc', - 'run_all_unittests.cc', - 'test_suite.cc', - 'test_suite.h', - '<(SHARED_INTERMEDIATE_DIR)/ui/gfx/gfx_resources.rc', - '<(SHARED_INTERMEDIATE_DIR)/ui/ui_resources/ui_resources.rc', - ], - }, ], } diff --git a/aura/demo/demo_main.cc b/aura/demo/demo_main.cc index a0758c5..09cfc29 100644 --- a/aura/demo/demo_main.cc +++ b/aura/demo/demo_main.cc @@ -3,7 +3,6 @@ // found in the LICENSE file. #include "aura/desktop.h" -#include "aura/event.h" #include "aura/window.h" #include "aura/window_delegate.h" #include "base/at_exit.h" @@ -13,6 +12,7 @@ #include "third_party/skia/include/core/SkXfermode.h" #include "ui/base/resource/resource_bundle.h" #include "ui/base/ui_base_paths.h" +#include "ui/gfx/canvas.h" #include "ui/gfx/canvas_skia.h" #include "ui/gfx/rect.h" @@ -23,10 +23,6 @@ class DemoWindowDelegate : public aura::WindowDelegate { public: explicit DemoWindowDelegate(SkColor color) : color_(color) {} - virtual bool OnMouseEvent(const aura::MouseEvent& event) OVERRIDE { - return true; - } - virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { canvas->AsCanvasSkia()->drawColor(color_, SkXfermode::kSrc_Mode); } diff --git a/aura/desktop.cc b/aura/desktop.cc index 2bac955..f427daf 100644 --- a/aura/desktop.cc +++ b/aura/desktop.cc @@ -5,7 +5,6 @@ #include "aura/desktop.h" #include "aura/desktop_host.h" -#include "aura/root_window.h" #include "aura/window.h" #include "base/logging.h" #include "base/message_loop.h" @@ -22,34 +21,27 @@ Desktop::Desktop() host_->GetSize()); host_->SetDesktop(this); DCHECK(compositor_.get()); - window_.reset(new internal::RootWindow); + window_.reset(new Window(NULL)); } Desktop::~Desktop() { } -void Desktop::Show() { - host_->Show(); -} - -void Desktop::SetSize(const gfx::Size& size) { - host_->SetSize(size); -} - void Desktop::Run() { - Show(); + host_->Show(); MessageLoop main_message_loop(MessageLoop::TYPE_UI); MessageLoopForUI::current()->Run(host_); } void Desktop::Draw() { + // Second pass renders the layers. compositor_->NotifyStart(); window_->DrawTree(); compositor_->NotifyEnd(); } bool Desktop::OnMouseEvent(const MouseEvent& event) { - return window_->HandleMouseEvent(event); + return window_->OnMouseEvent(event); } // static diff --git a/aura/desktop.h b/aura/desktop.h index 3139cae..a894e24 100644 --- a/aura/desktop.h +++ b/aura/desktop.h @@ -6,7 +6,7 @@ #define AURA_DESKTOP_H_ #pragma once -#include "aura/root_window.h" +#include "aura/window.h" #include "base/basictypes.h" #include "base/memory/ref_counted.h" #include "ui/gfx/native_widget_types.h" @@ -23,6 +23,7 @@ namespace aura { class DesktopHost; class MouseEvent; +class Window; // Desktop is responsible for hosting a set of windows. class Desktop { @@ -30,12 +31,6 @@ class Desktop { Desktop(); ~Desktop(); - // Shows the desktop host. - void Show(); - - // Sets the size of the desktop. - void SetSize(const gfx::Size& size); - // Shows the desktop host and runs an event loop for it. void Run(); @@ -55,7 +50,7 @@ class Desktop { private: scoped_refptr compositor_; - scoped_ptr window_; + scoped_ptr window_; DesktopHost* host_; diff --git a/aura/desktop_host.h b/aura/desktop_host.h index ad60f40..f7ba295 100644 --- a/aura/desktop_host.h +++ b/aura/desktop_host.h @@ -37,9 +37,8 @@ class DesktopHost : public MessageLoop::Dispatcher { // Shows the DesktopHost. virtual void Show() = 0; - // Gets/Sets the size of the DesktopHost. + // Returns the size of the DesktopHost. virtual gfx::Size GetSize() = 0; - virtual void SetSize(const gfx::Size& size) = 0; }; } // namespace aura diff --git a/aura/desktop_host_win.cc b/aura/desktop_host_win.cc index d922221..f235b6e 100644 --- a/aura/desktop_host_win.cc +++ b/aura/desktop_host_win.cc @@ -47,17 +47,6 @@ gfx::Size DesktopHostWin::GetSize() { return gfx::Rect(r).size(); } -void DesktopHostWin::SetSize(const gfx::Size& size) { - SetWindowPos( - hwnd(), - NULL, - 0, - 0, - size.width(), - size.height(), - SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW | SWP_NOREPOSITION); -} - void DesktopHostWin::OnClose() { // TODO: this obviously shouldn't be here. MessageLoopForUI::current()->Quit(); diff --git a/aura/desktop_host_win.h b/aura/desktop_host_win.h index 185cf1f..6a8165b 100644 --- a/aura/desktop_host_win.h +++ b/aura/desktop_host_win.h @@ -25,7 +25,6 @@ class DesktopHostWin : public DesktopHost, public ui::WindowImpl { virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE; virtual void Show() OVERRIDE; virtual gfx::Size GetSize() OVERRIDE; - virtual void SetSize(const gfx::Size& size) OVERRIDE; private: BEGIN_MSG_MAP_EX(DesktopHostWin) diff --git a/aura/event.cc b/aura/event.cc index 055f004..2a62a4c 100644 --- a/aura/event.cc +++ b/aura/event.cc @@ -4,8 +4,6 @@ #include "aura/event.h" -#include "aura/window.h" - namespace aura { Event::Event(ui::EventType type, int flags) @@ -29,18 +27,5 @@ Event::Event(const Event& copy) flags_(copy.flags_) { } -LocatedEvent::LocatedEvent(const LocatedEvent& model, - Window* source, - Window* target) - : Event(model), - location_(model.location_) { - if (target && target != source) - Window::ConvertPointToWindow(source, target, &location_); -} - -MouseEvent::MouseEvent(const MouseEvent& model, Window* source, Window* target) - : LocatedEvent(model, source, target) { -} - } // namespace aura diff --git a/aura/event.h b/aura/event.h index be08345..27a8f30 100644 --- a/aura/event.h +++ b/aura/event.h @@ -21,8 +21,6 @@ typedef union _XEvent XEvent; typedef XEvent* NativeEvent; #endif -class Window; - class Event { public: const NativeEvent& native_event() const { return native_event_; } @@ -57,11 +55,6 @@ class LocatedEvent : public Event { protected: explicit LocatedEvent(NativeEvent native_event); - // Create a new LocatedEvent which is identical to the provided model. - // If source / target windows are provided, the model location will be - // converted from |source| coordinate system to |target| coordinate system. - LocatedEvent(const LocatedEvent& model, Window* source, Window* target); - gfx::Point location_; private: @@ -72,11 +65,6 @@ class MouseEvent : public LocatedEvent { public: explicit MouseEvent(NativeEvent native_event); - // Create a new MouseEvent which is identical to the provided model. - // If source / target windows are provided, the model location will be - // converted from |source| coordinate system to |target| coordinate system. - MouseEvent(const MouseEvent& model, Window* source, Window* target); - private: DISALLOW_COPY_AND_ASSIGN(MouseEvent); }; diff --git a/aura/root_window.cc b/aura/root_window.cc deleted file mode 100644 index 6e3df25..0000000 --- a/aura/root_window.cc +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2011 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 "aura/root_window.h" - -#include "base/logging.h" -#include "aura/event.h" -#include "aura/window_delegate.h" -#include "ui/base/events.h" - -namespace aura { -namespace internal { - -RootWindow::RootWindow() : Window(NULL), mouse_pressed_handler_(NULL) { -} - -RootWindow::~RootWindow() { -} - -bool RootWindow::HandleMouseEvent(const MouseEvent& event) { - Window* target = mouse_pressed_handler_; - if (!target) - target = GetEventHandlerForPoint(event.location()); - if (event.type() == ui::ET_MOUSE_PRESSED && !mouse_pressed_handler_) - mouse_pressed_handler_ = target; - if (target->delegate()) { - MouseEvent translated_event(event, this, target); - return target->delegate()->OnMouseEvent(translated_event); - } - return false; -} - -} // namespace internal -} // namespace aura diff --git a/aura/root_window.h b/aura/root_window.h deleted file mode 100644 index e2f02fd..0000000 --- a/aura/root_window.h +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) 2011 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. - -#ifndef AURA_ROOT_WINDOW_H_ -#define AURA_ROOT_WINDOW_H_ -#pragma once - -#include "aura/window.h" - -namespace aura { -namespace internal { - -// A Window subclass that handles event targeting for certain types of -// MouseEvent. -class RootWindow : public Window { - public: - RootWindow(); - virtual ~RootWindow(); - - // Handles a mouse event. Returns true if handled. - bool HandleMouseEvent(const MouseEvent& event); - - private: - Window* mouse_pressed_handler_; - - DISALLOW_COPY_AND_ASSIGN(RootWindow); -}; - -} // namespace internal -} // namespace aura - -#endif // AURA_ROOT_WINDOW_H_ diff --git a/aura/run_all_unittests.cc b/aura/run_all_unittests.cc deleted file mode 100644 index 5afef6d..0000000 --- a/aura/run_all_unittests.cc +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) 2011 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 "aura/test_suite.h" - -int main(int argc, char** argv) { - return AuraTestSuite(argc, argv).Run(); -} diff --git a/aura/test_suite.cc b/aura/test_suite.cc deleted file mode 100644 index 54165f3..0000000 --- a/aura/test_suite.cc +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2011 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 "aura/test_suite.h" - -#include "base/file_path.h" -#include "base/path_service.h" -#include "build/build_config.h" -#include "ui/base/resource/resource_bundle.h" -#include "ui/base/ui_base_paths.h" -#include "ui/gfx/gfx_paths.h" - -AuraTestSuite::AuraTestSuite(int argc, char** argv) - : TestSuite(argc, argv) {} - -void AuraTestSuite::Initialize() { - base::TestSuite::Initialize(); - - gfx::RegisterPathProvider(); - ui::RegisterPathProvider(); - - // Force unittests to run using en-US so if we test against string - // output, it'll pass regardless of the system language. - ui::ResourceBundle::InitSharedInstance("en-US"); -} - -void AuraTestSuite::Shutdown() { - ui::ResourceBundle::CleanupSharedInstance(); - - base::TestSuite::Shutdown(); -} diff --git a/aura/test_suite.h b/aura/test_suite.h deleted file mode 100644 index 43465a5..0000000 --- a/aura/test_suite.h +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2011 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. - -#ifndef AURA_TEST_SUITE_H_ -#define AURA_TEST_SUITE_H_ -#pragma once - -#include "base/compiler_specific.h" -#include "base/test/test_suite.h" - -class AuraTestSuite : public base::TestSuite { - public: - AuraTestSuite(int argc, char** argv); - - protected: - // base::TestSuite: - virtual void Initialize() OVERRIDE; - virtual void Shutdown() OVERRIDE; -}; - -#endif // AURA_TEST_SUITE_H_ diff --git a/aura/window.cc b/aura/window.cc index 5a81bfe..e6f62a8 100644 --- a/aura/window.cc +++ b/aura/window.cc @@ -24,10 +24,6 @@ Window::Window(WindowDelegate* delegate) } Window::~Window() { - if (delegate_) - delegate_->OnWindowDestroyed(); - if (parent_) - parent_->RemoveChild(this); } void Window::Init() { @@ -74,6 +70,7 @@ void Window::DrawTree() { UpdateLayerCanvas(); Draw(); + // First pass updates the layer bitmaps. for (Windows::iterator i = children_.begin(); i != children_.end(); ++i) (*i)->DrawTree(); } @@ -94,37 +91,10 @@ void Window::RemoveChild(Window* child) { children_.erase(i); } -// static -void Window::ConvertPointToWindow(Window* source, - Window* target, - gfx::Point* point) { - ui::Layer::ConvertPointToLayer(source->layer(), target->layer(), point); -} - bool Window::OnMouseEvent(const MouseEvent& event) { return true; } -bool Window::HitTest(const gfx::Point& point) { - gfx::Rect local_bounds(gfx::Point(), bounds().size()); - // TODO(beng): hittest masks. - return local_bounds.Contains(point); -} - -Window* Window::GetEventHandlerForPoint(const gfx::Point& point) { - Windows::const_reverse_iterator i = children_.rbegin(); - for (; i != children_.rend(); ++i) { - Window* child = *i; - if (child->visibility() == Window::VISIBILITY_HIDDEN) - continue; - gfx::Point point_in_child_coords(point); - Window::ConvertPointToWindow(this, child, &point_in_child_coords); - if (child->HitTest(point_in_child_coords)) - return child->GetEventHandlerForPoint(point_in_child_coords); - } - return this; -} - void Window::UpdateLayerCanvas() { if (needs_paint_all_) { needs_paint_all_ = false; diff --git a/aura/window.h b/aura/window.h index f5667b8..871ceb6 100644 --- a/aura/window.h +++ b/aura/window.h @@ -27,7 +27,6 @@ class WindowDelegate; // Aura window implementation. Interesting events are sent to the // WindowDelegate. -// TODO(beng): resolve ownership. class Window { public: enum Visibility { @@ -81,26 +80,9 @@ class Window { void AddChild(Window* child); void RemoveChild(Window* child); - static void ConvertPointToWindow(Window* source, - Window* target, - gfx::Point* point); - // Handles a mouse event. Returns true if handled. bool OnMouseEvent(const MouseEvent& event); - WindowDelegate* delegate() { return delegate_; } - - // Returns true if the mouse pointer at the specified |point| can trigger an - // event for this Window. - // TODO(beng): - // A Window can supply a hit-test mask to cause some portions of itself to not - // trigger events, causing the events to fall through to the Window behind. - bool HitTest(const gfx::Point& point); - - // Returns the Window that most closely encloses |point| for the purposes of - // event targeting. - Window* GetEventHandlerForPoint(const gfx::Point& point); - private: typedef std::vector Windows; diff --git a/aura/window_delegate.h b/aura/window_delegate.h index 93ad8f5..ffa3191 100644 --- a/aura/window_delegate.h +++ b/aura/window_delegate.h @@ -12,19 +12,11 @@ class Canvas; namespace aura { -class MouseEvent; - // Delegate interface for aura::Window. class WindowDelegate { public: - virtual bool OnMouseEvent(const MouseEvent& event) = 0; - // Asks the delegate to paint window contents into the supplied canvas. - virtual void OnPaint(gfx::Canvas* canvas) = 0; - - // Called when the Window has been destroyed (i.e. from its destructor). - // The delegate can use this as an opportunity to delete itself if necessary. - virtual void OnWindowDestroyed() = 0; + virtual void OnPaint(gfx::Canvas* canvas) = 0; protected: virtual ~WindowDelegate() {} diff --git a/aura/window_unittest.cc b/aura/window_unittest.cc deleted file mode 100644 index 7e16519..0000000 --- a/aura/window_unittest.cc +++ /dev/null @@ -1,124 +0,0 @@ -// Copyright (c) 2011 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 "aura/desktop.h" -#include "aura/window_delegate.h" -#include "base/basictypes.h" -#include "base/compiler_specific.h" -#include "base/message_loop.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "ui/gfx/canvas_skia.h" - -namespace aura { -namespace internal { - -namespace { - -// A simple WindowDelegate implementation for these tests. It owns itself -// (deletes itself when the Window it is attached to is destroyed). -class TestWindowDelegate : public WindowDelegate { - public: - TestWindowDelegate(SkColor color) : color_(color) {} - virtual ~TestWindowDelegate() {} - - // Overridden from WindowDelegate: - virtual bool OnMouseEvent(const MouseEvent& event) OVERRIDE { return false; } - virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { - canvas->AsCanvasSkia()->drawColor(color_, SkXfermode::kSrc_Mode); - } - virtual void OnWindowDestroyed() OVERRIDE { - delete this; - } - - private: - SkColor color_; - - DISALLOW_COPY_AND_ASSIGN(TestWindowDelegate); -}; - -class WindowTest : public testing::Test { - public: - WindowTest() { - aura::Desktop::GetInstance()->Show(); - aura::Desktop::GetInstance()->SetSize(gfx::Size(500, 500)); - } - virtual ~WindowTest() {} - - // Overridden from testing::Test: - virtual void SetUp() OVERRIDE { - } - - virtual void TearDown() OVERRIDE { - } - - Window* CreateTestWindow(SkColor color, - int id, - const gfx::Rect& bounds, - Window* parent) { - Window* window = new Window(new TestWindowDelegate(color)); - window->set_id(id); - window->Init(); - window->SetBounds(bounds, 0); - window->SetVisibility(Window::VISIBILITY_SHOWN); - window->SetParent(parent); - return window; - } - - void RunPendingMessages() { - MessageLoop main_message_loop(MessageLoop::TYPE_UI); - MessageLoopForUI::current()->Run(NULL); - } - - private: - DISALLOW_COPY_AND_ASSIGN(WindowTest); -}; - -} // namespace - -TEST_F(WindowTest, HitTest) { - Window w1(new TestWindowDelegate(SK_ColorWHITE)); - w1.set_id(1); - w1.Init(); - w1.SetBounds(gfx::Rect(10, 10, 50, 50), 0); - w1.SetVisibility(Window::VISIBILITY_SHOWN); - w1.SetParent(NULL); - - // Points are in the Window's coordinates. - EXPECT_TRUE(w1.HitTest(gfx::Point(1, 1))); - EXPECT_FALSE(w1.HitTest(gfx::Point(-1, -1))); - - // TODO(beng): clip Window to parent. -} - -TEST_F(WindowTest, GetEventHandlerForPoint) { - scoped_ptr w1( - CreateTestWindow(SK_ColorWHITE, 1, gfx::Rect(10, 10, 500, 500), NULL)); - scoped_ptr w11( - CreateTestWindow(SK_ColorGREEN, 11, gfx::Rect(5, 5, 100, 100), w1.get())); - scoped_ptr w111( - CreateTestWindow(SK_ColorCYAN, 111, gfx::Rect(5, 5, 75, 75), w11.get())); - scoped_ptr w1111( - CreateTestWindow(SK_ColorRED, 1111, gfx::Rect(5, 5, 50, 50), w111.get())); - scoped_ptr w12( - CreateTestWindow(SK_ColorMAGENTA, 12, gfx::Rect(10, 420, 25, 25), - w1.get())); - scoped_ptr w121( - CreateTestWindow(SK_ColorYELLOW, 121, gfx::Rect(5, 5, 5, 5), w12.get())); - scoped_ptr w13( - CreateTestWindow(SK_ColorGRAY, 13, gfx::Rect(5, 470, 50, 50), w1.get())); - - Window* desktop = Desktop::GetInstance()->window(); - EXPECT_EQ(desktop, desktop->GetEventHandlerForPoint(gfx::Point(5, 5))); - EXPECT_EQ(w1.get(), desktop->GetEventHandlerForPoint(gfx::Point(11, 11))); - EXPECT_EQ(w11.get(), desktop->GetEventHandlerForPoint(gfx::Point(16, 16))); - EXPECT_EQ(w111.get(), desktop->GetEventHandlerForPoint(gfx::Point(21, 21))); - EXPECT_EQ(w1111.get(), desktop->GetEventHandlerForPoint(gfx::Point(26, 26))); - EXPECT_EQ(w12.get(), desktop->GetEventHandlerForPoint(gfx::Point(21, 431))); - EXPECT_EQ(w121.get(), desktop->GetEventHandlerForPoint(gfx::Point(26, 436))); - EXPECT_EQ(w13.get(), desktop->GetEventHandlerForPoint(gfx::Point(26, 481))); -} - -} // namespace internal -} // namespace aura - -- cgit v1.1