| 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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "third_party/skia/include/core/SkCanvas.h" | 11 #include "third_party/skia/include/core/SkCanvas.h" |
| 12 #include "third_party/skia/include/core/SkColor.h" | 12 #include "third_party/skia/include/core/SkColor.h" |
| 13 #include "third_party/skia/include/core/SkPaint.h" | 13 #include "third_party/skia/include/core/SkPaint.h" |
| 14 #include "ui/base/accelerators/accelerator.h" |
| 14 #include "ui/base/accessibility/accessible_view_state.h" | 15 #include "ui/base/accessibility/accessible_view_state.h" |
| 15 #include "ui/base/animation/slide_animation.h" | 16 #include "ui/base/animation/slide_animation.h" |
| 16 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/point.h" | 18 #include "ui/gfx/point.h" |
| 18 #include "ui/gfx/rect.h" | 19 #include "ui/gfx/rect.h" |
| 19 #include "ui/views/widget/widget.h" | 20 #include "ui/views/widget/widget.h" |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 const int kSlideValueChangeDurationMS = 150; | 23 const int kSlideValueChangeDurationMS = 150; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace views { | 26 namespace views { |
| 26 | 27 |
| 27 Slider::Slider(SliderListener* listener, Orientation orientation) | 28 Slider::Slider(SliderListener* listener, Orientation orientation) |
| 28 : listener_(listener), | 29 : listener_(listener), |
| 29 orientation_(orientation), | 30 orientation_(orientation), |
| 30 value_(0.f), | 31 value_(0.f), |
| 31 keyboard_increment_(0.1f), | 32 keyboard_increment_(0.1f), |
| 32 animating_value_(0.f), | 33 animating_value_(0.f), |
| 33 value_is_valid_(false) { | 34 value_is_valid_(false), |
| 35 esc_was_pressed_(false) { |
| 34 EnableCanvasFlippingForRTLUI(true); | 36 EnableCanvasFlippingForRTLUI(true); |
| 35 set_focusable(true); | 37 set_focusable(true); |
| 36 } | 38 } |
| 37 | 39 |
| 38 Slider::~Slider() { | 40 Slider::~Slider() { |
| 39 } | 41 } |
| 40 | 42 |
| 41 void Slider::SetValue(float value) { | 43 void Slider::SetValue(float value) { |
| 42 SetValueInternal(value, VALUE_CHANGED_BY_API); | 44 SetValueInternal(value, VALUE_CHANGED_BY_API); |
| 43 } | 45 } |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 paint.setStyle(SkPaint::kFill_Style); | 140 paint.setStyle(SkPaint::kFill_Style); |
| 139 paint.setAntiAlias(true); | 141 paint.setAntiAlias(true); |
| 140 paint.setColor(kButtonColor); | 142 paint.setColor(kButtonColor); |
| 141 canvas->sk_canvas()->drawCircle(button_cx, button_cy, kButtonRadius, paint); | 143 canvas->sk_canvas()->drawCircle(button_cx, button_cy, kButtonRadius, paint); |
| 142 View::OnPaint(canvas); | 144 View::OnPaint(canvas); |
| 143 } | 145 } |
| 144 | 146 |
| 145 bool Slider::OnMousePressed(const views::MouseEvent& event) { | 147 bool Slider::OnMousePressed(const views::MouseEvent& event) { |
| 146 if (listener_) | 148 if (listener_) |
| 147 listener_->SliderDragStarted(this); | 149 listener_->SliderDragStarted(this); |
| 150 original_value_ = value_; |
| 151 ui::Accelerator escape_accelerator(ui::VKEY_ESCAPE, false, false, false); |
| 152 AddAccelerator(escape_accelerator); |
| 148 return OnMouseDragged(event); | 153 return OnMouseDragged(event); |
| 149 } | 154 } |
| 150 | 155 |
| 151 bool Slider::OnMouseDragged(const views::MouseEvent& event) { | 156 bool Slider::OnMouseDragged(const views::MouseEvent& event) { |
| 157 if (esc_was_pressed_) |
| 158 return true; |
| 152 gfx::Insets inset = GetInsets(); | 159 gfx::Insets inset = GetInsets(); |
| 153 if (orientation_ == HORIZONTAL) { | 160 if (orientation_ == HORIZONTAL) { |
| 154 int amount = base::i18n::IsRTL() ? width() - inset.left() - event.x() : | 161 int amount = base::i18n::IsRTL() ? width() - inset.left() - event.x() : |
| 155 event.x() - inset.left(); | 162 event.x() - inset.left(); |
| 156 SetValueInternal(static_cast<float>(amount) / (width() - inset.width()), | 163 SetValueInternal(static_cast<float>(amount) / (width() - inset.width()), |
| 157 VALUE_CHANGED_BY_USER); | 164 VALUE_CHANGED_BY_USER); |
| 158 } else { | 165 } else { |
| 159 SetValueInternal(1.0f - static_cast<float>(event.y()) / height(), | 166 SetValueInternal(1.0f - static_cast<float>(event.y()) / height(), |
| 160 VALUE_CHANGED_BY_USER); | 167 VALUE_CHANGED_BY_USER); |
| 161 } | 168 } |
| 162 return true; | 169 return true; |
| 163 } | 170 } |
| 164 | 171 |
| 165 void Slider::OnMouseReleased(const views::MouseEvent& event) { | 172 void Slider::OnMouseReleased(const views::MouseEvent& event) { |
| 166 if (listener_) | 173 if (listener_) |
| 167 listener_->SliderDragEnded(this); | 174 listener_->SliderDragEnded(this); |
| 175 esc_was_pressed_ = false; |
| 176 ResetAccelerators(); |
| 168 } | 177 } |
| 169 | 178 |
| 170 bool Slider::OnKeyPressed(const views::KeyEvent& event) { | 179 bool Slider::OnKeyPressed(const views::KeyEvent& event) { |
| 171 if (orientation_ == HORIZONTAL) { | 180 if (orientation_ == HORIZONTAL) { |
| 172 if (event.key_code() == ui::VKEY_LEFT) { | 181 if (event.key_code() == ui::VKEY_LEFT) { |
| 173 SetValueInternal(value_ - keyboard_increment_, VALUE_CHANGED_BY_USER); | 182 SetValueInternal(value_ - keyboard_increment_, VALUE_CHANGED_BY_USER); |
| 174 return true; | 183 return true; |
| 175 } else if (event.key_code() == ui::VKEY_RIGHT) { | 184 } else if (event.key_code() == ui::VKEY_RIGHT) { |
| 176 SetValueInternal(value_ + keyboard_increment_, VALUE_CHANGED_BY_USER); | 185 SetValueInternal(value_ + keyboard_increment_, VALUE_CHANGED_BY_USER); |
| 177 return true; | 186 return true; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 193 SchedulePaint(); | 202 SchedulePaint(); |
| 194 } | 203 } |
| 195 | 204 |
| 196 void Slider::GetAccessibleState(ui::AccessibleViewState* state) { | 205 void Slider::GetAccessibleState(ui::AccessibleViewState* state) { |
| 197 state->role = ui::AccessibilityTypes::ROLE_SLIDER; | 206 state->role = ui::AccessibilityTypes::ROLE_SLIDER; |
| 198 state->name = accessible_name_; | 207 state->name = accessible_name_; |
| 199 state->value = UTF8ToUTF16( | 208 state->value = UTF8ToUTF16( |
| 200 base::StringPrintf("%d%%", (int)(value_ * 100 + 0.5))); | 209 base::StringPrintf("%d%%", (int)(value_ * 100 + 0.5))); |
| 201 } | 210 } |
| 202 | 211 |
| 212 bool Slider::AcceleratorPressed(const ui::Accelerator& accelerator) { |
| 213 SetValueInternal(original_value_, VALUE_CHANGED_BY_USER); |
| 214 esc_was_pressed_ = true; |
| 215 return true; |
| 216 } |
| 217 |
| 203 } // namespace views | 218 } // namespace views |
| OLD | NEW |