tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
85.24k stars 2.58k forks source link

[bug] Any idea on how to fix taskbar z-order issue in windows 11? #11176

Open ninetynin opened 1 month ago

ninetynin commented 1 month ago

Describe the bug

I implemented my application using Tauri. However, whenever I switch to a new application by clicking on the taskbar, the application window disappears. After some research, I found that this is likely due to a Windows taskbar Z-order issue, as mentioned in this Tauri GitHub issue.

I tried the suggested approach from the GitHub issue using unsafe Win32 code (see the code snippet below), but the issue still persists.

Reproduction

fn main() {
    tauri::Builder::default()
        // .setup(|app| {
        //     let window = app.get_window("main").unwrap();
        //     let handle = HANDLE(null_mut());
        //     let mut name: Vec<u16> = wchz!("NonRudeHWND").to_vec();
        //     unsafe {
        //         SetPropW(GetForegroundWindow(), PCWSTR(name.as_mut_ptr()), handle);
        //     }
        //     Ok(())
        // })
        // .setup(|app| {

        //     Ok(())
        // })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

and set

      {
        "decorations": false,
        "skipTaskbar": true,
        "alwaysOnTop": true,
        "resizable": false,
        "focus": true
      }

and in js


  async function setupWin() {
    try {
      const monitor = await currentMonitor();
      if(monitor) {
        screenWidth = monitor.size.width;
        screenHeight = monitor.size.height;
        const windowWidth = 300;
        const windowHeight = 50;
        xoffset = 0;
        yoffset = 0;
        const windowX = (screenWidth - windowWidth*3.5 + xoffset);
        const windowY = (screenHeight - windowHeight*5 + yoffset);

        await appWindow.setSize(new LogicalSize(windowWidth, windowHeight));
        await appWindow.setPosition(new LogicalPosition(windowX, windowY));
      }
    } catch (error) {
      console.error("Error setting up window:", error);
    }
  }

so that i can use this as a widget for personal use on desktop taskbar

Expected behavior

The Tauri window gets hidden when switching to another application from the taskbar, likely due to a taskbar Z-order issue.

I attempted the code snippet provided in the GitHub issue but am not experienced with Win32 unsafe code. Despite implementing the code, the issue persists.

Full tauri info output


[✔] Environment
    - OS: Windows 10.0.22631 X64
    ✔ WebView2: 129.0.2792.65
    ✔ MSVC: Visual Studio Community 2022
    ✔ rustc: 1.83.0-nightly (ed04567ba 2024-09-28)
    ✔ cargo: 1.83.0-nightly (80d82ca22 2024-09-27)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: nightly-x86_64-pc-windows-msvc (default)
    - node: 20.16.0
    - npm: 10.8.3

[-] Packages
    - tauri [RUST]: 1.8.0
    - tauri-build [RUST]: 1.5.5
    - wry [RUST]: 0.24.11
    - tao [RUST]: 0.16.10
    - tauri-cli [RUST]: 1.6.2
    - @tauri-apps/api [NPM]: 1.6.0
    - @tauri-apps/cli [NPM]: 1.6.2

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../build
    - devPath: http://localhost:1420/
    - framework: Svelte
    - bundler: Vite

Stack trace

No response

Additional context

If there’s any further guidance on how to handle the taskbar Z-order issue, or a correct implementation of Win32 calls in Tauri, it would be much appreciated! Let me know if you need more details or if I can provide further context.

amrbashir commented 1 month ago

I couldn't reproduce, could you make a minimal repro? and for good measures could you record the buggy behavior?

ninetynin commented 1 month ago

I couldn't reproduce, could you make a minimal repro? and for good measures could you record the buggy behavior?

https://github.com/user-attachments/assets/244d1dc4-6727-4186-8f36-149cad196610

@amrbashir as you can see its getting hidden after i click on some icon present in taskbar even when always on top is enabled and this is main.rs rn

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

use tauri::Manager;
use wchar::wchz;
use windows::{core::PCWSTR, Win32::{Foundation::HANDLE, UI::WindowsAndMessaging::{GetForegroundWindow, SetPropW}}};

fn main() {
    tauri::Builder::default()
        .setup(|app| {
            let win = app.get_window("main").unwrap();
            unsafe {
                let mut name: Vec<u16> = wchz!("NonRudeHWND").to_vec();
                let handle = HANDLE(std::ptr::null_mut()); // Use a null pointer
                SetPropW(GetForegroundWindow(), PCWSTR(name.as_mut_ptr()), handle);
            }
            win.show();
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

ive tried other ways by setting up using https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos this fn and setting SWP_NOZORDER | SWP_NOOWNERZORDER but its of no use its still getting hidden and there is another problem arising with setting up those is its taking this as main full screen program and is not opening any other window while clicking on taskbar so i had to go to desktop and click on application icon in taskbar then only it works but im not using that fn and using the main way suggested from the linked issue and its of no use too and i went to that author github repo and he hasnt implemented in his project rather it is only in todo so any help is appreciated thanks

amrbashir commented 3 weeks ago

I still can't reproduce, please upload a full repo so I can test it