Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_thumb.h" | 5 #include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h" |
| 6 | 6 |
| 7 #include "ui/gfx/canvas.h" | 7 #include "ui/gfx/canvas.h" |
| 8 #include "ui/gfx/geometry/rect.h" | 8 #include "ui/gfx/geometry/rect.h" |
| 9 #include "ui/views/controls/scrollbar/base_scroll_bar.h" | 9 #include "ui/views/controls/scrollbar/base_scroll_bar.h" |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 // The distance the mouse can be dragged outside the bounds of the thumb during | 12 // The distance the mouse can be dragged outside the bounds of the thumb during |
| 13 // dragging before the scrollbar will snap back to its regular position. | 13 // dragging before the scrollbar will snap back to its regular position. |
| 14 static const int kScrollThumbDragOutSnap = 100; | 14 static const int kScrollThumbDragOutSnap = 100; |
| 15 } | 15 } |
| 16 | 16 |
| 17 namespace views { | 17 namespace views { |
| 18 | 18 |
| 19 BaseScrollBarThumb::BaseScrollBarThumb(BaseScrollBar* scroll_bar) | 19 BaseScrollBarThumb::BaseScrollBarThumb(BaseScrollBar* scroll_bar) |
| 20 : scroll_bar_(scroll_bar), | 20 : scroll_bar_(scroll_bar), |
| 21 drag_start_position_(-1), | 21 drag_start_position_(-1), |
| 22 mouse_offset_(-1), | 22 mouse_offset_(-1), |
| 23 state_(CustomButton::STATE_NORMAL) { | 23 state_(CustomButton::STATE_NORMAL) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 BaseScrollBarThumb::~BaseScrollBarThumb() { | 26 BaseScrollBarThumb::~BaseScrollBarThumb() { |
| 27 } | 27 } |
| 28 | 28 |
| 29 void BaseScrollBarThumb::SetSize(int size) { | 29 void BaseScrollBarThumb::SetLength(int length) { |
| 30 // Make sure the thumb is never sized smaller than its minimum possible | 30 // Make sure the thumb is never sized smaller than its minimum possible |
| 31 // display size. | 31 // display size. |
| 32 gfx::Size prefsize = GetPreferredSize(); | 32 gfx::Size size = GetPreferredSize(); |
| 33 size = std::max(size, scroll_bar_->IsHorizontal() ? prefsize.width() : | 33 if (IsHorizontal()) |
| 34 prefsize.height()); | 34 size.set_width(length); |
| 35 gfx::Rect thumb_bounds = bounds(); | 35 else |
| 36 if (scroll_bar_->IsHorizontal()) { | 36 size.set_height(length); |
| 37 thumb_bounds.set_width(size); | 37 SetSize(size); |
|
Evan Stade
2016/11/10 19:31:40
the changes here are functionally idempotent; it's
| |
| 38 } else { | |
| 39 thumb_bounds.set_height(size); | |
| 40 } | |
| 41 SetBoundsRect(thumb_bounds); | |
| 42 } | 38 } |
| 43 | 39 |
| 44 int BaseScrollBarThumb::GetSize() const { | 40 int BaseScrollBarThumb::GetSize() const { |
| 45 if (scroll_bar_->IsHorizontal()) | 41 if (IsHorizontal()) |
| 46 return width(); | 42 return width(); |
| 47 return height(); | 43 return height(); |
| 48 } | 44 } |
| 49 | 45 |
| 50 void BaseScrollBarThumb::SetPosition(int position) { | 46 void BaseScrollBarThumb::SetPosition(int position) { |
| 51 gfx::Rect thumb_bounds = bounds(); | 47 gfx::Rect thumb_bounds = bounds(); |
| 52 gfx::Rect track_bounds = scroll_bar_->GetTrackBounds(); | 48 gfx::Rect track_bounds = scroll_bar_->GetTrackBounds(); |
| 53 if (scroll_bar_->IsHorizontal()) { | 49 if (IsHorizontal()) { |
| 54 thumb_bounds.set_x(track_bounds.x() + position); | 50 thumb_bounds.set_x(track_bounds.x() + position); |
| 55 } else { | 51 } else { |
| 56 thumb_bounds.set_y(track_bounds.y() + position); | 52 thumb_bounds.set_y(track_bounds.y() + position); |
| 57 } | 53 } |
| 58 SetBoundsRect(thumb_bounds); | 54 SetBoundsRect(thumb_bounds); |
| 59 } | 55 } |
| 60 | 56 |
| 61 int BaseScrollBarThumb::GetPosition() const { | 57 int BaseScrollBarThumb::GetPosition() const { |
| 62 gfx::Rect track_bounds = scroll_bar_->GetTrackBounds(); | 58 gfx::Rect track_bounds = scroll_bar_->GetTrackBounds(); |
| 63 if (scroll_bar_->IsHorizontal()) | 59 if (IsHorizontal()) |
| 64 return x() - track_bounds.x(); | 60 return x() - track_bounds.x(); |
| 65 return y() - track_bounds.y(); | 61 return y() - track_bounds.y(); |
| 66 } | 62 } |
| 67 | 63 |
| 68 void BaseScrollBarThumb::OnMouseEntered(const ui::MouseEvent& event) { | 64 void BaseScrollBarThumb::OnMouseEntered(const ui::MouseEvent& event) { |
| 69 SetState(CustomButton::STATE_HOVERED); | 65 SetState(CustomButton::STATE_HOVERED); |
| 70 } | 66 } |
| 71 | 67 |
| 72 void BaseScrollBarThumb::OnMouseExited(const ui::MouseEvent& event) { | 68 void BaseScrollBarThumb::OnMouseExited(const ui::MouseEvent& event) { |
| 73 SetState(CustomButton::STATE_NORMAL); | 69 SetState(CustomButton::STATE_NORMAL); |
| 74 } | 70 } |
| 75 | 71 |
| 76 bool BaseScrollBarThumb::OnMousePressed(const ui::MouseEvent& event) { | 72 bool BaseScrollBarThumb::OnMousePressed(const ui::MouseEvent& event) { |
| 77 mouse_offset_ = scroll_bar_->IsHorizontal() ? event.x() : event.y(); | 73 mouse_offset_ = IsHorizontal() ? event.x() : event.y(); |
| 78 drag_start_position_ = GetPosition(); | 74 drag_start_position_ = GetPosition(); |
| 79 SetState(CustomButton::STATE_PRESSED); | 75 SetState(CustomButton::STATE_PRESSED); |
| 80 return true; | 76 return true; |
| 81 } | 77 } |
| 82 | 78 |
| 83 bool BaseScrollBarThumb::OnMouseDragged(const ui::MouseEvent& event) { | 79 bool BaseScrollBarThumb::OnMouseDragged(const ui::MouseEvent& event) { |
| 84 // If the user moves the mouse more than |kScrollThumbDragOutSnap| outside | 80 // If the user moves the mouse more than |kScrollThumbDragOutSnap| outside |
| 85 // the bounds of the thumb, the scrollbar will snap the scroll back to the | 81 // the bounds of the thumb, the scrollbar will snap the scroll back to the |
| 86 // point it was at before the drag began. | 82 // point it was at before the drag began. |
| 87 if (scroll_bar_->IsHorizontal()) { | 83 if (IsHorizontal()) { |
| 88 if ((event.y() < y() - kScrollThumbDragOutSnap) || | 84 if ((event.y() < y() - kScrollThumbDragOutSnap) || |
| 89 (event.y() > (y() + height() + kScrollThumbDragOutSnap))) { | 85 (event.y() > (y() + height() + kScrollThumbDragOutSnap))) { |
| 90 scroll_bar_->ScrollToThumbPosition(drag_start_position_, false); | 86 scroll_bar_->ScrollToThumbPosition(drag_start_position_, false); |
| 91 return true; | 87 return true; |
| 92 } | 88 } |
| 93 } else { | 89 } else { |
| 94 if ((event.x() < x() - kScrollThumbDragOutSnap) || | 90 if ((event.x() < x() - kScrollThumbDragOutSnap) || |
| 95 (event.x() > (x() + width() + kScrollThumbDragOutSnap))) { | 91 (event.x() > (x() + width() + kScrollThumbDragOutSnap))) { |
| 96 scroll_bar_->ScrollToThumbPosition(drag_start_position_, false); | 92 scroll_bar_->ScrollToThumbPosition(drag_start_position_, false); |
| 97 return true; | 93 return true; |
| 98 } | 94 } |
| 99 } | 95 } |
| 100 if (scroll_bar_->IsHorizontal()) { | 96 if (IsHorizontal()) { |
| 101 int thumb_x = event.x() - mouse_offset_; | 97 int thumb_x = event.x() - mouse_offset_; |
| 102 if (base::i18n::IsRTL()) | 98 if (base::i18n::IsRTL()) |
| 103 thumb_x *= -1; | 99 thumb_x *= -1; |
| 104 scroll_bar_->ScrollToThumbPosition(GetPosition() + thumb_x, false); | 100 scroll_bar_->ScrollToThumbPosition(GetPosition() + thumb_x, false); |
| 105 } else { | 101 } else { |
| 106 int thumb_y = event.y() - mouse_offset_; | 102 int thumb_y = event.y() - mouse_offset_; |
| 107 scroll_bar_->ScrollToThumbPosition(GetPosition() + thumb_y, false); | 103 scroll_bar_->ScrollToThumbPosition(GetPosition() + thumb_y, false); |
| 108 } | 104 } |
| 109 return true; | 105 return true; |
| 110 } | 106 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 125 void BaseScrollBarThumb::SetState(CustomButton::ButtonState state) { | 121 void BaseScrollBarThumb::SetState(CustomButton::ButtonState state) { |
| 126 if (state_ == state) | 122 if (state_ == state) |
| 127 return; | 123 return; |
| 128 | 124 |
| 129 CustomButton::ButtonState old_state = state_; | 125 CustomButton::ButtonState old_state = state_; |
| 130 state_ = state; | 126 state_ = state; |
| 131 scroll_bar_->OnThumbStateChanged(old_state, state); | 127 scroll_bar_->OnThumbStateChanged(old_state, state); |
| 132 SchedulePaint(); | 128 SchedulePaint(); |
| 133 } | 129 } |
| 134 | 130 |
| 131 bool BaseScrollBarThumb::IsHorizontal() const { | |
| 132 return scroll_bar_->IsHorizontal(); | |
| 133 } | |
| 134 | |
| 135 } // namespace views | 135 } // namespace views |
| OLD | NEW |