FabianLars/tauri-plugin-deep-link

Issue focusing app after activating from Windows Notification

RandomEngy opened this issue · 6 comments

https://github.com/RandomEngy/tauri-deep-link-focus-repro

Repro steps:

  1. Set up tauri_plugin_deep_link::register with a handler that tries to call window.set_focus()
  2. Open the app
  3. Fire a notification that opens the app with a protocol
  4. Put the app in the background by focusing a different app
  5. Click the notification

Expected result:
App comes to the foreground

Actual result:
App flashes in the taskbar and does not come to the foreground

FYI i'm kinda working on this issue (when i have time for it) but wasn't able to make it work with the workaround you mentioned in the tauri issue. Mostly hitting Access denied errors when sending key events so far for some reason 🤷

This one works for me: https://gist.github.com/littletsu/d1c1b512d6843071144b7b89109a8de2 I changed it to 0x55 and it typed out "u" on the screen, with no Access denied errors.

https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

That's effectively what i was doing with the windows(-sys) crate, but i copied that gist anyway and it still gives me the same errors (and it still isn't able to transfer the focus) :(

I'll put this issue on ice for now since Tauri doesn't support interactive notifications anyway. If in the meantime someone else figures out how to add it to the plugin i'll gladly take a look at PRs but i can't spend any more time on this myself right now.

Gave it a shot and the call to AllowSetForegroundWindow with ASFW_ANY returned true. I understand that this is a request to let any process take foreground focus, and the return value meant it succeeded. But somehow the focus still does not work!

I tried directly calling SetForegroundWindow on the hwnd instead of set_focus() and that returned false and had the same blinking icon behavior. GetLastError gave me WIN32_ERROR(183). Which apparently is 0xB7 - ERROR_ALREADY_EXISTS - Cannot create a file when that file already exists.

Which makes no sense, and I can't find anyone else who's gotten that error code calling the function.

Ahh! It appears that ASFW_ANY doesn't actually work. You need to supply the PID of your target. That combined with the SendInput trick WORKS. I'll see if I can cook up a PR.

A third wrinkle is that Tauri's set_focus() method does not work. you need to do this instead:

let hwnd = main_window.hwnd().unwrap();
unsafe {
    let success = windows::Win32::UI::WindowsAndMessaging::SetForegroundWindow(HWND(hwnd.0)).as_bool();
    if !success {
        warn!("SetForegroundWindow failed");
    }
}