ink_drop_util.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2017 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 "ui/views/animation/ink_drop_util.h"
  5. #include <cmath>
  6. #include "base/check_op.h"
  7. #include "ui/gfx/geometry/point3_f.h"
  8. #include "ui/gfx/geometry/transform.h"
  9. #include "ui/gfx/geometry/vector2d_f.h"
  10. #include "ui/native_theme/native_theme.h"
  11. #include "ui/views/view.h"
  12. #include "ui/views/views_features.h"
  13. namespace views {
  14. gfx::Transform GetTransformSubpixelCorrection(const gfx::Transform& transform,
  15. float device_scale_factor) {
  16. gfx::Point3F origin;
  17. transform.TransformPoint(&origin);
  18. const gfx::Vector2dF offset_in_dip = origin.AsPointF().OffsetFromOrigin();
  19. // Scale the origin to screen space
  20. origin.Scale(device_scale_factor);
  21. // Compute the rounded offset in screen space and finally unscale it back to
  22. // DIP space.
  23. gfx::Vector2dF aligned_offset_in_dip = origin.AsPointF().OffsetFromOrigin();
  24. aligned_offset_in_dip.set_x(std::round(aligned_offset_in_dip.x()));
  25. aligned_offset_in_dip.set_y(std::round(aligned_offset_in_dip.y()));
  26. aligned_offset_in_dip.Scale(1.f / device_scale_factor);
  27. // Compute the subpixel offset correction and apply it to the transform.
  28. gfx::Transform subpixel_correction;
  29. subpixel_correction.Translate(aligned_offset_in_dip - offset_in_dip);
  30. #if DCHECK_IS_ON()
  31. const float kEpsilon = 0.0001f;
  32. gfx::Point3F offset;
  33. gfx::Transform transform_corrected(transform);
  34. transform_corrected.ConcatTransform(subpixel_correction);
  35. transform_corrected.TransformPoint(&offset);
  36. offset.Scale(device_scale_factor);
  37. if (!std::isnan(offset.x()))
  38. DCHECK_LT(std::abs(std::round(offset.x()) - offset.x()), kEpsilon);
  39. if (!std::isnan(offset.y()))
  40. DCHECK_LT(std::abs(std::round(offset.y()) - offset.y()), kEpsilon);
  41. #endif
  42. return subpixel_correction;
  43. }
  44. bool UsingPlatformHighContrastInkDrop(const View* view) {
  45. return view->GetWidget() &&
  46. view->GetNativeTheme()->GetDefaultSystemColorScheme() ==
  47. ui::NativeTheme::ColorScheme::kPlatformHighContrast &&
  48. base::FeatureList::IsEnabled(
  49. features::kEnablePlatformHighContrastInkDrop);
  50. }
  51. } // namespace views