microsoft / PowerToys

Windows system utilities to maximize productivity
MIT License
111.07k stars 6.54k forks source link

Peek doesn't close on Escape after clicking inside a text file that can be previewed #28715

Open PesBandi opened 1 year ago

PesBandi commented 1 year ago

Microsoft PowerToys version

0.73.0

Installation method

GitHub

Running as admin

Yes

Area(s) with issue?

Peek

Steps to reproduce

  1. Preview a text file that Peek can actually preview (a .txt or a .ini)
  2. Click somewhere in the file, so that it places your text cursor
  3. Press Esc

I couldn't get a screen recording of this, but I hope that you will be able to reproduce this from the provided steps.

✔️ Expected Behavior

The Peek window should close

❌ Actual Behavior

The window doesn't close

Other Software

No response

crutkas commented 4 months ago

Key thing is clicking inside the webview to repro this

drawbyperpetual commented 2 months ago

I spent some time looking into this. As @crutkas points out, the issue here is that it's not easy for Peek to detect keystrokes made inside WebView2 as it works by hosting Edge running in its own process. This is a well-known issue with WebView2 and there are many issues tracking this on GitHub, such as this one.

Possible fixes I considered were:

  1. JavaScript - This would partially work but wouldn't completely fix the problem as we disable JS in some situations (HTML/ MD) for security reasons.
  2. WebView2 msWebView2BrowserHitTransparent - This unfortunately didn't work for me*. I suspect propagating key events on WebView2 just hasn't been implemented for WinUI 3, but I've asked a question to clarify.
  3. WebView2 ControllerOptions feature - This is still in preview, but looks promising and will hopefully fix the issue eventually - I've asked for clarification.
  4. Low-level keyboard hook - We could certainly get this to work (I got it to work in my test), but this is a tool to use very sparingly and might be overkill for this minor bug.

I suggest we wait until we can clarify whether # 3 will address things for us before we pursue other fixes.

* I enabled the feature flag and then tried to trap the KeyDown and PreviewKeyDown events on WebView2 as well as on the hosting UserControl. I also tried enabling ESC as a keyboard accelerator on WebView2 and trapping ProcessKeyboardAccelerators. To be sure this wasn't specific to something we're doing in Peek, I created a separate WinUI 3 app outside PowerToys, but I couldn't get this to work there either.