From 4b3569c6ad882852ee2a664e061345c303808461 Mon Sep 17 00:00:00 2001 From: DK Liao Date: Mon, 4 Mar 2024 19:01:59 +0800 Subject: [PATCH 1/6] Update window-state.mdx --- src/content/docs/features/window-state.mdx | 133 ++++++++++++++++++++- 1 file changed, 128 insertions(+), 5 deletions(-) diff --git a/src/content/docs/features/window-state.mdx b/src/content/docs/features/window-state.mdx index 75850fe1a3..b853425766 100644 --- a/src/content/docs/features/window-state.mdx +++ b/src/content/docs/features/window-state.mdx @@ -3,12 +3,135 @@ 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" +# you can add the dependencies on the `[dependencies]` section if you do not target mobile +[target."cfg(not(any(target_os = \"android\", target_os = \"ios\")))".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. Install the JavaScript Guest bindings using your preferred JavaScript package manager: + + + + + +## Usage + +First you need to register the core plugin with Tauri: + +```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"); +} +``` + +Afterwards all windows will remember their state when the app is being closed and will restore to their previous state on the next launch. + +Optionally you can also tell the plugin to save the state of all open window to disk by using 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 +``` + +or through Javascript + +```javascript +import { saveWindowState, StateFlags } from "@tauri-apps/plugin-window-state"; + +saveWindowState(StateFlags.ALL); +``` + +To manually restore a windows state from disk you can call 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 windows state from disk +``` + +or through Javascript + +```javascript +import { + restoreStateCurrent, + StateFlags, +} from "@tauri-apps/plugin-window-state"; + +restoreStateCurrent(StateFlags.ALL); +``` + +## 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 From 235865e867480799c839d0eacc8c81e1185b041e Mon Sep 17 00:00:00 2001 From: DK Liao Date: Mon, 4 Mar 2024 19:22:05 +0800 Subject: [PATCH 2/6] Update window-state.mdx --- src/content/docs/features/window-state.mdx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/content/docs/features/window-state.mdx b/src/content/docs/features/window-state.mdx index b853425766..de111088d5 100644 --- a/src/content/docs/features/window-state.mdx +++ b/src/content/docs/features/window-state.mdx @@ -43,8 +43,7 @@ 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" -# you can add the dependencies on the `[dependencies]` section if you do not target mobile -[target."cfg(not(any(target_os = \"android\", target_os = \"ios\")))".dependencies] +[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" } From f0416f228811a94547daf51d6ad9e47d68043ad1 Mon Sep 17 00:00:00 2001 From: DK Liao Date: Mon, 4 Mar 2024 22:58:20 +0800 Subject: [PATCH 3/6] Update using tabs to show code --- src/content/docs/features/window-state.mdx | 46 +++++++++++++++------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/content/docs/features/window-state.mdx b/src/content/docs/features/window-state.mdx index de111088d5..22a2b2b4c8 100644 --- a/src/content/docs/features/window-state.mdx +++ b/src/content/docs/features/window-state.mdx @@ -74,16 +74,10 @@ fn main() { Afterwards all windows will remember their state when the app is being closed and will restore to their previous state on the next launch. -Optionally you can also tell the plugin to save the state of all open window to disk by using the `save_window_state()` method exposed by the `AppHandleExt` trait: +Optionally the window-state plugin can also be access with both JavaScript and Rust. -```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 -``` - -or through Javascript + + ```javascript import { saveWindowState, StateFlags } from "@tauri-apps/plugin-window-state"; @@ -91,16 +85,24 @@ import { saveWindowState, StateFlags } from "@tauri-apps/plugin-window-state"; saveWindowState(StateFlags.ALL); ``` -To manually restore a windows state from disk you can call the `restore_state()` method exposed by the `WindowExt` trait: + + +You can use the `save_window_state()` method exposed by the `AppHandleExt` trait: ```rust -use tauri_plugin_window_state::{WindowExt, StateFlags}; +use tauri_plugin_window_state::{AppHandleExt, StateFlags}; -// all `Window` types now have the following additional method -window.restore_state(StateFlags::all()); // will restore the windows state from disk +// `tauri::AppHandle` now has the following additional method +app.save_window_state(StateFlags::all()); // will save the state of all open windows to disk ``` -or through Javascript + + + +Similarly you can manually restore a windows state from disk through JavaScript and Rust. + + + ```javascript import { @@ -111,6 +113,22 @@ import { restoreStateCurrent(StateFlags.ALL); ``` + + + +You can call 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 windows state from disk +``` + + + + + ## Permissions By default all plugin commands are blocked and cannot be accessed. From c5144eb234344849985283ae8b8838c2fe2af994 Mon Sep 17 00:00:00 2001 From: DK Liao Date: Mon, 4 Mar 2024 23:12:42 +0800 Subject: [PATCH 4/6] Update window-state.mdx --- src/content/docs/features/window-state.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/features/window-state.mdx b/src/content/docs/features/window-state.mdx index 22a2b2b4c8..e1e5759664 100644 --- a/src/content/docs/features/window-state.mdx +++ b/src/content/docs/features/window-state.mdx @@ -74,7 +74,7 @@ fn main() { Afterwards all windows will remember their state when the app is being closed and will restore to their previous state on the next launch. -Optionally the window-state plugin can also be access with both JavaScript and Rust. +Optionally the window-state plugin can also be accessed through both JavaScript and Rust. From b8d95e37b45f9978e615e2b26a87a56e26ca036f Mon Sep 17 00:00:00 2001 From: DK Liao Date: Tue, 5 Mar 2024 00:55:49 +0800 Subject: [PATCH 5/6] Update window-state.mdx --- src/content/docs/features/window-state.mdx | 68 +++++++++------------- 1 file changed, 28 insertions(+), 40 deletions(-) diff --git a/src/content/docs/features/window-state.mdx b/src/content/docs/features/window-state.mdx index e1e5759664..0a79d76cc6 100644 --- a/src/content/docs/features/window-state.mdx +++ b/src/content/docs/features/window-state.mdx @@ -49,7 +49,18 @@ tauri-plugin-window-state = "2.0.0-beta" tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } ``` -2. Install the JavaScript Guest bindings using your preferred JavaScript package manager: +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: - +You can use `saveWindowState` to manually save the window state: ```javascript import { saveWindowState, StateFlags } from "@tauri-apps/plugin-window-state"; @@ -85,24 +86,7 @@ import { saveWindowState, StateFlags } from "@tauri-apps/plugin-window-state"; saveWindowState(StateFlags.ALL); ``` - - - -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 windows state from disk through JavaScript and Rust. - - - +Similarly you can manually restore a windows state from disk: ```javascript import { @@ -113,10 +97,18 @@ import { restoreStateCurrent(StateFlags.ALL); ``` - - +### Rust + +You can use the `save_window_state()` method exposed by the `AppHandleExt` trait: -You can call the `restore_state()` method exposed by the `WindowExt` 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 windows state from disk using the `restore_state()` method exposed by the `WindowExt` trait: ```rust use tauri_plugin_window_state::{WindowExt, StateFlags}; @@ -125,10 +117,6 @@ use tauri_plugin_window_state::{WindowExt, StateFlags}; window.restore_state(StateFlags::all()); // will restore the windows state from disk ``` - - - - ## Permissions By default all plugin commands are blocked and cannot be accessed. From 8e8404e36dd367945798c9796ec843a5d6dcc012 Mon Sep 17 00:00:00 2001 From: DK Liao Date: Tue, 5 Mar 2024 01:07:31 +0800 Subject: [PATCH 6/6] Fix typo --- src/content/docs/features/window-state.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/content/docs/features/window-state.mdx b/src/content/docs/features/window-state.mdx index 0a79d76cc6..3004b2c422 100644 --- a/src/content/docs/features/window-state.mdx +++ b/src/content/docs/features/window-state.mdx @@ -86,7 +86,7 @@ import { saveWindowState, StateFlags } from "@tauri-apps/plugin-window-state"; saveWindowState(StateFlags.ALL); ``` -Similarly you can manually restore a windows state from disk: +Similarly you can manually restore a window's state from disk: ```javascript import { @@ -108,13 +108,13 @@ use tauri_plugin_window_state::{AppHandleExt, StateFlags}; app.save_window_state(StateFlags::all()); // will save the state of all open windows to disk ``` -Similarly you can manually restore a windows state from disk using the `restore_state()` method exposed by the `WindowExt` trait: +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 windows state from disk +window.restore_state(StateFlags::all()); // will restore the window's state from disk ``` ## Permissions