cefsharp / CefSharp

.NET (WPF and Windows Forms) bindings for the Chromium Embedded Framework
http://cefsharp.github.io/
Other
9.87k stars 2.92k forks source link

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

Closed MaTToX3 closed 1 month ago

MaTToX3 commented 9 months ago

Is there an existing issue for this?

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

amaitland commented 9 months ago

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.

MaTToX3 commented 9 months ago

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?

amaitland commented 9 months ago

You'd have to debug to find that out.

browser.GetBrowserHost().SetFocus(true);
MaTToX3 commented 9 months ago

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
amaitland commented 9 months ago

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 commented 1 month ago

Testing with M128 and I'm no longer able to reproduce this issue with the MinimalExample loading https://cefsharp.github.io/demo/isssue4692.html