tauri-apps / plugins-workspace

All of the official Tauri plugins in one place!
https://tauri.app
Apache License 2.0
963 stars 272 forks source link

[window-state] Only saving window position and not size (v2) #926

Open jaydevelopsstuff opened 9 months ago

jaydevelopsstuff commented 9 months ago

The window-state plugin (v2 off of crates.io) fails to save/restore window size (at least on MacOS, I haven't tested on any other operating systems).

MRE Repo (Just run the app, resize, close, relaunch, and observe the size not being restored)

I've seen some similarish looking issues but they've been closed and don't seem to be addressing the same problem. If this has already been fixed and is just finding its way to release let me know.

amrbashir commented 9 months ago

Couldn't reproduce on Windows, can't really investigate macos

bartekc7 commented 8 months ago

I confirm. This error also occurs on my macOS

legends-killer commented 8 months ago

I find that this issue is caused by tauri::window::Window::inner_size() function. It always returns the width and height defined in tauri.conf.json but not the actual number of the current window size.

image

In the picture above, you'll find that the inner size is always 1024x768 which is the same as what in tauri.conf.json, but the outer size changes very differently. So, it seems to be a bug of tauri v2.0-beta... But it can be a simple workaround by changing it into self.outer_size(). https://github.com/tauri-apps/plugins-workspace/blob/v2/plugins/window-state/src/lib.rs#L269

legends-killer commented 8 months ago

Hey guys, I found some PR related to this bug. https://github.com/tauri-apps/tauri/pull/8280/files#diff-9fff33774bba86e0056dc083e3ec624f275b9d05427031c76dc1b313ade17a82R736 In this PR, Tauri 2.0 beta supported multiple webviews in a single window, but some functions about get_webview were marked as unstable causing the get_size function to return an unexpected value without unstable feature. So, you just need to enable the unstable feature in Tauri's dependency and this plugin could work normally.

// enable unstable features
tauri = { version = "2.0.0-beta", features = ["unstable"] }

Finally, I'm not sure if the unstable flag should be removed in Tauri, so I just created a PR to this repo to update docs in v2 README to workaround it. https://github.com/tauri-apps/plugins-workspace/pull/1086

roniemartinez commented 7 months ago

I can confirm that this bug exists (MacOS). Maybe because the WindowEvent::Resized is missing from the code?

https://github.com/tauri-apps/plugins-workspace/blob/c013fa52cd66885cf457a64e75373cb2066bc849/plugins/window-state/src/lib.rs#L369-L388

FabianLars commented 3 months ago

Can someone confirm whether this is still an issue in the rc version?

Zamoca42 commented 1 month ago

I reproduced this issue in the plugin-workspace example. The window size state is saved in rc3. However, I found that the outer size height is being stored in the inner size.

1.

1

2.

2

3.

스크린샷 2024-10-03 오후 11 53 58

In my opinion, since the inner size =/= the total window size, the total window size should be stored when saving the window-state value.

        if flags.contains(StateFlags::SIZE) && !is_maximized && !is_minimized {
            let size = self.inner_size()?;
            let outer_size = self.outer_size().unwrap();
            println!("inner_size: {:?}", size);
            println!("outer_size: {:?}", self.outer_size()?);
            // It doesn't make sense to save a window with 0 height or width
            if size.width > 0 && size.height > 0 {
                state.width = outer_size.width; // inner -> outer
                state.height = outer_size.height; // inner -> outer
            }
        }
ayangweb commented 1 month ago

@amrbashir @FabianLars Hey👋, I used the plugin in tauri v2.0.2 and the terminal keeps outputting😱

.plugin(
    tauri_plugin_window_state::Builder::default()
        .with_state_flags(StateFlags::all() & !StateFlags::VISIBLE)
        .build(),
)

https://github.com/user-attachments/assets/405a2918-531e-488c-89dd-fbeb0bf39d0b