1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/slider.h"
#include <algorithm>
#include <iterator>
#include <memory>
#include <utility>
#include "base/check_op.h"
#include "base/i18n/rtl.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/current_thread.h"
#include "build/build_config.h"
#include "cc/paint/paint_flags.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/events/event.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/widget/widget.h"
namespace views {
namespace {
// The thickness of the slider.
constexpr int kLineThickness = 2;
// The radius used to draw rounded slider ends.
constexpr float kSliderRoundedRadius = 2.f;
// The padding used to hide the slider underneath the thumb.
constexpr int kSliderPadding = 2;
// The radius of the highlighted thumb of the slider
constexpr float kThumbHighlightRadius = 12.f;
float GetNearestAllowedValue(const base::flat_set<float>& allowed_values,
float suggested_value) {
if (allowed_values.empty()) {
return suggested_value;
}
const base::flat_set<float>::const_iterator greater =
allowed_values.upper_bound(suggested_value);
if (greater == allowed_values.end()) {
return *allowed_values.rbegin();
}
if (greater == allowed_values.begin()) {
return *allowed_values.cbegin();
}
// Select a value nearest to the |suggested_value|.
if ((*greater - suggested_value) > (suggested_value - *std::prev(greater))) {
return *std::prev(greater);
}
return *greater;
}
} // namespace
Slider::Slider(SliderListener* listener) : listener_(listener) {
highlight_animation_.SetSlideDuration(base::Milliseconds(150));
SetFlipCanvasOnPaintForRTLUI(true);
#if BUILDFLAG(IS_MAC)
SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
#else
SetFocusBehavior(FocusBehavior::ALWAYS);
#endif
SchedulePaint();
GetViewAccessibility().SetRole(ax::mojom::Role::kSlider);
GetViewAccessibility().AddAction(ax::mojom::Action::kIncrement);
GetViewAccessibility().AddAction(ax::mojom::Action::kDecrement);
}
Slider::~Slider() = default;
float Slider::GetValue() const {
return value_;
}
void Slider::SetValue(float value) {
SetValueInternal(value, SliderChangeReason::kByApi);
}
float Slider::GetValueIndicatorRadius() const {
return value_indicator_radius_;
}
void Slider::SetValueIndicatorRadius(float radius) {
value_indicator_radius_ = radius;
}
bool Slider::GetEnableAccessibilityEvents() const {
return accessibility_events_enabled_;
}
void Slider::SetEnableAccessibilityEvents(bool enabled) {
if (accessibility_events_enabled_ == enabled) {
return;
}
accessibility_events_enabled_ = enabled;
OnPropertyChanged(&accessibility_events_enabled_, kPropertyEffectsNone);
}
void Slider::SetRenderingStyle(RenderingStyle style) {
style_ = style;
SchedulePaint();
}
void Slider::SetAllowedValues(const base::flat_set<float>* allowed_values) {
if (!allowed_values) {
allowed_values_.clear();
return;
}
#if DCHECK_IS_ON()
// Disallow empty sliders.
DCHECK(allowed_values->size());
for (const float v : *allowed_values) {
// sanity check.
DCHECK_GE(v, 0.0f);
DCHECK_LE(v, 1.0f);
}
#endif
allowed_values_ = *allowed_values;
const auto position = allowed_values_.lower_bound(value_);
const float new_value = (position == allowed_values_.end())
? *allowed_values_.cbegin()
: *position;
if (new_value != value_) {
SetValue(new_value);
}
}
float Slider::GetAnimatingValue() const {
return move_animation_ && move_animation_->is_animating()
? move_animation_->CurrentValueBetween(initial_animating_value_,
value_)
: value_;
}
void Slider::SetHighlighted(bool is_highlighted) {
if (is_highlighted) {
highlight_animation_.Show();
} else {
highlight_animation_.Hide();
}
}
void Slider::AnimationProgressed(const gfx::Animation* animation) {
if (animation == &highlight_animation_) {
thumb_highlight_radius_ =
animation->CurrentValueBetween(kThumbRadius, kThumbHighlightRadius);
}
SchedulePaint();
}
void Slider::AnimationEnded(const gfx::Animation* animation) {
if (animation == move_animation_.get()) {
move_animation_.reset();
return;
}
DCHECK_EQ(animation, &highlight_animation_);
}
void Slider::SetValueInternal(float value, SliderChangeReason reason) {
bool old_value_valid = value_is_valid_;
value_is_valid_ = true;
if (value < 0.0) {
value = 0.0;
} else if (value > 1.0) {
value = 1.0;
}
value = GetNearestAllowedValue(allowed_values_, value);
if (value_ == value) {
return;
}
float old_value = value_;
value_ = value;
UpdateAccessibleValue();
if (listener_) {
listener_->SliderValueChanged(this, value_, old_value, reason);
}
if (old_value_valid && base::CurrentThread::Get()) {
// Do not animate when setting the value of the slider for the first time.
// There is no message-loop when running tests. So we cannot animate then.
if (!move_animation_) {
initial_animating_value_ = old_value;
move_animation_ = std::make_unique<gfx::SlideAnimation>(this);
move_animation_->SetSlideDuration(base::Milliseconds(150));
move_animation_->Show();
}
OnPropertyChanged(&value_, kPropertyEffectsNone);
} else {
OnPropertyChanged(&value_, kPropertyEffectsPaint);
}
if (accessibility_events_enabled_) {
if (GetWidget() && GetWidget()->IsVisible() && GetVisible()) {
DCHECK(!pending_accessibility_value_change_);
NotifyAccessibilityEventDeprecated(ax::mojom::Event::kValueChanged, true);
} else {
pending_accessibility_value_change_ = true;
}
}
}
void Slider::PrepareForMove(const int new_x) {
// Try to remember the position of the mouse cursor on the button.
gfx::Insets inset = GetInsets();
gfx::Rect content = GetContentsBounds();
float value = GetAnimatingValue();
const int thumb_x = value * (content.width() - 2 * value_indicator_radius_);
const int candidate_x = GetMirroredXInView(new_x - inset.left()) - thumb_x;
if (candidate_x >= value_indicator_radius_ - kThumbRadius &&
candidate_x < value_indicator_radius_ + kThumbRadius) {
initial_button_offset_ = candidate_x;
} else {
initial_button_offset_ = value_indicator_radius_;
}
}
void Slider::MoveButtonTo(const gfx::Point& point) {
const gfx::Insets inset = GetInsets();
// Calculate the value.
int amount = base::i18n::IsRTL()
? width() - inset.left() - point.x() - initial_button_offset_
: point.x() - inset.left() - initial_button_offset_;
SetValueInternal(static_cast<float>(amount) /
(width() - inset.width() - 2 * value_indicator_radius_),
SliderChangeReason::kByUser);
}
void Slider::OnSliderDragStarted() {
SetHighlighted(true);
if (listener_) {
listener_->SliderDragStarted(this);
}
}
void Slider::OnSliderDragEnded() {
SetHighlighted(false);
if (listener_) {
listener_->SliderDragEnded(this);
}
}
gfx::Size Slider::CalculatePreferredSize(
const SizeBounds& available_size) const {
constexpr int kSizeMajor = 200;
constexpr int kSizeMinor = 40;
return gfx::Size(std::max(available_size.width().value_or(0), kSizeMajor),
kSizeMinor);
}
bool Slider::OnMousePressed(const ui::MouseEvent& event) {
if (!event.IsOnlyLeftMouseButton()) {
return false;
}
OnSliderDragStarted();
PrepareForMove(event.location().x());
MoveButtonTo(event.location());
return true;
}
bool Slider::OnMouseDragged(const ui::MouseEvent& event) {
MoveButtonTo(event.location());
return true;
}
void Slider::OnMouseReleased(const ui::MouseEvent& event) {
OnSliderDragEnded();
}
bool Slider::OnKeyPressed(const ui::KeyEvent& event) {
int direction = 1;
switch (event.key_code()) {
case ui::VKEY_LEFT:
direction = base::i18n::IsRTL() ? 1 : -1;
break;
case ui::VKEY_RIGHT:
direction = base::i18n::IsRTL() ? -1 : 1;
break;
case ui::VKEY_UP:
direction = 1;
break;
case ui::VKEY_DOWN:
direction = -1;
break;
default:
return false;
}
if (allowed_values_.empty()) {
SetValueInternal(value_ + direction * keyboard_increment_,
SliderChangeReason::kByUser);
} else {
// discrete slider.
if (direction > 0) {
const base::flat_set<float>::const_iterator greater =
allowed_values_.upper_bound(value_);
SetValueInternal(greater == allowed_values_.cend()
? *allowed_values_.crend()
: *greater,
SliderChangeReason::kByUser);
} else {
const base::flat_set<float>::const_iterator lesser =
allowed_values_.lower_bound(value_);
// Current value must be in the list of allowed values.
DCHECK(lesser != allowed_values_.cend());
SetValueInternal(lesser == allowed_values_.cbegin()
? *allowed_values_.cbegin()
: *std::prev(lesser),
SliderChangeReason::kByUser);
}
}
return true;
}
bool Slider::HandleAccessibleAction(const ui::AXActionData& action_data) {
if (action_data.action == ax::mojom::Action::kIncrement) {
SetValueInternal(value_ + keyboard_increment_, SliderChangeReason::kByUser);
return true;
} else if (action_data.action == ax::mojom::Action::kDecrement) {
SetValueInternal(value_ - keyboard_increment_, SliderChangeReason::kByUser);
return true;
} else {
return views::View::HandleAccessibleAction(action_data);
}
}
void Slider::OnPaint(gfx::Canvas* canvas) {
// Paint the slider.
const gfx::Rect content = GetContentsBounds();
const int width = content.width() - kThumbRadius * 2;
const int full = GetAnimatingValue() * width;
const int empty = width - full;
const int y = content.height() / 2 - kLineThickness / 2;
const int x = content.x() + full + kThumbRadius;
cc::PaintFlags slider_flags;
slider_flags.setAntiAlias(true);
slider_flags.setColor(GetThumbColor());
canvas->DrawRoundRect(
gfx::Rect(content.x(), y, full - GetSliderExtraPadding(), kLineThickness),
kSliderRoundedRadius, slider_flags);
slider_flags.setColor(GetTroughColor());
canvas->DrawRoundRect(
gfx::Rect(x + kThumbRadius + GetSliderExtraPadding(), y,
empty - GetSliderExtraPadding(), kLineThickness),
kSliderRoundedRadius, slider_flags);
gfx::Point thumb_center(x, content.height() / 2);
// Paint the thumb highlight if it exists.
const int thumb_highlight_radius =
HasFocus() ? kThumbHighlightRadius : thumb_highlight_radius_;
if (thumb_highlight_radius > kThumbRadius) {
cc::PaintFlags highlight_background;
highlight_background.setColor(GetTroughColor());
highlight_background.setAntiAlias(true);
canvas->DrawCircle(thumb_center, thumb_highlight_radius,
highlight_background);
cc::PaintFlags highlight_border;
highlight_border.setColor(GetThumbColor());
highlight_border.setAntiAlias(true);
highlight_border.setStyle(cc::PaintFlags::kStroke_Style);
highlight_border.setStrokeWidth(kLineThickness);
canvas->DrawCircle(thumb_center, thumb_highlight_radius, highlight_border);
}
// Paint the thumb of the slider.
cc::PaintFlags flags;
flags.setColor(GetThumbColor());
flags.setAntiAlias(true);
canvas->DrawCircle(thumb_center, kThumbRadius, flags);
}
void Slider::OnFocus() {
View::OnFocus();
SchedulePaint();
}
void Slider::OnBlur() {
View::OnBlur();
SchedulePaint();
}
void Slider::VisibilityChanged(View* starting_from, bool is_visible) {
if (is_visible && GetWidget() && GetWidget()->IsVisible() && GetVisible()) {
ApplyPendingAccessibleValueUpdate();
}
}
void Slider::AddedToWidget() {
if (GetWidget()->IsVisible() && GetVisible()) {
ApplyPendingAccessibleValueUpdate();
}
}
void Slider::ApplyPendingAccessibleValueUpdate() {
if (!pending_accessibility_value_change_)
return;
NotifyAccessibilityEventDeprecated(ax::mojom::Event::kValueChanged, true);
pending_accessibility_value_change_ = false;
}
void Slider::OnGestureEvent(ui::GestureEvent* event) {
switch (event->type()) {
// In a multi point gesture only the touch point will generate
// an EventType::kGestureTapDown event.
case ui::EventType::kGestureTapDown:
OnSliderDragStarted();
PrepareForMove(event->location().x());
[[fallthrough]];
case ui::EventType::kGestureScrollBegin:
case ui::EventType::kGestureScrollUpdate:
MoveButtonTo(event->location());
event->SetHandled();
break;
case ui::EventType::kGestureEnd:
MoveButtonTo(event->location());
event->SetHandled();
if (event->details().touch_points() <= 1) {
OnSliderDragEnded();
}
break;
default:
break;
}
}
SkColor Slider::GetThumbColor() const {
switch (style_) {
case RenderingStyle::kDefaultStyle:
return GetColorProvider()->GetColor(ui::kColorSliderThumb);
case RenderingStyle::kMinimalStyle:
return GetColorProvider()->GetColor(ui::kColorSliderThumbMinimal);
}
}
SkColor Slider::GetTroughColor() const {
switch (style_) {
case RenderingStyle::kDefaultStyle:
return GetColorProvider()->GetColor(ui::kColorSliderTrack);
case RenderingStyle::kMinimalStyle:
return GetColorProvider()->GetColor(ui::kColorSliderTrackMinimal);
}
}
int Slider::GetSliderExtraPadding() const {
// Padding is negative when slider style is default so that there is no
// separation between slider and thumb.
switch (style_) {
case RenderingStyle::kDefaultStyle:
return -kSliderPadding;
case RenderingStyle::kMinimalStyle:
return kSliderPadding;
}
}
void Slider::UpdateAccessibleValue() {
views::ScopedAccessibilityEventBlocker scoped_event_blocker(
GetViewAccessibility());
GetViewAccessibility().SetValue(base::UTF8ToUTF16(
base::StringPrintf("%d%%", static_cast<int>(value_ * 100 + 0.5))));
}
BEGIN_METADATA(Slider)
ADD_PROPERTY_METADATA(float, Value)
ADD_PROPERTY_METADATA(bool, EnableAccessibilityEvents)
ADD_PROPERTY_METADATA(float, ValueIndicatorRadius)
END_METADATA
} // namespace views
|