Hello,
We are developing an hybrid application with some mix between wxWidgets controls and wxWebview.
We have a problem with our webview because we would like to set the focus on one of the control of the webpage but it doesn't work.
It seems if we load a url while the webview do not have the focus,the javascript code do not put the focus on the control.
If we click on F5 the page is reloaded and the focus is ok.
My question is: how do you set focus on the webview ? Do you call SetFocus on the wxWebview itself ? On the parent ?
I tried to call SetFocus on the webview and on the parent just before calling LoadURL but still does not work.
On the screenshot below the first edit box should have focus
In addition I will also post a video explaining the problem because I am not sure my explanation is good enough.
Thanks
[wxMSW][3.1.5]: webview and focus
-
- Earned a small fee
- Posts: 10
- Joined: Fri Dec 06, 2019 1:10 pm
Re: [wxMSW][3.1.5]: webview and focus
Hi,
If you call SetFocus() on the webview and then FindFocus() what window will be returned?
Thank you.
If you call SetFocus() on the webview and then FindFocus() what window will be returned?
Thank you.
Re: [wxMSW][3.1.5]: webview and focus
I know little about web dev but out of curiosity, I tried to take a look at it. It does seem the focus does not behave as expected. When pressing the button, the form input is selected but the cursor is not shown:
Keyboard navigation does not seem to work as I would expect it either. Running the code above and tabbing out of the button, there is no visible focus, tabbing again gets to the text ctrl. I would expect after pressing <Tab> on button the webview getting a focused and the next <Tab> focusing the form input.
When the webview is focused via a mouse click and the form input there has a focus, it IS possible to <Tab> forwards and backwards to wxWidgets controls.
Perhaps wxWebView should call ICoreWebView2Controller::MoveFocus() when it gets focus?
BTW, when running the code above from MSVS and pressing <F12> to display dev tools, I get an exception? I could not reproduce this with the webview sample and did not look into into it.
EDIT
The code above basically works as expected (both programmatic focus and keyboard navigation) after making this simple change to wxWebViewEdge:
https://github.com/PBfordev/wxWidgets/c ... ebce1aeea8
However, I am not sure about this so I will not submit it as PR ATM. I would ask about this issue in wx-users mailing list, perhaps also including information from this post.
Code: Select all
#include <wx/wx.h>
#include <wx/webview.h>
const char* HTMLSource =
R"(<!DOCTYPE html>
<html>
<body>
<h1>Log In</h1>
<form>
<label for="uname">User name:</label>
<input type="text" id="uname" name="uname">
<label for="passwd">Password:</label>
<input type="text" id="passwd" name="passwd">
</form>
</body>
</html>)";
class MyFrame: public wxFrame
{
public:
MyFrame() : wxFrame(nullptr, wxID_ANY, "Test")
{
wxPanel* mainPanel = new wxPanel(this);
wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
mainSizer->Add(new wxButton(mainPanel, wxID_ANY, "Focus wxWebView Form"), wxSizerFlags().Expand().Border());
Bind(wxEVT_BUTTON, [this](wxCommandEvent&){ FocusWebForm(); });
m_webView = wxWebView::New(mainPanel, wxID_ANY, wxWebViewDefaultURLStr,
wxDefaultPosition, wxDefaultSize, wxWebViewBackendEdge);
m_webView->Bind(wxEVT_WEBVIEW_LOADED, &MyFrame::OnWebViewLoaded, this);
mainSizer->Add(m_webView, wxSizerFlags(1).Expand().DoubleBorder());
mainSizer->Add(new wxTextCtrl(mainPanel, wxID_ANY, ""), wxSizerFlags().Expand().Border());
SetMinClientSize(FromDIP(wxSize(600,300)));
mainPanel->SetSizer(mainSizer);
}
private:
wxWebView* m_webView;
void FocusWebForm()
{
m_webView->SetFocus();
m_webView->RunScript(R"(document.getElementById("uname").focus();)");
}
void OnWebViewLoaded(wxWebViewEvent&)
{ // SetPage() can be called only when the view was loaded
m_webView->Unbind(wxEVT_WEBVIEW_LOADED, &MyFrame::OnWebViewLoaded, this);
m_webView->SetPage(HTMLSource, wxWebViewDefaultURLStr);
}
};
class MyApp : public wxApp
{
public:
bool OnInit() override
{
(new MyFrame())->Show();
return true;
}
}; wxIMPLEMENT_APP(MyApp);
When the webview is focused via a mouse click and the form input there has a focus, it IS possible to <Tab> forwards and backwards to wxWidgets controls.
Perhaps wxWebView should call ICoreWebView2Controller::MoveFocus() when it gets focus?
BTW, when running the code above from MSVS and pressing <F12> to display dev tools, I get an exception? I could not reproduce this with the webview sample and did not look into into it.
EDIT
The code above basically works as expected (both programmatic focus and keyboard navigation) after making this simple change to wxWebViewEdge:
https://github.com/PBfordev/wxWidgets/c ... ebce1aeea8
However, I am not sure about this so I will not submit it as PR ATM. I would ask about this issue in wx-users mailing list, perhaps also including information from this post.