Markpad icon indicating copy to clipboard operation
Markpad copied to clipboard

Opening a file right after Show Desktop (Win+D / taskbar strip) leaves the window restored but hidden behind the desktop

Open wqzhellohhwy opened this issue 1 month ago • 3 comments

Version: 2.8.3, Windows 11 (Tauri 2.12, tao 0.37.1, tauri-plugin-single-instance 2.5.0)

Steps to reproduce

  1. Open Markpad (window visible and maximized in my case).
  2. Press Win+D, or click the taskbar's Show Desktop strip.
  3. Double-click any .md file.
  4. The file opens in Markpad, but the window never comes up — it stays invisible until you click its taskbar button.

Minimizing Markpad by hand (or just having another window in front) does not reproduce it: the window comes up normally then.

Measurements

Polling IsIconic, GetWindowPlacement.showCmd, GetForegroundWindow and the z-order while reproducing. deskZ / mpZ are indices into the EnumWindows z-order (lower = closer to the front):

Show Desktop          iconic=true   showCmd=SW_MINIMIZED   deskZ=34  mpZ=173  fg=WorkerW (desktop)
the .md click lands   iconic=true   showCmd=SW_MINIMIZED   deskZ=34  mpZ=173  fg=WorkerW   (2nd instance starts)
+480 ms               iconic=false  showCmd=SW_MAXIMIZED   deskZ=34  mpZ=42   fg=WorkerW

unminimize does restore the window — right size, right maximize state, IsIconic false. What it does not do is end up on top: the restore returns the window to the z-order slot it was minimized from, which is under the desktop window, and the activation is refused. The window is restored, covered, and invisible.

Root cause

  • Show Desktop puts the desktop window (WorkerW / Progman) into the topmost band and leaves it as the foreground window.
  • A restore returns the window to its previous z-order slot, i.e. under that desktop window.
  • set_focus()'s SetForegroundWindow is then refused: the process holds neither the foreground nor the last input event. The AllowSetForegroundWindow hand-off in the single-instance plugin does help, but tao's attempt runs before the window is above the desktop, and Windows will not make a window the foreground one while another window is covering it.
  • An ordinary raise is not enough either: SetWindowPos(HWND_TOP) cannot reach over a topmost window. Measured on the same window, same state:
    attempt z-index of the app window desktop window
    plain ShowWindow(SW_RESTORE) 36 34
    SetWindowPos(HWND_TOP) 36 (unmoved — no effect) 34
    SetWindowPos(HWND_TOPMOST) 11 35
    then SetWindowPos(HWND_NOTOPMOST) 33 35

Fix that works here

In src-tauri/src/window_runtime.rs (bring_to_front), raise first and activate after — both, and in that order:

let _ = window.show();
let _ = window.unminimize();

// Raise above the (topmost) desktop window: HWND_TOPMOST, then straight back to
// HWND_NOTOPMOST so the window lands at the top of the ordinary band.
if let Ok(hwnd) = window.hwnd() {
    windows_front::raise(hwnd.0 as *mut std::ffi::c_void);
}

let _ = window.set_focus();   // tao's attempt, now that nothing covers the window

if let Ok(hwnd) = window.hwnd() {
    windows_front::activate(hwnd.0 as *mut std::ffi::c_void);
}

where activate is: SetForegroundWindow → on refusal AttachThreadInput(thisThread, foregroundThread, true) + retry + detach → on refusal SwitchToThisWindow(hwnd, 1).

raise only calls ShowWindow(SW_RESTORE) when IsIconic(hwnd) is true, since SW_RESTORE on an already-maximized window un-maximizes it.

The half-fix is worse than no fix

Doing only the raise (no activation) trades one bug for another: a window that is raised above the desktop but never activated leaves the shell in its "desktop shown" state. Because the taskbar strip is a toggle, the user's next click on it then means "put the windows back" rather than "show me the desktop", so the desktop becomes unreachable — I hit exactly that with a partially correct fix.

With the activation succeeding, the shell drops out of that state on its own (measured: deskZ 34 → 60 immediately after the file opens) and Show Desktop keeps working normally.

Verification

Deterministic harness: build the failing precondition (Show Desktop, then ShowWindow(SW_RESTORE) by hand so the window settles under the desktop), then let the app handle a file open with its own code path.

precondition    fg=WorkerW       focused=false  mpZ=42  deskZ=35   (under the desktop)
after the open  fg=Tauri Window  focused=true   mpZ=33  deskZ=60   (above the desktop, focused, shell state ended)
Win+D after     window minimized -> desktop reached (no side effect)

Happy to send a PR if you want it.

wqzhellohhwy avatar Oct 05 '26 08:10 wqzhellohhwy

Thanks for the measurements.

Some context on why we're careful here. #704 fixed the opposite problem: a cold start took focus from whatever app the user had switched to while Markpad was loading. set_focus on Windows is not a polite request. It is SetForegroundWindow, and when Windows refuses, tao fakes an Alt keypress to get past focus-stealing prevention. So every extra step we add to bring a window forward also applies to cases where the user didn't ask for it. bring_to_front runs on a cold start with a file argument too, which is exactly the #704 scenario.

bring_to_front currently uses only Tauri's own show, unminimize and set_focus. We've worked with upstream on Windows focus a few times already, e.g. tauri-apps/plugins-workspace#3592, the AllowSetForegroundWindow hand-off you mentioned. Layering our own TOPMOST raise plus AttachThreadInput and SwitchToThisWindow on top of set_focus isn't something we want to take as is. If set_focus can't reach a window restored after Show Desktop, that's a gap in tao's set_focus, and that's where it should be fixed. We'll keep this open and look at it from that side.

PathGao avatar Oct 05 '26 10:10 PathGao

Before deciding anything, could you run the same steps with two other apps and say whether the window comes up?

  1. Windows 11 Notepad: one window open, Win+D, then double-click a .txt file.
  2. VS Code: one window open, Win+D, then double-click a file it opens.

This tells us whether it's specific to Tauri/tao or common to apps that forward a file to an already running instance.

One more thing: your own data shows the desktop drops out of the Show Desktop state (deskZ 34 → 60) as soon as activation succeeds. That points at the activation being refused as the cause and the window staying covered as the result, rather than the other way round. If you can log whether SetForegroundWindow returns false in the plain bring_to_front path, that would help.

PathGao avatar Oct 05 '26 10:10 PathGao

Not answered for a while ,closed

PathGao avatar Oct 29 '26 03:10 PathGao