use crate::{window::DetachedWindow, Icon};
use crate::menu::Menu;
use serde::Deserialize;
use serde_json::Value as JsonValue;
use tauri_utils::config::{WindowConfig, WindowUrl};
#[cfg(windows)]
use winapi::shared::windef::HWND;
use std::{collections::HashMap, fmt, 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>>,
pub file_drop_handler_enabled: bool,
}
impl fmt::Debug for WebviewAttributes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WebviewAttributes")
.field("url", &self.url)
.field("initialization_scripts", &self.initialization_scripts)
.field("data_directory", &self.data_directory)
.field("file_drop_handler_enabled", &self.file_drop_handler_enabled)
.finish()
}
}
impl WebviewAttributes {
pub fn new(url: WindowUrl) -> Self {
Self {
url,
initialization_scripts: Vec::new(),
data_directory: None,
uri_scheme_protocols: Default::default(),
file_drop_handler_enabled: true,
}
}
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 fn disable_file_drop_handler(mut self) -> Self {
self.file_drop_handler_enabled = false;
self
}
}
pub trait WindowBuilderBase: fmt::Debug + Sized {}
pub trait WindowBuilder: WindowBuilderBase {
fn new() -> Self;
fn with_config(config: WindowConfig) -> Self;
fn menu(self, menu: Menu) -> Self;
fn center(self) -> 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, max_width: f64, max_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 focus(self) -> 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 skip_taskbar(self, skip: bool) -> Self;
#[cfg(windows)]
fn parent_window(self, parent: HWND) -> Self;
#[cfg(windows)]
fn owner_window(self, owner: HWND) -> Self;
fn has_icon(&self) -> bool;
fn has_menu(&self) -> bool;
}
#[derive(Debug)]
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<R> = Box<dyn Fn(DetachedWindow<R>, RpcRequest) + Send>;
pub type FileDropHandler<R> = Box<dyn Fn(FileDropEvent, DetachedWindow<R>) -> bool + Send>;
#[derive(Debug, Deserialize)]
pub struct InvokePayload {
#[serde(rename = "__tauriModule")]
pub tauri_module: Option<String>,
pub callback: String,
pub error: String,
#[serde(rename = "__invokeKey")]
pub key: u32,
#[serde(flatten)]
pub inner: JsonValue,
}