summaryrefslogtreecommitdiffstats
path: root/views/controls
diff options
context:
space:
mode:
authorderat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-15 23:15:45 +0000
committerderat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-15 23:15:45 +0000
commitadc407950219a661c21804ee68729ce0fd1e460c (patch)
tree7923da1423b440c9f6f677f153314e988d948a12 /views/controls
parente2f0954d5f4ecbcd8b701e0b74b677c8b2ed3eeb (diff)
downloadchromium_src-adc407950219a661c21804ee68729ce0fd1e460c.zip
chromium_src-adc407950219a661c21804ee68729ce0fd1e460c.tar.gz
chromium_src-adc407950219a661c21804ee68729ce0fd1e460c.tar.bz2
chromeos: Improve brightness and volume animations.
Make the onscreen brightness and volume indicators not be janky. Also change views::ProgressBar to permit specifying min and max double values, instead of just using a hardcoded integer range between 0 and 100. BUG=chromium-os:19141,chromium-os:8473 TEST=manual: held volume keys and checked that animation is smooth Review URL: http://codereview.chromium.org/7646004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@96853 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'views/controls')
-rw-r--r--views/controls/progress_bar.cc147
-rw-r--r--views/controls/progress_bar.h56
-rw-r--r--views/controls/progress_bar_unittest.cc28
3 files changed, 104 insertions, 127 deletions
diff --git a/views/controls/progress_bar.cc b/views/controls/progress_bar.cc
index 46b89a2..7091500 100644
--- a/views/controls/progress_bar.cc
+++ b/views/controls/progress_bar.cc
@@ -4,6 +4,7 @@
#include "views/controls/progress_bar.h"
+#include <algorithm>
#include <string>
#include "base/logging.h"
@@ -20,6 +21,9 @@
#include "views/border.h"
#include "views/painter.h"
+using std::max;
+using std::min;
+
namespace {
// Corner radius for the progress bar's border.
@@ -28,11 +32,11 @@ const int kCornerRadius = 3;
// Progress bar's border width
const int kBorderWidth = 1;
-static void AddRoundRectPathWithPadding(int x, int y,
- int w, int h,
- int corner_radius,
- SkScalar padding,
- SkPath* path) {
+void AddRoundRectPathWithPadding(int x, int y,
+ int w, int h,
+ int corner_radius,
+ SkScalar padding,
+ SkPath* path) {
DCHECK(path);
if (path == NULL)
return;
@@ -46,22 +50,22 @@ static void AddRoundRectPathWithPadding(int x, int y,
SkIntToScalar(corner_radius) - padding);
}
-static void AddRoundRectPath(int x, int y,
- int w, int h,
- int corner_radius,
- SkPath* path) {
+void AddRoundRectPath(int x, int y,
+ int w, int h,
+ int corner_radius,
+ SkPath* path) {
static const SkScalar half = SkIntToScalar(1) / 2;
AddRoundRectPathWithPadding(x, y, w, h, corner_radius, half, path);
}
-static void FillRoundRect(gfx::Canvas* canvas,
- int x, int y,
- int w, int h,
- int corner_radius,
- const SkColor colors[],
- const SkScalar points[],
- int count,
- bool gradient_horizontal) {
+void FillRoundRect(gfx::Canvas* canvas,
+ int x, int y,
+ int w, int h,
+ int corner_radius,
+ const SkColor colors[],
+ const SkScalar points[],
+ int count,
+ bool gradient_horizontal) {
SkPath path;
AddRoundRectPath(x, y, w, h, corner_radius, &path);
SkPaint paint;
@@ -84,13 +88,13 @@ static void FillRoundRect(gfx::Canvas* canvas,
canvas->AsCanvasSkia()->drawPath(path, paint);
}
-static void FillRoundRect(gfx::Canvas* canvas,
- int x, int y,
- int w, int h,
- int corner_radius,
- SkColor gradient_start_color,
- SkColor gradient_end_color,
- bool gradient_horizontal) {
+void FillRoundRect(gfx::Canvas* canvas,
+ int x, int y,
+ int w, int h,
+ int corner_radius,
+ SkColor gradient_start_color,
+ SkColor gradient_end_color,
+ bool gradient_horizontal) {
if (gradient_start_color != gradient_end_color) {
SkColor colors[2] = { gradient_start_color, gradient_end_color };
FillRoundRect(canvas, x, y, w, h, corner_radius,
@@ -106,12 +110,12 @@ static void FillRoundRect(gfx::Canvas* canvas,
}
}
-static void StrokeRoundRect(gfx::Canvas* canvas,
- int x, int y,
- int w, int h,
- int corner_radius,
- SkColor stroke_color,
- int stroke_width) {
+void StrokeRoundRect(gfx::Canvas* canvas,
+ int x, int y,
+ int w, int h,
+ int corner_radius,
+ SkColor stroke_color,
+ int stroke_width) {
SkPath path;
AddRoundRectPath(x, y, w, h, corner_radius, &path);
SkPaint paint;
@@ -129,11 +133,11 @@ namespace views {
// static
const char ProgressBar::kViewClassName[] = "views/ProgressBar";
-// static: progress bar's maximum value.
-const int ProgressBar::kMaxProgress = 100;
-
-ProgressBar::ProgressBar(): progress_(0) {
+ProgressBar::ProgressBar()
+ : min_display_value_(0.0),
+ max_display_value_(1.0),
+ current_value_(0.0) {
}
ProgressBar::~ProgressBar() {
@@ -143,7 +147,23 @@ gfx::Size ProgressBar::GetPreferredSize() {
return gfx::Size(100, 16);
}
+std::string ProgressBar::GetClassName() const {
+ return kViewClassName;
+}
+
+void ProgressBar::GetAccessibleState(ui::AccessibleViewState* state) {
+ state->role = ui::AccessibilityTypes::ROLE_PROGRESSBAR;
+ state->state = ui::AccessibilityTypes::STATE_READONLY;
+}
+
void ProgressBar::OnPaint(gfx::Canvas* canvas) {
+ const double capped_value =
+ min(max(current_value_, min_display_value_), max_display_value_);
+ const double capped_fraction =
+ (capped_value - min_display_value_) /
+ (max_display_value_ - min_display_value_);
+ const int progress_width = static_cast<int>(width() * capped_fraction + 0.5);
+
#if defined(OS_CHROMEOS)
const SkColor background_colors[] = {
SkColorSetRGB(0xBB, 0xBB, 0xBB),
@@ -173,10 +193,8 @@ void ProgressBar::OnPaint(gfx::Canvas* canvas) {
background_border_color,
kBorderWidth);
- if (progress_ * width() > 1) {
- int progress_width = progress_ * width() / kMaxProgress;
-
- bool enabled = IsEnabled();
+ if (progress_width > 1) {
+ const bool enabled = IsEnabled();
const SkColor bar_color_start = enabled ?
SkColorSetRGB(100, 116, 147) :
@@ -253,10 +271,10 @@ void ProgressBar::OnPaint(gfx::Canvas* canvas) {
background_color_start,
background_color_end,
false);
- if (progress_ * width() > 1) {
+ if (progress_width > 1) {
FillRoundRect(canvas,
0, 0,
- progress_ * width() / kMaxProgress, height(),
+ progress_width, height(),
kCornerRadius,
bar_color_start,
bar_color_end,
@@ -271,31 +289,6 @@ void ProgressBar::OnPaint(gfx::Canvas* canvas) {
#endif
}
-std::string ProgressBar::GetClassName() const {
- return kViewClassName;
-}
-
-void ProgressBar::SetProgress(int progress) {
- progress_ = progress;
- if (progress_ < 0)
- progress_ = 0;
- else if (progress_ > kMaxProgress)
- progress_ = kMaxProgress;
- SchedulePaint();
-}
-
-int ProgressBar::GetProgress() const {
- return progress_;
-}
-
-void ProgressBar::AddProgress(int tick) {
- SetProgress(progress_ + tick);
-}
-
-void ProgressBar::SetTooltipText(const std::wstring& tooltip_text) {
- tooltip_text_ = WideToUTF16Hack(tooltip_text);
-}
-
bool ProgressBar::GetTooltipText(const gfx::Point& p, std::wstring* tooltip) {
DCHECK(tooltip);
if (tooltip == NULL)
@@ -304,14 +297,26 @@ bool ProgressBar::GetTooltipText(const gfx::Point& p, std::wstring* tooltip) {
return !tooltip_text_.empty();
}
-void ProgressBar::OnEnabledChanged() {
- View::OnEnabledChanged();
- // TODO(denisromanov): Need to switch progress bar color here?
+void ProgressBar::SetDisplayRange(double min_display_value,
+ double max_display_value) {
+ if (min_display_value != min_display_value_ ||
+ max_display_value != max_display_value_) {
+ DCHECK(min_display_value < max_display_value);
+ min_display_value_ = min_display_value;
+ max_display_value_ = max_display_value;
+ SchedulePaint();
+ }
}
-void ProgressBar::GetAccessibleState(ui::AccessibleViewState* state) {
- state->role = ui::AccessibilityTypes::ROLE_PROGRESSBAR;
- state->state = ui::AccessibilityTypes::STATE_READONLY;
+void ProgressBar::SetValue(double value) {
+ if (value != current_value_) {
+ current_value_ = value;
+ SchedulePaint();
+ }
+}
+
+void ProgressBar::SetTooltipText(const std::wstring& tooltip_text) {
+ tooltip_text_ = WideToUTF16Hack(tooltip_text);
}
} // namespace views
diff --git a/views/controls/progress_bar.h b/views/controls/progress_bar.h
index 61c050f..5156c8f 100644
--- a/views/controls/progress_bar.h
+++ b/views/controls/progress_bar.h
@@ -28,52 +28,48 @@ namespace views {
class VIEWS_EXPORT ProgressBar : public View {
public:
+ // The value range defaults to [0.0, 1.0].
ProgressBar();
virtual ~ProgressBar();
- // Overridden to return preferred size of the progress bar.
- virtual gfx::Size GetPreferredSize();
+ double current_value() const { return current_value_; }
- // Returns views/ProgressBar.
- virtual std::string GetClassName() const;
+ // View implementation.
+ virtual gfx::Size GetPreferredSize() OVERRIDE;
+ virtual std::string GetClassName() const OVERRIDE;
+ virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
+ virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
+ virtual bool GetTooltipText(const gfx::Point& p, std::wstring* tooltip)
+ OVERRIDE;
- // Overridden to paint
- virtual void OnPaint(gfx::Canvas* canvas);
+ // Sets the inclusive range of values to be displayed. Values outside of the
+ // range will be capped when displayed.
+ virtual void SetDisplayRange(double min_display_value,
+ double max_display_value);
- // Set and get the progress bar progress in range [0, kMaxProgress].
- virtual void SetProgress(int progress);
- virtual int GetProgress() const;
- // Add progress to current.
- virtual void AddProgress(int tick);
+ // Sets the current value. Values outside of the range [min_display_value_,
+ // max_display_value_] will be stored unmodified and capped for display.
+ virtual void SetValue(double value);
- // Sets the tooltip text. Default behavior for a progress bar is to show
- // no tooltip on mouse hover. Calling this lets you set a custom tooltip.
- // To revert to default behavior, call this with an empty string.
+ // Sets the tooltip text. Default behavior for a progress bar is to show no
+ // tooltip on mouse hover. Calling this lets you set a custom tooltip. To
+ // revert to default behavior, call this with an empty string.
virtual void SetTooltipText(const std::wstring& tooltip_text);
- // Gets the tooltip text if has been specified with SetTooltipText().
- virtual bool GetTooltipText(const gfx::Point& p, std::wstring* tooltip)
- OVERRIDE;
-
- // Sets the enabled state.
- virtual void OnEnabledChanged() OVERRIDE;
- // Accessibility accessors, overridden from View.
- virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
+ private:
+ static const char kViewClassName[];
- // Maximum value of progress.
- static const int kMaxProgress;
+ // Inclusive range used when displaying values.
+ double min_display_value_;
+ double max_display_value_;
- private:
- // Progress in range [0, kMaxProgress].
- int progress_;
+ // Current value. May be outside of [min_display_value_, max_display_value_].
+ double current_value_;
// Tooltip text.
string16 tooltip_text_;
- // The view class name.
- static const char kViewClassName[];
-
DISALLOW_COPY_AND_ASSIGN(ProgressBar);
};
diff --git a/views/controls/progress_bar_unittest.cc b/views/controls/progress_bar_unittest.cc
index c7fc4df..16f1581 100644
--- a/views/controls/progress_bar_unittest.cc
+++ b/views/controls/progress_bar_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// 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.
@@ -10,30 +10,6 @@
namespace views {
-TEST(ProgressBarTest, ProgressProperty) {
- ProgressBar bar;
- bar.SetProgress(-1);
- int progress = bar.GetProgress();
- EXPECT_EQ(0, progress);
- bar.SetProgress(300);
- progress = bar.GetProgress();
- EXPECT_EQ(100, progress);
- bar.SetProgress(62);
- progress = bar.GetProgress();
- EXPECT_EQ(62, progress);
-}
-
-TEST(ProgressBarTest, AddProgressMethod) {
- ProgressBar bar;
- bar.SetProgress(10);
- bar.AddProgress(22);
- int progress = bar.GetProgress();
- EXPECT_EQ(32, progress);
- bar.AddProgress(200);
- progress = bar.GetProgress();
- EXPECT_EQ(100, progress);
-}
-
TEST(ProgressBarTest, TooltipTextProperty) {
ProgressBar bar;
std::wstring tooltip = L"Some text";
@@ -47,7 +23,7 @@ TEST(ProgressBarTest, TooltipTextProperty) {
TEST(ProgressBarTest, Accessibility) {
ProgressBar bar;
- bar.SetProgress(62);
+ bar.SetValue(62);
ui::AccessibleViewState state;
bar.GetAccessibleState(&state);