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

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

Issue 10089001: views slider: Add support for gestures. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « ui/views/controls/slider.h ('k') | no next file » | 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 "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"
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 AnimationProgressed(move_animation_.get()); 71 AnimationProgressed(move_animation_.get());
72 } else { 72 } else {
73 SchedulePaint(); 73 SchedulePaint();
74 } 74 }
75 if (GetWidget()) { 75 if (GetWidget()) {
76 GetWidget()->NotifyAccessibilityEvent( 76 GetWidget()->NotifyAccessibilityEvent(
77 this, ui::AccessibilityTypes::EVENT_VALUE_CHANGED, true); 77 this, ui::AccessibilityTypes::EVENT_VALUE_CHANGED, true);
78 } 78 }
79 } 79 }
80 80
81 void Slider::MoveButtonTo(const gfx::Point& point) {
82 gfx::Insets inset = GetInsets();
83 if (orientation_ == HORIZONTAL) {
84 int amount = base::i18n::IsRTL() ? width() - inset.left() - point.x() :
85 point.x() - inset.left();
86 SetValueInternal(static_cast<float>(amount) / (width() - inset.width()),
87 VALUE_CHANGED_BY_USER);
88 } else {
89 SetValueInternal(1.0f - static_cast<float>(point.y()) / height(),
90 VALUE_CHANGED_BY_USER);
91 }
92 }
93
81 void Slider::SetAccessibleName(const string16& name) { 94 void Slider::SetAccessibleName(const string16& name) {
82 accessible_name_ = name; 95 accessible_name_ = name;
83 } 96 }
84 97
85 gfx::Size Slider::GetPreferredSize() { 98 gfx::Size Slider::GetPreferredSize() {
86 const int kSizeMajor = 200; 99 const int kSizeMajor = 200;
87 const int kSizeMinor = 40; 100 const int kSizeMinor = 40;
88 101
89 if (orientation_ == HORIZONTAL) 102 if (orientation_ == HORIZONTAL)
90 return gfx::Size(std::max(width(), kSizeMajor), kSizeMinor); 103 return gfx::Size(std::max(width(), kSizeMajor), kSizeMinor);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 paint.setStyle(SkPaint::kFill_Style); 151 paint.setStyle(SkPaint::kFill_Style);
139 paint.setAntiAlias(true); 152 paint.setAntiAlias(true);
140 paint.setColor(kButtonColor); 153 paint.setColor(kButtonColor);
141 canvas->sk_canvas()->drawCircle(button_cx, button_cy, kButtonRadius, paint); 154 canvas->sk_canvas()->drawCircle(button_cx, button_cy, kButtonRadius, paint);
142 View::OnPaint(canvas); 155 View::OnPaint(canvas);
143 } 156 }
144 157
145 bool Slider::OnMousePressed(const views::MouseEvent& event) { 158 bool Slider::OnMousePressed(const views::MouseEvent& event) {
146 if (listener_) 159 if (listener_)
147 listener_->SliderDragStarted(this); 160 listener_->SliderDragStarted(this);
148 return OnMouseDragged(event); 161 MoveButtonTo(event.location());
162 return true;
149 } 163 }
150 164
151 bool Slider::OnMouseDragged(const views::MouseEvent& event) { 165 bool Slider::OnMouseDragged(const views::MouseEvent& event) {
152 gfx::Insets inset = GetInsets(); 166 MoveButtonTo(event.location());
153 if (orientation_ == HORIZONTAL) {
154 int amount = base::i18n::IsRTL() ? width() - inset.left() - event.x() :
155 event.x() - inset.left();
156 SetValueInternal(static_cast<float>(amount) / (width() - inset.width()),
157 VALUE_CHANGED_BY_USER);
158 } else {
159 SetValueInternal(1.0f - static_cast<float>(event.y()) / height(),
160 VALUE_CHANGED_BY_USER);
161 }
162 return true; 167 return true;
163 } 168 }
164 169
165 void Slider::OnMouseReleased(const views::MouseEvent& event) { 170 void Slider::OnMouseReleased(const views::MouseEvent& event) {
166 if (listener_) 171 if (listener_)
167 listener_->SliderDragEnded(this); 172 listener_->SliderDragEnded(this);
168 } 173 }
169 174
170 bool Slider::OnKeyPressed(const views::KeyEvent& event) { 175 bool Slider::OnKeyPressed(const views::KeyEvent& event) {
171 if (orientation_ == HORIZONTAL) { 176 if (orientation_ == HORIZONTAL) {
172 if (event.key_code() == ui::VKEY_LEFT) { 177 if (event.key_code() == ui::VKEY_LEFT) {
173 SetValueInternal(value_ - keyboard_increment_, VALUE_CHANGED_BY_USER); 178 SetValueInternal(value_ - keyboard_increment_, VALUE_CHANGED_BY_USER);
174 return true; 179 return true;
175 } else if (event.key_code() == ui::VKEY_RIGHT) { 180 } else if (event.key_code() == ui::VKEY_RIGHT) {
176 SetValueInternal(value_ + keyboard_increment_, VALUE_CHANGED_BY_USER); 181 SetValueInternal(value_ + keyboard_increment_, VALUE_CHANGED_BY_USER);
177 return true; 182 return true;
178 } 183 }
179 } else { 184 } else {
180 if (event.key_code() == ui::VKEY_DOWN) { 185 if (event.key_code() == ui::VKEY_DOWN) {
181 SetValueInternal(value_ - keyboard_increment_, VALUE_CHANGED_BY_USER); 186 SetValueInternal(value_ - keyboard_increment_, VALUE_CHANGED_BY_USER);
182 return true; 187 return true;
183 } else if (event.key_code() == ui::VKEY_UP) { 188 } else if (event.key_code() == ui::VKEY_UP) {
184 SetValueInternal(value_ + keyboard_increment_, VALUE_CHANGED_BY_USER); 189 SetValueInternal(value_ + keyboard_increment_, VALUE_CHANGED_BY_USER);
185 return true; 190 return true;
186 } 191 }
187 } 192 }
188 return false; 193 return false;
189 } 194 }
190 195
196 ui::GestureStatus Slider::OnGestureEvent(const views::GestureEvent& event) {
197 if (event.type() == ui::ET_GESTURE_SCROLL_UPDATE ||
198 event.type() == ui::ET_GESTURE_TAP_DOWN) {
199 MoveButtonTo(event.location());
200 return ui::GESTURE_STATUS_CONSUMED;
201 }
202 return ui::GESTURE_STATUS_UNKNOWN;
203 }
204
191 void Slider::AnimationProgressed(const ui::Animation* animation) { 205 void Slider::AnimationProgressed(const ui::Animation* animation) {
192 animating_value_ = animation->CurrentValueBetween(animating_value_, value_); 206 animating_value_ = animation->CurrentValueBetween(animating_value_, value_);
193 SchedulePaint(); 207 SchedulePaint();
194 } 208 }
195 209
196 void Slider::GetAccessibleState(ui::AccessibleViewState* state) { 210 void Slider::GetAccessibleState(ui::AccessibleViewState* state) {
197 state->role = ui::AccessibilityTypes::ROLE_SLIDER; 211 state->role = ui::AccessibilityTypes::ROLE_SLIDER;
198 state->name = accessible_name_; 212 state->name = accessible_name_;
199 state->value = UTF8ToUTF16( 213 state->value = UTF8ToUTF16(
200 base::StringPrintf("%d%%", (int)(value_ * 100 + 0.5))); 214 base::StringPrintf("%d%%", (int)(value_ * 100 + 0.5)));
201 } 215 }
202 216
203 } // namespace views 217 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/slider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698