use std::{future::Future, pin::Pin};
use super::{
DialogAnswer, PropError, TsType, TsTypes,
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>) -> 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,
}
}
}
#[derive(Default)]
pub struct ModuleCx {
pub(crate) actions: Vec<Action>,
}
impl ModuleCx {
pub fn dialog(&mut self, dialog: Dialog) -> DialogAnswer {
let (answer, slot) = DialogAnswer::new();
self.actions.push(Action::Dialog(dialog, slot));
answer
}
pub fn dismiss_keyboard(&mut self) {
self.actions.push(Action::Command(ROOT_ID, Command::Blur));
}
pub fn set_status_bar(&mut self, patch: StatusBarSetting) {
self.actions.push(task::status_bar(patch));
}
pub fn animate_layout(&mut self, animation: LayoutAnimation) {
self.actions.push(Action::Command(
ROOT_ID,
Command::LayoutAnimation(animation),
));
}
pub fn announce_for_accessibility(&mut self, message: impl Into<String>) {
self.actions.push(super::services::announce(message.into()));
}
pub fn set_clipboard(&mut self, text: impl Into<String>) {
self.actions
.push(super::services::set_clipboard(text.into()));
}
pub fn toast(&mut self, message: impl Into<String>, duration: super::ToastDuration) {
self.actions
.push(super::services::toast(message.into(), duration));
}
pub fn set_color_scheme(&mut self, appearance: crate::platform::Appearance) {
self.actions.push(Action::Appearance(appearance));
}
pub fn force_rtl(&mut self, force: bool) {
self.actions
.push(Action::Direction(super::services::Direction::Force(force)));
}
pub fn allow_rtl(&mut self, allow: bool) {
self.actions
.push(Action::Direction(super::services::Direction::Allow(allow)));
}
pub(crate) fn act(&mut self, action: Action) {
self.actions.push(action);
}
}