gfx_converter.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright (c) 2019 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/gin_converters/gfx_converter.h"
  5. #include "shell/common/gin_helper/dictionary.h"
  6. #include "ui/display/display.h"
  7. #include "ui/display/screen.h"
  8. #include "ui/gfx/geometry/point.h"
  9. #include "ui/gfx/geometry/point_f.h"
  10. #include "ui/gfx/geometry/rect.h"
  11. #include "ui/gfx/geometry/resize_utils.h"
  12. #include "ui/gfx/geometry/size.h"
  13. namespace gin {
  14. v8::Local<v8::Value> Converter<gfx::Point>::ToV8(v8::Isolate* isolate,
  15. const gfx::Point& val) {
  16. gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
  17. dict.SetHidden("simple", true);
  18. dict.Set("x", val.x());
  19. dict.Set("y", val.y());
  20. return dict.GetHandle();
  21. }
  22. bool Converter<gfx::Point>::FromV8(v8::Isolate* isolate,
  23. v8::Local<v8::Value> val,
  24. gfx::Point* out) {
  25. gin::Dictionary dict(isolate);
  26. if (!gin::ConvertFromV8(isolate, val, &dict))
  27. return false;
  28. double x, y;
  29. if (!dict.Get("x", &x) || !dict.Get("y", &y))
  30. return false;
  31. *out = gfx::Point(static_cast<int>(std::round(x)),
  32. static_cast<int>(std::round(y)));
  33. return true;
  34. }
  35. v8::Local<v8::Value> Converter<gfx::PointF>::ToV8(v8::Isolate* isolate,
  36. const gfx::PointF& val) {
  37. gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
  38. dict.SetHidden("simple", true);
  39. dict.Set("x", val.x());
  40. dict.Set("y", val.y());
  41. return dict.GetHandle();
  42. }
  43. bool Converter<gfx::PointF>::FromV8(v8::Isolate* isolate,
  44. v8::Local<v8::Value> val,
  45. gfx::PointF* out) {
  46. gin::Dictionary dict(isolate);
  47. if (!gin::ConvertFromV8(isolate, val, &dict))
  48. return false;
  49. float x, y;
  50. if (!dict.Get("x", &x) || !dict.Get("y", &y))
  51. return false;
  52. *out = gfx::PointF(x, y);
  53. return true;
  54. }
  55. v8::Local<v8::Value> Converter<gfx::Size>::ToV8(v8::Isolate* isolate,
  56. const gfx::Size& val) {
  57. gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
  58. dict.SetHidden("simple", true);
  59. dict.Set("width", val.width());
  60. dict.Set("height", val.height());
  61. return dict.GetHandle();
  62. }
  63. bool Converter<gfx::Size>::FromV8(v8::Isolate* isolate,
  64. v8::Local<v8::Value> val,
  65. gfx::Size* out) {
  66. gin::Dictionary dict(isolate);
  67. if (!gin::ConvertFromV8(isolate, val, &dict))
  68. return false;
  69. int width, height;
  70. if (!dict.Get("width", &width) || !dict.Get("height", &height))
  71. return false;
  72. *out = gfx::Size(width, height);
  73. return true;
  74. }
  75. v8::Local<v8::Value> Converter<gfx::Rect>::ToV8(v8::Isolate* isolate,
  76. const gfx::Rect& val) {
  77. gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
  78. dict.SetHidden("simple", true);
  79. dict.Set("x", val.x());
  80. dict.Set("y", val.y());
  81. dict.Set("width", val.width());
  82. dict.Set("height", val.height());
  83. return dict.GetHandle();
  84. }
  85. bool Converter<gfx::Rect>::FromV8(v8::Isolate* isolate,
  86. v8::Local<v8::Value> val,
  87. gfx::Rect* out) {
  88. gin::Dictionary dict(isolate);
  89. if (!gin::ConvertFromV8(isolate, val, &dict))
  90. return false;
  91. int x, y, width, height;
  92. if (!dict.Get("x", &x) || !dict.Get("y", &y) || !dict.Get("width", &width) ||
  93. !dict.Get("height", &height))
  94. return false;
  95. *out = gfx::Rect(x, y, width, height);
  96. return true;
  97. }
  98. template <>
  99. struct Converter<display::Display::AccelerometerSupport> {
  100. static v8::Local<v8::Value> ToV8(
  101. v8::Isolate* isolate,
  102. const display::Display::AccelerometerSupport& val) {
  103. switch (val) {
  104. case display::Display::AccelerometerSupport::AVAILABLE:
  105. return StringToV8(isolate, "available");
  106. case display::Display::AccelerometerSupport::UNAVAILABLE:
  107. return StringToV8(isolate, "unavailable");
  108. default:
  109. return StringToV8(isolate, "unknown");
  110. }
  111. }
  112. };
  113. template <>
  114. struct Converter<display::Display::TouchSupport> {
  115. static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
  116. const display::Display::TouchSupport& val) {
  117. switch (val) {
  118. case display::Display::TouchSupport::AVAILABLE:
  119. return StringToV8(isolate, "available");
  120. case display::Display::TouchSupport::UNAVAILABLE:
  121. return StringToV8(isolate, "unavailable");
  122. default:
  123. return StringToV8(isolate, "unknown");
  124. }
  125. }
  126. };
  127. v8::Local<v8::Value> Converter<display::Display>::ToV8(
  128. v8::Isolate* isolate,
  129. const display::Display& val) {
  130. gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
  131. dict.SetHidden("simple", true);
  132. dict.Set("id", val.id());
  133. dict.Set("label", val.label());
  134. dict.Set("bounds", val.bounds());
  135. dict.Set("workArea", val.work_area());
  136. dict.Set("accelerometerSupport", val.accelerometer_support());
  137. dict.Set("monochrome", val.is_monochrome());
  138. dict.Set("colorDepth", val.color_depth());
  139. dict.Set("colorSpace", val.GetColorSpaces().GetRasterColorSpace().ToString());
  140. dict.Set("depthPerComponent", val.depth_per_component());
  141. dict.Set("size", val.size());
  142. dict.Set("displayFrequency", val.display_frequency());
  143. dict.Set("workAreaSize", val.work_area_size());
  144. dict.Set("scaleFactor", val.device_scale_factor());
  145. dict.Set("rotation", val.RotationAsDegree());
  146. dict.Set("internal", val.IsInternal());
  147. dict.Set("touchSupport", val.touch_support());
  148. return dict.GetHandle();
  149. }
  150. v8::Local<v8::Value> Converter<gfx::ResizeEdge>::ToV8(
  151. v8::Isolate* isolate,
  152. const gfx::ResizeEdge& val) {
  153. switch (val) {
  154. case gfx::ResizeEdge::kRight:
  155. return StringToV8(isolate, "right");
  156. case gfx::ResizeEdge::kBottom:
  157. return StringToV8(isolate, "bottom");
  158. case gfx::ResizeEdge::kTop:
  159. return StringToV8(isolate, "top");
  160. case gfx::ResizeEdge::kLeft:
  161. return StringToV8(isolate, "left");
  162. case gfx::ResizeEdge::kTopLeft:
  163. return StringToV8(isolate, "top-left");
  164. case gfx::ResizeEdge::kTopRight:
  165. return StringToV8(isolate, "top-right");
  166. case gfx::ResizeEdge::kBottomLeft:
  167. return StringToV8(isolate, "bottom-left");
  168. case gfx::ResizeEdge::kBottomRight:
  169. return StringToV8(isolate, "bottom-right");
  170. default:
  171. return StringToV8(isolate, "unknown");
  172. }
  173. }
  174. } // namespace gin