1 // Copyright 2016 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 5 #ifndef UI_GFX_GEOMETRY_MOJO_GEOMETRY_STRUCT_TRAITS_H_ 6 #define UI_GFX_GEOMETRY_MOJO_GEOMETRY_STRUCT_TRAITS_H_ 7 8 #include "ui/gfx/geometry/insets.h" 9 #include "ui/gfx/geometry/insets_f.h" 10 #include "ui/gfx/geometry/mojo/geometry.mojom-shared.h" 11 #include "ui/gfx/geometry/point.h" 12 #include "ui/gfx/geometry/point_f.h" 13 #include "ui/gfx/geometry/rect.h" 14 #include "ui/gfx/geometry/rect_f.h" 15 #include "ui/gfx/geometry/scroll_offset.h" 16 #include "ui/gfx/geometry/size.h" 17 #include "ui/gfx/geometry/size_f.h" 18 #include "ui/gfx/geometry/vector2d.h" 19 #include "ui/gfx/geometry/vector2d_f.h" 20 21 namespace mojo { 22 23 template <> 24 struct StructTraits<gfx::mojom::InsetsDataView, gfx::Insets> { 25 static int top(const gfx::Insets& p) { return p.top(); } 26 static int left(const gfx::Insets& p) { return p.left(); } 27 static int bottom(const gfx::Insets& p) { return p.bottom(); } 28 static int right(const gfx::Insets& p) { return p.right(); } 29 static bool Read(gfx::mojom::InsetsDataView data, gfx::Insets* out) { 30 out->Set(data.top(), data.left(), data.bottom(), data.right()); 31 return true; 32 } 33 }; 34 35 template <> 36 struct StructTraits<gfx::mojom::InsetsFDataView, gfx::InsetsF> { 37 static float top(const gfx::InsetsF& p) { return p.top(); } 38 static float left(const gfx::InsetsF& p) { return p.left(); } 39 static float bottom(const gfx::InsetsF& p) { return p.bottom(); } 40 static float right(const gfx::InsetsF& p) { return p.right(); } 41 static bool Read(gfx::mojom::InsetsFDataView data, gfx::InsetsF* out) { 42 out->Set(data.top(), data.left(), data.bottom(), data.right()); 43 return true; 44 } 45 }; 46 47 template <> 48 struct StructTraits<gfx::mojom::PointDataView, gfx::Point> { 49 static int x(const gfx::Point& p) { return p.x(); } 50 static int y(const gfx::Point& p) { return p.y(); } 51 static bool Read(gfx::mojom::PointDataView data, gfx::Point* out) { 52 out->SetPoint(data.x(), data.y()); 53 return true; 54 } 55 }; 56 57 template <> 58 struct StructTraits<gfx::mojom::PointFDataView, gfx::PointF> { 59 static float x(const gfx::PointF& p) { return p.x(); } 60 static float y(const gfx::PointF& p) { return p.y(); } 61 static bool Read(gfx::mojom::PointFDataView data, gfx::PointF* out) { 62 out->SetPoint(data.x(), data.y()); 63 return true; 64 } 65 }; 66 67 template <> 68 struct StructTraits<gfx::mojom::RectDataView, gfx::Rect> { 69 static int x(const gfx::Rect& p) { return p.x(); } 70 static int y(const gfx::Rect& p) { return p.y(); } 71 static int width(const gfx::Rect& p) { return p.width(); } 72 static int height(const gfx::Rect& p) { return p.height(); } 73 static bool Read(gfx::mojom::RectDataView data, gfx::Rect* out) { 74 if (data.width() < 0 || data.height() < 0) 75 return false; 76 77 out->SetRect(data.x(), data.y(), data.width(), data.height()); 78 return true; 79 } 80 }; 81 82 template <> 83 struct StructTraits<gfx::mojom::RectFDataView, gfx::RectF> { 84 static float x(const gfx::RectF& p) { return p.x(); } 85 static float y(const gfx::RectF& p) { return p.y(); } 86 static float width(const gfx::RectF& p) { return p.width(); } 87 static float height(const gfx::RectF& p) { return p.height(); } 88 static bool Read(gfx::mojom::RectFDataView data, gfx::RectF* out) { 89 if (data.width() < 0 || data.height() < 0) 90 return false; 91 92 out->SetRect(data.x(), data.y(), data.width(), data.height()); 93 return true; 94 } 95 }; 96 97 template <> 98 struct StructTraits<gfx::mojom::SizeDataView, gfx::Size> { 99 static int width(const gfx::Size& p) { return p.width(); } 100 static int height(const gfx::Size& p) { return p.height(); } 101 static bool Read(gfx::mojom::SizeDataView data, gfx::Size* out) { 102 if (data.width() < 0 || data.height() < 0) 103 return false; 104 105 out->SetSize(data.width(), data.height()); 106 return true; 107 } 108 }; 109 110 template <> 111 struct StructTraits<gfx::mojom::SizeFDataView, gfx::SizeF> { 112 static float width(const gfx::SizeF& p) { return p.width(); } 113 static float height(const gfx::SizeF& p) { return p.height(); } 114 static bool Read(gfx::mojom::SizeFDataView data, gfx::SizeF* out) { 115 if (data.width() < 0 || data.height() < 0) 116 return false; 117 118 out->SetSize(data.width(), data.height()); 119 return true; 120 } 121 }; 122 123 template <> 124 struct StructTraits<gfx::mojom::Vector2dDataView, gfx::Vector2d> { 125 static int x(const gfx::Vector2d& v) { return v.x(); } 126 static int y(const gfx::Vector2d& v) { return v.y(); } 127 static bool Read(gfx::mojom::Vector2dDataView data, gfx::Vector2d* out) { 128 out->set_x(data.x()); 129 out->set_y(data.y()); 130 return true; 131 } 132 }; 133 134 template <> 135 struct StructTraits<gfx::mojom::Vector2dFDataView, gfx::Vector2dF> { 136 static float x(const gfx::Vector2dF& v) { return v.x(); } 137 static float y(const gfx::Vector2dF& v) { return v.y(); } 138 static bool Read(gfx::mojom::Vector2dFDataView data, gfx::Vector2dF* out) { 139 out->set_x(data.x()); 140 out->set_y(data.y()); 141 return true; 142 } 143 }; 144 145 template <> 146 struct StructTraits<gfx::mojom::ScrollOffsetDataView, gfx::ScrollOffset> { 147 static float x(const gfx::ScrollOffset& v) { return v.x(); } 148 static float y(const gfx::ScrollOffset& v) { return v.y(); } 149 static bool Read(gfx::mojom::ScrollOffsetDataView data, 150 gfx::ScrollOffset* out) { 151 out->set_x(data.x()); 152 out->set_y(data.y()); 153 return true; 154 } 155 }; 156 157 } // namespace mojo 158 159 #endif // UI_GFX_GEOMETRY_MOJO_GEOMETRY_STRUCT_TRAITS_H_ 160