[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 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> |
[email protected] | adeda57 | 2014-01-31 00:49:47 | [diff] [blame] | 8 | #include "cc/trees/layer_tree_impl.h" |
heejin.r.chung | d28506ba | 2014-10-23 16:36:20 | [diff] [blame^] | 9 | #include "ui/gfx/geometry/rect_conversions.h" |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 10 | |
| 11 | namespace cc { |
| 12 | |
[email protected] | 2ea5aba | 2013-09-11 14:26:56 | [diff] [blame] | 13 | ScrollbarLayerImplBase::ScrollbarLayerImplBase( |
| 14 | LayerTreeImpl* tree_impl, |
| 15 | int id, |
| 16 | ScrollbarOrientation orientation, |
[email protected] | adeda57 | 2014-01-31 00:49:47 | [diff] [blame] | 17 | bool is_left_side_vertical_scrollbar, |
| 18 | bool is_overlay) |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 19 | : LayerImpl(tree_impl, id), |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 20 | scroll_layer_(nullptr), |
| 21 | clip_layer_(nullptr), |
[email protected] | adeda57 | 2014-01-31 00:49:47 | [diff] [blame] | 22 | is_overlay_scrollbar_(is_overlay), |
[email protected] | 2ea5aba | 2013-09-11 14:26:56 | [diff] [blame] | 23 | thumb_thickness_scale_factor_(1.f), |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 24 | current_pos_(0.f), |
| 25 | maximum_(0), |
| 26 | orientation_(orientation), |
[email protected] | 2ea5aba | 2013-09-11 14:26:56 | [diff] [blame] | 27 | is_left_side_vertical_scrollbar_(is_left_side_vertical_scrollbar), |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 28 | vertical_adjust_(0.f), |
kulkarni.a | 4015690f1 | 2014-10-10 13:50:06 | [diff] [blame] | 29 | visible_to_total_length_ratio_(1.f) { |
| 30 | } |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 31 | |
[email protected] | adeda57 | 2014-01-31 00:49:47 | [diff] [blame] | 32 | ScrollbarLayerImplBase::~ScrollbarLayerImplBase() {} |
| 33 | |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 34 | void ScrollbarLayerImplBase::PushPropertiesTo(LayerImpl* layer) { |
[email protected] | 930ff43b | 2014-05-02 05:24:00 | [diff] [blame] | 35 | float active_opacity = layer->opacity(); |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 36 | LayerImpl::PushPropertiesTo(layer); |
[email protected] | 930ff43b | 2014-05-02 05:24:00 | [diff] [blame] | 37 | layer->SetOpacity(active_opacity); |
[email protected] | adeda57 | 2014-01-31 00:49:47 | [diff] [blame] | 38 | DCHECK(layer->ToScrollbarLayer()); |
| 39 | layer->ToScrollbarLayer()->set_is_overlay_scrollbar(is_overlay_scrollbar_); |
| 40 | PushScrollClipPropertiesTo(layer); |
| 41 | } |
| 42 | |
| 43 | void ScrollbarLayerImplBase::PushScrollClipPropertiesTo(LayerImpl* layer) { |
| 44 | DCHECK(layer->ToScrollbarLayer()); |
[email protected] | 9942a73 | 2014-06-27 03:58:57 | [diff] [blame] | 45 | layer->ToScrollbarLayer()->SetScrollLayerAndClipLayerByIds(ScrollLayerId(), |
| 46 | ClipLayerId()); |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | ScrollbarLayerImplBase* ScrollbarLayerImplBase::ToScrollbarLayer() { |
| 50 | return this; |
| 51 | } |
| 52 | |
[email protected] | fe739df | 2014-04-28 22:25:18 | [diff] [blame] | 53 | namespace { |
| 54 | |
| 55 | typedef void (LayerImpl::*ScrollbarRegistrationOperation)( |
| 56 | ScrollbarLayerImplBase*); |
| 57 | |
| 58 | void RegisterScrollbarWithLayers(ScrollbarLayerImplBase* scrollbar, |
| 59 | LayerImpl* container_layer, |
| 60 | LayerImpl* scroll_layer, |
| 61 | ScrollbarRegistrationOperation operation) { |
| 62 | if (!container_layer || !scroll_layer) |
| 63 | return; |
| 64 | |
| 65 | DCHECK(scrollbar); |
| 66 | |
| 67 | // Scrollbars must be notifed of changes to their scroll and container layers |
| 68 | // and all scrollable layers in between. |
| 69 | for (LayerImpl* current_layer = scroll_layer; |
| 70 | current_layer && current_layer != container_layer->parent(); |
| 71 | current_layer = current_layer->parent()) { |
| 72 | // TODO(wjmaclean) We shouldn't need to exempt the scroll_layer from the |
| 73 | // scrollable() test below. https://crbug.com/367858. |
| 74 | if (current_layer->scrollable() || current_layer == container_layer || |
| 75 | current_layer == scroll_layer) |
| 76 | (current_layer->*operation)(scrollbar); |
| 77 | } |
| 78 | } |
| 79 | } // namespace |
| 80 | |
[email protected] | 9942a73 | 2014-06-27 03:58:57 | [diff] [blame] | 81 | void ScrollbarLayerImplBase::SetScrollLayerAndClipLayerByIds( |
| 82 | int scroll_layer_id, |
| 83 | int clip_layer_id) { |
| 84 | LayerImpl* scroll_layer = layer_tree_impl()->LayerById(scroll_layer_id); |
| 85 | LayerImpl* clip_layer = layer_tree_impl()->LayerById(clip_layer_id); |
| 86 | if (scroll_layer_ == scroll_layer && clip_layer_ == clip_layer) |
[email protected] | adeda57 | 2014-01-31 00:49:47 | [diff] [blame] | 87 | return; |
| 88 | |
[email protected] | fe739df | 2014-04-28 22:25:18 | [diff] [blame] | 89 | RegisterScrollbarWithLayers( |
| 90 | this, clip_layer_, scroll_layer_, &LayerImpl::RemoveScrollbar); |
[email protected] | adeda57 | 2014-01-31 00:49:47 | [diff] [blame] | 91 | scroll_layer_ = scroll_layer; |
[email protected] | adeda57 | 2014-01-31 00:49:47 | [diff] [blame] | 92 | clip_layer_ = clip_layer; |
[email protected] | fe739df | 2014-04-28 22:25:18 | [diff] [blame] | 93 | RegisterScrollbarWithLayers( |
| 94 | this, clip_layer_, scroll_layer_, &LayerImpl::AddScrollbar); |
[email protected] | 9942a73 | 2014-06-27 03:58:57 | [diff] [blame] | 95 | |
sataya.m | 07f11a8 | 2014-10-07 14:29:18 | [diff] [blame] | 96 | ScrollbarParametersDidChange(false); |
[email protected] | adeda57 | 2014-01-31 00:49:47 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 99 | gfx::Rect ScrollbarLayerImplBase::ScrollbarLayerRectToContentRect( |
[email protected] | 0023fc7 | 2014-01-10 20:05:06 | [diff] [blame] | 100 | const gfx::RectF& layer_rect) const { |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 101 | // Don't intersect with the bounds as in LayerRectToContentRect() because |
| 102 | // layer_rect here might be in coordinates of the containing layer. |
| 103 | gfx::RectF content_rect = gfx::ScaleRect(layer_rect, |
| 104 | contents_scale_x(), |
| 105 | contents_scale_y()); |
| 106 | return gfx::ToEnclosingRect(content_rect); |
| 107 | } |
| 108 | |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 109 | bool ScrollbarLayerImplBase::SetCurrentPos(float current_pos) { |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 110 | if (current_pos_ == current_pos) |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 111 | return false; |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 112 | current_pos_ = current_pos; |
| 113 | NoteLayerPropertyChanged(); |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 114 | return true; |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 115 | } |
| 116 | |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 117 | bool ScrollbarLayerImplBase::SetMaximum(int maximum) { |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 118 | if (maximum_ == maximum) |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 119 | return false; |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 120 | maximum_ = maximum; |
| 121 | NoteLayerPropertyChanged(); |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 122 | return true; |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 123 | } |
| 124 | |
timav | 87a47f5 | 2014-10-16 01:09:56 | [diff] [blame] | 125 | bool ScrollbarLayerImplBase::CanScrollOrientation() const { |
timav | 3ca69693 | 2014-10-15 02:19:04 | [diff] [blame] | 126 | if (!scroll_layer_) |
| 127 | return false; |
| 128 | return scroll_layer_->user_scrollable(orientation()) && (0 < maximum()); |
| 129 | } |
| 130 | |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 131 | bool ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust) { |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 132 | if (vertical_adjust_ == vertical_adjust) |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 133 | return false; |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 134 | vertical_adjust_ = vertical_adjust; |
| 135 | NoteLayerPropertyChanged(); |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 136 | return true; |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 137 | } |
| 138 | |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 139 | bool ScrollbarLayerImplBase::SetVisibleToTotalLengthRatio(float ratio) { |
[email protected] | 41bee9f0 | 2014-01-09 01:11:35 | [diff] [blame] | 140 | if (!IsThumbResizable()) |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 141 | return false; |
[email protected] | 41bee9f0 | 2014-01-09 01:11:35 | [diff] [blame] | 142 | |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 143 | if (visible_to_total_length_ratio_ == ratio) |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 144 | return false; |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 145 | visible_to_total_length_ratio_ = ratio; |
| 146 | NoteLayerPropertyChanged(); |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 147 | return true; |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 148 | } |
| 149 | |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 150 | bool ScrollbarLayerImplBase::SetThumbThicknessScaleFactor(float factor) { |
[email protected] | 534236b | 2013-10-25 21:19:20 | [diff] [blame] | 151 | if (thumb_thickness_scale_factor_ == factor) |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 152 | return false; |
[email protected] | 534236b | 2013-10-25 21:19:20 | [diff] [blame] | 153 | thumb_thickness_scale_factor_ = factor; |
| 154 | NoteLayerPropertyChanged(); |
sataya.m | 68d7d9b4 | 2014-09-22 16:01:59 | [diff] [blame] | 155 | return true; |
[email protected] | 534236b | 2013-10-25 21:19:20 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 158 | gfx::Rect ScrollbarLayerImplBase::ComputeThumbQuadRect() const { |
| 159 | // Thumb extent is the length of the thumb in the scrolling direction, thumb |
| 160 | // thickness is in the perpendicular direction. Here's an example of a |
| 161 | // horizontal scrollbar - inputs are above the scrollbar, computed values |
| 162 | // below: |
| 163 | // |
| 164 | // |<------------------- track_length_ ------------------->| |
| 165 | // |
| 166 | // |--| <-- start_offset |
| 167 | // |
| 168 | // +--+----------------------------+------------------+-------+--+ |
| 169 | // |<|| |##################| ||>| |
| 170 | // +--+----------------------------+------------------+-------+--+ |
| 171 | // |
| 172 | // |<- thumb_length ->| |
| 173 | // |
| 174 | // |<------- thumb_offset -------->| |
| 175 | // |
| 176 | // For painted, scrollbars, the length is fixed. For solid color scrollbars we |
| 177 | // have to compute it. The ratio of the thumb's length to the track's length |
| 178 | // is the same as that of the visible viewport to the total viewport, unless |
| 179 | // that would make the thumb's length less than its thickness. |
| 180 | // |
| 181 | // vertical_adjust_ is used when the layer geometry from the main thread is |
| 182 | // not in sync with what the user sees. For instance on Android scrolling the |
| 183 | // top bar controls out of view reveals more of the page content. We want the |
| 184 | // root layer scrollbars to reflect what the user sees even if we haven't |
| 185 | // received new layer geometry from the main thread. If the user has scrolled |
| 186 | // down by 50px and the initial viewport size was 950px the geometry would |
| 187 | // look something like this: |
| 188 | // |
| 189 | // vertical_adjust_ = 50, scroll position 0, visible ratios 99% |
| 190 | // Layer geometry: Desired thumb positions: |
| 191 | // +--------------------+-+ +----------------------+ <-- 0px |
| 192 | // | |v| | #| |
| 193 | // | |e| | #| |
| 194 | // | |r| | #| |
| 195 | // | |t| | #| |
| 196 | // | |i| | #| |
| 197 | // | |c| | #| |
| 198 | // | |a| | #| |
| 199 | // | |l| | #| |
| 200 | // | | | | #| |
| 201 | // | |l| | #| |
| 202 | // | |a| | #| |
| 203 | // | |y| | #| |
| 204 | // | |e| | #| |
| 205 | // | |r| | #| |
| 206 | // +--------------------+-+ | #| |
| 207 | // | horizontal layer | | | #| |
| 208 | // +--------------------+-+ | #| <-- 950px |
| 209 | // | | | #| |
| 210 | // | | |##################### | |
| 211 | // +----------------------+ +----------------------+ <-- 1000px |
| 212 | // |
| 213 | // The layer geometry is set up for a 950px tall viewport, but the user can |
| 214 | // actually see down to 1000px. Thus we have to move the quad for the |
| 215 | // horizontal scrollbar down by the vertical_adjust_ factor and lay the |
| 216 | // vertical thumb out on a track lengthed by the vertical_adjust_ factor. This |
| 217 | // means the quads may extend outside the layer's bounds. |
| 218 | |
| 219 | // With the length known, we can compute the thumb's position. |
| 220 | float track_length = TrackLength(); |
| 221 | int thumb_length = ThumbLength(); |
| 222 | int thumb_thickness = ThumbThickness(); |
| 223 | |
| 224 | // With the length known, we can compute the thumb's position. |
| 225 | float clamped_current_pos = |
| 226 | std::min(std::max(current_pos_, 0.f), static_cast<float>(maximum_)); |
[email protected] | 443898f5 | 2014-07-22 21:16:18 | [diff] [blame] | 227 | |
| 228 | int thumb_offset = TrackStart(); |
| 229 | if (maximum_ > 0) { |
| 230 | float ratio = clamped_current_pos / maximum_; |
| 231 | float max_offset = track_length - thumb_length; |
| 232 | thumb_offset += static_cast<int>(ratio * max_offset); |
| 233 | } |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 234 | |
[email protected] | 2ea5aba | 2013-09-11 14:26:56 | [diff] [blame] | 235 | float thumb_thickness_adjustment = |
| 236 | thumb_thickness * (1.f - thumb_thickness_scale_factor_); |
| 237 | |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 238 | gfx::RectF thumb_rect; |
[email protected] | 2ea5aba | 2013-09-11 14:26:56 | [diff] [blame] | 239 | if (orientation_ == HORIZONTAL) { |
| 240 | thumb_rect = gfx::RectF(thumb_offset, |
| 241 | vertical_adjust_ + thumb_thickness_adjustment, |
| 242 | thumb_length, |
| 243 | thumb_thickness - thumb_thickness_adjustment); |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 244 | } else { |
[email protected] | 2ea5aba | 2013-09-11 14:26:56 | [diff] [blame] | 245 | thumb_rect = gfx::RectF( |
| 246 | is_left_side_vertical_scrollbar_ |
| 247 | ? bounds().width() - thumb_thickness |
| 248 | : thumb_thickness_adjustment, |
| 249 | thumb_offset, |
| 250 | thumb_thickness - thumb_thickness_adjustment, |
| 251 | thumb_length); |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | return ScrollbarLayerRectToContentRect(thumb_rect); |
| 255 | } |
| 256 | |
sataya.m | 07f11a8 | 2014-10-07 14:29:18 | [diff] [blame] | 257 | void ScrollbarLayerImplBase::ScrollbarParametersDidChange(bool on_resize) { |
[email protected] | adeda57 | 2014-01-31 00:49:47 | [diff] [blame] | 258 | if (!clip_layer_ || !scroll_layer_) |
| 259 | return; |
| 260 | |
sataya.m | 07f11a8 | 2014-10-07 14:29:18 | [diff] [blame] | 261 | scroll_layer_->SetScrollbarPosition(this, clip_layer_, on_resize); |
[email protected] | adeda57 | 2014-01-31 00:49:47 | [diff] [blame] | 262 | } |
| 263 | |
[email protected] | 80413d7 | 2013-08-30 20:25:33 | [diff] [blame] | 264 | } // namespace cc |