diff options
Diffstat (limited to 'ui/views/controls/scrollbar/overlay_scroll_bar.cc')
-rw-r--r-- | ui/views/controls/scrollbar/overlay_scroll_bar.cc | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/ui/views/controls/scrollbar/overlay_scroll_bar.cc b/ui/views/controls/scrollbar/overlay_scroll_bar.cc new file mode 100644 index 0000000..4fdfda2 --- /dev/null +++ b/ui/views/controls/scrollbar/overlay_scroll_bar.cc @@ -0,0 +1,148 @@ +// Copyright 2013 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/scrollbar/overlay_scroll_bar.h" + +#include "third_party/skia/include/core/SkColor.h" +#include "third_party/skia/include/core/SkXfermode.h" +#include "ui/gfx/canvas.h" +#include "ui/views/background.h" +#include "ui/views/border.h" +#include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h" + +namespace views { +namespace { + +const int kScrollbarWidth = 10; +const int kThumbInsetInside = 3; +const int kThumbInsetFromEdge = 1; +const int kThumbCornerRadius = 2; +const int kThumbMinimumSize = kScrollbarWidth; +const int kThumbHoverAlpha = 128; +const int kThumbDefaultAlpha = 64; + +class OverlayScrollBarThumb : public BaseScrollBarThumb, + public ui::AnimationDelegate { + public: + explicit OverlayScrollBarThumb(BaseScrollBar* scroll_bar); + virtual ~OverlayScrollBarThumb(); + + protected: + // View overrides: + virtual gfx::Size GetPreferredSize() OVERRIDE; + virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; + + // ui::AnimationDelegate overrides: + virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE; + + private: + double animation_opacity_; + DISALLOW_COPY_AND_ASSIGN(OverlayScrollBarThumb); +}; + +OverlayScrollBarThumb::OverlayScrollBarThumb(BaseScrollBar* scroll_bar) + : BaseScrollBarThumb(scroll_bar), + animation_opacity_(0.0) { + if (get_use_acceleration_when_possible()) { + // This is necessary, otherwise the thumb will be rendered below the views + // if those views paint to their own layers. + SetPaintToLayer(true); + SetFillsBoundsOpaquely(false); + } +} + +OverlayScrollBarThumb::~OverlayScrollBarThumb() { +} + +gfx::Size OverlayScrollBarThumb::GetPreferredSize() { + return gfx::Size(kThumbMinimumSize, kThumbMinimumSize); +} + +void OverlayScrollBarThumb::OnPaint(gfx::Canvas* canvas) { + gfx::Rect local_bounds(GetLocalBounds()); + SkPaint paint; + int alpha = (GetState() == CustomButton::STATE_HOVERED || + GetState() == CustomButton::STATE_PRESSED) ? + kThumbHoverAlpha : kThumbDefaultAlpha; + alpha *= animation_opacity_; + paint.setStyle(SkPaint::kFill_Style); + paint.setColor(SkColorSetARGB(alpha, 0, 0, 0)); + canvas->DrawRoundRect(local_bounds, kThumbCornerRadius, paint); +} + +void OverlayScrollBarThumb::AnimationProgressed( + const ui::Animation* animation) { + animation_opacity_ = animation->GetCurrentValue(); + SchedulePaint(); +} + +} // namespace + +OverlayScrollBar::OverlayScrollBar(bool horizontal) + : BaseScrollBar(horizontal, new OverlayScrollBarThumb(this)), + animation_(static_cast<OverlayScrollBarThumb*>(GetThumb())) { + set_notify_enter_exit_on_child(true); +} + +OverlayScrollBar::~OverlayScrollBar() { +} + +gfx::Rect OverlayScrollBar::GetTrackBounds() const { + gfx::Rect local_bounds(GetLocalBounds()); + if (IsHorizontal()) { + local_bounds.Inset(kThumbInsetFromEdge, kThumbInsetInside, + kThumbInsetFromEdge, kThumbInsetFromEdge); + } else { + local_bounds.Inset(kThumbInsetInside, kThumbInsetFromEdge, + kThumbInsetFromEdge, kThumbInsetFromEdge); + } + gfx::Size track_size = local_bounds.size(); + track_size.SetToMax(GetThumb()->size()); + local_bounds.set_size(track_size); + return local_bounds; +} + +int OverlayScrollBar::GetLayoutSize() const { + return 0; +} + +int OverlayScrollBar::GetContentOverlapSize() const { + return kScrollbarWidth; +} + +void OverlayScrollBar::OnMouseEnteredScrollView(const ui::MouseEvent& event) { + if (GetThumb()->layer()) + animation_.Show(); + else + GetThumb()->SetVisible(true); +} + +void OverlayScrollBar::OnMouseExitedScrollView(const ui::MouseEvent& event) { + if (GetThumb()->layer()) + animation_.Hide(); + else + GetThumb()->SetVisible(false); +} + +gfx::Size OverlayScrollBar::GetPreferredSize() { + return gfx::Size(); +} + +void OverlayScrollBar::Layout() { + gfx::Rect thumb_bounds = GetTrackBounds(); + BaseScrollBarThumb* thumb = GetThumb(); + if (IsHorizontal()) { + thumb_bounds.set_x(thumb->x()); + thumb_bounds.set_width(thumb->width()); + } else { + thumb_bounds.set_y(thumb->y()); + thumb_bounds.set_height(thumb->height()); + } + thumb->SetBoundsRect(thumb_bounds); +} + +void OverlayScrollBar::OnPaint(gfx::Canvas* canvas) { +} + +} // namespace views |