OLD | NEW |
---|---|
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(); | |
msw
2017/02/16 21:08:20
q: needed? (presumably it'll schedule a paint when
Evan Stade
2017/02/16 22:48:01
yea, this is weird. I don't know why it's here (ju
| |
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) { |
108 if (is_highlighted) | |
109 highlight_animation_.Show(); | |
110 else | |
111 highlight_animation_.Hide(); | |
112 } | |
90 | 113 |
91 void Slider::OnPaint(gfx::Canvas* canvas) { | 114 void Slider::OnPaint(gfx::Canvas* canvas) { |
92 View::OnPaint(canvas); | 115 // Paint the slider. |
116 const gfx::Rect content = GetContentsBounds(); | |
117 const int width = content.width() - kThumbRadius * 2; | |
118 const int full = GetAnimatingValue() * width; | |
119 const int empty = width - full; | |
120 const int y = content.height() / 2 - kLineThickness / 2; | |
121 const int x = content.x() + full + kThumbRadius; | |
122 const SkColor current_thumb_color = | |
123 is_active_ ? kActiveColor : kDisabledColor; | |
124 | |
125 // Extra space used to hide slider ends behind the thumb. | |
126 const int extra_padding = 1; | |
127 | |
128 cc::PaintFlags slider_flags; | |
129 slider_flags.setAntiAlias(true); | |
130 slider_flags.setColor(current_thumb_color); | |
131 canvas->DrawRoundRect( | |
132 gfx::Rect(content.x(), y, full + extra_padding, kLineThickness), | |
133 kSliderRoundedRadius, slider_flags); | |
134 slider_flags.setColor(kDisabledColor); | |
135 canvas->DrawRoundRect(gfx::Rect(x + kThumbRadius - extra_padding, y, | |
136 empty + extra_padding, kLineThickness), | |
137 kSliderRoundedRadius, slider_flags); | |
138 | |
139 gfx::Point thumb_center(x, content.height() / 2); | |
140 | |
141 // Paint the thumb highlight if it exists. | |
142 const int thumb_highlight_radius = | |
143 HasFocus() ? kThumbHighlightRadius : thumb_highlight_radius_; | |
144 if (is_active_ && thumb_highlight_radius > kThumbRadius) { | |
145 cc::PaintFlags highlight; | |
146 SkColor kHighlightColor = SkColorSetA(kActiveColor, kHighlightColorAlpha); | |
147 highlight.setColor(kHighlightColor); | |
148 highlight.setFlags(cc::PaintFlags::kAntiAlias_Flag); | |
149 canvas->DrawCircle(thumb_center, thumb_highlight_radius, highlight); | |
150 } | |
151 | |
152 // Paint the thumb of the slider. | |
153 cc::PaintFlags flags; | |
154 flags.setColor(current_thumb_color); | |
155 flags.setFlags(cc::PaintFlags::kAntiAlias_Flag); | |
156 | |
157 if (!is_active_) { | |
158 flags.setStrokeWidth(kThumbStroke); | |
159 flags.setStyle(cc::PaintFlags::kStroke_Style); | |
160 } | |
161 canvas->DrawCircle( | |
162 thumb_center, | |
163 is_active_ ? kThumbRadius : (kThumbRadius - kThumbStroke / 2), flags); | |
164 | |
93 OnPaintFocus(canvas); | 165 OnPaintFocus(canvas); |
94 } | 166 } |
95 | 167 |
96 void Slider::AnimationProgressed(const gfx::Animation* animation) { | 168 void Slider::AnimationProgressed(const gfx::Animation* animation) { |
97 if (animation == move_animation_.get()) | 169 if (animation == &highlight_animation_) { |
98 SchedulePaint(); | 170 thumb_highlight_radius_ = |
171 animation->CurrentValueBetween(kThumbRadius, kThumbHighlightRadius); | |
172 } | |
173 | |
174 SchedulePaint(); | |
99 } | 175 } |
100 | 176 |
101 void Slider::AnimationEnded(const gfx::Animation* animation) { | 177 void Slider::AnimationEnded(const gfx::Animation* animation) { |
102 if (animation == move_animation_.get()) | 178 if (animation == move_animation_.get()) { |
103 move_animation_.reset(); | 179 move_animation_.reset(); |
180 return; | |
181 } | |
182 DCHECK_EQ(animation, &highlight_animation_); | |
104 } | 183 } |
105 | 184 |
106 void Slider::SetValueInternal(float value, SliderChangeReason reason) { | 185 void Slider::SetValueInternal(float value, SliderChangeReason reason) { |
107 bool old_value_valid = value_is_valid_; | 186 bool old_value_valid = value_is_valid_; |
108 | 187 |
109 value_is_valid_ = true; | 188 value_is_valid_ = true; |
110 if (value < 0.0) | 189 if (value < 0.0) |
111 value = 0.0; | 190 value = 0.0; |
112 else if (value > 1.0) | 191 else if (value > 1.0) |
113 value = 1.0; | 192 value = 1.0; |
(...skipping 19 matching lines...) Expand all Loading... | |
133 if (accessibility_events_enabled_ && GetWidget()) | 212 if (accessibility_events_enabled_ && GetWidget()) |
134 NotifyAccessibilityEvent(ui::AX_EVENT_VALUE_CHANGED, true); | 213 NotifyAccessibilityEvent(ui::AX_EVENT_VALUE_CHANGED, true); |
135 } | 214 } |
136 | 215 |
137 void Slider::PrepareForMove(const int new_x) { | 216 void Slider::PrepareForMove(const int new_x) { |
138 // Try to remember the position of the mouse cursor on the button. | 217 // Try to remember the position of the mouse cursor on the button. |
139 gfx::Insets inset = GetInsets(); | 218 gfx::Insets inset = GetInsets(); |
140 gfx::Rect content = GetContentsBounds(); | 219 gfx::Rect content = GetContentsBounds(); |
141 float value = GetAnimatingValue(); | 220 float value = GetAnimatingValue(); |
142 | 221 |
143 const int thumb_width = GetThumbWidth(); | 222 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() ? | 223 const int candidate_x = (base::i18n::IsRTL() ? |
146 width() - (new_x - inset.left()) : | 224 width() - (new_x - inset.left()) : |
147 new_x - inset.left()) - thumb_x; | 225 new_x - inset.left()) - thumb_x; |
148 if (candidate_x >= 0 && candidate_x < thumb_width) | 226 if (candidate_x >= 0 && candidate_x < kThumbWidth) |
149 initial_button_offset_ = candidate_x; | 227 initial_button_offset_ = candidate_x; |
150 else | 228 else |
151 initial_button_offset_ = thumb_width / 2; | 229 initial_button_offset_ = kThumbRadius; |
152 } | 230 } |
153 | 231 |
154 void Slider::MoveButtonTo(const gfx::Point& point) { | 232 void Slider::MoveButtonTo(const gfx::Point& point) { |
155 const gfx::Insets inset = GetInsets(); | 233 const gfx::Insets inset = GetInsets(); |
156 const int thumb_width = GetThumbWidth(); | |
157 // Calculate the value. | 234 // Calculate the value. |
158 int amount = base::i18n::IsRTL() | 235 int amount = base::i18n::IsRTL() |
159 ? width() - inset.left() - point.x() - initial_button_offset_ | 236 ? width() - inset.left() - point.x() - initial_button_offset_ |
160 : point.x() - inset.left() - initial_button_offset_; | 237 : point.x() - inset.left() - initial_button_offset_; |
161 SetValueInternal( | 238 SetValueInternal( |
162 static_cast<float>(amount) / (width() - inset.width() - thumb_width), | 239 static_cast<float>(amount) / (width() - inset.width() - kThumbWidth), |
163 VALUE_CHANGED_BY_USER); | 240 VALUE_CHANGED_BY_USER); |
164 } | 241 } |
165 | 242 |
166 void Slider::OnPaintFocus(gfx::Canvas* canvas) { | 243 void Slider::OnPaintFocus(gfx::Canvas* canvas) { |
167 if (!HasFocus()) | 244 if (!HasFocus()) |
168 return; | 245 return; |
169 | 246 |
170 // TODO(estade): make this a glow effect instead: crbug.com/658783 | 247 // TODO(estade): make this a glow effect instead: crbug.com/658783 |
171 gfx::Rect focus_bounds = GetLocalBounds(); | 248 gfx::Rect focus_bounds = GetLocalBounds(); |
172 focus_bounds.Inset(gfx::Insets(1)); | 249 focus_bounds.Inset(gfx::Insets(1)); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 |
OLD | NEW |