summaryrefslogtreecommitdiffstats
path: root/ui/views
diff options
context:
space:
mode:
authortfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-15 21:01:14 +0000
committertfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-15 21:01:14 +0000
commitebaf09fc99d68821c265297499a64246222bf64d (patch)
treefa92c24dd800064692dfd8bb7846b26abd09635a /ui/views
parentdfb6089698f447ef9d564e0222a8137b0c017321 (diff)
downloadchromium_src-ebaf09fc99d68821c265297499a64246222bf64d.zip
chromium_src-ebaf09fc99d68821c265297499a64246222bf64d.tar.gz
chromium_src-ebaf09fc99d68821c265297499a64246222bf64d.tar.bz2
views: Move more two directories into ui/views/.
BUG=104039 R=ben@chromium.org Review URL: http://codereview.chromium.org/8572016 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110162 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/views')
-rw-r--r--ui/views/animation/bounds_animator.cc262
-rw-r--r--ui/views/animation/bounds_animator.h187
-rw-r--r--ui/views/animation/bounds_animator_unittest.cc181
-rw-r--r--ui/views/aura_desktop/aura_desktop_main.cc187
4 files changed, 817 insertions, 0 deletions
diff --git a/ui/views/animation/bounds_animator.cc b/ui/views/animation/bounds_animator.cc
new file mode 100644
index 0000000..d656d0a
--- /dev/null
+++ b/ui/views/animation/bounds_animator.cc
@@ -0,0 +1,262 @@
+// 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 "ui/views/animation/bounds_animator.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "ui/base/animation/animation_container.h"
+#include "ui/base/animation/slide_animation.h"
+#include "views/view.h"
+
+// Duration in milliseconds for animations.
+static const int kAnimationDuration = 200;
+
+using ui::Animation;
+using ui::AnimationContainer;
+using ui::SlideAnimation;
+using ui::Tween;
+
+namespace views {
+
+BoundsAnimator::BoundsAnimator(View* parent)
+ : parent_(parent),
+ observer_(NULL),
+ container_(new AnimationContainer()) {
+ container_->set_observer(this);
+}
+
+BoundsAnimator::~BoundsAnimator() {
+ // Reset the delegate so that we don't attempt to notify our observer from
+ // the destructor.
+ container_->set_observer(NULL);
+
+ // Delete all the animations, but don't remove any child views. We assume the
+ // view owns us and is going to be deleted anyway.
+ for (ViewToDataMap::iterator i = data_.begin(); i != data_.end(); ++i)
+ CleanupData(false, &(i->second), i->first);
+}
+
+void BoundsAnimator::AnimateViewTo(View* view, const gfx::Rect& target) {
+ DCHECK(view);
+ DCHECK_EQ(view->parent(), parent_);
+
+ Data existing_data;
+
+ if (IsAnimating(view)) {
+ // Don't immediatly delete the animation, that might trigger a callback from
+ // the animationcontainer.
+ existing_data = data_[view];
+
+ RemoveFromMaps(view);
+ }
+
+ // NOTE: we don't check if the view is already at the target location. Doing
+ // so leads to odd cases where no animations may be present after invoking
+ // AnimateViewTo. AnimationProgressed does nothing when the bounds of the
+ // view don't change.
+
+ Data& data = data_[view];
+ data.start_bounds = view->bounds();
+ data.target_bounds = target;
+ data.animation = CreateAnimation();
+
+ animation_to_view_[data.animation] = view;
+
+ data.animation->Show();
+
+ CleanupData(true, &existing_data, NULL);
+}
+
+void BoundsAnimator::SetTargetBounds(View* view, const gfx::Rect& target) {
+ if (!IsAnimating(view)) {
+ AnimateViewTo(view, target);
+ return;
+ }
+
+ data_[view].target_bounds = target;
+}
+
+void BoundsAnimator::SetAnimationForView(View* view,
+ SlideAnimation* animation) {
+ DCHECK(animation);
+
+ scoped_ptr<SlideAnimation> animation_wrapper(animation);
+
+ if (!IsAnimating(view))
+ return;
+
+ // We delay deleting the animation until the end so that we don't prematurely
+ // send out notification that we're done.
+ scoped_ptr<Animation> old_animation(ResetAnimationForView(view));
+
+ data_[view].animation = animation_wrapper.release();
+ animation_to_view_[animation] = view;
+
+ animation->set_delegate(this);
+ animation->SetContainer(container_.get());
+ animation->Show();
+}
+
+const SlideAnimation* BoundsAnimator::GetAnimationForView(View* view) {
+ return !IsAnimating(view) ? NULL : data_[view].animation;
+}
+
+void BoundsAnimator::SetAnimationDelegate(View* view,
+ AnimationDelegate* delegate,
+ bool delete_when_done) {
+ DCHECK(IsAnimating(view));
+
+ data_[view].delegate = delegate;
+ data_[view].delete_delegate_when_done = delete_when_done;
+}
+
+void BoundsAnimator::StopAnimatingView(View* view) {
+ if (!IsAnimating(view))
+ return;
+
+ data_[view].animation->Stop();
+}
+
+bool BoundsAnimator::IsAnimating(View* view) const {
+ return data_.find(view) != data_.end();
+}
+
+bool BoundsAnimator::IsAnimating() const {
+ return !data_.empty();
+}
+
+void BoundsAnimator::Cancel() {
+ if (data_.empty())
+ return;
+
+ while (!data_.empty())
+ data_.begin()->second.animation->Stop();
+
+ // Invoke AnimationContainerProgressed to force a repaint and notify delegate.
+ AnimationContainerProgressed(container_.get());
+}
+
+SlideAnimation* BoundsAnimator::CreateAnimation() {
+ SlideAnimation* animation = new SlideAnimation(this);
+ animation->SetContainer(container_.get());
+ animation->SetSlideDuration(kAnimationDuration);
+ animation->SetTweenType(Tween::EASE_OUT);
+ return animation;
+}
+
+void BoundsAnimator::RemoveFromMaps(View* view) {
+ DCHECK(data_.count(view) > 0);
+ DCHECK(animation_to_view_.count(data_[view].animation) > 0);
+
+ animation_to_view_.erase(data_[view].animation);
+ data_.erase(view);
+}
+
+void BoundsAnimator::CleanupData(bool send_cancel, Data* data, View* view) {
+ if (send_cancel && data->delegate)
+ data->delegate->AnimationCanceled(data->animation);
+
+ if (data->delete_delegate_when_done) {
+ delete static_cast<OwnedAnimationDelegate*>(data->delegate);
+ data->delegate = NULL;
+ }
+
+ if (data->animation) {
+ data->animation->set_delegate(NULL);
+ delete data->animation;
+ data->animation = NULL;
+ }
+}
+
+Animation* BoundsAnimator::ResetAnimationForView(View* view) {
+ if (!IsAnimating(view))
+ return NULL;
+
+ Animation* old_animation = data_[view].animation;
+ animation_to_view_.erase(old_animation);
+ data_[view].animation = NULL;
+ // Reset the delegate so that we don't attempt any processing when the
+ // animation calls us back.
+ old_animation->set_delegate(NULL);
+ return old_animation;
+}
+
+void BoundsAnimator::AnimationEndedOrCanceled(const Animation* animation,
+ AnimationEndType type) {
+ DCHECK(animation_to_view_.find(animation) != animation_to_view_.end());
+
+ View* view = animation_to_view_[animation];
+ DCHECK(view);
+
+ // Make a copy of the data as Remove empties out the maps.
+ Data data = data_[view];
+
+ RemoveFromMaps(view);
+
+ if (data.delegate) {
+ if (type == ANIMATION_ENDED) {
+ data.delegate->AnimationEnded(animation);
+ } else {
+ DCHECK_EQ(ANIMATION_CANCELED, type);
+ data.delegate->AnimationCanceled(animation);
+ }
+ }
+
+ CleanupData(false, &data, view);
+}
+
+void BoundsAnimator::AnimationProgressed(const Animation* animation) {
+ DCHECK(animation_to_view_.find(animation) != animation_to_view_.end());
+
+ View* view = animation_to_view_[animation];
+ DCHECK(view);
+ const Data& data = data_[view];
+ gfx::Rect new_bounds =
+ animation->CurrentValueBetween(data.start_bounds, data.target_bounds);
+ if (new_bounds != view->bounds()) {
+ gfx::Rect total_bounds = new_bounds.Union(view->bounds());
+
+ // Build up the region to repaint in repaint_bounds_. We'll do the repaint
+ // when all animations complete (in AnimationContainerProgressed).
+ if (repaint_bounds_.IsEmpty())
+ repaint_bounds_ = total_bounds;
+ else
+ repaint_bounds_ = repaint_bounds_.Union(total_bounds);
+
+ view->SetBoundsRect(new_bounds);
+ }
+
+ if (data.delegate)
+ data.delegate->AnimationProgressed(animation);
+}
+
+void BoundsAnimator::AnimationEnded(const Animation* animation) {
+ AnimationEndedOrCanceled(animation, ANIMATION_ENDED);
+}
+
+void BoundsAnimator::AnimationCanceled(const Animation* animation) {
+ AnimationEndedOrCanceled(animation, ANIMATION_CANCELED);
+}
+
+void BoundsAnimator::AnimationContainerProgressed(
+ AnimationContainer* container) {
+ if (!repaint_bounds_.IsEmpty()) {
+ // Adjust for rtl.
+ repaint_bounds_.set_x(parent_->GetMirroredXWithWidthInView(
+ repaint_bounds_.x(), repaint_bounds_.width()));
+ parent_->SchedulePaintInRect(repaint_bounds_);
+ repaint_bounds_.SetRect(0, 0, 0, 0);
+ }
+
+ if (observer_ && !IsAnimating()) {
+ // Notify here rather than from AnimationXXX to avoid deleting the animation
+ // while the animation is calling us.
+ observer_->OnBoundsAnimatorDone(this);
+ }
+}
+
+void BoundsAnimator::AnimationContainerEmpty(AnimationContainer* container) {
+}
+
+} // namespace views
diff --git a/ui/views/animation/bounds_animator.h b/ui/views/animation/bounds_animator.h
new file mode 100644
index 0000000..b7e0e62
--- /dev/null
+++ b/ui/views/animation/bounds_animator.h
@@ -0,0 +1,187 @@
+// 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 UI_VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_
+#define UI_VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_
+#pragma once
+
+#include <map>
+
+#include "base/memory/ref_counted.h"
+#include "ui/base/animation/animation_container_observer.h"
+#include "ui/base/animation/animation_delegate.h"
+#include "ui/gfx/rect.h"
+#include "views/views_export.h"
+
+namespace ui {
+class SlideAnimation;
+}
+
+namespace views {
+
+class BoundsAnimator;
+class View;
+
+class BoundsAnimatorObserver {
+ public:
+ // Invoked when all animations are complete.
+ virtual void OnBoundsAnimatorDone(BoundsAnimator* animator) = 0;
+};
+
+// Bounds animator is responsible for animating the bounds of a view from the
+// the views current location and size to a target position and size. To use
+// BoundsAnimator invoke AnimateViewTo for the set of views you want to
+// animate.
+//
+// BoundsAnimator internally creates an animation for each view. If you need
+// a specific animation invoke SetAnimationForView after invoking AnimateViewTo.
+// You can attach an AnimationDelegate to the individual animation for a view
+// by way of SetAnimationDelegate. Additionally you can attach an observer to
+// the BoundsAnimator that is notified when all animations are complete.
+class VIEWS_EXPORT BoundsAnimator : public ui::AnimationDelegate,
+ public ui::AnimationContainerObserver {
+ public:
+ // If |delete_when_done| is set to true in |SetAnimationDelegate| the
+ // |AnimationDelegate| must subclass this class.
+ class OwnedAnimationDelegate : public ui::AnimationDelegate {
+ public:
+ virtual ~OwnedAnimationDelegate() {}
+ };
+
+ explicit BoundsAnimator(View* view);
+ virtual ~BoundsAnimator();
+
+ // Starts animating |view| from its current bounds to |target|. If there is
+ // already an animation running for the view it's stopped and a new one
+ // started. If an AnimationDelegate has been set for |view| it is removed
+ // (after being notified that the animation was canceled).
+ void AnimateViewTo(View* view, const gfx::Rect& target);
+
+ // Similar to |AnimateViewTo|, but does not reset the animation, only the
+ // target bounds. If |view| is not being animated this is the same as
+ // invoking |AnimateViewTo|.
+ void SetTargetBounds(View* view, const gfx::Rect& target);
+
+ // Sets the animation for the specified view. BoundsAnimator takes ownership
+ // of the specified animation.
+ void SetAnimationForView(View* view, ui::SlideAnimation* animation);
+
+ // Returns the animation for the specified view. BoundsAnimator owns the
+ // returned Animation.
+ const ui::SlideAnimation* GetAnimationForView(View* view);
+
+ // Stops animating the specified view.
+ void StopAnimatingView(View* view);
+
+ // Sets the delegate for the animation created for the specified view. If
+ // |delete_when_done| is true the |delegate| is deleted when done and
+ // |delegate| must subclass OwnedAnimationDelegate.
+ void SetAnimationDelegate(View* view,
+ ui::AnimationDelegate* delegate,
+ bool delete_when_done);
+
+ // Returns true if BoundsAnimator is animating the bounds of |view|.
+ bool IsAnimating(View* view) const;
+
+ // Returns true if BoundsAnimator is animating any view.
+ bool IsAnimating() const;
+
+ // Cancels all animations, leaving the views at their current location and
+ // size. Any views marked for deletion are deleted.
+ void Cancel();
+
+ void set_observer(BoundsAnimatorObserver* observer) {
+ observer_ = observer;
+ }
+
+ protected:
+ // Creates the animation to use for animating views.
+ virtual ui::SlideAnimation* CreateAnimation();
+
+ private:
+ // Tracks data about the view being animated.
+ struct Data {
+ Data()
+ : delete_delegate_when_done(false),
+ animation(NULL),
+ delegate(NULL) {}
+
+ // If true the delegate is deleted when done.
+ bool delete_delegate_when_done;
+
+ // The initial bounds.
+ gfx::Rect start_bounds;
+
+ // Target bounds.
+ gfx::Rect target_bounds;
+
+ // The animation. We own this.
+ ui::SlideAnimation* animation;
+
+ // Additional delegate for the animation, may be null.
+ ui::AnimationDelegate* delegate;
+ };
+
+ // Used by AnimationEndedOrCanceled.
+ enum AnimationEndType {
+ ANIMATION_ENDED,
+ ANIMATION_CANCELED
+ };
+
+ typedef std::map<View*, Data> ViewToDataMap;
+
+ typedef std::map<const ui::Animation*, View*> AnimationToViewMap;
+
+ // Removes references to |view| and its animation. This does NOT delete the
+ // animation or delegate.
+ void RemoveFromMaps(View* view);
+
+ // Does the necessary cleanup for |data|. If |send_cancel| is true and a
+ // delegate has been installed on |data| AnimationCanceled is invoked on it.
+ void CleanupData(bool send_cancel, Data* data, View* view);
+
+ // Used when changing the animation for a view. This resets the maps for
+ // the animation used by view and returns the current animation. Ownership
+ // of the returned animation passes to the caller.
+ ui::Animation* ResetAnimationForView(View* view);
+
+ // Invoked from AnimationEnded and AnimationCanceled.
+ void AnimationEndedOrCanceled(const ui::Animation* animation,
+ AnimationEndType type);
+
+ // ui::AnimationDelegate overrides.
+ virtual void AnimationProgressed(const ui::Animation* animation);
+ virtual void AnimationEnded(const ui::Animation* animation);
+ virtual void AnimationCanceled(const ui::Animation* animation);
+
+ // ui::AnimationContainerObserver overrides.
+ virtual void AnimationContainerProgressed(ui::AnimationContainer* container);
+ virtual void AnimationContainerEmpty(ui::AnimationContainer* container);
+
+ // Parent of all views being animated.
+ View* parent_;
+
+ BoundsAnimatorObserver* observer_;
+
+ // All animations we create up with the same container.
+ scoped_refptr<ui::AnimationContainer> container_;
+
+ // Maps from view being animated to info about the view.
+ ViewToDataMap data_;
+
+ // Maps from animation to view.
+ AnimationToViewMap animation_to_view_;
+
+ // As the animations we create update (AnimationProgressed is invoked) this
+ // is updated. When all the animations have completed for a given tick of
+ // the timer (AnimationContainerProgressed is invoked) the parent_ is asked
+ // to repaint these bounds.
+ gfx::Rect repaint_bounds_;
+
+ DISALLOW_COPY_AND_ASSIGN(BoundsAnimator);
+};
+
+} // namespace views
+
+#endif // UI_VIEWS_ANIMATION_BOUNDS_ANIMATOR_H_
diff --git a/ui/views/animation/bounds_animator_unittest.cc b/ui/views/animation/bounds_animator_unittest.cc
new file mode 100644
index 0000000..d030ba1
--- /dev/null
+++ b/ui/views/animation/bounds_animator_unittest.cc
@@ -0,0 +1,181 @@
+// 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 "testing/gtest/include/gtest/gtest.h"
+#include "ui/base/animation/slide_animation.h"
+#include "ui/base/animation/test_animation_delegate.h"
+#include "ui/views/animation/bounds_animator.h"
+#include "views/view.h"
+
+using views::BoundsAnimator;
+using ui::Animation;
+using ui::SlideAnimation;
+using ui::TestAnimationDelegate;
+
+namespace {
+
+class TestBoundsAnimator : public BoundsAnimator {
+ public:
+ explicit TestBoundsAnimator(views::View* view) : BoundsAnimator(view) {
+ }
+
+ protected:
+ SlideAnimation* CreateAnimation() {
+ SlideAnimation* animation = BoundsAnimator::CreateAnimation();
+ animation->SetSlideDuration(10);
+ return animation;
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TestBoundsAnimator);
+};
+
+class OwnedDelegate : public BoundsAnimator::OwnedAnimationDelegate {
+ public:
+ OwnedDelegate() {
+ deleted_ = false;
+ canceled_ = false;
+ }
+
+ ~OwnedDelegate() {
+ deleted_ = true;
+ }
+
+ static bool get_and_clear_deleted() {
+ bool value = deleted_;
+ deleted_ = false;
+ return value;
+ }
+
+ static bool get_and_clear_canceled() {
+ bool value = canceled_;
+ canceled_ = false;
+ return value;
+ }
+
+ // AnimationDelegate:
+ virtual void AnimationCanceled(const Animation* animation) {
+ canceled_ = true;
+ }
+
+ private:
+ static bool deleted_;
+ static bool canceled_;
+
+ DISALLOW_COPY_AND_ASSIGN(OwnedDelegate);
+};
+
+// static
+bool OwnedDelegate::deleted_ = false;
+
+// static
+bool OwnedDelegate::canceled_ = false;
+
+class TestView : public views::View {
+ public:
+ TestView() {}
+ virtual void SchedulePaintInRect(const gfx::Rect& r) {
+ if (dirty_rect_.IsEmpty())
+ dirty_rect_ = r;
+ else
+ dirty_rect_ = dirty_rect_.Union(r);
+ }
+
+ const gfx::Rect& dirty_rect() const { return dirty_rect_; }
+
+ private:
+ gfx::Rect dirty_rect_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestView);
+};
+
+} // namespace
+
+class BoundsAnimatorTest : public testing::Test {
+ public:
+ BoundsAnimatorTest() : child_(new TestView()), animator_(&parent_) {
+ parent_.AddChildView(child_);
+ }
+
+ TestView* parent() { return &parent_; }
+ TestView* child() { return child_; }
+ TestBoundsAnimator* animator() { return &animator_; }
+
+ private:
+ MessageLoopForUI message_loop_;
+ TestView parent_;
+ TestView* child_; // Owned by |parent_|.
+ TestBoundsAnimator animator_;
+
+ DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorTest);
+};
+
+// Checks animate view to.
+TEST_F(BoundsAnimatorTest, AnimateViewTo) {
+ TestAnimationDelegate delegate;
+ gfx::Rect initial_bounds(0, 0, 10, 10);
+ child()->SetBoundsRect(initial_bounds);
+ gfx::Rect target_bounds(10, 10, 20, 20);
+ animator()->AnimateViewTo(child(), target_bounds);
+ animator()->SetAnimationDelegate(child(), &delegate, false);
+
+ // The animator should be animating now.
+ EXPECT_TRUE(animator()->IsAnimating());
+
+ // Run the message loop; the delegate exits the loop when the animation is
+ // done.
+ MessageLoop::current()->Run();
+
+ // Make sure the bounds match of the view that was animated match.
+ EXPECT_EQ(target_bounds, child()->bounds());
+
+ // The parent should have been told to repaint as the animation progressed.
+ // The resulting rect is the union of the original and target bounds.
+ EXPECT_EQ(target_bounds.Union(initial_bounds), parent()->dirty_rect());
+}
+
+// Make sure an AnimationDelegate is deleted when canceled.
+TEST_F(BoundsAnimatorTest, DeleteDelegateOnCancel) {
+ animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
+ animator()->SetAnimationDelegate(child(), new OwnedDelegate(), true);
+
+ animator()->Cancel();
+
+ // The animator should no longer be animating.
+ EXPECT_FALSE(animator()->IsAnimating());
+
+ // The cancel should both cancel the delegate and delete it.
+ EXPECT_TRUE(OwnedDelegate::get_and_clear_canceled());
+ EXPECT_TRUE(OwnedDelegate::get_and_clear_deleted());
+}
+
+// Make sure an AnimationDelegate is deleted when another animation is
+// scheduled.
+TEST_F(BoundsAnimatorTest, DeleteDelegateOnNewAnimate) {
+ animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
+ animator()->SetAnimationDelegate(child(), new OwnedDelegate(), true);
+
+ animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
+
+ // Starting a new animation should both cancel the delegate and delete it.
+ EXPECT_TRUE(OwnedDelegate::get_and_clear_deleted());
+ EXPECT_TRUE(OwnedDelegate::get_and_clear_canceled());
+}
+
+// Makes sure StopAnimating works.
+TEST_F(BoundsAnimatorTest, StopAnimating) {
+ scoped_ptr<OwnedDelegate> delegate(new OwnedDelegate());
+
+ animator()->AnimateViewTo(child(), gfx::Rect(0, 0, 10, 10));
+ animator()->SetAnimationDelegate(child(), new OwnedDelegate(), true);
+
+ animator()->StopAnimatingView(child());
+
+ // Shouldn't be animating now.
+ EXPECT_FALSE(animator()->IsAnimating());
+
+ // Stopping should both cancel the delegate and delete it.
+ EXPECT_TRUE(OwnedDelegate::get_and_clear_deleted());
+ EXPECT_TRUE(OwnedDelegate::get_and_clear_canceled());
+}
diff --git a/ui/views/aura_desktop/aura_desktop_main.cc b/ui/views/aura_desktop/aura_desktop_main.cc
new file mode 100644
index 0000000..1e12b08
--- /dev/null
+++ b/ui/views/aura_desktop/aura_desktop_main.cc
@@ -0,0 +1,187 @@
+// 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 "base/at_exit.h"
+#include "base/command_line.h"
+#include "base/i18n/icu_util.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/message_loop.h"
+#include "base/utf_string_conversions.h"
+#include "third_party/skia/include/core/SkXfermode.h"
+#include "ui/aura/desktop.h"
+#include "ui/aura/desktop_host.h"
+#include "ui/aura/window.h"
+#include "ui/aura/window_delegate.h"
+#include "ui/base/hit_test.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"
+#include "views/widget/widget.h"
+#include "views/widget/widget_delegate.h"
+
+namespace {
+
+// Trivial WindowDelegate implementation that draws a colored background.
+class DemoWindowDelegate : public aura::WindowDelegate {
+ public:
+ explicit DemoWindowDelegate(SkColor color) : color_(color) {}
+
+ // Overridden from aura::WindowDelegate:
+ virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
+ const gfx::Rect& new_bounds) OVERRIDE {}
+ virtual void OnFocus() OVERRIDE {}
+ virtual void OnBlur() OVERRIDE {}
+ virtual bool OnKeyEvent(aura::KeyEvent* event) OVERRIDE {
+ return false;
+ }
+ virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
+ return gfx::kNullCursor;
+ }
+ virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
+ return HTCLIENT;
+ }
+ virtual bool OnMouseEvent(aura::MouseEvent* event) OVERRIDE {
+ return true;
+ }
+ virtual ui::TouchStatus OnTouchEvent(aura::TouchEvent* event) OVERRIDE {
+ return ui::TOUCH_STATUS_END;
+ }
+ virtual bool ShouldActivate(aura::Event* event) OVERRIDE {
+ return true;
+ }
+ virtual void OnActivated() OVERRIDE {}
+ virtual void OnLostActive() OVERRIDE {}
+ virtual void OnCaptureLost() OVERRIDE {}
+ virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
+ canvas->GetSkCanvas()->drawColor(color_, SkXfermode::kSrc_Mode);
+ }
+ virtual void OnWindowDestroying() OVERRIDE {
+ }
+ virtual void OnWindowDestroyed() OVERRIDE {
+ }
+ virtual void OnWindowVisibilityChanged(bool visible) OVERRIDE {
+ }
+
+ private:
+ SkColor color_;
+
+ DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
+};
+
+class TestView : public views::View {
+ public:
+ TestView() : color_shifting_(false), color_(SK_ColorYELLOW) {}
+ virtual ~TestView() {}
+
+ private:
+ // Overridden from views::View:
+ virtual void OnPaint(gfx::Canvas* canvas) {
+ canvas->FillRect(color_, GetLocalBounds());
+ }
+ virtual bool OnMousePressed(const views::MouseEvent& event) {
+ color_shifting_ = true;
+ return true;
+ }
+ virtual void OnMouseMoved(const views::MouseEvent& event) {
+ if (color_shifting_) {
+ color_ = SkColorSetRGB((SkColorGetR(color_) + 5) % 255,
+ SkColorGetG(color_),
+ SkColorGetB(color_));
+ SchedulePaint();
+ }
+ }
+ virtual void OnMouseReleased(const views::MouseEvent& event) {
+ color_shifting_ = false;
+ }
+
+ bool color_shifting_;
+ SkColor color_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestView);
+};
+
+class TestWindowContents : public views::WidgetDelegateView {
+ public:
+ TestWindowContents() {}
+ virtual ~TestWindowContents() {}
+
+ private:
+ // Overridden from views::View:
+ virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
+ canvas->FillRect(SK_ColorGRAY, GetLocalBounds());
+ }
+
+ // Overridden from views::WidgetDelegateView:
+ virtual string16 GetWindowTitle() const OVERRIDE {
+ return ASCIIToUTF16("Test Window!");
+ }
+ virtual View* GetContentsView() OVERRIDE {
+ return this;
+ }
+
+ DISALLOW_COPY_AND_ASSIGN(TestWindowContents);
+};
+
+} // namespace
+
+int main(int argc, char** argv) {
+ CommandLine::Init(argc, argv);
+
+ // The exit manager is in charge of calling the dtors of singleton objects.
+ base::AtExitManager exit_manager;
+
+ ui::RegisterPathProvider();
+ icu_util::Initialize();
+ ResourceBundle::InitSharedInstance("en-US");
+
+ // Create the message-loop here before creating the desktop.
+ MessageLoop message_loop(MessageLoop::TYPE_UI);
+
+ aura::Desktop::GetInstance();
+
+ // Create a hierarchy of test windows.
+ DemoWindowDelegate window_delegate1(SK_ColorBLUE);
+ aura::Window* window1 = new aura::Window(&window_delegate1);
+ window1->set_id(1);
+ window1->Init(ui::Layer::LAYER_HAS_TEXTURE);
+ window1->SetBounds(gfx::Rect(100, 100, 400, 400));
+ window1->Show();
+ window1->SetParent(NULL);
+
+ DemoWindowDelegate window_delegate2(SK_ColorRED);
+ aura::Window* window2 = new aura::Window(&window_delegate2);
+ window2->set_id(2);
+ window2->Init(ui::Layer::LAYER_HAS_TEXTURE);
+ window2->SetBounds(gfx::Rect(200, 200, 350, 350));
+ window2->Show();
+ window2->SetParent(NULL);
+
+ DemoWindowDelegate window_delegate3(SK_ColorGREEN);
+ aura::Window* window3 = new aura::Window(&window_delegate3);
+ window3->set_id(3);
+ window3->Init(ui::Layer::LAYER_HAS_TEXTURE);
+ window3->SetBounds(gfx::Rect(10, 10, 50, 50));
+ window3->Show();
+ window3->SetParent(window2);
+
+ views::Widget* widget = new views::Widget;
+ views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL);
+ params.bounds = gfx::Rect(75, 75, 80, 80);
+ params.parent = window2;
+ widget->Init(params);
+ widget->SetContentsView(new TestView);
+
+ TestWindowContents* contents = new TestWindowContents;
+ views::Widget* views_window = views::Widget::CreateWindowWithParentAndBounds(
+ contents, window2, gfx::Rect(120, 150, 200, 200));
+ views_window->Show();
+
+ aura::Desktop::GetInstance()->Run();
+
+ delete aura::Desktop::GetInstance();
+
+ return 0;
+}