| 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/scrollbar/base_scroll_bar.h" | 5 #include "ui/views/controls/scrollbar/base_scroll_bar.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 /////////////////////////////////////////////////////////////////////////////// | 32 /////////////////////////////////////////////////////////////////////////////// |
| 33 // BaseScrollBar, public: | 33 // BaseScrollBar, public: |
| 34 | 34 |
| 35 BaseScrollBar::BaseScrollBar(bool horizontal, BaseScrollBarThumb* thumb) | 35 BaseScrollBar::BaseScrollBar(bool horizontal, BaseScrollBarThumb* thumb) |
| 36 : ScrollBar(horizontal), | 36 : ScrollBar(horizontal), |
| 37 thumb_(thumb), | 37 thumb_(thumb), |
| 38 contents_size_(0), | 38 contents_size_(0), |
| 39 contents_scroll_offset_(0), | 39 contents_scroll_offset_(0), |
| 40 viewport_size_(0), | 40 viewport_size_(0), |
| 41 thumb_track_state_(CustomButton::STATE_NORMAL), | |
| 42 last_scroll_amount_(SCROLL_NONE), | 41 last_scroll_amount_(SCROLL_NONE), |
| 43 repeater_(base::Bind(&BaseScrollBar::TrackClicked, | 42 repeater_(base::Bind(&BaseScrollBar::TrackClicked, |
| 44 base::Unretained(this))), | 43 base::Unretained(this))), |
| 45 context_menu_mouse_position_(0) { | 44 context_menu_mouse_position_(0) { |
| 46 AddChildView(thumb_); | 45 AddChildView(thumb_); |
| 47 | 46 |
| 48 set_context_menu_controller(this); | 47 set_context_menu_controller(this); |
| 49 thumb_->set_context_menu_controller(this); | 48 thumb_->set_context_menu_controller(this); |
| 50 } | 49 } |
| 51 | 50 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 } else if (contents_scroll_offset_ > GetMaxPosition()) { | 104 } else if (contents_scroll_offset_ > GetMaxPosition()) { |
| 106 contents_scroll_offset_ = GetMaxPosition(); | 105 contents_scroll_offset_ = GetMaxPosition(); |
| 107 } | 106 } |
| 108 if (old_offset == contents_scroll_offset_) | 107 if (old_offset == contents_scroll_offset_) |
| 109 return false; | 108 return false; |
| 110 | 109 |
| 111 ScrollContentsToOffset(); | 110 ScrollContentsToOffset(); |
| 112 return true; | 111 return true; |
| 113 } | 112 } |
| 114 | 113 |
| 115 void BaseScrollBar::OnThumbStateChanged(CustomButton::ButtonState old_state, | |
| 116 CustomButton::ButtonState new_state) { | |
| 117 if (old_state == CustomButton::STATE_PRESSED && | |
| 118 new_state == CustomButton::STATE_NORMAL && | |
| 119 GetThumbTrackState() == CustomButton::STATE_HOVERED) { | |
| 120 SetThumbTrackState(CustomButton::STATE_NORMAL); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 /////////////////////////////////////////////////////////////////////////////// | 114 /////////////////////////////////////////////////////////////////////////////// |
| 125 // BaseScrollBar, View implementation: | 115 // BaseScrollBar, View implementation: |
| 126 | 116 |
| 127 bool BaseScrollBar::OnMousePressed(const ui::MouseEvent& event) { | 117 bool BaseScrollBar::OnMousePressed(const ui::MouseEvent& event) { |
| 128 if (event.IsOnlyLeftMouseButton()) | 118 if (event.IsOnlyLeftMouseButton()) |
| 129 ProcessPressEvent(event); | 119 ProcessPressEvent(event); |
| 130 return true; | 120 return true; |
| 131 } | 121 } |
| 132 | 122 |
| 133 void BaseScrollBar::OnMouseReleased(const ui::MouseEvent& event) { | 123 void BaseScrollBar::OnMouseReleased(const ui::MouseEvent& event) { |
| 134 SetState(HitTestPoint(event.location()) ? | 124 repeater_.Stop(); |
| 135 CustomButton::STATE_HOVERED : CustomButton::STATE_NORMAL); | |
| 136 } | 125 } |
| 137 | 126 |
| 138 void BaseScrollBar::OnMouseCaptureLost() { | 127 void BaseScrollBar::OnMouseCaptureLost() { |
| 139 SetState(CustomButton::STATE_NORMAL); | 128 repeater_.Stop(); |
| 140 } | |
| 141 | |
| 142 void BaseScrollBar::OnMouseEntered(const ui::MouseEvent& event) { | |
| 143 SetThumbTrackState(CustomButton::STATE_HOVERED); | |
| 144 } | |
| 145 | |
| 146 void BaseScrollBar::OnMouseExited(const ui::MouseEvent& event) { | |
| 147 if (GetThumbTrackState() == CustomButton::STATE_HOVERED) | |
| 148 SetState(CustomButton::STATE_NORMAL); | |
| 149 } | 129 } |
| 150 | 130 |
| 151 bool BaseScrollBar::OnKeyPressed(const ui::KeyEvent& event) { | 131 bool BaseScrollBar::OnKeyPressed(const ui::KeyEvent& event) { |
| 152 ScrollAmount amount = SCROLL_NONE; | 132 ScrollAmount amount = SCROLL_NONE; |
| 153 switch (event.key_code()) { | 133 switch (event.key_code()) { |
| 154 case ui::VKEY_UP: | 134 case ui::VKEY_UP: |
| 155 if (!IsHorizontal()) | 135 if (!IsHorizontal()) |
| 156 amount = SCROLL_PREV_LINE; | 136 amount = SCROLL_PREV_LINE; |
| 157 break; | 137 break; |
| 158 case ui::VKEY_DOWN: | 138 case ui::VKEY_DOWN: |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 event->SetHandled(); | 189 event->SetHandled(); |
| 210 return; | 190 return; |
| 211 } | 191 } |
| 212 | 192 |
| 213 if (event->type() == ui::ET_GESTURE_LONG_PRESS) { | 193 if (event->type() == ui::ET_GESTURE_LONG_PRESS) { |
| 214 // For a long-press, the repeater started in tap-down should continue. So | 194 // For a long-press, the repeater started in tap-down should continue. So |
| 215 // return early. | 195 // return early. |
| 216 return; | 196 return; |
| 217 } | 197 } |
| 218 | 198 |
| 219 SetState(CustomButton::STATE_NORMAL); | 199 repeater_.Stop(); |
| 220 | 200 |
| 221 if (event->type() == ui::ET_GESTURE_TAP) { | 201 if (event->type() == ui::ET_GESTURE_TAP) { |
| 222 // TAP_DOWN would have already scrolled some amount. So scrolling again on | 202 // TAP_DOWN would have already scrolled some amount. So scrolling again on |
| 223 // TAP is not necessary. | 203 // TAP is not necessary. |
| 224 event->SetHandled(); | 204 event->SetHandled(); |
| 225 return; | 205 return; |
| 226 } | 206 } |
| 227 | 207 |
| 228 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN || | 208 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN || |
| 229 event->type() == ui::ET_GESTURE_SCROLL_END) { | 209 event->type() == ui::ET_GESTURE_SCROLL_END) { |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 contents_scroll_offset = 0; | 380 contents_scroll_offset = 0; |
| 401 if (contents_scroll_offset > content_size) | 381 if (contents_scroll_offset > content_size) |
| 402 contents_scroll_offset = content_size; | 382 contents_scroll_offset = content_size; |
| 403 contents_scroll_offset_ = contents_scroll_offset; | 383 contents_scroll_offset_ = contents_scroll_offset; |
| 404 | 384 |
| 405 // Thumb Height and Thumb Pos. | 385 // Thumb Height and Thumb Pos. |
| 406 // The height of the thumb is the ratio of the Viewport height to the | 386 // The height of the thumb is the ratio of the Viewport height to the |
| 407 // content size multiplied by the height of the thumb track. | 387 // content size multiplied by the height of the thumb track. |
| 408 double ratio = | 388 double ratio = |
| 409 std::min(1.0, static_cast<double>(viewport_size) / contents_size_); | 389 std::min(1.0, static_cast<double>(viewport_size) / contents_size_); |
| 410 int thumb_size = static_cast<int>(ratio * GetTrackSize()); | 390 thumb_->SetLength(static_cast<int>(ratio * GetTrackSize())); |
| 411 thumb_->SetSize(thumb_size); | |
| 412 | 391 |
| 413 int thumb_position = CalculateThumbPosition(contents_scroll_offset); | 392 int thumb_position = CalculateThumbPosition(contents_scroll_offset); |
| 414 thumb_->SetPosition(thumb_position); | 393 thumb_->SetPosition(thumb_position); |
| 415 } | 394 } |
| 416 | 395 |
| 417 int BaseScrollBar::GetPosition() const { | 396 int BaseScrollBar::GetPosition() const { |
| 418 return thumb_->GetPosition(); | 397 return thumb_->GetPosition(); |
| 419 } | 398 } |
| 420 | 399 |
| 421 /////////////////////////////////////////////////////////////////////////////// | 400 /////////////////////////////////////////////////////////////////////////////// |
| 422 // BaseScrollBar, protected: | 401 // BaseScrollBar, protected: |
| 423 | 402 |
| 424 BaseScrollBarThumb* BaseScrollBar::GetThumb() const { | 403 BaseScrollBarThumb* BaseScrollBar::GetThumb() const { |
| 425 return thumb_; | 404 return thumb_; |
| 426 } | 405 } |
| 427 | 406 |
| 428 CustomButton::ButtonState BaseScrollBar::GetThumbTrackState() const { | |
| 429 return thumb_track_state_; | |
| 430 } | |
| 431 | |
| 432 void BaseScrollBar::ScrollToPosition(int position) { | 407 void BaseScrollBar::ScrollToPosition(int position) { |
| 433 controller()->ScrollToPosition(this, position); | 408 controller()->ScrollToPosition(this, position); |
| 434 } | 409 } |
| 435 | 410 |
| 436 int BaseScrollBar::GetScrollIncrement(bool is_page, bool is_positive) { | 411 int BaseScrollBar::GetScrollIncrement(bool is_page, bool is_positive) { |
| 437 return controller()->GetScrollIncrement(this, is_page, is_positive); | 412 return controller()->GetScrollIncrement(this, is_page, is_positive); |
| 438 } | 413 } |
| 439 | 414 |
| 440 /////////////////////////////////////////////////////////////////////////////// | 415 /////////////////////////////////////////////////////////////////////////////// |
| 441 // BaseScrollBar, private: | 416 // BaseScrollBar, private: |
| 442 | 417 |
| 443 int BaseScrollBar::GetThumbSizeForTest() { | 418 int BaseScrollBar::GetThumbSizeForTest() { |
| 444 return thumb_->GetSize(); | 419 return thumb_->GetSize(); |
| 445 } | 420 } |
| 446 | 421 |
| 447 void BaseScrollBar::ProcessPressEvent(const ui::LocatedEvent& event) { | 422 void BaseScrollBar::ProcessPressEvent(const ui::LocatedEvent& event) { |
| 448 SetThumbTrackState(CustomButton::STATE_PRESSED); | |
| 449 gfx::Rect thumb_bounds = thumb_->bounds(); | 423 gfx::Rect thumb_bounds = thumb_->bounds(); |
| 450 if (IsHorizontal()) { | 424 if (IsHorizontal()) { |
| 451 if (GetMirroredXInView(event.x()) < thumb_bounds.x()) { | 425 if (GetMirroredXInView(event.x()) < thumb_bounds.x()) { |
| 452 last_scroll_amount_ = SCROLL_PREV_PAGE; | 426 last_scroll_amount_ = SCROLL_PREV_PAGE; |
| 453 } else if (GetMirroredXInView(event.x()) > thumb_bounds.right()) { | 427 } else if (GetMirroredXInView(event.x()) > thumb_bounds.right()) { |
| 454 last_scroll_amount_ = SCROLL_NEXT_PAGE; | 428 last_scroll_amount_ = SCROLL_NEXT_PAGE; |
| 455 } | 429 } |
| 456 } else { | 430 } else { |
| 457 if (event.y() < thumb_bounds.y()) { | 431 if (event.y() < thumb_bounds.y()) { |
| 458 last_scroll_amount_ = SCROLL_PREV_PAGE; | 432 last_scroll_amount_ = SCROLL_PREV_PAGE; |
| 459 } else if (event.y() > thumb_bounds.bottom()) { | 433 } else if (event.y() > thumb_bounds.bottom()) { |
| 460 last_scroll_amount_ = SCROLL_NEXT_PAGE; | 434 last_scroll_amount_ = SCROLL_NEXT_PAGE; |
| 461 } | 435 } |
| 462 } | 436 } |
| 463 TrackClicked(); | 437 TrackClicked(); |
| 464 repeater_.Start(); | 438 repeater_.Start(); |
| 465 } | 439 } |
| 466 | 440 |
| 467 void BaseScrollBar::SetState(CustomButton::ButtonState state) { | |
| 468 SetThumbTrackState(state); | |
| 469 repeater_.Stop(); | |
| 470 } | |
| 471 | |
| 472 void BaseScrollBar::TrackClicked() { | 441 void BaseScrollBar::TrackClicked() { |
| 473 if (last_scroll_amount_ != SCROLL_NONE) | 442 if (last_scroll_amount_ != SCROLL_NONE) |
| 474 ScrollByAmount(last_scroll_amount_); | 443 ScrollByAmount(last_scroll_amount_); |
| 475 } | 444 } |
| 476 | 445 |
| 477 void BaseScrollBar::ScrollContentsToOffset() { | 446 void BaseScrollBar::ScrollContentsToOffset() { |
| 478 ScrollToPosition(contents_scroll_offset_); | 447 ScrollToPosition(contents_scroll_offset_); |
| 479 thumb_->SetPosition(CalculateThumbPosition(contents_scroll_offset_)); | 448 thumb_->SetPosition(CalculateThumbPosition(contents_scroll_offset_)); |
| 480 } | 449 } |
| 481 | 450 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 500 int thumb_size = thumb_->GetSize(); | 469 int thumb_size = thumb_->GetSize(); |
| 501 int track_size = GetTrackSize(); | 470 int track_size = GetTrackSize(); |
| 502 if (track_size == thumb_size) | 471 if (track_size == thumb_size) |
| 503 return 0; | 472 return 0; |
| 504 if (scroll_to_middle) | 473 if (scroll_to_middle) |
| 505 thumb_position = thumb_position - (thumb_size / 2); | 474 thumb_position = thumb_position - (thumb_size / 2); |
| 506 return (thumb_position * (contents_size_ - viewport_size_)) / | 475 return (thumb_position * (contents_size_ - viewport_size_)) / |
| 507 (track_size - thumb_size); | 476 (track_size - thumb_size); |
| 508 } | 477 } |
| 509 | 478 |
| 510 void BaseScrollBar::SetThumbTrackState(CustomButton::ButtonState state) { | |
| 511 thumb_track_state_ = state; | |
| 512 SchedulePaint(); | |
| 513 } | |
| 514 | |
| 515 } // namespace views | 479 } // namespace views |
| OLD | NEW |