#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
#[cfg(mobile)]
use tauri::RunEvent;
use tauri::{
plugin::{Builder, TauriPlugin},
Manager, Runtime,
};
#[cfg(desktop)]
use std::borrow::Cow;
#[cfg(desktop)]
#[allow(missing_docs)]
#[cfg_attr(docsrs, doc(cfg(desktop)))]
pub mod desktop;
#[cfg(mobile)]
#[allow(missing_docs)]
#[cfg_attr(docsrs, doc(cfg(mobile)))]
pub mod mobile;
#[allow(missing_docs)]
pub mod adaptive_card;
pub mod apply;
#[allow(missing_docs)]
pub mod capabilities;
#[allow(missing_docs)]
pub mod codegen;
mod commands;
#[allow(missing_docs)]
pub mod config;
pub mod error;
#[allow(missing_docs)]
pub mod models;
#[allow(missing_docs)]
pub mod rasterize;
#[allow(missing_docs)]
pub mod receipt;
#[allow(missing_docs)]
pub mod snapshot;
#[allow(missing_docs)]
pub mod store;
#[allow(missing_docs)]
pub mod trace;
#[allow(missing_docs)]
pub mod transport;
#[cfg(target_os = "windows")]
#[allow(missing_docs)]
#[cfg_attr(docsrs, doc(cfg(windows)))]
pub mod windows;
#[cfg(all(target_os = "linux", feature = "linux"))]
#[allow(missing_docs)]
#[cfg_attr(docsrs, doc(cfg(all(target_os = "linux", feature = "linux"))))]
pub mod linux;
#[cfg(target_os = "macos")]
#[allow(missing_docs)]
#[cfg_attr(docsrs, doc(cfg(macos)))]
pub mod macos_transport;
pub use adaptive_card::{to_adaptive_card, to_adaptive_card_for_size, TranspileResult};
pub use apply::{ApplyOutcome, ReloadOutcome, SkipReason};
pub use config::{TransportKind, WidgetsPluginConfig};
pub use error::{Error, Result};
pub use receipt::{SkippedElement, WidgetRenderReceipt};
pub use store::WidgetActionEnvelope;
pub use trace::{TraceEntry, TraceEvent, WidgetTrace};
pub use transport::{Receipt, Transport};
#[cfg(desktop)]
pub use desktop::Widget;
#[cfg(mobile)]
pub use mobile::Widget;
pub trait WidgetExt<R: Runtime> {
fn widget(&self) -> &Widget<R>;
}
impl<R: Runtime, T: Manager<R>> WidgetExt<R> for T {
fn widget(&self) -> &Widget<R> {
self.state::<Widget<R>>().inner()
}
}
pub fn init<R: Runtime>() -> TauriPlugin<R, Option<WidgetsPluginConfig>> {
let builder = Builder::<R, Option<WidgetsPluginConfig>>::new("widgets")
.invoke_handler(tauri::generate_handler![
commands::set_items,
commands::get_items,
commands::set_register_widget,
commands::reload_all_timelines,
commands::reload_timelines,
commands::request_widget,
commands::create_widget_window,
commands::close_widget_window,
commands::set_widget_config,
commands::get_widget_config,
commands::widget_action,
commands::poll_pending_actions,
commands::report_receipt,
commands::get_widget_diagnostics,
commands::get_widget_trace,
commands::flush_widget_trace,
])
.setup(|app, api| {
#[cfg(mobile)]
let widget = mobile::init(app, api)?;
#[cfg(desktop)]
let widget = desktop::init(app, api)?;
app.manage(widget);
Ok(())
});
#[cfg(mobile)]
let builder = builder.on_event(|app, event| match event {
RunEvent::Ready | RunEvent::Resumed => {
if let Some(widget) = app.try_state::<Widget<R>>() {
widget.inner().drain_pending_actions_to_events();
}
}
_ => {}
});
#[cfg(desktop)]
let builder =
builder.register_uri_scheme_protocol(desktop::BUILTIN_PROTOCOL, |_app, _request| {
const HTML: &[u8] = include_bytes!("../widget.html");
tauri::http::Response::builder()
.header("content-type", "text/html; charset=utf-8")
.body(Cow::Borrowed(HTML))
.unwrap()
});
builder.build()
}