1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/controls/scrollbar/kennedy_scroll_bar.h"
7 #include "third_party/skia/include/core/SkColor.h"
8 #include "third_party/skia/include/core/SkXfermode.h"
9 #include "ui/gfx/canvas.h"
10 #include "ui/views/background.h"
11 #include "ui/views/border.h"
12 #include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h"
17 const int kScrollbarWidth
= 10;
18 const int kThumbMinimumSize
= kScrollbarWidth
* 2;
19 const SkColor kBorderColor
= SkColorSetARGB(32, 0, 0, 0);
20 const SkColor kThumbHoverColor
= SkColorSetARGB(128, 0, 0, 0);
21 const SkColor kThumbDefaultColor
= SkColorSetARGB(64, 0, 0, 0);
22 const SkColor kTrackHoverColor
= SkColorSetARGB(32, 0, 0, 0);
24 class KennedyScrollBarThumb
: public BaseScrollBarThumb
{
26 explicit KennedyScrollBarThumb(BaseScrollBar
* scroll_bar
);
27 virtual ~KennedyScrollBarThumb();
31 virtual gfx::Size
GetPreferredSize() const OVERRIDE
;
32 virtual void OnPaint(gfx::Canvas
* canvas
) OVERRIDE
;
35 DISALLOW_COPY_AND_ASSIGN(KennedyScrollBarThumb
);
38 KennedyScrollBarThumb::KennedyScrollBarThumb(BaseScrollBar
* scroll_bar
)
39 : BaseScrollBarThumb(scroll_bar
) {
42 KennedyScrollBarThumb::~KennedyScrollBarThumb() {
45 gfx::Size
KennedyScrollBarThumb::GetPreferredSize() const {
46 return gfx::Size(kThumbMinimumSize
, kThumbMinimumSize
);
49 void KennedyScrollBarThumb::OnPaint(gfx::Canvas
* canvas
) {
50 gfx::Rect
local_bounds(GetLocalBounds());
51 canvas
->FillRect(local_bounds
,
52 (GetState() == CustomButton::STATE_HOVERED
||
53 GetState() == CustomButton::STATE_PRESSED
) ?
54 kThumbHoverColor
: kThumbDefaultColor
);
55 canvas
->DrawRect(local_bounds
, kBorderColor
);
60 KennedyScrollBar::KennedyScrollBar(bool horizontal
)
61 : BaseScrollBar(horizontal
, new KennedyScrollBarThumb(this)) {
62 set_notify_enter_exit_on_child(true);
65 KennedyScrollBar::~KennedyScrollBar() {
68 gfx::Rect
KennedyScrollBar::GetTrackBounds() const {
69 gfx::Rect
local_bounds(GetLocalBounds());
70 gfx::Size track_size
= local_bounds
.size();
71 track_size
.SetToMax(GetThumb()->size());
72 local_bounds
.set_size(track_size
);
76 int KennedyScrollBar::GetLayoutSize() const {
77 return kScrollbarWidth
;
80 gfx::Size
KennedyScrollBar::GetPreferredSize() const {
81 return GetTrackBounds().size();
84 void KennedyScrollBar::Layout() {
85 gfx::Rect thumb_bounds
= GetTrackBounds();
86 BaseScrollBarThumb
* thumb
= GetThumb();
88 thumb_bounds
.set_x(thumb
->x());
89 thumb_bounds
.set_width(thumb
->width());
91 thumb_bounds
.set_y(thumb
->y());
92 thumb_bounds
.set_height(thumb
->height());
94 thumb
->SetBoundsRect(thumb_bounds
);
97 void KennedyScrollBar::OnPaint(gfx::Canvas
* canvas
) {
98 CustomButton::ButtonState state
= GetThumbTrackState();
99 if ((state
== CustomButton::STATE_HOVERED
) ||
100 (state
== CustomButton::STATE_PRESSED
)) {
101 gfx::Rect
local_bounds(GetLocalBounds());
102 canvas
->FillRect(local_bounds
, kTrackHoverColor
);
103 canvas
->DrawRect(local_bounds
, kBorderColor
);