use crate::common::Dimension;
use crate::error::Error;
use alloc::vec::Vec;
use oc_wasm_futures::invoke::{component_method, Buffer};
use oc_wasm_helpers::{error::NullAndStringOr, Lockable};
use oc_wasm_safe::{component::Invoker, Address};
pub const TYPE: &str = "screen";
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Screen(Address);
impl Screen {
#[must_use = "This function is only useful for its return value"]
pub fn new(address: Address) -> Self {
Self(address)
}
#[must_use = "This function is only useful for its return value"]
pub fn address(&self) -> &Address {
&self.0
}
}
impl<'a, B: 'a + Buffer> Lockable<'a, 'a, B> for Screen {
type Locked = Locked<'a, B>;
fn lock(&self, invoker: &'a mut Invoker, buffer: &'a mut B) -> Self::Locked {
Locked {
address: self.0,
invoker,
buffer,
}
}
}
pub struct Locked<'a, B: Buffer> {
address: Address,
invoker: &'a mut Invoker,
buffer: &'a mut B,
}
impl<'a, B: Buffer> Locked<'a, B> {
pub async fn is_on(&mut self) -> Result<bool, Error> {
let ret: (bool,) =
component_method::<(), _, _>(self.invoker, self.buffer, &self.address, "isOn", None)
.await?;
Ok(ret.0)
}
pub async fn turn_on(&mut self) -> Result<bool, Error> {
let ret: (bool, bool) =
component_method::<(), _, _>(self.invoker, self.buffer, &self.address, "turnOn", None)
.await?;
Ok(ret.0)
}
pub async fn turn_off(&mut self) -> Result<bool, Error> {
let ret: (bool, bool) =
component_method::<(), _, _>(self.invoker, self.buffer, &self.address, "turnOff", None)
.await?;
Ok(ret.0)
}
pub async fn get_aspect_ratio(&mut self) -> Result<Dimension, Error> {
let ret: (f64, f64) = component_method::<(), _, _>(
self.invoker,
self.buffer,
&self.address,
"getAspectRatio",
None,
)
.await?;
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
Ok(Dimension {
width: ret.0 as u32,
height: ret.1 as u32,
})
}
pub async fn get_keyboards(&mut self) -> Result<Vec<Address>, Error> {
let ret: (Vec<Address>,) = component_method::<(), _, _>(
self.invoker,
self.buffer,
&self.address,
"getKeyboards",
None,
)
.await?;
Ok(ret.0)
}
pub async fn set_precise(&mut self, precise: bool) -> Result<bool, Error> {
let ret: NullAndStringOr<'_, (bool,)> = component_method(
self.invoker,
self.buffer,
&self.address,
"setPrecise",
Some(&(precise,)),
)
.await?;
match ret {
NullAndStringOr::Ok((f,)) => Ok(f),
NullAndStringOr::Err("unsupported operation") => Err(Error::Unsupported),
NullAndStringOr::Err(_) => {
Err(Error::BadComponent(oc_wasm_safe::error::Error::Unknown))
}
}
}
pub async fn is_precise(&mut self) -> Result<bool, Error> {
let ret: (bool,) = component_method::<(), _, _>(
self.invoker,
self.buffer,
&self.address,
"isPrecise",
None,
)
.await?;
Ok(ret.0)
}
pub async fn set_touch_mode_inverted(&mut self, inverted: bool) -> Result<bool, Error> {
let ret: (bool,) = component_method(
self.invoker,
self.buffer,
&self.address,
"setTouchModeInverted",
Some(&(inverted,)),
)
.await?;
Ok(ret.0)
}
pub async fn is_touch_mode_inverted(&mut self) -> Result<bool, Error> {
let ret: (bool,) = component_method::<(), _, _>(
self.invoker,
self.buffer,
&self.address,
"isTouchModeInverted",
None,
)
.await?;
Ok(ret.0)
}
}