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