0

Use gfx::RangeF for SnapVisibleRange

Since range.h includes windows.h, which pollutes the namespace, we have
to create SnapVisibleRange class in scroll_snap_data.h. Now that
windows.h has been removed, we should use gfx::RangeF for
scroll_snap_data.

Bug: 898975
Change-Id: If86417f2d32f40f4560fe54ac1c1388e883518de
Reviewed-on: https://chromium-review.googlesource.com/c/1308094
Reviewed-by: David Bokan <bokan@chromium.org>
Commit-Queue: Sandra Sun <sunyunjia@chromium.org>
Cr-Commit-Position: refs/heads/master@{#604307}
This commit is contained in:
Sandra Sun
2018-10-31 17:12:32 +00:00
committed by Commit Bot
parent 054a68b4b7
commit df4d1eaff5
2 changed files with 20 additions and 40 deletions

@ -12,8 +12,8 @@ namespace cc {
namespace {
bool IsMutualVisible(const SnapSearchResult& a, const SnapSearchResult& b) {
return a.visible_range().Contains(b.snap_offset()) &&
b.visible_range().Contains(a.snap_offset());
return a.visible_range().Contains(gfx::RangeF(b.snap_offset())) &&
b.visible_range().Contains(gfx::RangeF(a.snap_offset()));
}
bool SnappableOnAxis(const SnapAreaData& area, SearchAxis search_axis) {
@ -32,32 +32,26 @@ void SetOrUpdateResult(const SnapSearchResult& candidate,
} // namespace
bool SnapVisibleRange::Contains(float value) const {
if (start_ < end_)
return start_ <= value && value <= end_;
return end_ <= value && value <= start_;
}
SnapSearchResult::SnapSearchResult(float offset, const SnapVisibleRange& range)
SnapSearchResult::SnapSearchResult(float offset, const gfx::RangeF& range)
: snap_offset_(offset) {
set_visible_range(range);
}
void SnapSearchResult::set_visible_range(const SnapVisibleRange& range) {
void SnapSearchResult::set_visible_range(const gfx::RangeF& range) {
DCHECK(range.start() <= range.end());
visible_range_ = range;
}
void SnapSearchResult::Clip(float max_snap, float max_visible) {
snap_offset_ = std::max(std::min(snap_offset_, max_snap), 0.0f);
visible_range_ = SnapVisibleRange(
std::max(std::min(visible_range_.start(), max_visible), 0.0f),
std::max(std::min(visible_range_.end(), max_visible), 0.0f));
visible_range_ =
gfx::RangeF(std::max(std::min(visible_range_.start(), max_visible), 0.0f),
std::max(std::min(visible_range_.end(), max_visible), 0.0f));
}
void SnapSearchResult::Union(const SnapSearchResult& other) {
DCHECK(snap_offset_ == other.snap_offset_);
visible_range_ = SnapVisibleRange(
visible_range_ = gfx::RangeF(
std::min(visible_range_.start(), other.visible_range_.start()),
std::max(visible_range_.end(), other.visible_range_.end()));
}
@ -115,13 +109,13 @@ bool SnapContainerData::FindSnapPosition(
// Start from current position in the cross axis and assume it's always
// visible.
SnapSearchResult initial_snap_position_y = {
base_position.y(), SnapVisibleRange(0, max_position_.x())};
base_position.y(), gfx::RangeF(0, max_position_.x())};
closest_x =
FindClosestValidArea(SearchAxis::kX, strategy, initial_snap_position_y);
}
if (should_snap_on_y) {
SnapSearchResult initial_snap_position_x = {
base_position.x(), SnapVisibleRange(0, max_position_.y())};
base_position.x(), gfx::RangeF(0, max_position_.y())};
closest_y =
FindClosestValidArea(SearchAxis::kY, strategy, initial_snap_position_x);
}
@ -249,8 +243,8 @@ SnapSearchResult SnapContainerData::GetSnapSearchResult(
const SnapAreaData& area) const {
SnapSearchResult result;
if (axis == SearchAxis::kX) {
result.set_visible_range(SnapVisibleRange(area.rect.y() - rect_.bottom(),
area.rect.bottom() - rect_.y()));
result.set_visible_range(gfx::RangeF(area.rect.y() - rect_.bottom(),
area.rect.bottom() - rect_.y()));
// https://www.w3.org/TR/css-scroll-snap-1/#scroll-snap-align
switch (area.scroll_snap_align.alignment_inline) {
case SnapAlignment::kStart:
@ -268,8 +262,8 @@ SnapSearchResult SnapContainerData::GetSnapSearchResult(
}
result.Clip(max_position_.x(), max_position_.y());
} else {
result.set_visible_range(SnapVisibleRange(area.rect.x() - rect_.right(),
area.rect.right() - rect_.x()));
result.set_visible_range(gfx::RangeF(area.rect.x() - rect_.right(),
area.rect.right() - rect_.x()));
switch (area.scroll_snap_align.alignment_block) {
case SnapAlignment::kStart:
result.set_snap_offset(area.rect.y() - rect_.y());

@ -11,6 +11,7 @@
#include "cc/cc_export.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/scroll_offset.h"
#include "ui/gfx/range/range_f.h"
namespace cc {
@ -87,28 +88,13 @@ struct ScrollSnapAlign {
SnapAlignment alignment_inline;
};
// TODO(sunyunjia): Use gfx::RangeF as windows.h has already been removed from
// range.h.
class SnapVisibleRange {
public:
SnapVisibleRange() {}
SnapVisibleRange(float start, float end) : start_(start), end_(end) {}
bool Contains(float value) const;
float start() const { return start_; }
float end() const { return end_; }
private:
float start_;
float end_;
};
// This class includes snap offset and visible range needed to perform a snap
// operation on one axis for a specific area. The data can be used to determine
// whether this snap area provides a valid snap position for the current scroll.
class SnapSearchResult {
public:
SnapSearchResult() {}
SnapSearchResult(float offset, const SnapVisibleRange& range);
SnapSearchResult(float offset, const gfx::RangeF& range);
// Clips the |snap_offset| between 0 and |max_snap|. And clips the
// |visible_range| between 0 and |max_visible|.
void Clip(float max_snap, float max_visible);
@ -120,15 +106,15 @@ class SnapSearchResult {
float snap_offset() const { return snap_offset_; }
void set_snap_offset(float offset) { snap_offset_ = offset; }
SnapVisibleRange visible_range() const { return visible_range_; }
void set_visible_range(const SnapVisibleRange& range);
gfx::RangeF visible_range() const { return visible_range_; }
void set_visible_range(const gfx::RangeF& range);
private:
float snap_offset_;
// This is the range on the cross axis, within which the SnapArea generating
// this |snap_offset| is visible. We expect the range to be in order (as
// opposed to reversed), i.e., start < end.
SnapVisibleRange visible_range_;
// opposed to reversed), i.e., start() < end().
gfx::RangeF visible_range_;
};
// Snap area is a bounding box that could be snapped to when a scroll happens in