CefSharp icon indicating copy to clipboard operation
CefSharp copied to clipboard

WinForms - Opening file picker via <input type="file"> does not correctly restores focus after canceling/picking a file

Open MaTToX3 opened this issue 1 year ago • 5 comments

Is there an existing issue for this?

  • [X] I have searched both open/closed issues, no issue already exists.

CefSharp Version

120.2.70

Operating System

Windows 10

Architecture

x64

.Net Version

NET 4.7.2

Implementation

WinForms

Reproduction Steps

Repo (using MinimalExample): https://github.com/MaTToX3/cefsharp-winforms-focus-issue

When opening a file picker on a website via simple and picking a file/canceling it, the focus is not correctly restored to the browser component. How do I know: having a keyup event listener on that page the event is not triggered until website is manually clicked again.

Expected behavior

The focus should be correctly restored back to the browser component - e.g. keyup listener should be triggered if any key is pressed.

Actual behavior

Focus is not correctly restored until manually clicked.

Regression?

Issue exists since v102.0.90, the last version this works correctly is 101.0.180

Known Workarounds

No workaround found, I tried some options with manually focusing back to control but it did not work: https://stackoverflow.com/questions/77865196/cefsharp-chromiumbrowser-looses-focus-when-opening-file-picker-via-input-type

Does this problem also occur in the CEF Sample Application

No, focus is correctly restored in a CEF Sample App (tried Winforms version).

Other information

No response

MaTToX3 avatar Jan 24 '24 17:01 MaTToX3

Issue exists since v102.0.90, the last version this works correctly is 101.0.180

Likely the result of this upstream change. CEF switched to using Chromium dialogs.

amaitland avatar Jan 25 '24 20:01 amaitland

I see. I wonder, how come I cant just work around this and do browser.Focus() on the Form.Activated event (which is correctly triggered when file browser is closed)? Why does the Focus() return false? Or even when it does returns true, it still does not focus on the browser control. What would be the proper way to focus on the browser manually?

MaTToX3 avatar Jan 25 '24 20:01 MaTToX3

You'd have to debug to find that out.

  • https://github.com/cefsharp/CefSharp/blob/cefsharp/120/CefSharp.WinForms/ChromiumWebBrowser.cs#L901
  • https://github.com/cefsharp/CefSharp/blob/cefsharp/120/CefSharp.WinForms/Internals/DefaultFocusHandler.cs
  • https://github.com/cefsharp/CefSharp/blob/cefsharp/120/CefSharp.WinForms/Internals/ParentFormMessageInterceptor.cs
browser.GetBrowserHost().SetFocus(true);

amaitland avatar Jan 26 '24 01:01 amaitland

Issue exists since v102.0.90, the last version this works correctly is 101.0.180

Likely the result of this upstream change. CEF switched to using Chromium dialogs.

Seems like there are multiple issues regarding that change. We have a DownloadHandler implemented, where we are specifying the download path manually and it is being ignored - the last path is always used for the dialog.

var fileName = downloadItem.SuggestedFileName;
var pathToDownload = Path.Combine(path, fileName); 
callback.Continue(pathToDownload, showDialog: true); // no matter what the value of pathToDownload is, the dialog always points to last used download path

MaTToX3 avatar Jan 29 '24 10:01 MaTToX3

You can show your own custom dialog for downloading files and you should be able to do the same for the file picker as well.

https://cefsharp.github.io/api/118.6.x/html/M_CefSharp_Handler_DialogHandler_OnFileDialog.htm

amaitland avatar Feb 01 '24 08:02 amaitland