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 visible_to_total_length_ratio_(1.f), |
| 22 vertical_adjust_(0.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 |
| 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 gfx::Rect ScrollbarLayerImplBase::ComputeThumbQuadRect() const { |
| 43 // Thumb extent is the length of the thumb in the scrolling direction, thumb |
| 44 // thickness is in the perpendicular direction. Here's an example of a |
| 45 // horizontal scrollbar - inputs are above the scrollbar, computed values |
| 46 // below: |
| 47 // |
| 48 // |<------------------- track_length_ ------------------->| |
| 49 // |
| 50 // |--| <-- start_offset |
| 51 // |
| 52 // +--+----------------------------+------------------+-------+--+ |
| 53 // |<|| |##################| ||>| |
| 54 // +--+----------------------------+------------------+-------+--+ |
| 55 // |
| 56 // |<- thumb_length ->| |
| 57 // |
| 58 // |<------- thumb_offset -------->| |
| 59 // |
| 60 // For painted, scrollbars, the length is fixed. For solid color scrollbars we |
| 61 // have to compute it. The ratio of the thumb's length to the track's length |
| 62 // is the same as that of the visible viewport to the total viewport, unless |
| 63 // that would make the thumb's length less than its thickness. |
| 64 // |
| 65 // vertical_adjust_ is used when the layer geometry from the main thread is |
| 66 // not in sync with what the user sees. For instance on Android scrolling the |
| 67 // top bar controls out of view reveals more of the page content. We want the |
| 68 // root layer scrollbars to reflect what the user sees even if we haven't |
| 69 // received new layer geometry from the main thread. If the user has scrolled |
| 70 // down by 50px and the initial viewport size was 950px the geometry would |
| 71 // look something like this: |
| 72 // |
| 73 // vertical_adjust_ = 50, scroll position 0, visible ratios 99% |
| 74 // Layer geometry: Desired thumb positions: |
| 75 // +--------------------+-+ +----------------------+ <-- 0px |
| 76 // | |v| | #| |
| 77 // | |e| | #| |
| 78 // | |r| | #| |
| 79 // | |t| | #| |
| 80 // | |i| | #| |
| 81 // | |c| | #| |
| 82 // | |a| | #| |
| 83 // | |l| | #| |
| 84 // | | | | #| |
| 85 // | |l| | #| |
| 86 // | |a| | #| |
| 87 // | |y| | #| |
| 88 // | |e| | #| |
| 89 // | |r| | #| |
| 90 // +--------------------+-+ | #| |
| 91 // | horizontal layer | | | #| |
| 92 // +--------------------+-+ | #| <-- 950px |
| 93 // | | | #| |
| 94 // | | |##################### | |
| 95 // +----------------------+ +----------------------+ <-- 1000px |
| 96 // |
| 97 // The layer geometry is set up for a 950px tall viewport, but the user can |
| 98 // actually see down to 1000px. Thus we have to move the quad for the |
| 99 // horizontal scrollbar down by the vertical_adjust_ factor and lay the |
| 100 // vertical thumb out on a track lengthed by the vertical_adjust_ factor. This |
| 101 // means the quads may extend outside the layer's bounds. |
| 102 |
| 103 // With the length known, we can compute the thumb's position. |
| 104 float track_length = TrackLength(); |
| 105 int thumb_length = ThumbLength(); |
| 106 int thumb_thickness = ThumbThickness(); |
| 107 |
| 108 float ratio = current_pos_ / maximum_; |
| 109 float max_offset = track_length - thumb_length; |
| 110 int thumb_offset = static_cast<int>(ratio * max_offset) + TrackStart(); |
| 111 |
| 112 gfx::RectF thumb_rect; |
| 113 if (orientation() == HORIZONTAL) { |
| 114 thumb_rect = gfx::RectF(thumb_offset, vertical_adjust_, |
| 115 thumb_length, thumb_thickness); |
| 116 } else { |
| 117 thumb_rect = gfx::RectF(0.f, thumb_offset, |
| 118 thumb_thickness, thumb_length); |
| 119 } |
| 120 |
| 121 return ScrollbarLayerRectToContentRect(thumb_rect); |
| 122 } |
| 123 |
| 124 } // namespace cc |
OLD | NEW |