| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h" | 5 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "third_party/skia/include/core/SkColor.h" | 8 #include "third_party/skia/include/core/SkColor.h" |
| 9 #include "third_party/skia/include/core/SkXfermode.h" | 9 #include "third_party/skia/include/core/SkXfermode.h" |
| 10 #include "ui/compositor/scoped_layer_animation_settings.h" | 10 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 11 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
| 12 #include "ui/views/background.h" | 12 #include "ui/views/background.h" |
| 13 #include "ui/views/border.h" | 13 #include "ui/views/border.h" |
| 14 | 14 |
| 15 namespace views { | 15 namespace views { |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // Total thickness of the thumb (matches visuals when hovered). | 18 // Total thickness of the thumb (matches visuals when hovered). |
| 19 const int kThumbThickness = 11; | 19 const int kThumbThickness = 11; |
| 20 // When hovered, the thumb takes up the full width. Otherwise, it's a bit | 20 // When hovered, the thumb takes up the full width. Otherwise, it's a bit |
| 21 // slimmer. | 21 // slimmer. |
| 22 const int kThumbUnhoveredDifference = 4; | 22 const int kThumbHoverOffset = 4; |
| 23 const int kThumbStroke = 1; | 23 const int kThumbStroke = 1; |
| 24 const float kThumbHoverAlpha = 0.5f; | 24 const float kThumbHoverAlpha = 0.5f; |
| 25 const float kThumbDefaultAlpha = 0.3f; | 25 const float kThumbDefaultAlpha = 0.3f; |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 OverlayScrollBar::Thumb::Thumb(OverlayScrollBar* scroll_bar) | 29 OverlayScrollBar::Thumb::Thumb(OverlayScrollBar* scroll_bar) |
| 30 : BaseScrollBarThumb(scroll_bar), scroll_bar_(scroll_bar) { | 30 : BaseScrollBarThumb(scroll_bar), scroll_bar_(scroll_bar) { |
| 31 // |scroll_bar| isn't done being constructed; it's not safe to do anything |
| 32 // that might reference it yet. |
| 33 } |
| 34 |
| 35 OverlayScrollBar::Thumb::~Thumb() {} |
| 36 |
| 37 void OverlayScrollBar::Thumb::Init() { |
| 31 SetPaintToLayer(true); | 38 SetPaintToLayer(true); |
| 39 layer()->SetFillsBoundsOpaquely(false); |
| 32 // Animate all changes to the layer except the first one. | 40 // Animate all changes to the layer except the first one. |
| 33 OnStateChanged(); | 41 OnStateChanged(); |
| 34 layer()->SetAnimator(ui::LayerAnimator::CreateImplicitAnimator()); | 42 layer()->SetAnimator(ui::LayerAnimator::CreateImplicitAnimator()); |
| 35 } | 43 } |
| 36 | 44 |
| 37 OverlayScrollBar::Thumb::~Thumb() {} | |
| 38 | |
| 39 gfx::Size OverlayScrollBar::Thumb::GetPreferredSize() const { | 45 gfx::Size OverlayScrollBar::Thumb::GetPreferredSize() const { |
| 40 return gfx::Size(kThumbThickness, kThumbThickness); | 46 // The visual size of the thumb is kThumbThickness, but it slides back and |
| 47 // forth by kThumbHoverOffset. To make event targetting work well, expand the |
| 48 // width of the thumb such that it's always taking up the full width of the |
| 49 // track regardless of the offset. |
| 50 return gfx::Size(kThumbThickness + kThumbHoverOffset, |
| 51 kThumbThickness + kThumbHoverOffset); |
| 41 } | 52 } |
| 42 | 53 |
| 43 void OverlayScrollBar::Thumb::OnPaint(gfx::Canvas* canvas) { | 54 void OverlayScrollBar::Thumb::OnPaint(gfx::Canvas* canvas) { |
| 44 SkPaint fill_paint; | 55 SkPaint fill_paint; |
| 45 fill_paint.setStyle(SkPaint::kFill_Style); | 56 fill_paint.setStyle(SkPaint::kFill_Style); |
| 46 fill_paint.setColor(SK_ColorBLACK); | 57 fill_paint.setColor(SK_ColorBLACK); |
| 47 gfx::RectF fill_bounds(GetLocalBounds()); | 58 gfx::RectF fill_bounds(GetLocalBounds()); |
| 59 fill_bounds.Inset(gfx::InsetsF(IsHorizontal() ? kThumbHoverOffset : 0, |
| 60 IsHorizontal() ? 0 : kThumbHoverOffset, 0, 0)); |
| 48 fill_bounds.Inset(gfx::InsetsF(kThumbStroke, kThumbStroke, | 61 fill_bounds.Inset(gfx::InsetsF(kThumbStroke, kThumbStroke, |
| 49 IsHorizontal() ? 0 : kThumbStroke, | 62 IsHorizontal() ? 0 : kThumbStroke, |
| 50 IsHorizontal() ? kThumbStroke : 0)); | 63 IsHorizontal() ? kThumbStroke : 0)); |
| 51 canvas->DrawRect(fill_bounds, fill_paint); | 64 canvas->DrawRect(fill_bounds, fill_paint); |
| 52 | 65 |
| 53 SkPaint stroke_paint; | 66 SkPaint stroke_paint; |
| 54 stroke_paint.setStyle(SkPaint::kStroke_Style); | 67 stroke_paint.setStyle(SkPaint::kStroke_Style); |
| 55 stroke_paint.setColor(SK_ColorWHITE); | 68 stroke_paint.setColor(SK_ColorWHITE); |
| 56 stroke_paint.setStrokeWidth(kThumbStroke); | 69 stroke_paint.setStrokeWidth(kThumbStroke); |
| 57 gfx::RectF stroke_bounds(fill_bounds); | 70 gfx::RectF stroke_bounds(fill_bounds); |
| 58 stroke_bounds.Inset(gfx::InsetsF(-0.5f)); | 71 stroke_bounds.Inset(gfx::InsetsF(kThumbStroke / 2.f)); |
| 59 // The stroke doesn't apply to the far edge of the thumb. | 72 // The stroke doesn't apply to the far edge of the thumb. |
| 60 SkPath path; | 73 SkPath path; |
| 61 path.moveTo(gfx::PointFToSkPoint(stroke_bounds.top_right())); | 74 path.moveTo(gfx::PointFToSkPoint(stroke_bounds.top_right())); |
| 62 path.lineTo(gfx::PointFToSkPoint(stroke_bounds.origin())); | 75 path.lineTo(gfx::PointFToSkPoint(stroke_bounds.origin())); |
| 63 path.lineTo(gfx::PointFToSkPoint(stroke_bounds.bottom_left())); | 76 path.lineTo(gfx::PointFToSkPoint(stroke_bounds.bottom_left())); |
| 64 if (IsHorizontal()) { | 77 if (IsHorizontal()) { |
| 65 path.moveTo(gfx::PointFToSkPoint(stroke_bounds.bottom_right())); | 78 path.moveTo(gfx::PointFToSkPoint(stroke_bounds.bottom_right())); |
| 66 path.close(); | 79 path.close(); |
| 67 } else { | 80 } else { |
| 68 path.lineTo(gfx::PointFToSkPoint(stroke_bounds.bottom_right())); | 81 path.lineTo(gfx::PointFToSkPoint(stroke_bounds.bottom_right())); |
| 69 } | 82 } |
| 70 canvas->DrawPath(path, stroke_paint); | 83 canvas->DrawPath(path, stroke_paint); |
| 71 } | 84 } |
| 72 | 85 |
| 73 void OverlayScrollBar::Thumb::OnBoundsChanged( | 86 void OverlayScrollBar::Thumb::OnBoundsChanged( |
| 74 const gfx::Rect& previous_bounds) { | 87 const gfx::Rect& previous_bounds) { |
| 75 scroll_bar_->Show(); | 88 scroll_bar_->Show(); |
| 76 // Don't start the hide countdown if the thumb is still hovered or pressed. | 89 // Don't start the hide countdown if the thumb is still hovered or pressed. |
| 77 if (GetState() == CustomButton::STATE_NORMAL) | 90 if (GetState() == CustomButton::STATE_NORMAL) |
| 78 scroll_bar_->StartHideCountdown(); | 91 scroll_bar_->StartHideCountdown(); |
| 79 } | 92 } |
| 80 | 93 |
| 81 void OverlayScrollBar::Thumb::OnStateChanged() { | 94 void OverlayScrollBar::Thumb::OnStateChanged() { |
| 82 if (GetState() == CustomButton::STATE_NORMAL) { | 95 if (GetState() == CustomButton::STATE_NORMAL) { |
| 83 gfx::Transform translation; | 96 gfx::Transform translation; |
| 84 translation.Translate( | 97 translation.Translate( |
| 85 gfx::Vector2d(IsHorizontal() ? 0 : kThumbUnhoveredDifference, | 98 gfx::Vector2d(IsHorizontal() ? 0 : kThumbHoverOffset, |
| 86 IsHorizontal() ? kThumbUnhoveredDifference : 0)); | 99 IsHorizontal() ? kThumbHoverOffset: 0)); |
| 87 layer()->SetTransform(translation); | 100 layer()->SetTransform(translation); |
| 88 layer()->SetOpacity(kThumbDefaultAlpha); | 101 layer()->SetOpacity(kThumbDefaultAlpha); |
| 89 | 102 |
| 90 if (GetWidget()) | 103 if (GetWidget()) |
| 91 scroll_bar_->StartHideCountdown(); | 104 scroll_bar_->StartHideCountdown(); |
| 92 } else { | 105 } else { |
| 93 layer()->SetTransform(gfx::Transform()); | 106 layer()->SetTransform(gfx::Transform()); |
| 94 layer()->SetOpacity(kThumbHoverAlpha); | 107 layer()->SetOpacity(kThumbHoverAlpha); |
| 95 } | 108 } |
| 96 } | 109 } |
| 97 | 110 |
| 98 OverlayScrollBar::OverlayScrollBar(bool horizontal) | 111 OverlayScrollBar::OverlayScrollBar(bool horizontal) |
| 99 : BaseScrollBar(horizontal, new Thumb(this)), hide_timer_(false, false) { | 112 : BaseScrollBar(horizontal), hide_timer_(false, false) { |
| 113 auto thumb = new Thumb(this); |
| 114 SetThumb(thumb); |
| 115 thumb->Init(); |
| 100 set_notify_enter_exit_on_child(true); | 116 set_notify_enter_exit_on_child(true); |
| 101 SetPaintToLayer(true); | 117 SetPaintToLayer(true); |
| 102 layer()->SetMasksToBounds(true); | 118 layer()->SetMasksToBounds(true); |
| 103 layer()->SetFillsBoundsOpaquely(false); | 119 layer()->SetFillsBoundsOpaquely(false); |
| 104 } | 120 } |
| 105 | 121 |
| 106 OverlayScrollBar::~OverlayScrollBar() { | 122 OverlayScrollBar::~OverlayScrollBar() {} |
| 107 } | |
| 108 | 123 |
| 109 gfx::Rect OverlayScrollBar::GetTrackBounds() const { | 124 gfx::Rect OverlayScrollBar::GetTrackBounds() const { |
| 110 return GetLocalBounds(); | 125 gfx::Rect local = GetLocalBounds(); |
| 126 // The track has to be wide enough for the thumb. |
| 127 local.Inset(gfx::Insets(IsHorizontal() ? -kThumbHoverOffset : 0, |
| 128 IsHorizontal() ? 0 : -kThumbHoverOffset, 0, 0)); |
| 129 return local; |
| 111 } | 130 } |
| 112 | 131 |
| 113 int OverlayScrollBar::GetLayoutSize() const { | 132 int OverlayScrollBar::GetLayoutSize() const { |
| 114 return 0; | 133 return 0; |
| 115 } | 134 } |
| 116 | 135 |
| 117 int OverlayScrollBar::GetContentOverlapSize() const { | 136 int OverlayScrollBar::GetContentOverlapSize() const { |
| 118 return kThumbThickness; | 137 return kThumbThickness; |
| 119 } | 138 } |
| 120 | 139 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 thumb_bounds.set_x(thumb->x()); | 171 thumb_bounds.set_x(thumb->x()); |
| 153 thumb_bounds.set_width(thumb->width()); | 172 thumb_bounds.set_width(thumb->width()); |
| 154 } else { | 173 } else { |
| 155 thumb_bounds.set_y(thumb->y()); | 174 thumb_bounds.set_y(thumb->y()); |
| 156 thumb_bounds.set_height(thumb->height()); | 175 thumb_bounds.set_height(thumb->height()); |
| 157 } | 176 } |
| 158 thumb->SetBoundsRect(thumb_bounds); | 177 thumb->SetBoundsRect(thumb_bounds); |
| 159 } | 178 } |
| 160 | 179 |
| 161 } // namespace views | 180 } // namespace views |
| OLD | NEW |