scroll_utils.cc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2020 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. #include "cc/input/scroll_utils.h"
  5. #include <algorithm>
  6. #include "ui/gfx/geometry/size_f.h"
  7. #include "ui/gfx/geometry/vector2d_f.h"
  8. namespace cc {
  9. // static
  10. gfx::Vector2dF ScrollUtils::ResolveScrollPercentageToPixels(
  11. const gfx::Vector2dF& delta,
  12. const gfx::SizeF& scroller,
  13. const gfx::SizeF& viewport) {
  14. // Work with unsigned values and keep sign information in sign_x / sign_y.
  15. float sign_x = std::signbit(delta.x()) ? -1 : 1;
  16. float sign_y = std::signbit(delta.y()) ? -1 : 1;
  17. float delta_x = std::abs(delta.x());
  18. float delta_y = std::abs(delta.y());
  19. // Resolve and clamp horizontal scroll
  20. if (delta_x > 0)
  21. delta_x = delta_x * std::min(scroller.width(), viewport.width());
  22. // Resolve and clamps vertical scroll.
  23. if (delta_y > 0)
  24. delta_y = delta_y * std::min(scroller.height(), viewport.height());
  25. return gfx::Vector2dF(std::copysign(delta_x, sign_x),
  26. std::copysign(delta_y, sign_y));
  27. }
  28. gfx::Vector2dF ScrollUtils::ResolvePixelScrollToPercentageForTesting(
  29. const gfx::Vector2dF& delta,
  30. const gfx::SizeF& scroller,
  31. const gfx::SizeF& viewport) {
  32. float delta_x = delta.x() / std::min(scroller.width(), viewport.width());
  33. float delta_y = delta.y() / std::min(scroller.height(), viewport.height());
  34. return gfx::Vector2dF(delta_x, delta_y);
  35. }
  36. } // namespace cc