use crate::{window::DetachedWindow, Icon};
#[cfg(feature = "menu")]
use crate::{menu::Menu, MenuId};
use serde::Deserialize;
use serde_json::Value as JsonValue;
use tauri_utils::config::{WindowConfig, WindowUrl};
use std::{collections::HashMap, path::PathBuf};
type UriSchemeProtocol =
dyn Fn(&str) -> Result<Vec<u8>, Box<dyn std::error::Error>> + Send + Sync + 'static;
pub struct WebviewAttributes {
pub url: WindowUrl,
pub initialization_scripts: Vec<String>,
pub data_directory: Option<PathBuf>,
pub uri_scheme_protocols: HashMap<String, Box<UriSchemeProtocol>>,
}
impl WebviewAttributes {
pub fn new(url: WindowUrl) -> Self {
Self {
url,
initialization_scripts: Vec::new(),
data_directory: None,
uri_scheme_protocols: Default::default(),
}
}
pub fn initialization_script(mut self, script: &str) -> Self {
self.initialization_scripts.push(script.to_string());
self
}
pub fn data_directory(mut self, data_directory: PathBuf) -> Self {
self.data_directory.replace(data_directory);
self
}
pub fn has_uri_scheme_protocol(&self, name: &str) -> bool {
self.uri_scheme_protocols.contains_key(name)
}
pub fn register_uri_scheme_protocol<
N: Into<String>,
H: Fn(&str) -> Result<Vec<u8>, Box<dyn std::error::Error>> + Send + Sync + 'static,
>(
mut self,
uri_scheme: N,
protocol: H,
) -> Self {
let uri_scheme = uri_scheme.into();
self
.uri_scheme_protocols
.insert(uri_scheme, Box::new(move |data| (protocol)(data)));
self
}
}
pub trait WindowBuilderBase: Sized {}
pub trait WindowBuilder: WindowBuilderBase {
fn new() -> Self;
fn with_config(config: WindowConfig) -> Self;
#[cfg(feature = "menu")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
fn menu<I: MenuId>(self, menu: Vec<Menu<I>>) -> Self;
fn position(self, x: f64, y: f64) -> Self;
fn inner_size(self, min_width: f64, min_height: f64) -> Self;
fn min_inner_size(self, min_width: f64, min_height: f64) -> Self;
fn max_inner_size(self, min_width: f64, min_height: f64) -> Self;
fn resizable(self, resizable: bool) -> Self;
fn title<S: Into<String>>(self, title: S) -> Self;
fn fullscreen(self, fullscreen: bool) -> Self;
fn maximized(self, maximized: bool) -> Self;
fn visible(self, visible: bool) -> Self;
fn transparent(self, transparent: bool) -> Self;
fn decorations(self, decorations: bool) -> Self;
fn always_on_top(self, always_on_top: bool) -> Self;
fn icon(self, icon: Icon) -> crate::Result<Self>;
fn has_icon(&self) -> bool;
#[cfg(feature = "menu")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
fn has_menu(&self) -> bool;
}
pub struct RpcRequest {
pub command: String,
pub params: Option<JsonValue>,
}
pub struct CustomProtocol {
#[allow(clippy::type_complexity)]
pub protocol: Box<dyn Fn(&str) -> Result<Vec<u8>, Box<dyn std::error::Error>> + Send + Sync>,
}
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum FileDropEvent {
Hovered(Vec<PathBuf>),
Dropped(Vec<PathBuf>),
Cancelled,
}
pub type WebviewRpcHandler<P> = Box<dyn Fn(DetachedWindow<P>, RpcRequest) + Send>;
pub type FileDropHandler<P> = Box<dyn Fn(FileDropEvent, DetachedWindow<P>) -> bool + Send>;
#[derive(Deserialize)]
pub struct InvokePayload {
#[serde(rename = "__tauriModule")]
pub tauri_module: Option<String>,
pub callback: String,
pub error: String,
#[serde(flatten)]
pub inner: JsonValue,
}