1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
3
4use napi::{
5 bindgen_prelude::{FromNapiValue, Object},
6 JsUnknown,
7};
8
9/// SlintPoint implements {@link Point}.
10#[napi]
11pub struct SlintPoint {
12 pub x: f64,
13 pub y: f64,
14}
15
16#[napi]
17impl SlintPoint {
18 /// Constructs new point from x and y.
19 #[napi(constructor)]
20 pub fn new(x: f64, y: f64) -> Self {
21 Self { x, y }
22 }
23}
24
25impl FromNapiValue for SlintPoint {
26 unsafe fn from_napi_value(
27 env: napi::sys::napi_env,
28 napi_val: napi::sys::napi_value,
29 ) -> napi::Result<Self> {
30 let obj = unsafe { Object::from_napi_value(env, napi_val)? };
31 let x: f64 = obj
32 .get::<_, JsUnknown>("x")
33 .ok()
34 .flatten()
35 .and_then(|p| p.coerce_to_number().ok())
36 .and_then(|f64_num| f64_num.try_into().ok())
37 .ok_or_else(
38 || napi::Error::from_reason(
39 "Cannot convert object to Point, because the provided object does not have an f64 x property".to_string()
40 ))?;
41 let y: f64 = obj
42 .get::<_, JsUnknown>("y")
43 .ok()
44 .flatten()
45 .and_then(|p| p.coerce_to_number().ok())
46 .and_then(|f64_num| f64_num.try_into().ok())
47 .ok_or_else(
48 || napi::Error::from_reason(
49 "Cannot convert object to Point, because the provided object does not have an f64 y property".to_string()
50 ))?;
51
52 Ok(SlintPoint { x, y })
53 }
54}
55