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

Side by Side Diff: ui/views/controls/slider.cc

Issue 2335513002: Adding ripple effect for clicks on MD slider (Closed)
Patch Set: nits Created 4 years, 2 months 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
« ui/views/controls/slider.h ('K') | « ui/views/controls/slider.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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/slider.h" 5 #include "ui/views/controls/slider.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "third_party/skia/include/core/SkCanvas.h" 13 #include "third_party/skia/include/core/SkCanvas.h"
14 #include "third_party/skia/include/core/SkColor.h" 14 #include "third_party/skia/include/core/SkColor.h"
15 #include "third_party/skia/include/core/SkPaint.h" 15 #include "third_party/skia/include/core/SkPaint.h"
16 #include "ui/accessibility/ax_view_state.h" 16 #include "ui/accessibility/ax_view_state.h"
17 #include "ui/base/resource/resource_bundle.h" 17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/events/event.h" 18 #include "ui/events/event.h"
19 #include "ui/gfx/animation/slide_animation.h" 19 #include "ui/gfx/animation/slide_animation.h"
20 #include "ui/gfx/canvas.h" 20 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/geometry/point.h" 21 #include "ui/gfx/geometry/point.h"
22 #include "ui/gfx/geometry/rect.h" 22 #include "ui/gfx/geometry/rect.h"
23 #include "ui/resources/grit/ui_resources.h" 23 #include "ui/resources/grit/ui_resources.h"
24 #include "ui/views/controls/md_slider.h" 24 #include "ui/views/controls/md_slider.h"
25 #include "ui/views/controls/non_md_slider.h" 25 #include "ui/views/controls/non_md_slider.h"
26 #include "ui/views/resources/grit/views_resources.h" 26 #include "ui/views/resources/grit/views_resources.h"
27 #include "ui/views/widget/widget.h" 27 #include "ui/views/widget/widget.h"
28 28
29 namespace { 29 namespace {
30 const int kSlideValueChangeDurationMS = 150; 30 const int kSlideValueChangeDurationMs = 150;
31 31
32 // The image chunks. 32 // The image chunks.
33 enum BorderElements { 33 enum BorderElements {
34 LEFT, 34 LEFT,
35 CENTER_LEFT, 35 CENTER_LEFT,
36 CENTER_RIGHT, 36 CENTER_RIGHT,
37 RIGHT, 37 RIGHT,
38 }; 38 };
39 } // namespace 39 } // namespace
40 40
41 namespace views { 41 namespace views {
42 42
43 // static 43 // static
44 const char Slider::kViewClassName[] = "Slider"; 44 const char Slider::kViewClassName[] = "Slider";
45 45
46 // static 46 // static
47 Slider* Slider::CreateSlider(bool is_material_design, 47 Slider* Slider::CreateSlider(bool is_material_design,
48 SliderListener* listener) { 48 SliderListener* listener) {
49 if (is_material_design) 49 if (is_material_design)
50 return new MdSlider(listener); 50 return new MdSlider(listener);
51 return new NonMdSlider(listener); 51 return new NonMdSlider(listener);
52 } 52 }
53 53
54 Slider::Slider(SliderListener* listener) 54 Slider::Slider(SliderListener* listener)
55 : listener_(listener), 55 : listener_(listener),
56 value_(0.f), 56 value_(0.f),
57 keyboard_increment_(0.1f), 57 keyboard_increment_(0.1f),
58 animating_value_(0.f), 58 initial_animating_value_(0.f),
59 value_is_valid_(false), 59 value_is_valid_(false),
60 accessibility_events_enabled_(true), 60 accessibility_events_enabled_(true),
61 focus_border_color_(0), 61 focus_border_color_(0),
62 initial_button_offset_(0) { 62 initial_button_offset_(0) {
63 EnableCanvasFlippingForRTLUI(true); 63 EnableCanvasFlippingForRTLUI(true);
64 #if defined(OS_MACOSX) 64 #if defined(OS_MACOSX)
65 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); 65 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
66 #else 66 #else
67 SetFocusBehavior(FocusBehavior::ALWAYS); 67 SetFocusBehavior(FocusBehavior::ALWAYS);
68 #endif 68 #endif
69 } 69 }
70 70
71 Slider::~Slider() { 71 Slider::~Slider() {
72 } 72 }
73 73
74 void Slider::SetHighlighted(bool is_highlighted) {
75
bruthig 2016/09/23 22:24:39 nit: no newline required. the braces can be on the
yiyix 2016/09/24 06:24:36 Done. format did not fix this.... unfortunately.
76 }
77
78
74 void Slider::SetValue(float value) { 79 void Slider::SetValue(float value) {
75 SetValueInternal(value, VALUE_CHANGED_BY_API); 80 SetValueInternal(value, VALUE_CHANGED_BY_API);
76 } 81 }
77 82
78 void Slider::SetValueInternal(float value, SliderChangeReason reason) { 83 void Slider::SetValueInternal(float value, SliderChangeReason reason) {
79 bool old_value_valid = value_is_valid_; 84 bool old_value_valid = value_is_valid_;
80 85
81 value_is_valid_ = true; 86 value_is_valid_ = true;
82 if (value < 0.0) 87 if (value < 0.0)
83 value = 0.0; 88 value = 0.0;
84 else if (value > 1.0) 89 else if (value > 1.0)
85 value = 1.0; 90 value = 1.0;
86 if (value_ == value) 91 if (value_ == value)
87 return; 92 return;
88 float old_value = value_; 93 float old_value = value_;
89 value_ = value; 94 value_ = value;
90 if (listener_) 95 if (listener_)
91 listener_->SliderValueChanged(this, value_, old_value, reason); 96 listener_->SliderValueChanged(this, value_, old_value, reason);
92 97
93 if (old_value_valid && base::MessageLoop::current()) { 98 if (old_value_valid && base::MessageLoop::current()) {
94 // Do not animate when setting the value of the slider for the first time. 99 // Do not animate when setting the value of the slider for the first time.
95 // There is no message-loop when running tests. So we cannot animate then. 100 // There is no message-loop when running tests. So we cannot animate then.
96 animating_value_ = old_value; 101 if (!move_animation_.get() || !move_animation_->is_animating()) {
bruthig 2016/09/23 22:24:39 This new condition confuses me a little, what is i
yiyix 2016/09/24 06:24:36 This is because I had !move_animation_->is_animati
97 move_animation_.reset(new gfx::SlideAnimation(this)); 102 initial_animating_value_ = old_value;
98 move_animation_->SetSlideDuration(kSlideValueChangeDurationMS); 103 move_animation_.reset(new gfx::SlideAnimation(this));
99 move_animation_->Show(); 104 move_animation_->SetSlideDuration(kSlideValueChangeDurationMs);
100 AnimationProgressed(move_animation_.get()); 105 move_animation_->Show();
106 }
101 } else { 107 } else {
102 SchedulePaint(); 108 SchedulePaint();
103 } 109 }
104 if (accessibility_events_enabled_ && GetWidget()) { 110 if (accessibility_events_enabled_ && GetWidget())
105 NotifyAccessibilityEvent( 111 NotifyAccessibilityEvent(ui::AX_EVENT_VALUE_CHANGED, true);
106 ui::AX_EVENT_VALUE_CHANGED, true);
107 }
108 } 112 }
109 113
110 void Slider::PrepareForMove(const int new_x) { 114 void Slider::PrepareForMove(const int new_x) {
111 // Try to remember the position of the mouse cursor on the button. 115 // Try to remember the position of the mouse cursor on the button.
112 gfx::Insets inset = GetInsets(); 116 gfx::Insets inset = GetInsets();
113 gfx::Rect content = GetContentsBounds(); 117 gfx::Rect content = GetContentsBounds();
114 float value = move_animation_.get() && move_animation_->is_animating() ? 118 float value = GetAnimatingValue();
115 animating_value_ : value_;
116 119
117 const int thumb_width = GetThumbWidth(); 120 const int thumb_width = GetThumbWidth();
118 const int thumb_x = value * (content.width() - thumb_width); 121 const int thumb_x = value * (content.width() - thumb_width);
119 const int candidate_x = (base::i18n::IsRTL() ? 122 const int candidate_x = (base::i18n::IsRTL() ?
120 width() - (new_x - inset.left()) : 123 width() - (new_x - inset.left()) :
121 new_x - inset.left()) - thumb_x; 124 new_x - inset.left()) - thumb_x;
122 if (candidate_x >= 0 && candidate_x < thumb_width) 125 if (candidate_x >= 0 && candidate_x < thumb_width)
123 initial_button_offset_ = candidate_x; 126 initial_button_offset_ = candidate_x;
124 else 127 else
125 initial_button_offset_ = thumb_width / 2; 128 initial_button_offset_ = thumb_width / 2;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 return gfx::Size(std::max(width(), kSizeMajor), kSizeMinor); 168 return gfx::Size(std::max(width(), kSizeMajor), kSizeMinor);
166 } 169 }
167 170
168 void Slider::OnPaint(gfx::Canvas* canvas) { 171 void Slider::OnPaint(gfx::Canvas* canvas) {
169 View::OnPaint(canvas); 172 View::OnPaint(canvas);
170 OnPaintFocus(canvas); 173 OnPaintFocus(canvas);
171 } 174 }
172 175
173 float Slider::GetAnimatingValue() const{ 176 float Slider::GetAnimatingValue() const{
174 return move_animation_.get() && move_animation_->is_animating() 177 return move_animation_.get() && move_animation_->is_animating()
175 ? animating_value_ 178 ? move_animation_->CurrentValueBetween(initial_animating_value_,
179 value_)
176 : value_; 180 : value_;
177 } 181 }
178 182
179 bool Slider::OnMousePressed(const ui::MouseEvent& event) { 183 bool Slider::OnMousePressed(const ui::MouseEvent& event) {
180 if (!event.IsOnlyLeftMouseButton()) 184 if (!event.IsOnlyLeftMouseButton())
181 return false; 185 return false;
182 OnSliderDragStarted(); 186 OnSliderDragStarted();
183 PrepareForMove(event.location().x()); 187 PrepareForMove(event.location().x());
184 MoveButtonTo(event.location()); 188 MoveButtonTo(event.location());
185 return true; 189 return true;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 event->SetHandled(); 238 event->SetHandled();
235 if (event->details().touch_points() <= 1) 239 if (event->details().touch_points() <= 1)
236 OnSliderDragEnded(); 240 OnSliderDragEnded();
237 break; 241 break;
238 default: 242 default:
239 break; 243 break;
240 } 244 }
241 } 245 }
242 246
243 void Slider::AnimationProgressed(const gfx::Animation* animation) { 247 void Slider::AnimationProgressed(const gfx::Animation* animation) {
244 animating_value_ = animation->CurrentValueBetween(animating_value_, value_); 248 if (animation && animation != move_animation_.get())
bruthig 2016/09/23 22:24:39 nit: I think it's safe to assume |animation| is no
yiyix 2016/09/24 06:24:36 I think you mean "if it were possible for |animati
249 return;
245 SchedulePaint(); 250 SchedulePaint();
246 } 251 }
247 252
253 void Slider::AnimationEnded(const gfx::Animation* animation) {
254 if (animation == move_animation_.get() && !move_animation_->IsShowing())
bruthig 2016/09/23 22:24:39 From what I can see Hide() will never be used on t
yiyix 2016/09/24 06:24:36 You right, I never hide anything in this class
255 move_animation_.reset();
256 }
257
248 void Slider::GetAccessibleState(ui::AXViewState* state) { 258 void Slider::GetAccessibleState(ui::AXViewState* state) {
249 state->role = ui::AX_ROLE_SLIDER; 259 state->role = ui::AX_ROLE_SLIDER;
250 state->name = accessible_name_; 260 state->name = accessible_name_;
251 state->value = base::UTF8ToUTF16( 261 state->value = base::UTF8ToUTF16(
252 base::StringPrintf("%d%%", static_cast<int>(value_ * 100 + 0.5))); 262 base::StringPrintf("%d%%", static_cast<int>(value_ * 100 + 0.5)));
253 } 263 }
254 264
255 void Slider::OnSliderDragStarted() { 265 void Slider::OnSliderDragStarted() {
266 SetHighlighted(true);
256 if (listener_) 267 if (listener_)
257 listener_->SliderDragStarted(this); 268 listener_->SliderDragStarted(this);
258 } 269 }
259 270
260 void Slider::OnSliderDragEnded() { 271 void Slider::OnSliderDragEnded() {
272 SetHighlighted(false);
261 if (listener_) 273 if (listener_)
262 listener_->SliderDragEnded(this); 274 listener_->SliderDragEnded(this);
263 } 275 }
264 276
265 } // namespace views 277 } // namespace views
OLDNEW
« ui/views/controls/slider.h ('K') | « ui/views/controls/slider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698