[wxMSW][3.1.5]: webview and focus

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
smartmobili
Earned a small fee
Earned a small fee
Posts: 10
Joined: Fri Dec 06, 2019 1:10 pm

[wxMSW][3.1.5]: webview and focus

Post by smartmobili »

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
LoginPage.png
LoginPage.png (18.4 KiB) Viewed 1308 times
In addition I will also post a video explaining the problem because I am not sure my explanation is good enough.



Thanks
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7477
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: [wxMSW][3.1.5]: webview and focus

Post by ONEEYEMAN »

Hi,
If you call SetFocus() on the webview and then FindFocus() what window will be returned?

Thank you.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: [wxMSW][3.1.5]: webview and focus

Post by PB »

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:

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);
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.
Post Reply