summaryrefslogtreecommitdiffstats
path: root/ui
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-16 23:49:23 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-12-16 23:49:23 +0000
commit0c893bcfc8c118649c3d2aa21f0b9c45d233b9e8 (patch)
tree795684f6f20ca4ad74acab978cd90d733610f334 /ui
parentaf62a03111366db3b9991d1c7ed084297f2aa6e0 (diff)
downloadchromium_src-0c893bcfc8c118649c3d2aa21f0b9c45d233b9e8.zip
chromium_src-0c893bcfc8c118649c3d2aa21f0b9c45d233b9e8.tar.gz
chromium_src-0c893bcfc8c118649c3d2aa21f0b9c45d233b9e8.tar.bz2
Refactors the tab glow into views so that it can be used by the
launcher. BUG=none TEST=none R=ben@chromium.org Review URL: http://codereview.chromium.org/8964018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@114882 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r--ui/aura_shell/launcher/tabbed_launcher_button.cc23
-rw-r--r--ui/aura_shell/launcher/tabbed_launcher_button.h6
-rw-r--r--ui/views/controls/glow_hover_controller.cc115
-rw-r--r--ui/views/controls/glow_hover_controller.h83
-rw-r--r--ui/views/views.gyp2
5 files changed, 228 insertions, 1 deletions
diff --git a/ui/aura_shell/launcher/tabbed_launcher_button.cc b/ui/aura_shell/launcher/tabbed_launcher_button.cc
index 404030f..71571e8 100644
--- a/ui/aura_shell/launcher/tabbed_launcher_button.cc
+++ b/ui/aura_shell/launcher/tabbed_launcher_button.cc
@@ -64,7 +64,8 @@ TabbedLauncherButton::TabbedLauncherButton(views::ButtonListener* listener,
: views::ImageButton(listener),
host_(host),
ALLOW_THIS_IN_INITIALIZER_LIST(animation_delegate_(this)),
- show_image_(true) {
+ show_image_(true),
+ ALLOW_THIS_IN_INITIALIZER_LIST(hover_controller_(this)) {
if (!bg_image_1_) {
bg_image_1_ = CreateImageSet(IDR_AURA_LAUNCHER_TABBED_BROWSER_1,
IDR_AURA_LAUNCHER_TABBED_BROWSER_1_PUSHED,
@@ -117,6 +118,8 @@ void TabbedLauncherButton::SetImages(const LauncherTabbedImages& images) {
void TabbedLauncherButton::OnPaint(gfx::Canvas* canvas) {
ImageButton::OnPaint(canvas);
+ hover_controller_.Draw(canvas, *bg_image_1_->normal_image);
+
if (images_.empty() || images_[0].image.empty() || !show_image_)
return;
@@ -139,17 +142,20 @@ void TabbedLauncherButton::OnPaint(gfx::Canvas* canvas) {
bool TabbedLauncherButton::OnMousePressed(const views::MouseEvent& event) {
ImageButton::OnMousePressed(event);
host_->MousePressedOnButton(this, event);
+ hover_controller_.HideImmediately();
return true;
}
void TabbedLauncherButton::OnMouseReleased(const views::MouseEvent& event) {
host_->MouseReleasedOnButton(this, false);
ImageButton::OnMouseReleased(event);
+ hover_controller_.Show();
}
void TabbedLauncherButton::OnMouseCaptureLost() {
host_->MouseReleasedOnButton(this, true);
ImageButton::OnMouseCaptureLost();
+ hover_controller_.Hide();
}
bool TabbedLauncherButton::OnMouseDragged(const views::MouseEvent& event) {
@@ -158,6 +164,21 @@ bool TabbedLauncherButton::OnMouseDragged(const views::MouseEvent& event) {
return true;
}
+void TabbedLauncherButton::OnMouseEntered(const views::MouseEvent& event) {
+ ImageButton::OnMouseEntered(event);
+ hover_controller_.Show();
+}
+
+void TabbedLauncherButton::OnMouseMoved(const views::MouseEvent& event) {
+ ImageButton::OnMouseMoved(event);
+ hover_controller_.SetLocation(event.location());
+}
+
+void TabbedLauncherButton::OnMouseExited(const views::MouseEvent& event) {
+ ImageButton::OnMouseExited(event);
+ hover_controller_.Hide();
+}
+
// static
TabbedLauncherButton::ImageSet* TabbedLauncherButton::CreateImageSet(
int normal_id,
diff --git a/ui/aura_shell/launcher/tabbed_launcher_button.h b/ui/aura_shell/launcher/tabbed_launcher_button.h
index 589c28a..4618cc1 100644
--- a/ui/aura_shell/launcher/tabbed_launcher_button.h
+++ b/ui/aura_shell/launcher/tabbed_launcher_button.h
@@ -11,6 +11,7 @@
#include "ui/aura_shell/launcher/launcher_types.h"
#include "ui/base/animation/animation_delegate.h"
#include "ui/views/controls/button/image_button.h"
+#include "ui/views/controls/glow_hover_controller.h"
namespace ui {
class MultiAnimation;
@@ -41,6 +42,9 @@ class TabbedLauncherButton : public views::ImageButton {
virtual void OnMouseReleased(const views::MouseEvent& event) OVERRIDE;
virtual void OnMouseCaptureLost() OVERRIDE;
virtual bool OnMouseDragged(const views::MouseEvent& event) OVERRIDE;
+ virtual void OnMouseEntered(const views::MouseEvent& event) OVERRIDE;
+ virtual void OnMouseMoved(const views::MouseEvent& event) OVERRIDE;
+ virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE;
private:
// Used as the delegate for |animation_|. TabbedLauncherButton doesn't
@@ -91,6 +95,8 @@ class TabbedLauncherButton : public views::ImageButton {
static ImageSet* bg_image_2_;
static ImageSet* bg_image_3_;
+ views::GlowHoverController hover_controller_;
+
DISALLOW_COPY_AND_ASSIGN(TabbedLauncherButton);
};
diff --git a/ui/views/controls/glow_hover_controller.cc b/ui/views/controls/glow_hover_controller.cc
new file mode 100644
index 0000000..77f5321
--- /dev/null
+++ b/ui/views/controls/glow_hover_controller.cc
@@ -0,0 +1,115 @@
+// 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/controls/glow_hover_controller.h"
+
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/effects/SkGradientShader.h"
+#include "ui/gfx/canvas_skia.h"
+#include "ui/gfx/skbitmap_operations.h"
+#include "ui/views/view.h"
+
+namespace views {
+
+// Amount to scale the opacity.
+static const double kOpacityScale = 0.5;
+
+// How long the hover state takes.
+static const int kHoverDurationMs = 400;
+
+GlowHoverController::GlowHoverController(views::View* view)
+ : view_(view),
+ ALLOW_THIS_IN_INITIALIZER_LIST(animation_(this)) {
+ animation_.set_delegate(this);
+ animation_.SetSlideDuration(kHoverDurationMs);
+}
+
+GlowHoverController::~GlowHoverController() {
+}
+
+void GlowHoverController::SetAnimationContainer(
+ ui::AnimationContainer* container) {
+ animation_.SetContainer(container);
+}
+
+void GlowHoverController::SetLocation(const gfx::Point& location) {
+ location_ = location;
+ if (ShouldDraw())
+ view_->SchedulePaint();
+}
+
+void GlowHoverController::Show() {
+ animation_.SetTweenType(ui::Tween::EASE_OUT);
+ animation_.Show();
+}
+
+void GlowHoverController::Hide() {
+ animation_.SetTweenType(ui::Tween::EASE_IN);
+ animation_.Hide();
+}
+
+void GlowHoverController::HideImmediately() {
+ if (ShouldDraw())
+ view_->SchedulePaint();
+ animation_.Reset();
+}
+
+double GlowHoverController::GetAnimationValue() const {
+ return animation_.GetCurrentValue();
+}
+
+bool GlowHoverController::ShouldDraw() const {
+ return animation_.IsShowing() || animation_.is_animating();
+}
+
+void GlowHoverController::Draw(gfx::Canvas* canvas,
+ const SkBitmap& mask_image) const {
+ if (!ShouldDraw())
+ return;
+
+ // Draw a radial gradient to hover_canvas.
+ gfx::CanvasSkia hover_canvas(view_->width(), view_->height(), false);
+
+ // Draw a radial gradient to hover_canvas.
+ int radius = view_->width() / 3;
+
+ SkPaint paint;
+ paint.setStyle(SkPaint::kFill_Style);
+ paint.setFlags(SkPaint::kAntiAlias_Flag);
+ SkPoint loc = { SkIntToScalar(location_.x()), SkIntToScalar(location_.y()) };
+ SkColor colors[2];
+ int hover_alpha =
+ static_cast<int>(255 * kOpacityScale * animation_.GetCurrentValue());
+ colors[0] = SkColorSetARGB(hover_alpha, 255, 255, 255);
+ colors[1] = SkColorSetARGB(0, 255, 255, 255);
+ SkShader* shader = SkGradientShader::CreateRadial(
+ loc,
+ SkIntToScalar(radius),
+ colors,
+ NULL,
+ 2,
+ SkShader::kClamp_TileMode);
+ // Shader can end up null when radius = 0.
+ // If so, this results in default full tab glow behavior.
+ if (shader) {
+ paint.setShader(shader);
+ shader->unref();
+ hover_canvas.DrawRectInt(location_.x() - radius,
+ location_.y() - radius,
+ radius * 2, radius * 2, paint);
+ }
+ SkBitmap result = SkBitmapOperations::CreateMaskedBitmap(
+ hover_canvas.ExtractBitmap(), mask_image);
+ canvas->DrawBitmapInt(result, 0, 0);
+}
+
+void GlowHoverController::AnimationEnded(const ui::Animation* animation) {
+ view_->SchedulePaint();
+}
+
+void GlowHoverController::AnimationProgressed(const ui::Animation* animation) {
+ view_->SchedulePaint();
+}
+
+} // namespace views
diff --git a/ui/views/controls/glow_hover_controller.h b/ui/views/controls/glow_hover_controller.h
new file mode 100644
index 0000000..69aeecf
--- /dev/null
+++ b/ui/views/controls/glow_hover_controller.h
@@ -0,0 +1,83 @@
+// 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_CONTROLS_GLOW_HOVER_CONTROLLER_H_
+#define UI_VIEWS_CONTROLS_GLOW_HOVER_CONTROLLER_H_
+#pragma once
+
+#include "ui/base/animation/animation_delegate.h"
+#include "ui/base/animation/slide_animation.h"
+#include "ui/views/views_export.h"
+
+namespace gfx {
+class Canvas;
+class Point;
+}
+
+class SkBitmap;
+
+namespace views {
+
+class View;
+
+// GlowHoverController is responsible for drawing a hover effect as is used by
+// the tabstrip. Typical usage:
+// OnMouseEntered() -> invoke Show().
+// OnMouseMoved() -> invoke SetLocation().
+// OnMouseExited() -> invoke Hide().
+// OnPaint() -> if ShouldDraw() returns true invoke Draw().
+// Internally GlowHoverController uses an animation to animate the glow and
+// invokes SchedulePaint() back on the View as necessary.
+class VIEWS_EXPORT GlowHoverController : public ui::AnimationDelegate {
+ public:
+ explicit GlowHoverController(views::View* view);
+ virtual ~GlowHoverController();
+
+ // Sets the AnimationContainer used by the animation.
+ void SetAnimationContainer(ui::AnimationContainer* container);
+
+ // Sets the location of the hover, relative to the View passed to the
+ // constructor.
+ void SetLocation(const gfx::Point& location);
+
+ // Initiates showing the hover.
+ void Show();
+
+ // Hides the hover.
+ void Hide();
+
+ // Hides the hover immediately.
+ void HideImmediately();
+
+ // Returns the value of the animation.
+ double GetAnimationValue() const;
+
+ // Returns true if there is something to be drawn. Use this instead of
+ // invoking Draw() if creating |mask_image| is expensive.
+ bool ShouldDraw() const;
+
+ // If the hover is currently visible it is drawn to the supplied canvas.
+ // |mask_image| is used to determine what parts of the canvas to draw on.
+ void Draw(gfx::Canvas* canvas, const SkBitmap& mask_image) const;
+
+ // ui::AnimationDelegate overrides:
+ virtual void AnimationEnded(const ui::Animation* animation) OVERRIDE;
+ virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE;
+
+ private:
+ // View we're drawing to.
+ views::View* view_;
+
+ // Opacity of the glow ramps up over time.
+ ui::SlideAnimation animation_;
+
+ // Location of the glow, relative to view.
+ gfx::Point location_;
+
+ DISALLOW_COPY_AND_ASSIGN(GlowHoverController);
+};
+
+} // namespace views
+
+#endif // UI_VIEWS_CONTROLS_GLOW_HOVER_CONTROLLER_H_
diff --git a/ui/views/views.gyp b/ui/views/views.gyp
index 4749b2e..147c338 100644
--- a/ui/views/views.gyp
+++ b/ui/views/views.gyp
@@ -90,6 +90,8 @@
'controls/combobox/native_combobox_wrapper.h',
'controls/focusable_border.cc',
'controls/focusable_border.h',
+ 'controls/glow_hover_controller.cc',
+ 'controls/glow_hover_controller.h',
'controls/image_view.cc',
'controls/image_view.h',
'controls/label.cc',