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.

237 lines
7.9 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 <string>
#include "gin/data_object_builder.h"
#include "shell/common/color_util.h"
#include "shell/common/gin_helper/dictionary.h"
2016-08-26 15:30:02 -07:00
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/insets.h"
2015-03-10 15:35:53 -07:00
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/resize_utils.h"
2015-03-10 15:35:53 -07:00
#include "ui/gfx/geometry/size.h"
namespace gin {
2015-05-22 19:11:22 +08:00
v8::Local<v8::Value> Converter<gfx::Point>::ToV8(v8::Isolate* isolate,
2018-04-17 21:55:30 -04:00
const gfx::Point& val) {
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("x", val.x());
dict.Set("y", val.y());
return dict.GetHandle();
}
bool Converter<gfx::Point>::FromV8(v8::Isolate* isolate,
2015-05-22 19:11:22 +08: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) {
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
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 19:11:22 +08:00
v8::Local<v8::Value> Converter<gfx::Size>::ToV8(v8::Isolate* isolate,
2016-07-04 19:19:20 +09:00
const gfx::Size& val) {
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("width", val.width());
dict.Set("height", val.height());
return dict.GetHandle();
}
bool Converter<gfx::Size>::FromV8(v8::Isolate* isolate,
2015-05-22 19:11:22 +08: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 19:11:22 +08:00
v8::Local<v8::Value> Converter<gfx::Rect>::ToV8(v8::Isolate* isolate,
2018-04-17 21:55:30 -04:00
const gfx::Rect& val) {
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
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 19:11:22 +08:00
v8::Local<v8::Value> val,
gfx::Rect* out) {
gin::Dictionary dict(isolate);
if (!gin::ConvertFromV8(isolate, val, &dict))
return false;
float x, y, width, height;
2018-04-17 21:55:30 -04:00
if (!dict.Get("x", &x) || !dict.Get("y", &y) || !dict.Get("width", &width) ||
!dict.Get("height", &height))
return false;
*out = ToRoundedRect(gfx::RectF(x, y, width, height));
return true;
}
v8::Local<v8::Value> Converter<gfx::Insets>::ToV8(v8::Isolate* isolate,
const gfx::Insets& val) {
return gin::DataObjectBuilder(isolate)
.Set("top", val.top())
.Set("left", val.left())
.Set("bottom", val.bottom())
.Set("right", val.right())
.Build();
}
bool Converter<gfx::Insets>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
gfx::Insets* out) {
gin::Dictionary dict(isolate);
if (!gin::ConvertFromV8(isolate, val, &dict))
return false;
double top, left, right, bottom;
if (!dict.Get("top", &top))
return false;
if (!dict.Get("left", &left))
return false;
if (!dict.Get("bottom", &bottom))
return false;
if (!dict.Get("right", &right))
return false;
*out = gfx::Insets::TLBR(top, left, bottom, right);
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-17 21:55:30 -04:00
template <>
2016-07-04 15:08:55 +09:00
struct Converter<display::Display::TouchSupport> {
2015-05-22 19:11:22 +08:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2018-04-17 21:55:30 -04:00
const display::Display::TouchSupport& val) {
2015-01-14 14:51:20 -08:00
switch (val) {
case display::Display::TouchSupport::AVAILABLE:
2015-01-14 14:51:20 -08:00
return StringToV8(isolate, "available");
case display::Display::TouchSupport::UNAVAILABLE:
2015-01-14 14:51:20 -08:00
return StringToV8(isolate, "unavailable");
default:
return StringToV8(isolate, "unknown");
2015-01-14 14:51:20 -08:00
}
}
};
2016-07-04 19:19:20 +09:00
v8::Local<v8::Value> Converter<display::Display>::ToV8(
2018-04-17 21:55:30 -04:00
v8::Isolate* isolate,
const display::Display& val) {
auto dict = gin_helper::Dictionary::CreateEmpty(isolate);
dict.Set("accelerometerSupport", val.accelerometer_support());
dict.Set("bounds", val.bounds());
dict.Set("colorDepth", val.color_depth());
chore: bump chromium to 140.0.7327.0 (38-x-y) (#47930) * chore: bump chromium in DEPS to 140.0.7324.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * chore: bump chromium in DEPS to 140.0.7325.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * chore: remove @dsanders11's unused include patch CL: https://chromium-review.googlesource.com/c/chromium/src/+/6782507 Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * fix: apply keychain patch to new apple subdir CL: https://chromium-review.googlesource.com/c/chromium/src/+/6736212 Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * chore: update chromium patches Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * chore: update other patches Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * chore: bump chromium in DEPS to 140.0.7327.0 Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> * fix: mistake in reapplied patch Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * fixup! fix: apply keychain patch to new apple subdir CL: https://chromium-review.googlesource.com/c/chromium/src/+/6736212 Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * chore: update patches Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * fix: remove OnPrivateNetworkAccessPermissionRequired override CL: https://chromium-review.googlesource.com/c/chromium/src/+/6769208 Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * fix: update colorSpace property to use new unified value CL: https://chromium-review.googlesource.com/c/chromium/src/+/6795085 Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * fix: include OverlayWindowLiveCaptionButton CL: https://chromium-review.googlesource.com/c/chromium/src/+/6787420 Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * fixup! fix: apply keychain patch to new apple subdir CL: https://chromium-review.googlesource.com/c/chromium/src/+/6736212 Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * fix: format chromium_src/BUILD.gn CL: https://chromium-review.googlesource.com/c/chromium/src/+/6787427 Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * fix: format BUILD.gn CL: https://chromium-review.googlesource.com/c/chromium/src/+/6787427 Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * chore: include script/ in logged path Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * fix: update filenames.libcxx.gni CL: https://chromium-review.googlesource.com/c/chromium/src/+/6787279 Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> * chore: update patches --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: Samuel Maddock <smaddock@slack-corp.com> Co-authored-by: clavin <clavin@electronjs.org>
2025-08-05 21:45:37 -04:00
dict.Set("colorSpace", val.GetColorSpaces()
.GetRasterAndCompositeColorSpace(
gfx::ContentColorUsage::kWideColorGamut)
.ToString());
dict.Set("depthPerComponent", val.depth_per_component());
dict.Set("detected", val.detected());
dict.Set("displayFrequency", val.display_frequency());
dict.Set("id", val.id());
dict.Set("internal", val.IsInternal());
dict.Set("label", val.label());
dict.Set("maximumCursorSize", val.maximum_cursor_size());
dict.Set("monochrome", val.is_monochrome());
dict.Set("nativeOrigin", val.native_origin());
dict.Set("rotation", val.RotationAsDegree());
dict.Set("scaleFactor", val.device_scale_factor());
dict.Set("size", val.size());
dict.Set("workArea", val.work_area());
dict.Set("workAreaSize", val.work_area_size());
2015-01-14 14:51:20 -08: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");
}
}
bool Converter<WrappedSkColor>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
WrappedSkColor* out) {
std::string str;
if (!gin::ConvertFromV8(isolate, val, &str))
return false;
*out = electron::ParseCSSColor(str).value_or(SK_ColorWHITE);
return true;
}
} // namespace gin