tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[bug] Background mode does not work in full screen mode

Open gusxodnjs opened this issue 1 year ago • 5 comments

Describe the bug

After operating the window in full screen mode, hide the window, and run app.prevent_close(), a black screen appears to exit the window.

https://github.com/user-attachments/assets/f18099e2-c735-483d-aeb5-f871e8e73c1b

Reproduction

https://github.com/gusxodnjs/tauri-issues/tree/bug/background

// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

fn main() {
    tauri::Builder::default().on_window_event(|window, event| match event {
        tauri::WindowEvent::CloseRequested { api, .. } => {
          window.hide().unwrap();
          api.prevent_close();
        }
        _ => {}
      })
      .run(tauri::generate_context!())
      .expect("error while running tauri application");
}

Expected behavior

The window should be hidden and the desktop should be displayed.

Full tauri info output

[✔] Environment
    - OS: Mac OS 13.5.2 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.80.1 (3f5fd8dd4 2024-08-06)
    ✔ cargo: 1.80.1 (376290515 2024-07-16)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 18.18.2
    - yarn: 1.22.21
    - npm: 9.8.1

[-] Packages
    - tauri [RUST]: 2.0.0-rc.2
    - tauri-build [RUST]: 2.0.0-rc.2
    - wry [RUST]: 0.41.0
    - tao [RUST]: 0.28.1
    - tauri-cli [RUST]: 2.0.0-alpha.18
    - @tauri-apps/api [NPM]: 2.0.0-rc.0
    - @tauri-apps/cli [NPM]: 2.0.0-rc.3

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - framework: React
    - bundler: Vite
✨  Done in 6.84s.

Stack trace

No response

Additional context

No response

gusxodnjs avatar Aug 12 '24 15:08 gusxodnjs

@amrbashir I'm sorry, but could you please let me know the progress on this issue? I have an important release coming up next month, and I don't even know if this issue is a bug or not. If it's not a bug, I want to know how to bypass it.

gusxodnjs avatar Aug 20 '24 01:08 gusxodnjs

Hi, can you try if this works by handling the ExitRequest event like in the example:

https://github.com/tauri-apps/tauri/blob/762cf31a113f33cd524c8f6fa52ca47db8ea3705/examples/api/src-tauri/src/lib.rs#L155-L162

pewsheen avatar Aug 21 '24 08:08 pewsheen

@pewsheen thanks for your comment. I've tried it and it's the same. Even in the example, it seems to be reproduced if you do the hide() instead of the destory() in the CloseRequested event. https://github.com/tauri-apps/tauri/blob/762cf31a113f33cd524c8f6fa52ca47db8ea3705/examples/api/src-tauri/src/lib.rs#L175

gusxodnjs avatar Aug 21 '24 10:08 gusxodnjs

Usually, we'll just let the window close and recreate a new window when the user clicks the icon on the dock. Or do you have any particular use case? Otherwise, I think the code of the project may suit the scenario https://github.com/thewh1teagle/RustDuck/blob/main/src-tauri/src/runtime.rs

pewsheen avatar Aug 21 '24 11:08 pewsheen

When I close the main window, I have to run it in the background, so I can't destroy it. When I close the main window, there's no way I can go back from background mode to foreground mode. Doesn't the tray menu disappear when I close the window? Also, since I log in from the main window, I need to stay in the window state.

So hide the main window without closing it.

In my case, when I exit the main window, the only way is turn off full screen mode is . How about this?

gusxodnjs avatar Aug 23 '24 01:08 gusxodnjs

This worked for me in mac

        .on_window_event(|window, event| {
            if let tauri::WindowEvent::CloseRequested { api, .. } = event {
                // Prevent the default close behavior
                api.prevent_close();

                #[cfg(target_os = "macOS")]
                {
                    match window.is_fullscreen() {
                        Ok(true) => {

                            let app_handle = window.app_handle().clone();
                            let window_label = window.label().to_string();

                            // Exit fullscreen mode
                            let _ = window.set_fullscreen(false);

                            // Use tokio to handle the delay and subsequent hide
                            tauri::async_runtime::spawn(async move {
                                // Wait for animation to complete
                                tokio::time::sleep(Duration::from_millis(700)).await;

                                // Get a fresh reference to the window and hide it
                                if let Some(win) = app_handle.get_webview_window(&window_label) {

                                    // Hiding window after fullscreen exit
                                    win.hide().unwrap();
                                }
                            });
                        }
                        _ => {
                            // Not in fullscreen or error checking, just hide immediately
                            window.hide().unwrap();
                        }
                    }
                }
            }
        })

anuragkumar2921 avatar Apr 19 '25 23:04 anuragkumar2921