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

@ -53,14 +53,12 @@ features = [
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-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]
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-biometric = { path = "../../../plugins/biometric/", version = "2.0.0-rc.3" }
[target."cfg(target_os = \"windows\")".dependencies]
window-shadows = "0.2"
[features]
prod = ["tauri/custom-protocol"]

@ -7502,6 +7502,55 @@
"enum": [
"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_global_shortcut::Builder::new().build())?;
app.handle()
.plugin(tauri_plugin_window_state::Builder::new().build())?;
app.handle()
.plugin(tauri_plugin_updater::Builder::new().build())?;
}
@ -63,8 +65,7 @@ pub fn run() {
.user_agent(&format!("Tauri API - {}", std::env::consts::OS))
.title("Tauri API Validation")
.inner_size(1000., 800.)
.min_inner_size(600., 400.)
.content_protected(true);
.min_inner_size(600., 400.);
}
#[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",
"version": "2.0.0",
"identifier": "com.tauri.api",

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

Loading…
Cancel
Save