Chromium Code Reviews| 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 SetPaintToLayer(true); | 31 SetPaintToLayer(true); |
| 32 // Animate all changes to the layer except the first one. | 32 // Animate all changes to the layer except the first one. |
| 33 OnStateChanged(); | 33 OnStateChanged(); |
| 34 layer()->SetAnimator(ui::LayerAnimator::CreateImplicitAnimator()); | 34 layer()->SetAnimator(ui::LayerAnimator::CreateImplicitAnimator()); |
| 35 } | 35 } |
| 36 | 36 |
| 37 OverlayScrollBar::Thumb::~Thumb() {} | 37 OverlayScrollBar::Thumb::~Thumb() {} |
| 38 | 38 |
| 39 gfx::Size OverlayScrollBar::Thumb::GetPreferredSize() const { | 39 gfx::Size OverlayScrollBar::Thumb::GetPreferredSize() const { |
| 40 return gfx::Size(kThumbThickness, kThumbThickness); | 40 // The visual size of the thumb is kThumbThickness, but it slides back and |
| 41 // forth by kThumbHoverOffset. To make event targetting work well, expand the | |
| 42 // width of the thumb such that it's always taking up the full width of the | |
| 43 // track regardless of the offset. | |
| 44 return gfx::Size(kThumbThickness + kThumbHoverOffset, | |
| 45 kThumbThickness + kThumbHoverOffset); | |
|
sadrul
2016/11/19 01:43:04
This would affect layout (and thus painting) too,
| |
| 41 } | 46 } |
| 42 | 47 |
| 43 void OverlayScrollBar::Thumb::OnPaint(gfx::Canvas* canvas) { | 48 void OverlayScrollBar::Thumb::OnPaint(gfx::Canvas* canvas) { |
| 44 SkPaint fill_paint; | 49 SkPaint fill_paint; |
| 45 fill_paint.setStyle(SkPaint::kFill_Style); | 50 fill_paint.setStyle(SkPaint::kFill_Style); |
| 46 fill_paint.setColor(SK_ColorBLACK); | 51 fill_paint.setColor(SK_ColorBLACK); |
| 47 gfx::RectF fill_bounds(GetLocalBounds()); | 52 gfx::RectF fill_bounds(GetLocalBounds()); |
| 53 fill_bounds.Inset(gfx::InsetsF(IsHorizontal() ? kThumbHoverOffset : 0, | |
| 54 IsHorizontal() ? 0 : kThumbHoverOffset, 0, 0)); | |
| 48 fill_bounds.Inset(gfx::InsetsF(kThumbStroke, kThumbStroke, | 55 fill_bounds.Inset(gfx::InsetsF(kThumbStroke, kThumbStroke, |
| 49 IsHorizontal() ? 0 : kThumbStroke, | 56 IsHorizontal() ? 0 : kThumbStroke, |
| 50 IsHorizontal() ? kThumbStroke : 0)); | 57 IsHorizontal() ? kThumbStroke : 0)); |
| 51 canvas->DrawRect(fill_bounds, fill_paint); | 58 canvas->DrawRect(fill_bounds, fill_paint); |
| 52 | 59 |
| 53 SkPaint stroke_paint; | 60 SkPaint stroke_paint; |
| 54 stroke_paint.setStyle(SkPaint::kStroke_Style); | 61 stroke_paint.setStyle(SkPaint::kStroke_Style); |
| 55 stroke_paint.setColor(SK_ColorWHITE); | 62 stroke_paint.setColor(SK_ColorWHITE); |
| 56 stroke_paint.setStrokeWidth(kThumbStroke); | 63 stroke_paint.setStrokeWidth(kThumbStroke); |
| 57 gfx::RectF stroke_bounds(fill_bounds); | 64 gfx::RectF stroke_bounds(fill_bounds); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 75 scroll_bar_->Show(); | 82 scroll_bar_->Show(); |
| 76 // Don't start the hide countdown if the thumb is still hovered or pressed. | 83 // Don't start the hide countdown if the thumb is still hovered or pressed. |
| 77 if (GetState() == CustomButton::STATE_NORMAL) | 84 if (GetState() == CustomButton::STATE_NORMAL) |
| 78 scroll_bar_->StartHideCountdown(); | 85 scroll_bar_->StartHideCountdown(); |
| 79 } | 86 } |
| 80 | 87 |
| 81 void OverlayScrollBar::Thumb::OnStateChanged() { | 88 void OverlayScrollBar::Thumb::OnStateChanged() { |
| 82 if (GetState() == CustomButton::STATE_NORMAL) { | 89 if (GetState() == CustomButton::STATE_NORMAL) { |
| 83 gfx::Transform translation; | 90 gfx::Transform translation; |
| 84 translation.Translate( | 91 translation.Translate( |
| 85 gfx::Vector2d(IsHorizontal() ? 0 : kThumbUnhoveredDifference, | 92 gfx::Vector2d(IsHorizontal() ? 0 : kThumbHoverOffset, |
| 86 IsHorizontal() ? kThumbUnhoveredDifference : 0)); | 93 IsHorizontal() ? kThumbHoverOffset: 0)); |
| 87 layer()->SetTransform(translation); | 94 layer()->SetTransform(translation); |
| 88 layer()->SetOpacity(kThumbDefaultAlpha); | 95 layer()->SetOpacity(kThumbDefaultAlpha); |
| 89 | 96 |
| 90 if (GetWidget()) | 97 if (GetWidget()) |
| 91 scroll_bar_->StartHideCountdown(); | 98 scroll_bar_->StartHideCountdown(); |
| 92 } else { | 99 } else { |
| 93 layer()->SetTransform(gfx::Transform()); | 100 layer()->SetTransform(gfx::Transform()); |
| 94 layer()->SetOpacity(kThumbHoverAlpha); | 101 layer()->SetOpacity(kThumbHoverAlpha); |
| 95 } | 102 } |
| 96 } | 103 } |
| 97 | 104 |
| 98 OverlayScrollBar::OverlayScrollBar(bool horizontal) | 105 OverlayScrollBar::OverlayScrollBar(bool horizontal) |
| 99 : BaseScrollBar(horizontal, new Thumb(this)), hide_timer_(false, false) { | 106 : BaseScrollBar(horizontal, new Thumb(this)), hide_timer_(false, false) { |
| 100 set_notify_enter_exit_on_child(true); | 107 set_notify_enter_exit_on_child(true); |
| 101 SetPaintToLayer(true); | 108 SetPaintToLayer(true); |
| 102 layer()->SetMasksToBounds(true); | 109 layer()->SetMasksToBounds(true); |
| 103 layer()->SetFillsBoundsOpaquely(false); | 110 layer()->SetFillsBoundsOpaquely(false); |
| 104 } | 111 } |
| 105 | 112 |
| 106 OverlayScrollBar::~OverlayScrollBar() { | 113 OverlayScrollBar::~OverlayScrollBar() { |
| 107 } | 114 } |
| 108 | 115 |
| 109 gfx::Rect OverlayScrollBar::GetTrackBounds() const { | 116 gfx::Rect OverlayScrollBar::GetTrackBounds() const { |
| 110 return GetLocalBounds(); | 117 gfx::Rect local = GetLocalBounds(); |
| 118 // The track has to be wide enough for the thumb. | |
| 119 local.Inset(gfx::Insets(IsHorizontal() ? -kThumbHoverOffset : 0, | |
| 120 IsHorizontal() ? 0 : -kThumbHoverOffset, 0, 0)); | |
| 121 return local; | |
| 111 } | 122 } |
| 112 | 123 |
| 113 int OverlayScrollBar::GetLayoutSize() const { | 124 int OverlayScrollBar::GetLayoutSize() const { |
| 114 return 0; | 125 return 0; |
| 115 } | 126 } |
| 116 | 127 |
| 117 int OverlayScrollBar::GetContentOverlapSize() const { | 128 int OverlayScrollBar::GetContentOverlapSize() const { |
| 118 return kThumbThickness; | 129 return kThumbThickness; |
| 119 } | 130 } |
| 120 | 131 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 thumb_bounds.set_x(thumb->x()); | 163 thumb_bounds.set_x(thumb->x()); |
| 153 thumb_bounds.set_width(thumb->width()); | 164 thumb_bounds.set_width(thumb->width()); |
| 154 } else { | 165 } else { |
| 155 thumb_bounds.set_y(thumb->y()); | 166 thumb_bounds.set_y(thumb->y()); |
| 156 thumb_bounds.set_height(thumb->height()); | 167 thumb_bounds.set_height(thumb->height()); |
| 157 } | 168 } |
| 158 thumb->SetBoundsRect(thumb_bounds); | 169 thumb->SetBoundsRect(thumb_bounds); |
| 159 } | 170 } |
| 160 | 171 |
| 161 } // namespace views | 172 } // namespace views |
| OLD | NEW |