Remove "keyboard" from keyboard strings.
[chromium-blink-merge.git] / cc / layers / scrollbar_layer_impl_base.cc
blob978e8b8bb0e8eb5b81ba9432cca0d8969564ff4a
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.
5 #include "cc/layers/scrollbar_layer_impl_base.h"
7 #include <algorithm>
8 #include "cc/trees/layer_tree_impl.h"
9 #include "ui/gfx/rect_conversions.h"
11 namespace cc {
13 ScrollbarLayerImplBase::ScrollbarLayerImplBase(
14 LayerTreeImpl* tree_impl,
15 int id,
16 ScrollbarOrientation orientation,
17 bool is_left_side_vertical_scrollbar,
18 bool is_overlay)
19 : LayerImpl(tree_impl, id),
20 scroll_layer_(NULL),
21 clip_layer_(NULL),
22 is_overlay_scrollbar_(is_overlay),
23 thumb_thickness_scale_factor_(1.f),
24 current_pos_(0.f),
25 maximum_(0),
26 orientation_(orientation),
27 is_left_side_vertical_scrollbar_(is_left_side_vertical_scrollbar),
28 vertical_adjust_(0.f),
29 visible_to_total_length_ratio_(1.f) {}
31 ScrollbarLayerImplBase::~ScrollbarLayerImplBase() {}
33 void ScrollbarLayerImplBase::PushPropertiesTo(LayerImpl* layer) {
34 LayerImpl::PushPropertiesTo(layer);
35 DCHECK(layer->ToScrollbarLayer());
36 layer->ToScrollbarLayer()->set_is_overlay_scrollbar(is_overlay_scrollbar_);
37 PushScrollClipPropertiesTo(layer);
40 void ScrollbarLayerImplBase::PushScrollClipPropertiesTo(LayerImpl* layer) {
41 DCHECK(layer->ToScrollbarLayer());
42 layer->ToScrollbarLayer()->SetScrollLayerById(ScrollLayerId());
43 layer->ToScrollbarLayer()->SetClipLayerById(ClipLayerId());
46 ScrollbarLayerImplBase* ScrollbarLayerImplBase::ToScrollbarLayer() {
47 return this;
50 void ScrollbarLayerImplBase::SetScrollLayerById(int id) {
51 LayerImpl* scroll_layer = layer_tree_impl()->LayerById(id);
52 if (scroll_layer_ == scroll_layer)
53 return;
55 if (scroll_layer_)
56 scroll_layer_->RemoveScrollbar(this);
57 scroll_layer_ = scroll_layer;
58 if (scroll_layer_)
59 scroll_layer_->AddScrollbar(this);
62 void ScrollbarLayerImplBase::SetClipLayerById(int id) {
63 LayerImpl* clip_layer = layer_tree_impl()->LayerById(id);
64 if (clip_layer_ == clip_layer)
65 return;
67 if (clip_layer_)
68 clip_layer_->RemoveScrollbar(this);
69 clip_layer_ = clip_layer;
70 if (clip_layer_)
71 clip_layer_->AddScrollbar(this);
74 gfx::Rect ScrollbarLayerImplBase::ScrollbarLayerRectToContentRect(
75 const gfx::RectF& layer_rect) const {
76 // Don't intersect with the bounds as in LayerRectToContentRect() because
77 // layer_rect here might be in coordinates of the containing layer.
78 gfx::RectF content_rect = gfx::ScaleRect(layer_rect,
79 contents_scale_x(),
80 contents_scale_y());
81 return gfx::ToEnclosingRect(content_rect);
84 void ScrollbarLayerImplBase::SetCurrentPos(float current_pos) {
85 if (current_pos_ == current_pos)
86 return;
87 current_pos_ = current_pos;
88 NoteLayerPropertyChanged();
91 void ScrollbarLayerImplBase::SetMaximum(int maximum) {
92 if (maximum_ == maximum)
93 return;
94 maximum_ = maximum;
95 NoteLayerPropertyChanged();
98 void ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust) {
99 if (vertical_adjust_ == vertical_adjust)
100 return;
101 vertical_adjust_ = vertical_adjust;
102 NoteLayerPropertyChanged();
105 void ScrollbarLayerImplBase::SetVisibleToTotalLengthRatio(float ratio) {
106 if (!IsThumbResizable())
107 return;
109 if (visible_to_total_length_ratio_ == ratio)
110 return;
111 visible_to_total_length_ratio_ = ratio;
112 NoteLayerPropertyChanged();
115 void ScrollbarLayerImplBase::SetThumbThicknessScaleFactor(float factor) {
116 if (thumb_thickness_scale_factor_ == factor)
117 return;
118 thumb_thickness_scale_factor_ = factor;
119 NoteLayerPropertyChanged();
122 gfx::Rect ScrollbarLayerImplBase::ComputeThumbQuadRect() const {
123 // Thumb extent is the length of the thumb in the scrolling direction, thumb
124 // thickness is in the perpendicular direction. Here's an example of a
125 // horizontal scrollbar - inputs are above the scrollbar, computed values
126 // below:
128 // |<------------------- track_length_ ------------------->|
130 // |--| <-- start_offset
132 // +--+----------------------------+------------------+-------+--+
133 // |<|| |##################| ||>|
134 // +--+----------------------------+------------------+-------+--+
136 // |<- thumb_length ->|
138 // |<------- thumb_offset -------->|
140 // For painted, scrollbars, the length is fixed. For solid color scrollbars we
141 // have to compute it. The ratio of the thumb's length to the track's length
142 // is the same as that of the visible viewport to the total viewport, unless
143 // that would make the thumb's length less than its thickness.
145 // vertical_adjust_ is used when the layer geometry from the main thread is
146 // not in sync with what the user sees. For instance on Android scrolling the
147 // top bar controls out of view reveals more of the page content. We want the
148 // root layer scrollbars to reflect what the user sees even if we haven't
149 // received new layer geometry from the main thread. If the user has scrolled
150 // down by 50px and the initial viewport size was 950px the geometry would
151 // look something like this:
153 // vertical_adjust_ = 50, scroll position 0, visible ratios 99%
154 // Layer geometry: Desired thumb positions:
155 // +--------------------+-+ +----------------------+ <-- 0px
156 // | |v| | #|
157 // | |e| | #|
158 // | |r| | #|
159 // | |t| | #|
160 // | |i| | #|
161 // | |c| | #|
162 // | |a| | #|
163 // | |l| | #|
164 // | | | | #|
165 // | |l| | #|
166 // | |a| | #|
167 // | |y| | #|
168 // | |e| | #|
169 // | |r| | #|
170 // +--------------------+-+ | #|
171 // | horizontal layer | | | #|
172 // +--------------------+-+ | #| <-- 950px
173 // | | | #|
174 // | | |##################### |
175 // +----------------------+ +----------------------+ <-- 1000px
177 // The layer geometry is set up for a 950px tall viewport, but the user can
178 // actually see down to 1000px. Thus we have to move the quad for the
179 // horizontal scrollbar down by the vertical_adjust_ factor and lay the
180 // vertical thumb out on a track lengthed by the vertical_adjust_ factor. This
181 // means the quads may extend outside the layer's bounds.
183 // With the length known, we can compute the thumb's position.
184 float track_length = TrackLength();
185 int thumb_length = ThumbLength();
186 int thumb_thickness = ThumbThickness();
188 // With the length known, we can compute the thumb's position.
189 float clamped_current_pos =
190 std::min(std::max(current_pos_, 0.f), static_cast<float>(maximum_));
191 float ratio = clamped_current_pos / maximum_;
192 float max_offset = track_length - thumb_length;
193 int thumb_offset = static_cast<int>(ratio * max_offset) + TrackStart();
195 float thumb_thickness_adjustment =
196 thumb_thickness * (1.f - thumb_thickness_scale_factor_);
198 gfx::RectF thumb_rect;
199 if (orientation_ == HORIZONTAL) {
200 thumb_rect = gfx::RectF(thumb_offset,
201 vertical_adjust_ + thumb_thickness_adjustment,
202 thumb_length,
203 thumb_thickness - thumb_thickness_adjustment);
204 } else {
205 thumb_rect = gfx::RectF(
206 is_left_side_vertical_scrollbar_
207 ? bounds().width() - thumb_thickness
208 : thumb_thickness_adjustment,
209 thumb_offset,
210 thumb_thickness - thumb_thickness_adjustment,
211 thumb_length);
214 return ScrollbarLayerRectToContentRect(thumb_rect);
217 void ScrollbarLayerImplBase::ScrollbarParametersDidChange() {
218 if (!clip_layer_ || !scroll_layer_)
219 return;
221 scroll_layer_->SetScrollbarPosition(this, clip_layer_);
224 } // namespace cc