This source file includes following definitions.
- visible_to_total_length_ratio_
- PushPropertiesTo
- PushScrollClipPropertiesTo
- ToScrollbarLayer
- SetScrollLayerById
- SetClipLayerById
- ScrollbarLayerRectToContentRect
- SetCurrentPos
- SetMaximum
- SetVerticalAdjust
- SetVisibleToTotalLengthRatio
- SetThumbThicknessScaleFactor
- ComputeThumbQuadRect
- ScrollbarParametersDidChange
#include "cc/layers/scrollbar_layer_impl_base.h"
#include <algorithm>
#include "cc/trees/layer_tree_impl.h"
#include "ui/gfx/rect_conversions.h"
namespace cc {
ScrollbarLayerImplBase::ScrollbarLayerImplBase(
LayerTreeImpl* tree_impl,
int id,
ScrollbarOrientation orientation,
bool is_left_side_vertical_scrollbar,
bool is_overlay)
: LayerImpl(tree_impl, id),
scroll_layer_(NULL),
clip_layer_(NULL),
is_overlay_scrollbar_(is_overlay),
thumb_thickness_scale_factor_(1.f),
current_pos_(0.f),
maximum_(0),
orientation_(orientation),
is_left_side_vertical_scrollbar_(is_left_side_vertical_scrollbar),
vertical_adjust_(0.f),
visible_to_total_length_ratio_(1.f) {}
ScrollbarLayerImplBase::~ScrollbarLayerImplBase() {}
void ScrollbarLayerImplBase::PushPropertiesTo(LayerImpl* layer) {
LayerImpl::PushPropertiesTo(layer);
DCHECK(layer->ToScrollbarLayer());
layer->ToScrollbarLayer()->set_is_overlay_scrollbar(is_overlay_scrollbar_);
PushScrollClipPropertiesTo(layer);
}
void ScrollbarLayerImplBase::PushScrollClipPropertiesTo(LayerImpl* layer) {
DCHECK(layer->ToScrollbarLayer());
layer->ToScrollbarLayer()->SetScrollLayerById(ScrollLayerId());
layer->ToScrollbarLayer()->SetClipLayerById(ClipLayerId());
}
ScrollbarLayerImplBase* ScrollbarLayerImplBase::ToScrollbarLayer() {
return this;
}
void ScrollbarLayerImplBase::SetScrollLayerById(int id) {
LayerImpl* scroll_layer = layer_tree_impl()->LayerById(id);
if (scroll_layer_ == scroll_layer)
return;
if (scroll_layer_)
scroll_layer_->RemoveScrollbar(this);
scroll_layer_ = scroll_layer;
if (scroll_layer_)
scroll_layer_->AddScrollbar(this);
}
void ScrollbarLayerImplBase::SetClipLayerById(int id) {
LayerImpl* clip_layer = layer_tree_impl()->LayerById(id);
if (clip_layer_ == clip_layer)
return;
if (clip_layer_)
clip_layer_->RemoveScrollbar(this);
clip_layer_ = clip_layer;
if (clip_layer_)
clip_layer_->AddScrollbar(this);
}
gfx::Rect ScrollbarLayerImplBase::ScrollbarLayerRectToContentRect(
const gfx::RectF& layer_rect) const {
gfx::RectF content_rect = gfx::ScaleRect(layer_rect,
contents_scale_x(),
contents_scale_y());
return gfx::ToEnclosingRect(content_rect);
}
void ScrollbarLayerImplBase::SetCurrentPos(float current_pos) {
if (current_pos_ == current_pos)
return;
current_pos_ = current_pos;
NoteLayerPropertyChanged();
}
void ScrollbarLayerImplBase::SetMaximum(int maximum) {
if (maximum_ == maximum)
return;
maximum_ = maximum;
NoteLayerPropertyChanged();
}
void ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust) {
if (vertical_adjust_ == vertical_adjust)
return;
vertical_adjust_ = vertical_adjust;
NoteLayerPropertyChanged();
}
void ScrollbarLayerImplBase::SetVisibleToTotalLengthRatio(float ratio) {
if (!IsThumbResizable())
return;
if (visible_to_total_length_ratio_ == ratio)
return;
visible_to_total_length_ratio_ = ratio;
NoteLayerPropertyChanged();
}
void ScrollbarLayerImplBase::SetThumbThicknessScaleFactor(float factor) {
if (thumb_thickness_scale_factor_ == factor)
return;
thumb_thickness_scale_factor_ = factor;
NoteLayerPropertyChanged();
}
gfx::Rect ScrollbarLayerImplBase::ComputeThumbQuadRect() const {
float track_length = TrackLength();
int thumb_length = ThumbLength();
int thumb_thickness = ThumbThickness();
float clamped_current_pos =
std::min(std::max(current_pos_, 0.f), static_cast<float>(maximum_));
float ratio = clamped_current_pos / maximum_;
float max_offset = track_length - thumb_length;
int thumb_offset = static_cast<int>(ratio * max_offset) + TrackStart();
float thumb_thickness_adjustment =
thumb_thickness * (1.f - thumb_thickness_scale_factor_);
gfx::RectF thumb_rect;
if (orientation_ == HORIZONTAL) {
thumb_rect = gfx::RectF(thumb_offset,
vertical_adjust_ + thumb_thickness_adjustment,
thumb_length,
thumb_thickness - thumb_thickness_adjustment);
} else {
thumb_rect = gfx::RectF(
is_left_side_vertical_scrollbar_
? bounds().width() - thumb_thickness
: thumb_thickness_adjustment,
thumb_offset,
thumb_thickness - thumb_thickness_adjustment,
thumb_length);
}
return ScrollbarLayerRectToContentRect(thumb_rect);
}
void ScrollbarLayerImplBase::ScrollbarParametersDidChange() {
if (!clip_layer_ || !scroll_layer_)
return;
scroll_layer_->SetScrollbarPosition(this, clip_layer_);
}
}