Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: ui/views/controls/scrollbar/overlay_scroll_bar.cc

Issue 2520433003: Fix event targeting for overlay scrollbar thumbs (in native UI). (Closed)
Patch Set: fix 2 bugs Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/controls/scrollbar/overlay_scroll_bar.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
Evan Stade 2016/11/19 02:23:34 different bug here --- don't do work in constructo
varkha 2016/11/19 02:58:00 Is this responsible for the first Thumb's paint be
Evan Stade 2016/11/21 15:26:20 yes
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);
Evan Stade 2016/11/19 02:23:34 this is the fix for the graphical glitch Valery po
varkha 2016/11/19 02:58:00 Yay! Glad you caught it.
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);
(...skipping 17 matching lines...) Expand all
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, new Thumb(this)), hide_timer_(false, false) {
113 static_cast<Thumb*>(GetThumb())->Init();
varkha 2016/11/19 02:58:00 Is it possible to make a virtual BaseScrollBarThum
Evan Stade 2016/11/21 15:26:20 "Avoid virtual method calls in constructors" http
varkha 2016/11/21 17:16:46 Fair enough, although I thought this was about cal
100 set_notify_enter_exit_on_child(true); 114 set_notify_enter_exit_on_child(true);
101 SetPaintToLayer(true); 115 SetPaintToLayer(true);
102 layer()->SetMasksToBounds(true); 116 layer()->SetMasksToBounds(true);
103 layer()->SetFillsBoundsOpaquely(false); 117 layer()->SetFillsBoundsOpaquely(false);
104 } 118 }
105 119
106 OverlayScrollBar::~OverlayScrollBar() { 120 OverlayScrollBar::~OverlayScrollBar() {}
107 }
108 121
109 gfx::Rect OverlayScrollBar::GetTrackBounds() const { 122 gfx::Rect OverlayScrollBar::GetTrackBounds() const {
110 return GetLocalBounds(); 123 gfx::Rect local = GetLocalBounds();
124 // The track has to be wide enough for the thumb.
125 local.Inset(gfx::Insets(IsHorizontal() ? -kThumbHoverOffset : 0,
126 IsHorizontal() ? 0 : -kThumbHoverOffset, 0, 0));
127 return local;
111 } 128 }
112 129
113 int OverlayScrollBar::GetLayoutSize() const { 130 int OverlayScrollBar::GetLayoutSize() const {
114 return 0; 131 return 0;
115 } 132 }
116 133
117 int OverlayScrollBar::GetContentOverlapSize() const { 134 int OverlayScrollBar::GetContentOverlapSize() const {
118 return kThumbThickness; 135 return kThumbThickness;
119 } 136 }
120 137
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 thumb_bounds.set_x(thumb->x()); 169 thumb_bounds.set_x(thumb->x());
153 thumb_bounds.set_width(thumb->width()); 170 thumb_bounds.set_width(thumb->width());
154 } else { 171 } else {
155 thumb_bounds.set_y(thumb->y()); 172 thumb_bounds.set_y(thumb->y());
156 thumb_bounds.set_height(thumb->height()); 173 thumb_bounds.set_height(thumb->height());
157 } 174 }
158 thumb->SetBoundsRect(thumb_bounds); 175 thumb->SetBoundsRect(thumb_bounds);
159 } 176 }
160 177
161 } // namespace views 178 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/scrollbar/overlay_scroll_bar.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698