electron/shell/common/gin_converters/gfx_converter.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

191 lines
6.5 KiB
C++
Raw Normal View History

// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/gin_converters/gfx_converter.h"
#include "shell/common/gin_helper/dictionary.h"
2016-08-26 22:30:02 +00:00
#include "ui/display/display.h"
#include "ui/display/screen.h"
2015-03-10 22:35:53 +00:00
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_f.h"
2015-03-10 22:35:53 +00:00
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/resize_utils.h"
2015-03-10 22:35:53 +00:00
#include "ui/gfx/geometry/size.h"
namespace gin {
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> Converter<gfx::Point>::ToV8(v8::Isolate* isolate,
2018-04-18 01:55:30 +00:00
const gfx::Point& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("x", val.x());
dict.Set("y", val.y());
return dict.GetHandle();
}
bool Converter<gfx::Point>::FromV8(v8::Isolate* isolate,
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> val,
gfx::Point* out) {
gin::Dictionary dict(isolate);
if (!gin::ConvertFromV8(isolate, val, &dict))
return false;
double x, y;
if (!dict.Get("x", &x) || !dict.Get("y", &y))
return false;
*out = gfx::Point(static_cast<int>(std::round(x)),
static_cast<int>(std::round(y)));
return true;
}
v8::Local<v8::Value> Converter<gfx::PointF>::ToV8(v8::Isolate* isolate,
const gfx::PointF& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("x", val.x());
dict.Set("y", val.y());
return dict.GetHandle();
}
bool Converter<gfx::PointF>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
gfx::PointF* out) {
gin::Dictionary dict(isolate);
if (!gin::ConvertFromV8(isolate, val, &dict))
return false;
float x, y;
if (!dict.Get("x", &x) || !dict.Get("y", &y))
return false;
*out = gfx::PointF(x, y);
return true;
}
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> Converter<gfx::Size>::ToV8(v8::Isolate* isolate,
2016-07-04 10:19:20 +00:00
const gfx::Size& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("width", val.width());
dict.Set("height", val.height());
return dict.GetHandle();
}
bool Converter<gfx::Size>::FromV8(v8::Isolate* isolate,
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> val,
gfx::Size* out) {
gin::Dictionary dict(isolate);
if (!gin::ConvertFromV8(isolate, val, &dict))
return false;
int width, height;
if (!dict.Get("width", &width) || !dict.Get("height", &height))
return false;
*out = gfx::Size(width, height);
return true;
}
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> Converter<gfx::Rect>::ToV8(v8::Isolate* isolate,
2018-04-18 01:55:30 +00:00
const gfx::Rect& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("x", val.x());
dict.Set("y", val.y());
dict.Set("width", val.width());
dict.Set("height", val.height());
return dict.GetHandle();
}
bool Converter<gfx::Rect>::FromV8(v8::Isolate* isolate,
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> val,
gfx::Rect* out) {
gin::Dictionary dict(isolate);
if (!gin::ConvertFromV8(isolate, val, &dict))
return false;
int x, y, width, height;
2018-04-18 01:55:30 +00:00
if (!dict.Get("x", &x) || !dict.Get("y", &y) || !dict.Get("width", &width) ||
!dict.Get("height", &height))
return false;
*out = gfx::Rect(x, y, width, height);
return true;
}
template <>
struct Converter<display::Display::AccelerometerSupport> {
static v8::Local<v8::Value> ToV8(
v8::Isolate* isolate,
const display::Display::AccelerometerSupport& val) {
switch (val) {
case display::Display::AccelerometerSupport::AVAILABLE:
return StringToV8(isolate, "available");
case display::Display::AccelerometerSupport::UNAVAILABLE:
return StringToV8(isolate, "unavailable");
default:
return StringToV8(isolate, "unknown");
}
}
};
2018-04-18 01:55:30 +00:00
template <>
2016-07-04 06:08:55 +00:00
struct Converter<display::Display::TouchSupport> {
2015-05-22 11:11:22 +00:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2018-04-18 01:55:30 +00:00
const display::Display::TouchSupport& val) {
2015-01-14 22:51:20 +00:00
switch (val) {
case display::Display::TouchSupport::AVAILABLE:
2015-01-14 22:51:20 +00:00
return StringToV8(isolate, "available");
case display::Display::TouchSupport::UNAVAILABLE:
2015-01-14 22:51:20 +00:00
return StringToV8(isolate, "unavailable");
default:
return StringToV8(isolate, "unknown");
2015-01-14 22:51:20 +00:00
}
}
};
2016-07-04 10:19:20 +00:00
v8::Local<v8::Value> Converter<display::Display>::ToV8(
2018-04-18 01:55:30 +00:00
v8::Isolate* isolate,
const display::Display& val) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
dict.SetHidden("simple", true);
dict.Set("id", val.id());
dict.Set("label", val.label());
dict.Set("bounds", val.bounds());
dict.Set("workArea", val.work_area());
dict.Set("accelerometerSupport", val.accelerometer_support());
dict.Set("monochrome", val.is_monochrome());
dict.Set("colorDepth", val.color_depth());
chore: bump chromium to 117.0.5938.0 (main) (#39375) * chore: bump chromium in DEPS to 117.0.5929.0 * chore: bump chromium in DEPS to 117.0.5931.0 * chore: bump chromium in DEPS to 117.0.5932.0 * chore: update patches * 4728317: Prevent PrintRenderFrameHelper from printing when already printing | https://chromium-review.googlesource.com/c/chromium/src/+/4728317 * 4739501: Use base::SequenceBound to manage SerialPortManagerImpl | https://chromium-review.googlesource.com/c/chromium/src/+/4739501 * 4702051: Allow overriding source in install-sysroot.py | https://chromium-review.googlesource.com/c/chromium/src/+/4702051 * chore: update filenames.libcxx.gni * 4727002: Rename "enable_arc2" to "enable_arc" | https://chromium-review.googlesource.com/c/chromium/src/+/4727002 * chore: bump chromium in DEPS to 117.0.5934.0 * 4736873: Rename ColorSpaces methods on display::Display | https://chromium-review.googlesource.com/c/chromium/src/+/4736873 * 4727203: Replace bool with an enum in as suggested in DevtoolsManagerDelegate. | https://chromium-review.googlesource.com/c/chromium/src/+/4727203 * 4744479: [DevTools] Add 'generateTaggedPDF' option to DevTools Page.printToPDF | https://chromium-review.googlesource.com/c/chromium/src/+/4744479 * 4735893: Don't share WebUSB permissions with webviews | https://chromium-review.googlesource.com/c/chromium/src/+/4735893 * revert: update filenames.libcxx.gni * chore: bump chromium in DEPS to 117.0.5936.0 * chore: update patches * 4746465: SAA: Query for embargoed StorageAccess permissions | https://chromium-review.googlesource.com/c/chromium/src/+/4746465 * 4666325: Move buildtools/third_party/lib*/trunk source paths to third_party/lib*/src. | https://chromium-review.googlesource.com/c/chromium/src/+/4666325 * chore: bump chromium in DEPS to 117.0.5938.0 * chore: bump chromium in DEPS to 118.0.5939.0 * chore: update patches * Send line bounds through CursorAnchorInfo on requestCursorUpdate https://chromium-review.googlesource.com/c/chromium/src/+/4394588 * Fixup lint for Move buildtools/third_party/lib*/trunk source paths to third_party/lib*/src * 4700305: [mac] Fix override of CHILD_PROCESS_EXE https://chromium-review.googlesource.com/c/chromium/src/+/4700305 Needed because of 4729689: Reland "Remove redundant existence check in PathService" | https://chromium-review.googlesource.com/c/chromium/src/+/4729689 * 4753759: More consistent icon handling for menus. https://chromium-review.googlesource.com/c/chromium/src/+/4753759 * chore: bump chromium in DEPS to 118.0.5941.0 * chore: update patches * chore: bump chromium in DEPS to 117.0.5938.0 * test: update nan-spec-runner cflags * build: fix isystem include path in nan-spec-runner * fixup! 4666325: Move buildtools/third_party/lib*/trunk source paths to third_party/lib*/src. | https://chromium-review.googlesource.com/c/chromium/src/+/4666325 fix a few more instances of the old path libc++.a and libc++abi.a are still in buildtools/ --------- Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: VerteDinde <vertedinde@electronjs.org> Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org> Co-authored-by: Charles Kerr <charles@charleskerr.com>
2023-08-15 15:49:41 +00:00
dict.Set("colorSpace", val.GetColorSpaces().GetRasterColorSpace().ToString());
dict.Set("depthPerComponent", val.depth_per_component());
dict.Set("size", val.size());
dict.Set("displayFrequency", val.display_frequency());
dict.Set("workAreaSize", val.work_area_size());
dict.Set("scaleFactor", val.device_scale_factor());
2015-01-14 22:51:20 +00:00
dict.Set("rotation", val.RotationAsDegree());
dict.Set("internal", val.IsInternal());
2015-01-14 22:51:20 +00:00
dict.Set("touchSupport", val.touch_support());
return dict.GetHandle();
}
v8::Local<v8::Value> Converter<gfx::ResizeEdge>::ToV8(
v8::Isolate* isolate,
const gfx::ResizeEdge& val) {
switch (val) {
case gfx::ResizeEdge::kRight:
return StringToV8(isolate, "right");
case gfx::ResizeEdge::kBottom:
return StringToV8(isolate, "bottom");
case gfx::ResizeEdge::kTop:
return StringToV8(isolate, "top");
case gfx::ResizeEdge::kLeft:
return StringToV8(isolate, "left");
case gfx::ResizeEdge::kTopLeft:
return StringToV8(isolate, "top-left");
case gfx::ResizeEdge::kTopRight:
return StringToV8(isolate, "top-right");
case gfx::ResizeEdge::kBottomLeft:
return StringToV8(isolate, "bottom-left");
case gfx::ResizeEdge::kBottomRight:
return StringToV8(isolate, "bottom-right");
default:
return StringToV8(isolate, "unknown");
}
}
} // namespace gin