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::{fmt, path::PathBuf};
pub struct WebviewAttributes {
pub url: WindowUrl,
pub initialization_scripts: Vec<String>,
pub data_directory: Option<PathBuf>,
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,
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 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>,
}
#[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,
}