1use crate::common::Dimension;
4use crate::error::Error;
5use alloc::vec::Vec;
6use oc_wasm_futures::invoke::{component_method, Buffer};
7use oc_wasm_helpers::{error::NullAndStringOr, Lockable};
8use oc_wasm_safe::{component::Invoker, Address};
9
10pub const TYPE: &str = "screen";
12
13#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
15pub struct Screen(Address);
16
17impl Screen {
18 #[must_use = "This function is only useful for its return value"]
24 pub fn new(address: Address) -> Self {
25 Self(address)
26 }
27
28 #[must_use = "This function is only useful for its return value"]
30 pub fn address(&self) -> &Address {
31 &self.0
32 }
33}
34
35impl<'a, B: 'a + Buffer> Lockable<'a, 'a, B> for Screen {
36 type Locked = Locked<'a, B>;
37
38 fn lock(&self, invoker: &'a mut Invoker, buffer: &'a mut B) -> Self::Locked {
39 Locked {
40 address: self.0,
41 invoker,
42 buffer,
43 }
44 }
45}
46
47pub struct Locked<'a, B: Buffer> {
57 address: Address,
59
60 invoker: &'a mut Invoker,
62
63 buffer: &'a mut B,
65}
66
67impl<'a, B: Buffer> Locked<'a, B> {
68 pub async fn is_on(&mut self) -> Result<bool, Error> {
74 let ret: (bool,) =
75 component_method::<(), _, _>(self.invoker, self.buffer, &self.address, "isOn", None)
76 .await?;
77 Ok(ret.0)
78 }
79
80 pub async fn turn_on(&mut self) -> Result<bool, Error> {
86 let ret: (bool, bool) =
87 component_method::<(), _, _>(self.invoker, self.buffer, &self.address, "turnOn", None)
88 .await?;
89 Ok(ret.0)
90 }
91
92 pub async fn turn_off(&mut self) -> Result<bool, Error> {
98 let ret: (bool, bool) =
99 component_method::<(), _, _>(self.invoker, self.buffer, &self.address, "turnOff", None)
100 .await?;
101 Ok(ret.0)
102 }
103
104 pub async fn get_aspect_ratio(&mut self) -> Result<Dimension, Error> {
110 let ret: (f64, f64) = component_method::<(), _, _>(
111 self.invoker,
112 self.buffer,
113 &self.address,
114 "getAspectRatio",
115 None,
116 )
117 .await?;
118 #[allow(clippy::cast_possible_truncation)]
121 #[allow(clippy::cast_sign_loss)]
122 Ok(Dimension {
123 width: ret.0 as u32,
124 height: ret.1 as u32,
125 })
126 }
127
128 pub async fn get_keyboards(&mut self) -> Result<Vec<Address>, Error> {
134 let ret: (Vec<Address>,) = component_method::<(), _, _>(
135 self.invoker,
136 self.buffer,
137 &self.address,
138 "getKeyboards",
139 None,
140 )
141 .await?;
142 Ok(ret.0)
143 }
144
145 pub async fn set_precise(&mut self, precise: bool) -> Result<bool, Error> {
154 let ret: NullAndStringOr<'_, (bool,)> = component_method(
155 self.invoker,
156 self.buffer,
157 &self.address,
158 "setPrecise",
159 Some(&(precise,)),
160 )
161 .await?;
162 match ret {
163 NullAndStringOr::Ok((f,)) => Ok(f),
164 NullAndStringOr::Err("unsupported operation") => Err(Error::Unsupported),
165 NullAndStringOr::Err(_) => {
166 Err(Error::BadComponent(oc_wasm_safe::error::Error::Unknown))
167 }
168 }
169 }
170
171 pub async fn is_precise(&mut self) -> Result<bool, Error> {
177 let ret: (bool,) = component_method::<(), _, _>(
178 self.invoker,
179 self.buffer,
180 &self.address,
181 "isPrecise",
182 None,
183 )
184 .await?;
185 Ok(ret.0)
186 }
187
188 pub async fn set_touch_mode_inverted(&mut self, inverted: bool) -> Result<bool, Error> {
195 let ret: (bool,) = component_method(
196 self.invoker,
197 self.buffer,
198 &self.address,
199 "setTouchModeInverted",
200 Some(&(inverted,)),
201 )
202 .await?;
203 Ok(ret.0)
204 }
205
206 pub async fn is_touch_mode_inverted(&mut self) -> Result<bool, Error> {
213 let ret: (bool,) = component_method::<(), _, _>(
214 self.invoker,
215 self.buffer,
216 &self.address,
217 "isTouchModeInverted",
218 None,
219 )
220 .await?;
221 Ok(ret.0)
222 }
223}