wip
This commit is contained in:
parent
e5b7d397f0
commit
e638b5c289
12 changed files with 247 additions and 1 deletions
108
src/window.rs
108
src/window.rs
|
@ -1,3 +1,48 @@
|
|||
//! Provides APIs to create windows, communicate with other windows and manipulate the current window.
|
||||
//!
|
||||
//! The APIs must be added to tauri.allowlist.window in tauri.conf.json:
|
||||
//! ```json
|
||||
//! {
|
||||
//! "tauri": {
|
||||
//! "allowlist": {
|
||||
//! "window": {
|
||||
//! "all": true, // enable all window APIs
|
||||
//! "create": true, // enable window creation
|
||||
//! "center": true,
|
||||
//! "requestUserAttention": true,
|
||||
//! "setResizable": true,
|
||||
//! "setTitle": true,
|
||||
//! "maximize": true,
|
||||
//! "unmaximize": true,
|
||||
//! "minimize": true,
|
||||
//! "unminimize": true,
|
||||
//! "show": true,
|
||||
//! "hide": true,
|
||||
//! "close": true,
|
||||
//! "setDecorations": true,
|
||||
//! "setAlwaysOnTop": true,
|
||||
//! "setSize": true,
|
||||
//! "setMinSize": true,
|
||||
//! "setMaxSize": true,
|
||||
//! "setPosition": true,
|
||||
//! "setFullscreen": true,
|
||||
//! "setFocus": true,
|
||||
//! "setIcon": true,
|
||||
//! "setSkipTaskbar": true,
|
||||
//! "setCursorGrab": true,
|
||||
//! "setCursorVisible": true,
|
||||
//! "setCursorIcon": true,
|
||||
//! "setCursorPosition": true,
|
||||
//! "setIgnoreCursorEvents": true,
|
||||
//! "startDragging": true,
|
||||
//! "print": true
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
//! It is recommended to allowlist only the APIs you use for optimal bundle size and security.
|
||||
|
||||
use crate::event::Event;
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
use std::fmt::Display;
|
||||
|
@ -310,6 +355,9 @@ impl<'a> WebviewWindowBuilder<'a> {
|
|||
self.inner.user_agent = Some(user_agent);
|
||||
}
|
||||
|
||||
/// Creates a new webview window.
|
||||
///
|
||||
/// Requires [`allowlist > window > create`](https://tauri.app/v1/api/config#windowallowlistconfig.create) to be enabled.
|
||||
pub fn build(self) -> crate::Result<WebviewWindow> {
|
||||
let opts = serde_wasm_bindgen::to_value(&self.inner)?;
|
||||
|
||||
|
@ -414,6 +462,8 @@ impl WebviewWindow {
|
|||
}
|
||||
|
||||
/// Centers the window.
|
||||
///
|
||||
/// Requires [`allowlist > window > center`](https://tauri.app/v1/api/config#windowallowlistconfig.center) to be enabled.
|
||||
pub async fn center(&self) -> crate::Result<()> {
|
||||
Ok(self.0.center().await?)
|
||||
}
|
||||
|
@ -425,6 +475,8 @@ impl WebviewWindow {
|
|||
/// #### Platform-specific
|
||||
/// - macOS: None has no effect.
|
||||
/// - Linux: Urgency levels have the same effect.
|
||||
///
|
||||
/// Requires [`allowlist > window > requestUserAttention`](https://tauri.app/v1/api/config#windowallowlistconfig.requestuserattention) to be enabled.
|
||||
pub async fn request_user_attention(
|
||||
&self,
|
||||
request_type: UserAttentionType,
|
||||
|
@ -432,27 +484,39 @@ impl WebviewWindow {
|
|||
Ok(self.0.requestUserAttention(request_type as u32).await?)
|
||||
}
|
||||
|
||||
/// Opens the dialog to prints the contents of the webview. Currently only supported on macOS on wry. window.print() works on all platforms.
|
||||
/// Opens the dialog to prints the contents of the webview.
|
||||
///
|
||||
/// Currently only supported on macOS on wry. window.print() works on all platforms.
|
||||
///
|
||||
/// Requires [`allowlist > window > print`](https://tauri.app/v1/api/config#windowallowlistconfig.print) to be enabled.
|
||||
pub fn print(&self) -> crate::Result<()> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
/// Determines if this window should be resizable.
|
||||
///
|
||||
/// Requires [`allowlist > window > setResizable`](https://tauri.app/v1/api/config#windowallowlistconfig.setresizable) to be enabled.
|
||||
pub async fn set_resizable(&self, resizable: bool) -> crate::Result<()> {
|
||||
Ok(self.0.setResizable(resizable).await?)
|
||||
}
|
||||
|
||||
/// Set this window’s title.
|
||||
///
|
||||
/// Requires [`allowlist > window > setTitle`](https://tauri.app/v1/api/config#windowallowlistconfig.settitle) to be enabled.
|
||||
pub async fn set_title(&self, title: impl AsRef<str>) -> crate::Result<()> {
|
||||
Ok(self.0.setTitle(title.as_ref()).await?)
|
||||
}
|
||||
|
||||
/// Maximizes this window.
|
||||
///
|
||||
/// Requires [`allowlist > window > maximize`](https://tauri.app/v1/api/config#windowallowlistconfig.maximize) to be enabled.
|
||||
pub async fn maximize(&self) -> crate::Result<()> {
|
||||
Ok(self.0.maximize().await?)
|
||||
}
|
||||
|
||||
/// Un-maximizes this window.
|
||||
///
|
||||
/// Requires [`allowlist > window > unmaximize`](https://tauri.app/v1/api/config#windowallowlistconfig.unmaximize) to be enabled.
|
||||
pub async fn unmaximize(&self) -> crate::Result<()> {
|
||||
Ok(self.0.unmaximize().await?)
|
||||
}
|
||||
|
@ -462,41 +526,57 @@ impl WebviewWindow {
|
|||
}
|
||||
|
||||
/// Minimizes this window.
|
||||
///
|
||||
/// Requires [`allowlist > window > minimize`](https://tauri.app/v1/api/config#windowallowlistconfig.minimize) to be enabled.
|
||||
pub async fn minimize(&self) -> crate::Result<()> {
|
||||
Ok(self.0.minimize().await?)
|
||||
}
|
||||
|
||||
/// Un-minimizes this window.
|
||||
///
|
||||
/// Requires [`allowlist > window > unminimize`](https://tauri.app/v1/api/config#windowallowlistconfig.unminimize) to be enabled.
|
||||
pub async fn unminimize(&self) -> crate::Result<()> {
|
||||
Ok(self.0.unminimize().await?)
|
||||
}
|
||||
|
||||
/// Show this window.
|
||||
///
|
||||
/// Requires [`allowlist > window > show`](https://tauri.app/v1/api/config#windowallowlistconfig.show) to be enabled.
|
||||
pub async fn show(&self) -> crate::Result<()> {
|
||||
Ok(self.0.show().await?)
|
||||
}
|
||||
|
||||
/// Hide this window.
|
||||
///
|
||||
/// Requires [`allowlist > window > hide`](https://tauri.app/v1/api/config#windowallowlistconfig.hide) to be enabled.
|
||||
pub async fn hide(&self) -> crate::Result<()> {
|
||||
Ok(self.0.hide().await?)
|
||||
}
|
||||
|
||||
/// Closes this window.
|
||||
///
|
||||
/// Requires [`allowlist > window > close`](https://tauri.app/v1/api/config#windowallowlistconfig.close) to be enabled.
|
||||
pub async fn close(&self) -> crate::Result<()> {
|
||||
Ok(self.0.close().await?)
|
||||
}
|
||||
|
||||
/// Determines if this window should be [decorated](https://en.wikipedia.org/wiki/Window_(computing)#Window_decoration).
|
||||
///
|
||||
/// Requires [`allowlist > window > setDecorations`](https://tauri.app/v1/api/config#windowallowlistconfig.setdecorations) to be enabled.
|
||||
pub async fn set_decorations(&self, decorations: bool) -> crate::Result<()> {
|
||||
Ok(self.0.setDecorations(decorations).await?)
|
||||
}
|
||||
|
||||
/// Determines if this window should always be on top of other windows.
|
||||
///
|
||||
/// Requires [`allowlist > window > setAlwaysOnTop`](https://tauri.app/v1/api/config#windowallowlistconfig.setalwaysontop) to be enabled.
|
||||
pub async fn set_always_on_top(&self, always_on_top: bool) -> crate::Result<()> {
|
||||
Ok(self.0.setAlwaysOnTop(always_on_top).await?)
|
||||
}
|
||||
|
||||
/// Resizes this window.
|
||||
///
|
||||
/// Requires [`allowlist > window > setSize`](https://tauri.app/v1/api/config#windowallowlistconfig.setsize) to be enabled.
|
||||
pub async fn set_size(&self, size: impl Into<Size>) -> crate::Result<()> {
|
||||
match size.into() {
|
||||
Size::Physical(size) => self.0.setSizePhysical(size.0).await?,
|
||||
|
@ -507,6 +587,8 @@ impl WebviewWindow {
|
|||
}
|
||||
|
||||
/// Sets this window’s minimum size.
|
||||
///
|
||||
/// Requires [`allowlist > window > setMinSize`](https://tauri.app/v1/api/config#windowallowlistconfig.setminsize) to be enabled.
|
||||
pub async fn set_min_size(&self, size: Option<impl Into<Size>>) -> crate::Result<()> {
|
||||
match size.map(Into::into) {
|
||||
None => self.0.setMinSizePhysical(None).await?,
|
||||
|
@ -518,6 +600,8 @@ impl WebviewWindow {
|
|||
}
|
||||
|
||||
/// Sets this window’s maximum size.
|
||||
///
|
||||
/// Requires [`allowlist > window > setMaxSize`](https://tauri.app/v1/api/config#windowallowlistconfig.setmaxsize) to be enabled.
|
||||
pub async fn set_max_size(&self, size: Option<impl Into<Size>>) -> crate::Result<()> {
|
||||
match size.map(Into::into) {
|
||||
None => self.0.setMaxSizePhysical(None).await?,
|
||||
|
@ -529,6 +613,8 @@ impl WebviewWindow {
|
|||
}
|
||||
|
||||
/// Sets this window’s position.
|
||||
///
|
||||
/// Requires [`allowlist > window > setPosition`](https://tauri.app/v1/api/config#windowallowlistconfig.setposition) to be enabled.
|
||||
pub async fn set_position(&self, position: impl Into<Position>) -> crate::Result<()> {
|
||||
match position.into() {
|
||||
Position::Physical(pos) => self.0.setPositionPhysical(pos.0).await?,
|
||||
|
@ -539,21 +625,29 @@ impl WebviewWindow {
|
|||
}
|
||||
|
||||
/// Determines if this window should be fullscreen.
|
||||
///
|
||||
/// Requires [`allowlist > window > setFullscreen`](https://tauri.app/v1/api/config#windowallowlistconfig.setfullscreen) to be enabled.
|
||||
pub async fn set_fullscreen(&self, fullscreen: bool) -> crate::Result<()> {
|
||||
Ok(self.0.setFullscreen(fullscreen).await?)
|
||||
}
|
||||
|
||||
/// Bring the window to front and focus.
|
||||
///
|
||||
/// Requires [`allowlist > window > setFocus`](https://tauri.app/v1/api/config#windowallowlistconfig.setfocus) to be enabled.
|
||||
pub async fn set_focus(&self) -> crate::Result<()> {
|
||||
Ok(self.0.setFocus().await?)
|
||||
}
|
||||
|
||||
/// Sets this window’ icon.
|
||||
///
|
||||
/// Requires [`allowlist > window > setIcon`](https://tauri.app/v1/api/config#windowallowlistconfig.seticon) to be enabled.
|
||||
pub async fn set_icon(&self, icon: &[u8]) -> crate::Result<()> {
|
||||
Ok(self.0.setIcon(icon).await?)
|
||||
}
|
||||
|
||||
/// Whether to show the window icon in the task bar or not.
|
||||
///
|
||||
/// Requires [`allowlist > window > setSkipTaskbar`](https://tauri.app/v1/api/config#windowallowlistconfig.setskiptaskbar) to be enabled.
|
||||
pub async fn set_skip_taskbar(&self, skip: bool) -> crate::Result<()> {
|
||||
Ok(self.0.setSkipTaskbar(skip).await?)
|
||||
}
|
||||
|
@ -565,6 +659,8 @@ impl WebviewWindow {
|
|||
/// #### Platform-specific
|
||||
/// - Linux: Unsupported.
|
||||
/// - macOS: This locks the cursor in a fixed location, which looks visually awkward.
|
||||
///
|
||||
/// Requires [`allowlist > window > setCursorGrab`](https://tauri.app/v1/api/config#windowallowlistconfig.setcursorgrab) to be enabled.
|
||||
pub async fn set_cursor_grab(&self, grab: bool) -> crate::Result<()> {
|
||||
Ok(self.0.setCursorGrab(grab).await?)
|
||||
}
|
||||
|
@ -576,16 +672,22 @@ impl WebviewWindow {
|
|||
/// #### Platform-specific
|
||||
/// - Windows: The cursor is only hidden within the confines of the window.
|
||||
/// - macOS: The cursor is hidden as long as the window has input focus, even if the cursor is outside of the window.
|
||||
///
|
||||
/// Requires [`allowlist > window > setCursorVisible`](https://tauri.app/v1/api/config#windowallowlistconfig.setcursorvisible) to be enabled.
|
||||
pub async fn set_cursor_visible(&self, visible: bool) -> crate::Result<()> {
|
||||
Ok(self.0.setCursorVisible(visible).await?)
|
||||
}
|
||||
|
||||
/// Modifies the cursor icon of the window.
|
||||
///
|
||||
/// Requires [`allowlist > window > setCursorIcon`](https://tauri.app/v1/api/config#windowallowlistconfig.setcursoricon) to be enabled.
|
||||
pub async fn set_cursor_icon(&self, icon: CursorIcon) -> crate::Result<()> {
|
||||
Ok(self.0.setCursorIcon(&icon.to_string()).await?)
|
||||
}
|
||||
|
||||
/// Changes the position of the cursor in window coordinates.
|
||||
///
|
||||
/// Requires [`allowlist > window > setCursorPosition`](https://tauri.app/v1/api/config#windowallowlistconfig.setcursorposition) to be enabled.
|
||||
pub async fn set_cursor_position(&self, position: Position) -> crate::Result<()> {
|
||||
match position {
|
||||
Position::Physical(pos) => self.0.setCursorPositionPhysical(pos.0).await?,
|
||||
|
@ -596,11 +698,15 @@ impl WebviewWindow {
|
|||
}
|
||||
|
||||
/// Ignores the window cursor events.
|
||||
///
|
||||
/// Requires [`allowlist > window > setIgnoreCursorEvents`](https://tauri.app/v1/api/config#windowallowlistconfig.setignorecursorevents) to be enabled.
|
||||
pub async fn set_ignore_cursor_events(&self, ignore: bool) -> crate::Result<()> {
|
||||
Ok(self.0.setIgnoreCursorEvents(ignore).await?)
|
||||
}
|
||||
|
||||
/// Starts dragging the window.
|
||||
///
|
||||
/// Requires [`allowlist > window > startDragging`](https://tauri.app/v1/api/config#windowallowlistconfig.startdragging) to be enabled.
|
||||
pub async fn start_dragging(&self) -> crate::Result<()> {
|
||||
Ok(self.0.startDragging().await?)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue