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

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

Issue 2692043009: Remove last MD reference from TrayPopupUtils. (Closed)
Patch Set: rebase Created 3 years, 10 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
« no previous file with comments | « ui/views/controls/slider.h ('k') | ui/views/controls/slider_unittest.cc » ('j') | 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 "cc/paint/paint_flags.h"
13 #include "third_party/skia/include/core/SkCanvas.h" 14 #include "third_party/skia/include/core/SkCanvas.h"
14 #include "third_party/skia/include/core/SkColor.h" 15 #include "third_party/skia/include/core/SkColor.h"
15 #include "third_party/skia/include/core/SkPaint.h" 16 #include "third_party/skia/include/core/SkPaint.h"
16 #include "ui/accessibility/ax_node_data.h" 17 #include "ui/accessibility/ax_node_data.h"
17 #include "ui/base/resource/resource_bundle.h" 18 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/events/event.h" 19 #include "ui/events/event.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/native_theme/native_theme.h" 23 #include "ui/native_theme/native_theme.h"
24 #include "ui/resources/grit/ui_resources.h" 24 #include "ui/resources/grit/ui_resources.h"
25 #include "ui/views/controls/md_slider.h"
26 #include "ui/views/controls/non_md_slider.h"
27 #include "ui/views/resources/grit/views_resources.h" 25 #include "ui/views/resources/grit/views_resources.h"
28 #include "ui/views/widget/widget.h" 26 #include "ui/views/widget/widget.h"
29 27
30 namespace { 28 namespace {
31 const int kSlideValueChangeDurationMs = 150; 29 const int kSlideValueChangeDurationMs = 150;
32 30
33 // The image chunks. 31 // The image chunks.
34 enum BorderElements { 32 enum BorderElements {
35 LEFT, 33 LEFT,
36 CENTER_LEFT, 34 CENTER_LEFT,
37 CENTER_RIGHT, 35 CENTER_RIGHT,
38 RIGHT, 36 RIGHT,
39 }; 37 };
40 } // namespace 38 } // namespace
41 39
42 namespace views { 40 namespace views {
43 41
42 namespace {
43
44 // Color of slider at the active and the disabled state, respectively.
45 const SkColor kActiveColor = SkColorSetARGB(0xFF, 0x42, 0x85, 0xF4);
46 const SkColor kDisabledColor = SkColorSetARGB(0xFF, 0xBD, 0xBD, 0xBD);
47 constexpr uint8_t kHighlightColorAlpha = 0x4D;
48
49 // The thickness of the slider.
50 constexpr int kLineThickness = 2;
51
52 // The radius used to draw rounded slider ends.
53 constexpr float kSliderRoundedRadius = 2.f;
54
55 // The radius of the thumb and the highlighted thumb of the slider,
56 // respectively.
57 constexpr float kThumbRadius = 6.f;
58 constexpr float kThumbWidth = 2 * kThumbRadius;
59 constexpr float kThumbHighlightRadius = 10.f;
60
61 // The stroke of the thumb when the slider is disabled.
62 constexpr int kThumbStroke = 2;
63
64 // Duration of the thumb highlight growing effect animation.
65 constexpr int kSlideHighlightChangeDurationMs = 150;
66
67 } // namespace
68
44 // static 69 // static
45 const char Slider::kViewClassName[] = "Slider"; 70 const char Slider::kViewClassName[] = "Slider";
46 71
47 // static 72 Slider::Slider(SliderListener* listener)
48 Slider* Slider::CreateSlider(bool is_material_design, 73 : listener_(listener), highlight_animation_(this) {
49 SliderListener* listener) { 74 highlight_animation_.SetSlideDuration(kSlideHighlightChangeDurationMs);
50 if (is_material_design) 75 EnableCanvasFlippingForRTLUI(true);
51 return new MdSlider(listener); 76 #if defined(OS_MACOSX)
52 return new NonMdSlider(listener); 77 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
78 #else
79 SetFocusBehavior(FocusBehavior::ALWAYS);
80 #endif
81
82 SchedulePaint();
53 } 83 }
54 84
55 Slider::~Slider() { 85 Slider::~Slider() {}
56 }
57 86
58 void Slider::SetValue(float value) { 87 void Slider::SetValue(float value) {
59 SetValueInternal(value, VALUE_CHANGED_BY_API); 88 SetValueInternal(value, VALUE_CHANGED_BY_API);
60 } 89 }
61 90
62 void Slider::SetAccessibleName(const base::string16& name) { 91 void Slider::SetAccessibleName(const base::string16& name) {
63 accessible_name_ = name; 92 accessible_name_ = name;
64 } 93 }
65 94
66 Slider::Slider(SliderListener* listener) 95 void Slider::UpdateState(bool control_on) {
67 : listener_(listener), 96 is_active_ = control_on;
68 value_(0.f), 97 SchedulePaint();
69 keyboard_increment_(0.1f),
70 initial_animating_value_(0.f),
71 value_is_valid_(false),
72 accessibility_events_enabled_(true),
73 initial_button_offset_(0) {
74 EnableCanvasFlippingForRTLUI(true);
75 #if defined(OS_MACOSX)
76 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
77 #else
78 SetFocusBehavior(FocusBehavior::ALWAYS);
79 #endif
80 } 98 }
81 99
82 float Slider::GetAnimatingValue() const{ 100 float Slider::GetAnimatingValue() const{
83 return move_animation_ && move_animation_->is_animating() 101 return move_animation_ && move_animation_->is_animating()
84 ? move_animation_->CurrentValueBetween(initial_animating_value_, 102 ? move_animation_->CurrentValueBetween(initial_animating_value_,
85 value_) 103 value_)
86 : value_; 104 : value_;
87 } 105 }
88 106
89 void Slider::SetHighlighted(bool is_highlighted) {} 107 void Slider::SetHighlighted(bool is_highlighted) {
90 108 if (is_highlighted)
91 void Slider::OnPaint(gfx::Canvas* canvas) { 109 highlight_animation_.Show();
92 View::OnPaint(canvas); 110 else
93 OnPaintFocus(canvas); 111 highlight_animation_.Hide();
94 } 112 }
95 113
96 void Slider::AnimationProgressed(const gfx::Animation* animation) { 114 void Slider::AnimationProgressed(const gfx::Animation* animation) {
97 if (animation == move_animation_.get()) 115 if (animation == &highlight_animation_) {
98 SchedulePaint(); 116 thumb_highlight_radius_ =
117 animation->CurrentValueBetween(kThumbRadius, kThumbHighlightRadius);
118 }
119
120 SchedulePaint();
99 } 121 }
100 122
101 void Slider::AnimationEnded(const gfx::Animation* animation) { 123 void Slider::AnimationEnded(const gfx::Animation* animation) {
102 if (animation == move_animation_.get()) 124 if (animation == move_animation_.get()) {
103 move_animation_.reset(); 125 move_animation_.reset();
126 return;
127 }
128 DCHECK_EQ(animation, &highlight_animation_);
104 } 129 }
105 130
106 void Slider::SetValueInternal(float value, SliderChangeReason reason) { 131 void Slider::SetValueInternal(float value, SliderChangeReason reason) {
107 bool old_value_valid = value_is_valid_; 132 bool old_value_valid = value_is_valid_;
108 133
109 value_is_valid_ = true; 134 value_is_valid_ = true;
110 if (value < 0.0) 135 if (value < 0.0)
111 value = 0.0; 136 value = 0.0;
112 else if (value > 1.0) 137 else if (value > 1.0)
113 value = 1.0; 138 value = 1.0;
(...skipping 19 matching lines...) Expand all
133 if (accessibility_events_enabled_ && GetWidget()) 158 if (accessibility_events_enabled_ && GetWidget())
134 NotifyAccessibilityEvent(ui::AX_EVENT_VALUE_CHANGED, true); 159 NotifyAccessibilityEvent(ui::AX_EVENT_VALUE_CHANGED, true);
135 } 160 }
136 161
137 void Slider::PrepareForMove(const int new_x) { 162 void Slider::PrepareForMove(const int new_x) {
138 // Try to remember the position of the mouse cursor on the button. 163 // Try to remember the position of the mouse cursor on the button.
139 gfx::Insets inset = GetInsets(); 164 gfx::Insets inset = GetInsets();
140 gfx::Rect content = GetContentsBounds(); 165 gfx::Rect content = GetContentsBounds();
141 float value = GetAnimatingValue(); 166 float value = GetAnimatingValue();
142 167
143 const int thumb_width = GetThumbWidth(); 168 const int thumb_x = value * (content.width() - kThumbWidth);
144 const int thumb_x = value * (content.width() - thumb_width);
145 const int candidate_x = (base::i18n::IsRTL() ? 169 const int candidate_x = (base::i18n::IsRTL() ?
146 width() - (new_x - inset.left()) : 170 width() - (new_x - inset.left()) :
147 new_x - inset.left()) - thumb_x; 171 new_x - inset.left()) - thumb_x;
148 if (candidate_x >= 0 && candidate_x < thumb_width) 172 if (candidate_x >= 0 && candidate_x < kThumbWidth)
149 initial_button_offset_ = candidate_x; 173 initial_button_offset_ = candidate_x;
150 else 174 else
151 initial_button_offset_ = thumb_width / 2; 175 initial_button_offset_ = kThumbRadius;
152 } 176 }
153 177
154 void Slider::MoveButtonTo(const gfx::Point& point) { 178 void Slider::MoveButtonTo(const gfx::Point& point) {
155 const gfx::Insets inset = GetInsets(); 179 const gfx::Insets inset = GetInsets();
156 const int thumb_width = GetThumbWidth();
157 // Calculate the value. 180 // Calculate the value.
158 int amount = base::i18n::IsRTL() 181 int amount = base::i18n::IsRTL()
159 ? width() - inset.left() - point.x() - initial_button_offset_ 182 ? width() - inset.left() - point.x() - initial_button_offset_
160 : point.x() - inset.left() - initial_button_offset_; 183 : point.x() - inset.left() - initial_button_offset_;
161 SetValueInternal( 184 SetValueInternal(
162 static_cast<float>(amount) / (width() - inset.width() - thumb_width), 185 static_cast<float>(amount) / (width() - inset.width() - kThumbWidth),
163 VALUE_CHANGED_BY_USER); 186 VALUE_CHANGED_BY_USER);
164 } 187 }
165 188
166 void Slider::OnPaintFocus(gfx::Canvas* canvas) { 189 void Slider::OnPaintFocus(gfx::Canvas* canvas) {
167 if (!HasFocus()) 190 if (!HasFocus())
168 return; 191 return;
169 192
170 // TODO(estade): make this a glow effect instead: crbug.com/658783 193 // TODO(estade): make this a glow effect instead: crbug.com/658783
171 gfx::Rect focus_bounds = GetLocalBounds(); 194 gfx::Rect focus_bounds = GetLocalBounds();
172 focus_bounds.Inset(gfx::Insets(1)); 195 focus_bounds.Inset(gfx::Insets(1));
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 return true; 254 return true;
232 } 255 }
233 256
234 void Slider::GetAccessibleNodeData(ui::AXNodeData* node_data) { 257 void Slider::GetAccessibleNodeData(ui::AXNodeData* node_data) {
235 node_data->role = ui::AX_ROLE_SLIDER; 258 node_data->role = ui::AX_ROLE_SLIDER;
236 node_data->SetName(accessible_name_); 259 node_data->SetName(accessible_name_);
237 node_data->SetValue(base::UTF8ToUTF16( 260 node_data->SetValue(base::UTF8ToUTF16(
238 base::StringPrintf("%d%%", static_cast<int>(value_ * 100 + 0.5)))); 261 base::StringPrintf("%d%%", static_cast<int>(value_ * 100 + 0.5))));
239 } 262 }
240 263
264 void Slider::OnPaint(gfx::Canvas* canvas) {
265 // Paint the slider.
266 const gfx::Rect content = GetContentsBounds();
267 const int width = content.width() - kThumbRadius * 2;
268 const int full = GetAnimatingValue() * width;
269 const int empty = width - full;
270 const int y = content.height() / 2 - kLineThickness / 2;
271 const int x = content.x() + full + kThumbRadius;
272 const SkColor current_thumb_color =
273 is_active_ ? kActiveColor : kDisabledColor;
274
275 // Extra space used to hide slider ends behind the thumb.
276 const int extra_padding = 1;
277
278 cc::PaintFlags slider_flags;
279 slider_flags.setAntiAlias(true);
280 slider_flags.setColor(current_thumb_color);
281 canvas->DrawRoundRect(
282 gfx::Rect(content.x(), y, full + extra_padding, kLineThickness),
283 kSliderRoundedRadius, slider_flags);
284 slider_flags.setColor(kDisabledColor);
285 canvas->DrawRoundRect(gfx::Rect(x + kThumbRadius - extra_padding, y,
286 empty + extra_padding, kLineThickness),
287 kSliderRoundedRadius, slider_flags);
288
289 gfx::Point thumb_center(x, content.height() / 2);
290
291 // Paint the thumb highlight if it exists.
292 const int thumb_highlight_radius =
293 HasFocus() ? kThumbHighlightRadius : thumb_highlight_radius_;
294 if (is_active_ && thumb_highlight_radius > kThumbRadius) {
295 cc::PaintFlags highlight;
296 SkColor kHighlightColor = SkColorSetA(kActiveColor, kHighlightColorAlpha);
297 highlight.setColor(kHighlightColor);
298 highlight.setFlags(cc::PaintFlags::kAntiAlias_Flag);
299 canvas->DrawCircle(thumb_center, thumb_highlight_radius, highlight);
300 }
301
302 // Paint the thumb of the slider.
303 cc::PaintFlags flags;
304 flags.setColor(current_thumb_color);
305 flags.setFlags(cc::PaintFlags::kAntiAlias_Flag);
306
307 if (!is_active_) {
308 flags.setStrokeWidth(kThumbStroke);
309 flags.setStyle(cc::PaintFlags::kStroke_Style);
310 }
311 canvas->DrawCircle(
312 thumb_center,
313 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), flags);
314
315 OnPaintFocus(canvas);
316 }
317
241 void Slider::OnFocus() { 318 void Slider::OnFocus() {
242 View::OnFocus(); 319 View::OnFocus();
243 SchedulePaint(); 320 SchedulePaint();
244 } 321 }
245 322
246 void Slider::OnBlur() { 323 void Slider::OnBlur() {
247 View::OnBlur(); 324 View::OnBlur();
248 SchedulePaint(); 325 SchedulePaint();
249 } 326 }
250 327
(...skipping 15 matching lines...) Expand all
266 event->SetHandled(); 343 event->SetHandled();
267 if (event->details().touch_points() <= 1) 344 if (event->details().touch_points() <= 1)
268 OnSliderDragEnded(); 345 OnSliderDragEnded();
269 break; 346 break;
270 default: 347 default:
271 break; 348 break;
272 } 349 }
273 } 350 }
274 351
275 } // namespace views 352 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/slider.h ('k') | ui/views/controls/slider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698