OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "cc/layers/scrollbar_layer_impl_base.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include "cc/layers/layer.h" |
| 9 #include "ui/gfx/rect_conversions.h" |
| 10 |
| 11 namespace cc { |
| 12 |
| 13 ScrollbarLayerImplBase::ScrollbarLayerImplBase(LayerTreeImpl* tree_impl, |
| 14 int id, |
| 15 ScrollbarOrientation orientation) |
| 16 : LayerImpl(tree_impl, id), |
| 17 scroll_layer_id_(Layer::INVALID_ID), |
| 18 is_overlay_scrollbar_(false), |
| 19 current_pos_(0.f), |
| 20 maximum_(0), |
| 21 orientation_(orientation), |
| 22 vertical_adjust_(0.f), |
| 23 visible_to_total_length_ratio_(1.f) {} |
| 24 |
| 25 void ScrollbarLayerImplBase::PushPropertiesTo(LayerImpl* layer) { |
| 26 LayerImpl::PushPropertiesTo(layer); |
| 27 } |
| 28 |
| 29 ScrollbarLayerImplBase* ScrollbarLayerImplBase::ToScrollbarLayer() { |
| 30 return this; |
| 31 } |
| 32 |
| 33 gfx::Rect ScrollbarLayerImplBase::ScrollbarLayerRectToContentRect( |
| 34 gfx::RectF layer_rect) const { |
| 35 // Don't intersect with the bounds as in LayerRectToContentRect() because |
| 36 // layer_rect here might be in coordinates of the containing layer. |
| 37 gfx::RectF content_rect = gfx::ScaleRect(layer_rect, |
| 38 contents_scale_x(), |
| 39 contents_scale_y()); |
| 40 return gfx::ToEnclosingRect(content_rect); |
| 41 } |
| 42 |
| 43 void ScrollbarLayerImplBase::SetCurrentPos(float current_pos) { |
| 44 if (current_pos_ == current_pos) |
| 45 return; |
| 46 current_pos_ = current_pos; |
| 47 NoteLayerPropertyChanged(); |
| 48 } |
| 49 |
| 50 void ScrollbarLayerImplBase::SetMaximum(int maximum) { |
| 51 if (maximum_ == maximum) |
| 52 return; |
| 53 maximum_ = maximum; |
| 54 NoteLayerPropertyChanged(); |
| 55 } |
| 56 |
| 57 void ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust) { |
| 58 if (vertical_adjust_ == vertical_adjust) |
| 59 return; |
| 60 vertical_adjust_ = vertical_adjust; |
| 61 NoteLayerPropertyChanged(); |
| 62 } |
| 63 |
| 64 void ScrollbarLayerImplBase::SetVisibleToTotalLengthRatio(float ratio) { |
| 65 if (visible_to_total_length_ratio_ == ratio) |
| 66 return; |
| 67 visible_to_total_length_ratio_ = ratio; |
| 68 NoteLayerPropertyChanged(); |
| 69 } |
| 70 |
| 71 gfx::Rect ScrollbarLayerImplBase::ComputeThumbQuadRect() const { |
| 72 // Thumb extent is the length of the thumb in the scrolling direction, thumb |
| 73 // thickness is in the perpendicular direction. Here's an example of a |
| 74 // horizontal scrollbar - inputs are above the scrollbar, computed values |
| 75 // below: |
| 76 // |
| 77 // |<------------------- track_length_ ------------------->| |
| 78 // |
| 79 // |--| <-- start_offset |
| 80 // |
| 81 // +--+----------------------------+------------------+-------+--+ |
| 82 // |<|| |##################| ||>| |
| 83 // +--+----------------------------+------------------+-------+--+ |
| 84 // |
| 85 // |<- thumb_length ->| |
| 86 // |
| 87 // |<------- thumb_offset -------->| |
| 88 // |
| 89 // For painted, scrollbars, the length is fixed. For solid color scrollbars we |
| 90 // have to compute it. The ratio of the thumb's length to the track's length |
| 91 // is the same as that of the visible viewport to the total viewport, unless |
| 92 // that would make the thumb's length less than its thickness. |
| 93 // |
| 94 // vertical_adjust_ is used when the layer geometry from the main thread is |
| 95 // not in sync with what the user sees. For instance on Android scrolling the |
| 96 // top bar controls out of view reveals more of the page content. We want the |
| 97 // root layer scrollbars to reflect what the user sees even if we haven't |
| 98 // received new layer geometry from the main thread. If the user has scrolled |
| 99 // down by 50px and the initial viewport size was 950px the geometry would |
| 100 // look something like this: |
| 101 // |
| 102 // vertical_adjust_ = 50, scroll position 0, visible ratios 99% |
| 103 // Layer geometry: Desired thumb positions: |
| 104 // +--------------------+-+ +----------------------+ <-- 0px |
| 105 // | |v| | #| |
| 106 // | |e| | #| |
| 107 // | |r| | #| |
| 108 // | |t| | #| |
| 109 // | |i| | #| |
| 110 // | |c| | #| |
| 111 // | |a| | #| |
| 112 // | |l| | #| |
| 113 // | | | | #| |
| 114 // | |l| | #| |
| 115 // | |a| | #| |
| 116 // | |y| | #| |
| 117 // | |e| | #| |
| 118 // | |r| | #| |
| 119 // +--------------------+-+ | #| |
| 120 // | horizontal layer | | | #| |
| 121 // +--------------------+-+ | #| <-- 950px |
| 122 // | | | #| |
| 123 // | | |##################### | |
| 124 // +----------------------+ +----------------------+ <-- 1000px |
| 125 // |
| 126 // The layer geometry is set up for a 950px tall viewport, but the user can |
| 127 // actually see down to 1000px. Thus we have to move the quad for the |
| 128 // horizontal scrollbar down by the vertical_adjust_ factor and lay the |
| 129 // vertical thumb out on a track lengthed by the vertical_adjust_ factor. This |
| 130 // means the quads may extend outside the layer's bounds. |
| 131 |
| 132 // With the length known, we can compute the thumb's position. |
| 133 float track_length = TrackLength(); |
| 134 int thumb_length = ThumbLength(); |
| 135 int thumb_thickness = ThumbThickness(); |
| 136 |
| 137 // With the length known, we can compute the thumb's position. |
| 138 float clamped_current_pos = |
| 139 std::min(std::max(current_pos_, 0.f), static_cast<float>(maximum_)); |
| 140 float ratio = clamped_current_pos / maximum_; |
| 141 float max_offset = track_length - thumb_length; |
| 142 int thumb_offset = static_cast<int>(ratio * max_offset) + TrackStart(); |
| 143 |
| 144 gfx::RectF thumb_rect; |
| 145 if (orientation() == HORIZONTAL) { |
| 146 thumb_rect = gfx::RectF(thumb_offset, vertical_adjust_, |
| 147 thumb_length, thumb_thickness); |
| 148 } else { |
| 149 thumb_rect = gfx::RectF(0.f, thumb_offset, |
| 150 thumb_thickness, thumb_length); |
| 151 } |
| 152 |
| 153 return ScrollbarLayerRectToContentRect(thumb_rect); |
| 154 } |
| 155 |
| 156 } // namespace cc |
OLD | NEW |