diff --git a/src/content/docs/features/window-state.mdx b/src/content/docs/features/window-state.mdx index 75850fe1a3..3004b2c422 100644 --- a/src/content/docs/features/window-state.mdx +++ b/src/content/docs/features/window-state.mdx @@ -3,12 +3,140 @@ title: Window State description: Persist window sizes and positions. --- -import Stub from '@components/Stub.astro'; import PluginLinks from '@components/PluginLinks.astro'; +import { Tabs, TabItem } from '@astrojs/starlight/components'; +import CommandTabs from '@components/CommandTabs.astro'; - - Based on - https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/window-state - +Save window positions and sizes and restore them when the app is reopened. + +## Supported Platforms + +- Windows +- Linux +- macOS + +## Setup + +_This plugin requires a Rust version of at least **1.75**_ + +Install the window-state plugin to get started. + + + + +Use your project's package manager to add the dependency: + +{' '} + + + + + + +1. Install the Core plugin by adding the following to your `Cargo.toml` file: + +```toml title="src-tauri/Cargo.toml" +[dependencies] +tauri-plugin-window-state = "2.0.0-beta" +# alternatively with Git: +tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } +``` + +2. Modify `lib.rs` to initialize the plugin: + +```rust title="src-tauri/src/main.rs" {3} +fn main() { + tauri::Builder::default() + .plugin(tauri_plugin_window_state::Builder::default().build()) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); +} +``` + +3. Install the JavaScript Guest bindings using your preferred JavaScript package manager: + + + + + +## Usage + +After adding the all windows will remember their state when the app is being closed and will restore to their previous state on the next launch. + +You can also access the window-state plugin in both JavaScript and Rust. + +### JavaScript + +You can use `saveWindowState` to manually save the window state: + +```javascript +import { saveWindowState, StateFlags } from "@tauri-apps/plugin-window-state"; + +saveWindowState(StateFlags.ALL); +``` + +Similarly you can manually restore a window's state from disk: + +```javascript +import { + restoreStateCurrent, + StateFlags, +} from "@tauri-apps/plugin-window-state"; + +restoreStateCurrent(StateFlags.ALL); +``` + +### Rust + +You can use the `save_window_state()` method exposed by the `AppHandleExt` trait: + +```rust +use tauri_plugin_window_state::{AppHandleExt, StateFlags}; + +// `tauri::AppHandle` now has the following additional method +app.save_window_state(StateFlags::all()); // will save the state of all open windows to disk +``` + +Similarly you can manually restore a window's state from disk using the `restore_state()` method exposed by the `WindowExt` trait: + +```rust +use tauri_plugin_window_state::{WindowExt, StateFlags}; + +// all `Window` types now have the following additional method +window.restore_state(StateFlags::all()); // will restore the window's state from disk +``` + +## Permissions + +By default all plugin commands are blocked and cannot be accessed. +You must define a list of permissions in your `capabilities` configuration. + +See [Access Control List](/references/v2/acl) for more information. + +```json title="src-tauri/capabilities/main.json" ins={4-5} +{ + "permissions": [ + ..., + "window-state:allow-restore-window-state", + "window-state:allow-save-window-state", + ] +} +``` + +| Permission | Description | +| ----------------------------- | ----------------------------------------------------------------- | +| `window-state:allow-restore-window-state` | Enables the restore_window_state command without any pre-configured scope. | +| `window-state:deny-restore-window-state` | Denies the restore_window_state command without any pre-configured scope. | +| `window-state:allow-save-window-state` | Enables the save_window_state command without any pre-configured scope. | +| `window-state:deny-save-window-state` | Denies the save_window_state command without any pre-configured scope. | \ No newline at end of file