summaryrefslogtreecommitdiff
path: root/chromium/ui/views/controls/slider.cc
blob: 2b74121c1e464029c4056b7282d2980ebf9a335a (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// 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 "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "grit/ui_resources.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/base/accessibility/accessible_view_state.h"
#include "ui/base/animation/slide_animation.h"
#include "ui/base/events/event.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/point.h"
#include "ui/gfx/rect.h"
#include "ui/views/widget/widget.h"

namespace {
const int kSlideValueChangeDurationMS = 150;

const int kBarImagesActive[] = {
    IDR_SLIDER_ACTIVE_LEFT,
    IDR_SLIDER_ACTIVE_CENTER,
    IDR_SLIDER_PRESSED_CENTER,
    IDR_SLIDER_PRESSED_RIGHT,
};

const int kBarImagesDisabled[] = {
    IDR_SLIDER_DISABLED_LEFT,
    IDR_SLIDER_DISABLED_CENTER,
    IDR_SLIDER_DISABLED_CENTER,
    IDR_SLIDER_DISABLED_RIGHT,
};

// The image chunks.
enum BorderElements {
  LEFT,
  CENTER_LEFT,
  CENTER_RIGHT,
  RIGHT,
};
}

namespace views {

Slider::Slider(SliderListener* listener, Orientation orientation)
    : listener_(listener),
      orientation_(orientation),
      value_(0.f),
      keyboard_increment_(0.1f),
      animating_value_(0.f),
      value_is_valid_(false),
      accessibility_events_enabled_(true),
      focus_border_color_(0),
      bar_active_images_(kBarImagesActive),
      bar_disabled_images_(kBarImagesDisabled) {
  EnableCanvasFlippingForRTLUI(true);
  set_focusable(true);
  UpdateState(true);
}

Slider::~Slider() {
}

void Slider::SetValue(float value) {
  SetValueInternal(value, VALUE_CHANGED_BY_API);
}

void Slider::SetKeyboardIncrement(float increment) {
  keyboard_increment_ = increment;
}

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;
  if (value_ == value)
    return;
  float old_value = value_;
  value_ = value;
  if (listener_)
    listener_->SliderValueChanged(this, value_, old_value, reason);

  if (old_value_valid && base::MessageLoop::current()) {
    // 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.
    animating_value_ = old_value;
    move_animation_.reset(new ui::SlideAnimation(this));
    move_animation_->SetSlideDuration(kSlideValueChangeDurationMS);
    move_animation_->Show();
    AnimationProgressed(move_animation_.get());
  } else {
    SchedulePaint();
  }
  if (accessibility_events_enabled_ && GetWidget()) {
    NotifyAccessibilityEvent(
        ui::AccessibilityTypes::EVENT_VALUE_CHANGED, true);
  }
}

void Slider::PrepareForMove(const gfx::Point& point) {
  // Try to remember the position of the mouse cursor on the button.
  gfx::Insets inset = GetInsets();
  gfx::Rect content = GetContentsBounds();
  float value = move_animation_.get() && move_animation_->is_animating() ?
        animating_value_ : value_;

  // For the horizontal orientation.
  const int thumb_x = value * (content.width() - thumb_->width());
  const int candidate_x = (base::i18n::IsRTL() ?
      width() - (point.x() - inset.left()) :
      point.x() - inset.left()) - thumb_x;
  if (candidate_x >= 0 && candidate_x < thumb_->width())
    initial_button_offset_.set_x(candidate_x);
  else
    initial_button_offset_.set_x(thumb_->width() / 2);

  // For the vertical orientation.
  const int thumb_y = (1.0 - value) * (content.height() - thumb_->height());
  const int candidate_y = point.y() - thumb_y;
  if (candidate_y >= 0 && candidate_y < thumb_->height())
    initial_button_offset_.set_y(candidate_y);
  else
    initial_button_offset_.set_y(thumb_->height() / 2);
}

void Slider::MoveButtonTo(const gfx::Point& point) {
  gfx::Insets inset = GetInsets();
  // Calculate the value.
  if (orientation_ == HORIZONTAL) {
    int amount = base::i18n::IsRTL() ?
        width() - inset.left() - point.x() - initial_button_offset_.x() :
        point.x() - inset.left() - initial_button_offset_.x();
    SetValueInternal(static_cast<float>(amount) /
                         (width() - inset.width() - thumb_->width()),
                     VALUE_CHANGED_BY_USER);
  } else {
    SetValueInternal(
        1.0f - static_cast<float>(point.y() - initial_button_offset_.y()) /
            (height() - thumb_->height()),
        VALUE_CHANGED_BY_USER);
  }
}

void Slider::UpdateState(bool control_on) {
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  if (control_on) {
    thumb_ = rb.GetImageNamed(IDR_SLIDER_ACTIVE_THUMB).ToImageSkia();
    for (int i = 0; i < 4; ++i)
      images_[i] = rb.GetImageNamed(bar_active_images_[i]).ToImageSkia();
  } else {
    thumb_ = rb.GetImageNamed(IDR_SLIDER_DISABLED_THUMB).ToImageSkia();
    for (int i = 0; i < 4; ++i)
      images_[i] = rb.GetImageNamed(bar_disabled_images_[i]).ToImageSkia();
  }
  bar_height_ = images_[LEFT]->height();
  SchedulePaint();
}

void Slider::SetAccessibleName(const string16& name) {
  accessible_name_ = name;
}

gfx::Size Slider::GetPreferredSize() {
  const int kSizeMajor = 200;
  const int kSizeMinor = 40;

  if (orientation_ == HORIZONTAL)
    return gfx::Size(std::max(width(), kSizeMajor), kSizeMinor);
  return gfx::Size(kSizeMinor, std::max(height(), kSizeMajor));
}

void Slider::OnPaint(gfx::Canvas* canvas) {
  gfx::Rect content = GetContentsBounds();
  float value = move_animation_.get() && move_animation_->is_animating() ?
      animating_value_ : value_;
  if (orientation_ == HORIZONTAL) {
    // Paint slider bar with image resources.

    // Inset the slider bar a little bit, so that the left or the right end of
    // the slider bar will not be exposed under the thumb button when the thumb
    // button slides to the left most or right most position.
    const int kBarInsetX = 2;
    int bar_width = content.width() - kBarInsetX * 2;
    int bar_cy = content.height() / 2 - bar_height_ / 2;

    int w = content.width() - thumb_->width();
    int full = value * w;
    int middle = std::max(full, images_[LEFT]->width());

    canvas->Save();
    canvas->Translate(gfx::Vector2d(kBarInsetX, bar_cy));
    canvas->DrawImageInt(*images_[LEFT], 0, 0);
    canvas->DrawImageInt(*images_[RIGHT],
                         bar_width - images_[RIGHT]->width(),
                         0);
    canvas->TileImageInt(*images_[CENTER_LEFT],
                         images_[LEFT]->width(),
                         0,
                         middle - images_[LEFT]->width(),
                         bar_height_);
    canvas->TileImageInt(*images_[CENTER_RIGHT],
                         middle,
                         0,
                         bar_width - middle - images_[RIGHT]->width(),
                         bar_height_);
    canvas->Restore();

    // Paint slider thumb.
    int button_cx = content.x() + full;
    int thumb_y = content.height() / 2 - thumb_->height() / 2;
    canvas->DrawImageInt(*thumb_, button_cx, thumb_y);
  } else {
    // TODO(jennyz): draw vertical slider bar with resources.
    // TODO(sad): The painting code should use NativeTheme for various
    // platforms.
    const int kButtonRadius = thumb_->width() / 2;
    const int kLineThickness = bar_height_ / 2;
    const SkColor kFullColor = SkColorSetARGB(125, 0, 0, 0);
    const SkColor kEmptyColor = SkColorSetARGB(50, 0, 0, 0);

    int h = content.height() - thumb_->height();
    int full = value * h;
    int empty = h - full;
    int x = content.width() / 2 - kLineThickness / 2;
    canvas->FillRect(gfx::Rect(x, content.y() + kButtonRadius,
                               kLineThickness, empty),
                     kEmptyColor);
    canvas->FillRect(gfx::Rect(x, content.y() + empty + 2 * kButtonRadius,
                               kLineThickness, full),
                     kFullColor);

    // TODO(mtomasz): We draw a thumb here because so far it is the same
    // for horizontal and vertical orientations. If it is different, then
    // we will need a separate resource.
    int button_cy = content.y() + h - full;
    int thumb_x = content.width() / 2 - thumb_->width() / 2;
    canvas->DrawImageInt(*thumb_, thumb_x, button_cy);
  }
  View::OnPaint(canvas);
}

bool Slider::OnMousePressed(const ui::MouseEvent& event) {
  if (!event.IsOnlyLeftMouseButton())
    return false;
  if (listener_)
    listener_->SliderDragStarted(this);
  PrepareForMove(event.location());
  MoveButtonTo(event.location());
  return true;
}

bool Slider::OnMouseDragged(const ui::MouseEvent& event) {
  MoveButtonTo(event.location());
  return true;
}

void Slider::OnMouseReleased(const ui::MouseEvent& event) {
  if (listener_)
    listener_->SliderDragEnded(this);
}

bool Slider::OnKeyPressed(const ui::KeyEvent& event) {
  if (orientation_ == HORIZONTAL) {
    if (event.key_code() == ui::VKEY_LEFT) {
      SetValueInternal(value_ - keyboard_increment_, VALUE_CHANGED_BY_USER);
      return true;
    } else if (event.key_code() == ui::VKEY_RIGHT) {
      SetValueInternal(value_ + keyboard_increment_, VALUE_CHANGED_BY_USER);
      return true;
    }
  } else {
    if (event.key_code() == ui::VKEY_DOWN) {
      SetValueInternal(value_ - keyboard_increment_, VALUE_CHANGED_BY_USER);
      return true;
    } else if (event.key_code() == ui::VKEY_UP) {
      SetValueInternal(value_ + keyboard_increment_, VALUE_CHANGED_BY_USER);
      return true;
    }
  }
  return false;
}

void Slider::OnGestureEvent(ui::GestureEvent* event) {
  if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN ||
      event->type() == ui::ET_GESTURE_TAP_DOWN) {
    PrepareForMove(event->location());
    MoveButtonTo(event->location());
    event->SetHandled();
  } else if (event->type() == ui::ET_GESTURE_SCROLL_UPDATE ||
             event->type() == ui::ET_GESTURE_SCROLL_END) {
    MoveButtonTo(event->location());
    event->SetHandled();
  }
}

void Slider::AnimationProgressed(const ui::Animation* animation) {
  animating_value_ = animation->CurrentValueBetween(animating_value_, value_);
  SchedulePaint();
}

void Slider::GetAccessibleState(ui::AccessibleViewState* state) {
  state->role = ui::AccessibilityTypes::ROLE_SLIDER;
  state->name = accessible_name_;
  state->value = UTF8ToUTF16(
      base::StringPrintf("%d%%", (int)(value_ * 100 + 0.5)));
}

void Slider::OnPaintFocusBorder(gfx::Canvas* canvas) {
  if (!focus_border_color_) {
    View::OnPaintFocusBorder(canvas);
  } else if (HasFocus() && (focusable() || IsAccessibilityFocusable())) {
    canvas->DrawRect(gfx::Rect(1, 1, width() - 3, height() - 3),
                     focus_border_color_);
  }
}

}  // namespace views