use std::{any::TypeId, future::Future, pin::Pin};
use super::{
AsyncCx, DialogAnswer, Environment, PropError, TsType, TsTypes,
element::ErasedCx,
task::{self, Action},
};
use crate::{Command, Dialog, JsonValue, LayoutAnimation, ROOT_ID, StatusBarSetting};
pub trait Module {
const NAME: &'static str;
const DOC: &'static str;
const FUNCTIONS: &'static [Function];
const FLAT: bool = false;
}
pub type ModuleParam = (&'static str, TsType, bool);
pub type ModuleFuture = Pin<Box<dyn Future<Output = Result<JsonValue, String>>>>;
pub enum FunctionCall {
Sync(fn(Vec<JsonValue>, &mut ModuleCx<'_>) -> Result<JsonValue, PropError>),
Async(fn(Vec<JsonValue>, AsyncCx) -> Result<ModuleFuture, PropError>),
}
pub struct Function {
pub name: &'static str,
pub doc: &'static str,
pub params: &'static [ModuleParam],
pub returns: Option<TsType>,
pub call: FunctionCall,
}
#[derive(Debug, Clone)]
pub struct FunctionSpec {
pub name: &'static str,
pub doc: &'static str,
pub params: Vec<(&'static str, String, bool)>,
pub returns: Option<String>,
}
#[derive(Debug, Clone)]
pub struct ModuleSpec {
pub name: &'static str,
pub doc: &'static str,
pub functions: Vec<FunctionSpec>,
pub flat: bool,
pub types: TsTypes,
}
impl ModuleSpec {
pub(crate) fn of<M: Module>() -> Self {
let mut types = TsTypes::default();
let functions = M::FUNCTIONS
.iter()
.map(|f| FunctionSpec {
name: f.name,
doc: f.doc,
params: f
.params
.iter()
.map(|(n, ts, optional)| (*n, ts(&mut types), *optional))
.collect(),
returns: f.returns.map(|ts| ts(&mut types)),
})
.collect();
Self {
name: M::NAME,
doc: M::DOC,
functions,
flat: M::FLAT,
types,
}
}
}
pub struct ModuleCx<'a> {
inner: &'a mut ErasedCx,
}
impl<'a> ModuleCx<'a> {
pub(crate) fn new(inner: &'a mut ErasedCx) -> Self {
Self { inner }
}
pub fn environment(&self) -> &Environment {
&self.inner.environment
}
pub fn update_global<G: Default + 'static, R>(
&mut self,
f: impl FnOnce(&mut G, &mut Self) -> R,
) -> R {
let id = TypeId::of::<G>();
let taken = self.inner.globals.borrow_mut().remove(&id);
let mut global = taken
.and_then(|g| g.downcast::<G>().ok())
.unwrap_or_default();
let result = f(&mut global, self);
self.inner.globals.borrow_mut().insert(id, global);
if !self.inner.changed_globals.contains(&id) {
self.inner.changed_globals.push(id);
}
result
}
pub fn dialog(&mut self, dialog: Dialog) -> DialogAnswer {
let (answer, slot) = DialogAnswer::new();
self.inner.actions.push(Action::Dialog(dialog, slot));
answer
}
pub fn dismiss_keyboard(&mut self) {
self.inner
.actions
.push(Action::Command(ROOT_ID, Command::Blur));
}
pub fn set_status_bar(&mut self, patch: StatusBarSetting) {
self.inner.actions.push(task::status_bar(patch));
}
pub fn animate_layout(&mut self, animation: LayoutAnimation) {
self.inner.actions.push(Action::Command(
ROOT_ID,
Command::LayoutAnimation(animation),
));
}
pub fn announce_for_accessibility(&mut self, message: impl Into<String>) {
self.inner
.actions
.push(super::services::announce(message.into()));
}
pub fn set_clipboard(&mut self, text: impl Into<String>) {
self.inner
.actions
.push(super::services::set_clipboard(text.into()));
}
pub fn toast(&mut self, message: impl Into<String>, duration: super::ToastDuration) {
self.inner
.actions
.push(super::services::toast(message.into(), duration));
}
pub fn set_color_scheme(&mut self, appearance: crate::platform::Appearance) {
self.inner.actions.push(Action::Appearance(appearance));
}
pub fn force_rtl(&mut self, force: bool) {
self.inner
.actions
.push(Action::Direction(super::services::Direction::Force(force)));
}
pub fn allow_rtl(&mut self, allow: bool) {
self.inner
.actions
.push(Action::Direction(super::services::Direction::Allow(allow)));
}
pub(crate) fn act(&mut self, action: Action) {
self.inner.actions.push(action);
}
}