insets.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2012 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. #ifndef UI_GFX_GEOMETRY_INSETS_H_
  5. #define UI_GFX_GEOMETRY_INSETS_H_
  6. #include "base/numerics/clamped_math.h"
  7. #include "ui/gfx/geometry/geometry_export.h"
  8. #include "ui/gfx/geometry/insets_f.h"
  9. #include "ui/gfx/geometry/insets_outsets_base.h"
  10. namespace gfx {
  11. class Outsets;
  12. class Vector2d;
  13. // This can be used to represent a space within a rectangle, by "shrinking" the
  14. // rectangle by the inset amount on all four sides. Alternatively, it can
  15. // represent a border that has a different thickness on each side.
  16. class GEOMETRY_EXPORT Insets : public InsetsOutsetsBase<Insets> {
  17. public:
  18. using InsetsOutsetsBase::InsetsOutsetsBase;
  19. // Conversion from Insets to Outsets negates all components.
  20. Outsets ToOutsets() const;
  21. // Adjusts the vertical and horizontal dimensions by the values described in
  22. // |vector|. Offsetting insets before applying to a rectangle would be
  23. // equivalent to offsetting the rectangle then applying the insets.
  24. void Offset(const gfx::Vector2d& vector);
  25. explicit operator InsetsF() const {
  26. return InsetsF()
  27. .set_top(static_cast<float>(top()))
  28. .set_left(static_cast<float>(left()))
  29. .set_bottom(static_cast<float>(bottom()))
  30. .set_right(static_cast<float>(right()));
  31. }
  32. };
  33. inline Insets operator+(Insets lhs, const Insets& rhs) {
  34. lhs += rhs;
  35. return lhs;
  36. }
  37. inline Insets operator-(Insets lhs, const Insets& rhs) {
  38. lhs -= rhs;
  39. return lhs;
  40. }
  41. inline Insets operator+(Insets insets, const gfx::Vector2d& offset) {
  42. insets.Offset(offset);
  43. return insets;
  44. }
  45. // Helper methods to scale a gfx::Insets to a new gfx::Insets.
  46. GEOMETRY_EXPORT Insets ScaleToCeiledInsets(const Insets& insets,
  47. float x_scale,
  48. float y_scale);
  49. GEOMETRY_EXPORT Insets ScaleToCeiledInsets(const Insets& insets, float scale);
  50. GEOMETRY_EXPORT Insets ScaleToFlooredInsets(const Insets& insets,
  51. float x_scale,
  52. float y_scale);
  53. GEOMETRY_EXPORT Insets ScaleToFlooredInsets(const Insets& insets, float scale);
  54. GEOMETRY_EXPORT Insets ScaleToRoundedInsets(const Insets& insets,
  55. float x_scale,
  56. float y_scale);
  57. GEOMETRY_EXPORT Insets ScaleToRoundedInsets(const Insets& insets, float scale);
  58. // This is declared here for use in gtest-based unit tests but is defined in
  59. // the //ui/gfx:test_support target. Depend on that to use this in your unit
  60. // test. This should not be used in production code - call ToString() instead.
  61. void PrintTo(const Insets&, ::std::ostream* os);
  62. } // namespace gfx
  63. #endif // UI_GFX_GEOMETRY_INSETS_H_