Home | History | Annotate | Download | only in scrollbar
      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.
      4 
      5 #include "ui/views/controls/scrollbar/kennedy_scroll_bar.h"
      6 
      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"
     13 
     14 namespace views {
     15 namespace {
     16 
     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);
     23 
     24 class KennedyScrollBarThumb : public BaseScrollBarThumb {
     25  public:
     26   explicit KennedyScrollBarThumb(BaseScrollBar* scroll_bar);
     27   virtual ~KennedyScrollBarThumb();
     28 
     29  protected:
     30   // View overrides:
     31   virtual gfx::Size GetPreferredSize() OVERRIDE;
     32   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
     33 
     34  private:
     35   DISALLOW_COPY_AND_ASSIGN(KennedyScrollBarThumb);
     36 };
     37 
     38 KennedyScrollBarThumb::KennedyScrollBarThumb(BaseScrollBar* scroll_bar)
     39     : BaseScrollBarThumb(scroll_bar) {
     40 }
     41 
     42 KennedyScrollBarThumb::~KennedyScrollBarThumb() {
     43 }
     44 
     45 gfx::Size KennedyScrollBarThumb::GetPreferredSize() {
     46   return gfx::Size(kThumbMinimumSize, kThumbMinimumSize);
     47 }
     48 
     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);
     56 }
     57 
     58 }  // namespace
     59 
     60 KennedyScrollBar::KennedyScrollBar(bool horizontal)
     61     : BaseScrollBar(horizontal, new KennedyScrollBarThumb(this)) {
     62   set_notify_enter_exit_on_child(true);
     63 }
     64 
     65 KennedyScrollBar::~KennedyScrollBar() {
     66 }
     67 
     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);
     73   return local_bounds;
     74 }
     75 
     76 int KennedyScrollBar::GetLayoutSize() const {
     77   return kScrollbarWidth;
     78 }
     79 
     80 gfx::Size KennedyScrollBar::GetPreferredSize() {
     81   return GetTrackBounds().size();
     82 }
     83 
     84 void KennedyScrollBar::Layout() {
     85   gfx::Rect thumb_bounds = GetTrackBounds();
     86   BaseScrollBarThumb* thumb = GetThumb();
     87   if (IsHorizontal()) {
     88     thumb_bounds.set_x(thumb->x());
     89     thumb_bounds.set_width(thumb->width());
     90   } else {
     91     thumb_bounds.set_y(thumb->y());
     92     thumb_bounds.set_height(thumb->height());
     93   }
     94   thumb->SetBoundsRect(thumb_bounds);
     95 }
     96 
     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);
    104   }
    105 }
    106 
    107 }  // namespace views
    108