fix(window-state)!: saving minimized state (#1743)

* Fix saving minimized state again

* Add change file

* Fix saving minimized size

* Add change file

* Fix missing .md

* Remove window-shadows
pull/1774/head
Tony 9 months ago committed by GitHub
parent 8a45c35160
commit 204e5aacad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"window-state": patch
---
Fix can't restore a minimized window's size and position properly

@ -0,0 +1,5 @@
---
"window-state": patch:breaking
---
Window's size is now stored in physical size instead of logical size

84
Cargo.lock generated

@ -235,8 +235,8 @@ dependencies = [
"tauri-plugin-shell", "tauri-plugin-shell",
"tauri-plugin-store", "tauri-plugin-store",
"tauri-plugin-updater", "tauri-plugin-updater",
"tauri-plugin-window-state",
"tiny_http", "tiny_http",
"window-shadows",
] ]
[[package]] [[package]]
@ -1025,22 +1025,6 @@ dependencies = [
"error-code", "error-code",
] ]
[[package]]
name = "cocoa"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6140449f97a6e97f9511815c5632d84c8aacf8ac271ad77c559218161a1373c"
dependencies = [
"bitflags 1.3.2",
"block",
"cocoa-foundation 0.1.2",
"core-foundation 0.9.4",
"core-graphics 0.23.2",
"foreign-types 0.5.0",
"libc",
"objc",
]
[[package]] [[package]]
name = "cocoa" name = "cocoa"
version = "0.26.0" version = "0.26.0"
@ -1049,7 +1033,7 @@ checksum = "f79398230a6e2c08f5c9760610eb6924b52aa9e7950a619602baba59dcbbdbb2"
dependencies = [ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"block", "block",
"cocoa-foundation 0.2.0", "cocoa-foundation",
"core-foundation 0.10.0", "core-foundation 0.10.0",
"core-graphics 0.24.0", "core-graphics 0.24.0",
"foreign-types 0.5.0", "foreign-types 0.5.0",
@ -1057,20 +1041,6 @@ dependencies = [
"objc", "objc",
] ]
[[package]]
name = "cocoa-foundation"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c6234cbb2e4c785b456c0644748b1ac416dd045799740356f8363dfe00c93f7"
dependencies = [
"bitflags 1.3.2",
"block",
"core-foundation 0.9.4",
"core-graphics-types 0.1.3",
"libc",
"objc",
]
[[package]] [[package]]
name = "cocoa-foundation" name = "cocoa-foundation"
version = "0.2.0" version = "0.2.0"
@ -3659,7 +3629,7 @@ version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba8ac4080fb1e097c2c22acae467e46e4da72d941f02e82b67a87a2a89fa38b1" checksum = "ba8ac4080fb1e097c2c22acae467e46e4da72d941f02e82b67a87a2a89fa38b1"
dependencies = [ dependencies = [
"cocoa 0.26.0", "cocoa",
"crossbeam-channel", "crossbeam-channel",
"dpi", "dpi",
"gtk", "gtk",
@ -3700,7 +3670,7 @@ dependencies = [
"log", "log",
"ndk-sys", "ndk-sys",
"num_enum", "num_enum",
"raw-window-handle 0.6.2", "raw-window-handle",
"thiserror", "thiserror",
] ]
@ -4824,12 +4794,6 @@ dependencies = [
"rand_core 0.5.1", "rand_core 0.5.1",
] ]
[[package]]
name = "raw-window-handle"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9"
[[package]] [[package]]
name = "raw-window-handle" name = "raw-window-handle"
version = "0.6.2" version = "0.6.2"
@ -5024,7 +4988,7 @@ dependencies = [
"objc", "objc",
"objc-foundation", "objc-foundation",
"objc_id", "objc_id",
"raw-window-handle 0.6.2", "raw-window-handle",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"web-sys", "web-sys",
@ -5711,7 +5675,7 @@ dependencies = [
"objc2", "objc2",
"objc2-foundation", "objc2-foundation",
"objc2-quartz-core", "objc2-quartz-core",
"raw-window-handle 0.6.2", "raw-window-handle",
"redox_syscall", "redox_syscall",
"wasm-bindgen", "wasm-bindgen",
"web-sys", "web-sys",
@ -6229,7 +6193,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a93f2c6b8fdaeb7f417bda89b5bc767999745c3052969664ae1fa65892deb7e" checksum = "2a93f2c6b8fdaeb7f417bda89b5bc767999745c3052969664ae1fa65892deb7e"
dependencies = [ dependencies = [
"bitflags 2.6.0", "bitflags 2.6.0",
"cocoa 0.26.0", "cocoa",
"core-foundation 0.10.0", "core-foundation 0.10.0",
"core-graphics 0.24.0", "core-graphics 0.24.0",
"crossbeam-channel", "crossbeam-channel",
@ -6250,7 +6214,7 @@ dependencies = [
"objc", "objc",
"once_cell", "once_cell",
"parking_lot", "parking_lot",
"raw-window-handle 0.6.2", "raw-window-handle",
"scopeguard", "scopeguard",
"tao-macros", "tao-macros",
"unicode-segmentation", "unicode-segmentation",
@ -6303,7 +6267,7 @@ checksum = "4f60dc86a0513f775a6515d79cf2a54ce38f2fa7225e0c5b9d5ae8241e599afa"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bytes", "bytes",
"cocoa 0.26.0", "cocoa",
"dirs 5.0.1", "dirs 5.0.1",
"dunce", "dunce",
"embed_plist", "embed_plist",
@ -6322,7 +6286,7 @@ dependencies = [
"muda", "muda",
"objc", "objc",
"percent-encoding", "percent-encoding",
"raw-window-handle 0.6.2", "raw-window-handle",
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
@ -6518,7 +6482,7 @@ name = "tauri-plugin-dialog"
version = "2.0.0-rc.6" version = "2.0.0-rc.6"
dependencies = [ dependencies = [
"log", "log",
"raw-window-handle 0.6.2", "raw-window-handle",
"rfd", "rfd",
"serde", "serde",
"serde_json", "serde_json",
@ -6628,7 +6592,7 @@ version = "2.0.0-rc.2"
dependencies = [ dependencies = [
"android_logger", "android_logger",
"byte-unit", "byte-unit",
"cocoa 0.26.0", "cocoa",
"fern", "fern",
"log", "log",
"objc", "objc",
@ -6897,7 +6861,7 @@ dependencies = [
"gtk", "gtk",
"http", "http",
"jni", "jni",
"raw-window-handle 0.6.2", "raw-window-handle",
"serde", "serde",
"serde_json", "serde_json",
"tauri-utils", "tauri-utils",
@ -6912,13 +6876,13 @@ version = "2.0.0-rc.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78dd7f77e769630da5d91a55e4f102a84ff9c5a99c71e1b5c916a18b5ccafc16" checksum = "78dd7f77e769630da5d91a55e4f102a84ff9c5a99c71e1b5c916a18b5ccafc16"
dependencies = [ dependencies = [
"cocoa 0.26.0", "cocoa",
"gtk", "gtk",
"http", "http",
"jni", "jni",
"log", "log",
"percent-encoding", "percent-encoding",
"raw-window-handle 0.6.2", "raw-window-handle",
"softbuffer", "softbuffer",
"tao", "tao",
"tauri-runtime", "tauri-runtime",
@ -7995,18 +7959,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "window-shadows"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67ff424735b1ac21293b0492b069394b0a189c8a463fb015a16dea7c2e221c08"
dependencies = [
"cocoa 0.25.0",
"objc",
"raw-window-handle 0.5.2",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "window-vibrancy" name = "window-vibrancy"
version = "0.5.2" version = "0.5.2"
@ -8016,7 +7968,7 @@ dependencies = [
"objc2", "objc2",
"objc2-app-kit", "objc2-app-kit",
"objc2-foundation", "objc2-foundation",
"raw-window-handle 0.6.2", "raw-window-handle",
"windows-sys 0.59.0", "windows-sys 0.59.0",
"windows-version", "windows-version",
] ]
@ -8469,7 +8421,7 @@ checksum = "f4d715cf5fe88e9647f3d17b207b6d060d4a88e7171d4ccb2d2c657dd1d44728"
dependencies = [ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"block", "block",
"cocoa 0.26.0", "cocoa",
"core-graphics 0.24.0", "core-graphics 0.24.0",
"crossbeam-channel", "crossbeam-channel",
"dpi", "dpi",
@ -8487,7 +8439,7 @@ dependencies = [
"objc_id", "objc_id",
"once_cell", "once_cell",
"percent-encoding", "percent-encoding",
"raw-window-handle 0.6.2", "raw-window-handle",
"sha2", "sha2",
"soup3", "soup3",
"tao-macros", "tao-macros",

@ -53,14 +53,12 @@ features = [
tauri-plugin-cli = { path = "../../../plugins/cli", version = "2.0.0-rc.1" } tauri-plugin-cli = { path = "../../../plugins/cli", version = "2.0.0-rc.1" }
tauri-plugin-global-shortcut = { path = "../../../plugins/global-shortcut", version = "2.0.0-rc.2" } tauri-plugin-global-shortcut = { path = "../../../plugins/global-shortcut", version = "2.0.0-rc.2" }
tauri-plugin-updater = { path = "../../../plugins/updater", version = "2.0.0-rc.3" } tauri-plugin-updater = { path = "../../../plugins/updater", version = "2.0.0-rc.3" }
tauri-plugin-window-state = { path = "../../../plugins/window-state", version = "2.0.0-rc.3" }
[target."cfg(any(target_os = \"android\", target_os = \"ios\"))".dependencies] [target."cfg(any(target_os = \"android\", target_os = \"ios\"))".dependencies]
tauri-plugin-barcode-scanner = { path = "../../../plugins/barcode-scanner/", version = "2.0.0-rc.4" } tauri-plugin-barcode-scanner = { path = "../../../plugins/barcode-scanner/", version = "2.0.0-rc.4" }
tauri-plugin-nfc = { path = "../../../plugins/nfc", version = "2.0.0-rc.3" } tauri-plugin-nfc = { path = "../../../plugins/nfc", version = "2.0.0-rc.3" }
tauri-plugin-biometric = { path = "../../../plugins/biometric/", version = "2.0.0-rc.3" } tauri-plugin-biometric = { path = "../../../plugins/biometric/", version = "2.0.0-rc.3" }
[target."cfg(target_os = \"windows\")".dependencies]
window-shadows = "0.2"
[features] [features]
prod = ["tauri/custom-protocol"] prod = ["tauri/custom-protocol"]

@ -7502,6 +7502,55 @@
"enum": [ "enum": [
"updater:deny-install" "updater:deny-install"
] ]
},
{
"description": "window-state:default -> This permission set configures what kind of\noperations are available from the window state plugin.\n\n#### Granted Permissions\n\nAll operations are enabled by default.\n\n",
"type": "string",
"enum": [
"window-state:default"
]
},
{
"description": "window-state:allow-filename -> Enables the filename command without any pre-configured scope.",
"type": "string",
"enum": [
"window-state:allow-filename"
]
},
{
"description": "window-state:allow-restore-state -> Enables the restore_state command without any pre-configured scope.",
"type": "string",
"enum": [
"window-state:allow-restore-state"
]
},
{
"description": "window-state:allow-save-window-state -> Enables the save_window_state command without any pre-configured scope.",
"type": "string",
"enum": [
"window-state:allow-save-window-state"
]
},
{
"description": "window-state:deny-filename -> Denies the filename command without any pre-configured scope.",
"type": "string",
"enum": [
"window-state:deny-filename"
]
},
{
"description": "window-state:deny-restore-state -> Denies the restore_state command without any pre-configured scope.",
"type": "string",
"enum": [
"window-state:deny-restore-state"
]
},
{
"description": "window-state:deny-save-window-state -> Denies the save_window_state command without any pre-configured scope.",
"type": "string",
"enum": [
"window-state:deny-save-window-state"
]
} }
] ]
}, },

@ -45,6 +45,8 @@ pub fn run() {
app.handle().plugin(tauri_plugin_cli::init())?; app.handle().plugin(tauri_plugin_cli::init())?;
app.handle() app.handle()
.plugin(tauri_plugin_global_shortcut::Builder::new().build())?; .plugin(tauri_plugin_global_shortcut::Builder::new().build())?;
app.handle()
.plugin(tauri_plugin_window_state::Builder::new().build())?;
app.handle() app.handle()
.plugin(tauri_plugin_updater::Builder::new().build())?; .plugin(tauri_plugin_updater::Builder::new().build())?;
} }
@ -63,8 +65,7 @@ pub fn run() {
.user_agent(&format!("Tauri API - {}", std::env::consts::OS)) .user_agent(&format!("Tauri API - {}", std::env::consts::OS))
.title("Tauri API Validation") .title("Tauri API Validation")
.inner_size(1000., 800.) .inner_size(1000., 800.)
.min_inner_size(600., 400.) .min_inner_size(600., 400.);
.content_protected(true);
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]

@ -1,5 +1,5 @@
{ {
"$schema": "../node_modules/@tauri-apps/cli/schema.json", "$schema": "../node_modules/@tauri-apps/cli/config.schema.json",
"productName": "Tauri API", "productName": "Tauri API",
"version": "2.0.0", "version": "2.0.0",
"identifier": "com.tauri.api", "identifier": "com.tauri.api",

@ -16,8 +16,8 @@ use bitflags::bitflags;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tauri::{ use tauri::{
plugin::{Builder as PluginBuilder, TauriPlugin}, plugin::{Builder as PluginBuilder, TauriPlugin},
LogicalSize, Manager, Monitor, PhysicalPosition, PhysicalSize, RunEvent, Runtime, Manager, Monitor, PhysicalPosition, PhysicalSize, RunEvent, Runtime, WebviewWindow, Window,
WebviewWindow, Window, WindowEvent, WindowEvent,
}; };
use std::{ use std::{
@ -72,8 +72,8 @@ struct PluginState {
#[derive(Debug, Deserialize, Serialize, PartialEq)] #[derive(Debug, Deserialize, Serialize, PartialEq)]
struct WindowState { struct WindowState {
width: f64, width: u32,
height: f64, height: u32,
x: i32, x: i32,
y: i32, y: i32,
// prev_x and prev_y are used to store position // prev_x and prev_y are used to store position
@ -181,7 +181,7 @@ impl<R: Runtime> WindowExt for Window<R> {
} }
if flags.contains(StateFlags::SIZE) { if flags.contains(StateFlags::SIZE) {
self.set_size(LogicalSize { self.set_size(PhysicalSize {
width: state.width, width: state.width,
height: state.height, height: state.height,
})?; })?;
@ -223,11 +223,7 @@ impl<R: Runtime> WindowExt for Window<R> {
let mut metadata = WindowState::default(); let mut metadata = WindowState::default();
if flags.contains(StateFlags::SIZE) { if flags.contains(StateFlags::SIZE) {
let scale_factor = self let size = self.inner_size()?;
.current_monitor()?
.map(|m| m.scale_factor())
.unwrap_or(1.);
let size = self.inner_size()?.to_logical(scale_factor);
metadata.width = size.width; metadata.width = size.width;
metadata.height = size.height; metadata.height = size.height;
} }
@ -278,10 +274,11 @@ impl<R: Runtime> WindowExtInternal for WebviewWindow<R> {
impl<R: Runtime> WindowExtInternal for Window<R> { impl<R: Runtime> WindowExtInternal for Window<R> {
fn update_state(&self, state: &mut WindowState, flags: StateFlags) -> tauri::Result<()> { fn update_state(&self, state: &mut WindowState, flags: StateFlags) -> tauri::Result<()> {
let is_maximized = match flags.intersects(StateFlags::MAXIMIZED | StateFlags::SIZE) { let is_maximized = flags
true => self.is_maximized()?, .intersects(StateFlags::MAXIMIZED | StateFlags::POSITION | StateFlags::SIZE)
false => false, && self.is_maximized()?;
}; let is_minimized =
flags.intersects(StateFlags::POSITION | StateFlags::SIZE) && self.is_minimized()?;
if flags.contains(StateFlags::MAXIMIZED) { if flags.contains(StateFlags::MAXIMIZED) {
state.maximized = is_maximized; state.maximized = is_maximized;
@ -299,21 +296,16 @@ impl<R: Runtime> WindowExtInternal for Window<R> {
state.visible = self.is_visible()?; state.visible = self.is_visible()?;
} }
if flags.contains(StateFlags::SIZE) { if flags.contains(StateFlags::SIZE) && !is_maximized && !is_minimized {
let scale_factor = self let size = self.inner_size()?;
.current_monitor()?
.map(|m| m.scale_factor())
.unwrap_or(1.);
let size = self.inner_size()?.to_logical(scale_factor);
// It doesn't make sense to save a window with 0 height or width // It doesn't make sense to save a window with 0 height or width
if size.width > 0. && size.height > 0. && !is_maximized { if size.width > 0 && size.height > 0 {
state.width = size.width; state.width = size.width;
state.height = size.height; state.height = size.height;
} }
} }
if flags.contains(StateFlags::POSITION) && !is_maximized && !self.is_minimized()? { if flags.contains(StateFlags::POSITION) && !is_maximized && !is_minimized {
let position = self.outer_position()?; let position = self.outer_position()?;
state.x = position.x; state.x = position.x;
state.y = position.y; state.y = position.y;
@ -451,13 +443,26 @@ impl Builder {
} }
WindowEvent::Moved(position) if flags.contains(StateFlags::POSITION) => { WindowEvent::Moved(position) if flags.contains(StateFlags::POSITION) => {
let mut c = cache.lock().unwrap(); if !window_clone.is_minimized().unwrap_or_default() {
if let Some(state) = c.get_mut(&label) { let mut c = cache.lock().unwrap();
state.prev_x = state.x; if let Some(state) = c.get_mut(&label) {
state.prev_y = state.y; state.prev_x = state.x;
state.prev_y = state.y;
state.x = position.x;
state.y = position.y; state.x = position.x;
state.y = position.y;
}
}
}
WindowEvent::Resized(size) if flags.contains(StateFlags::SIZE) => {
if !window_clone.is_minimized().unwrap_or_default()
&& !window_clone.is_maximized().unwrap_or_default()
{
let mut c = cache.lock().unwrap();
if let Some(state) = c.get_mut(&label) {
state.width = size.width;
state.height = size.height;
}
} }
} }
_ => {} _ => {}
@ -473,13 +478,11 @@ impl Builder {
} }
trait MonitorExt { trait MonitorExt {
fn intersects(&self, position: PhysicalPosition<i32>, size: LogicalSize<u32>) -> bool; fn intersects(&self, position: PhysicalPosition<i32>, size: PhysicalSize<u32>) -> bool;
} }
impl MonitorExt for Monitor { impl MonitorExt for Monitor {
fn intersects(&self, position: PhysicalPosition<i32>, size: LogicalSize<u32>) -> bool { fn intersects(&self, position: PhysicalPosition<i32>, size: PhysicalSize<u32>) -> bool {
let size = size.to_physical::<u32>(self.scale_factor());
let PhysicalPosition { x, y } = *self.position(); let PhysicalPosition { x, y } = *self.position();
let PhysicalSize { width, height } = *self.size(); let PhysicalSize { width, height } = *self.size();

Loading…
Cancel
Save