Add wxWebView methods to enable dev tools

Currently only implemented for the Edge (Chromium) backend.
This commit is contained in:
Tobias Taschner
2020-01-10 17:02:44 +01:00
parent f2196abca3
commit d72e5874eb
5 changed files with 61 additions and 2 deletions

View File

@@ -111,6 +111,9 @@ public:
virtual void EnableContextMenu(bool enable = true) wxOVERRIDE; virtual void EnableContextMenu(bool enable = true) wxOVERRIDE;
virtual bool IsContextMenuEnabled() const wxOVERRIDE; virtual bool IsContextMenuEnabled() const wxOVERRIDE;
virtual void EnableDevTools(bool enable = true) wxOVERRIDE;
virtual bool IsDevToolsEnabled() const wxOVERRIDE;
virtual bool RunScript(const wxString& javascript, wxString* output = NULL) wxOVERRIDE; virtual bool RunScript(const wxString& javascript, wxString* output = NULL) wxOVERRIDE;
virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) wxOVERRIDE; virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) wxOVERRIDE;

View File

@@ -159,6 +159,7 @@ public:
{ {
m_showMenu = enable; m_showMenu = enable;
} }
virtual void EnableDevTools(bool WXUNUSED(enable) = true) { }
virtual wxString GetCurrentTitle() const = 0; virtual wxString GetCurrentTitle() const = 0;
virtual wxString GetCurrentURL() const = 0; virtual wxString GetCurrentURL() const = 0;
// TODO: handle choosing a frame when calling GetPageSource()? // TODO: handle choosing a frame when calling GetPageSource()?
@@ -166,6 +167,7 @@ public:
virtual wxString GetPageText() const = 0; virtual wxString GetPageText() const = 0;
virtual bool IsBusy() const = 0; virtual bool IsBusy() const = 0;
virtual bool IsContextMenuEnabled() const { return m_showMenu; } virtual bool IsContextMenuEnabled() const { return m_showMenu; }
virtual bool IsDevToolsEnabled() const { return false; }
virtual bool IsEditable() const = 0; virtual bool IsEditable() const = 0;
virtual void LoadURL(const wxString& url) = 0; virtual void LoadURL(const wxString& url) = 0;
virtual void Print() = 0; virtual void Print() = 0;

View File

@@ -687,6 +687,27 @@ public:
*/ */
virtual bool IsContextMenuEnabled() const; virtual bool IsContextMenuEnabled() const;
/**
@name Dev Tools
*/
/**
Enable or disable access to dev tools for the user.
This is currently only implemented for the Edge (Chromium) backend
where the dev tools are enabled by default.
@since 3.1.4
*/
virtual void EnableDevTools(bool enable = true);
/**
Returns @true if dev tools are available to the user.
@since 3.1.4
*/
virtual bool IsDevToolsEnabled() const;
/** /**
@name History @name History
*/ */

View File

@@ -164,6 +164,7 @@ public:
void OnFindText(wxCommandEvent& evt); void OnFindText(wxCommandEvent& evt);
void OnFindOptions(wxCommandEvent& evt); void OnFindOptions(wxCommandEvent& evt);
void OnEnableContextMenu(wxCommandEvent& evt); void OnEnableContextMenu(wxCommandEvent& evt);
void OnEnableDevTools(wxCommandEvent& evt);
private: private:
wxTextCtrl* m_url; wxTextCtrl* m_url;
@@ -226,6 +227,7 @@ private:
wxMenuItem* m_selection_delete; wxMenuItem* m_selection_delete;
wxMenuItem* m_find; wxMenuItem* m_find;
wxMenuItem* m_context_menu; wxMenuItem* m_context_menu;
wxMenuItem* m_dev_tools;
wxInfoBar *m_info; wxInfoBar *m_info;
wxStaticText* m_info_text; wxStaticText* m_info_text;
@@ -468,6 +470,7 @@ WebFrame::WebFrame(const wxString& url) :
wxMenuItem* usememoryfs = m_tools_menu->Append(wxID_ANY, _("Memory File System Example")); wxMenuItem* usememoryfs = m_tools_menu->Append(wxID_ANY, _("Memory File System Example"));
m_context_menu = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable Context Menu")); m_context_menu = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable Context Menu"));
m_dev_tools = m_tools_menu->AppendCheckItem(wxID_ANY, _("Enable Dev Tools"));
//By default we want to handle navigation and new windows //By default we want to handle navigation and new windows
m_tools_handle_navigation->Check(); m_tools_handle_navigation->Check();
@@ -549,6 +552,7 @@ WebFrame::WebFrame(const wxString& url) :
Bind(wxEVT_MENU, &WebFrame::OnUseMemoryFS, this, usememoryfs->GetId()); Bind(wxEVT_MENU, &WebFrame::OnUseMemoryFS, this, usememoryfs->GetId());
Bind(wxEVT_MENU, &WebFrame::OnFind, this, m_find->GetId()); Bind(wxEVT_MENU, &WebFrame::OnFind, this, m_find->GetId());
Bind(wxEVT_MENU, &WebFrame::OnEnableContextMenu, this, m_context_menu->GetId()); Bind(wxEVT_MENU, &WebFrame::OnEnableContextMenu, this, m_context_menu->GetId());
Bind(wxEVT_MENU, &WebFrame::OnEnableDevTools, this, m_dev_tools->GetId());
//Connect the idle events //Connect the idle events
Bind(wxEVT_IDLE, &WebFrame::OnIdle, this); Bind(wxEVT_IDLE, &WebFrame::OnIdle, this);
@@ -710,6 +714,11 @@ void WebFrame::OnEnableContextMenu(wxCommandEvent& evt)
m_browser->EnableContextMenu(evt.IsChecked()); m_browser->EnableContextMenu(evt.IsChecked());
} }
void WebFrame::OnEnableDevTools(wxCommandEvent& evt)
{
m_browser->EnableDevTools(evt.IsChecked());
}
void WebFrame::OnFind(wxCommandEvent& WXUNUSED(evt)) void WebFrame::OnFind(wxCommandEvent& WXUNUSED(evt))
{ {
wxString value = m_browser->GetSelectedText(); wxString value = m_browser->GetSelectedText();
@@ -943,6 +952,7 @@ void WebFrame::OnToolsClicked(wxCommandEvent& WXUNUSED(evt))
m_selection_delete->Enable(m_browser->HasSelection()); m_selection_delete->Enable(m_browser->HasSelection());
m_context_menu->Check(m_browser->IsContextMenuEnabled()); m_context_menu->Check(m_browser->IsContextMenuEnabled());
m_dev_tools->Check(m_browser->IsDevToolsEnabled());
//Firstly we clear the existing menu items, then we add the current ones //Firstly we clear the existing menu items, then we add the current ones
wxMenuHistoryMap::const_iterator it; wxMenuHistoryMap::const_iterator it;

View File

@@ -49,8 +49,7 @@ typedef HRESULT(__stdcall *PFNWXGetWebView2BrowserVersionInfo)(
PFNWXCreateWebView2EnvironmentWithDetails wxCreateWebView2EnvironmentWithDetails = NULL; PFNWXCreateWebView2EnvironmentWithDetails wxCreateWebView2EnvironmentWithDetails = NULL;
PFNWXGetWebView2BrowserVersionInfo wxGetWebView2BrowserVersionInfo = NULL; PFNWXGetWebView2BrowserVersionInfo wxGetWebView2BrowserVersionInfo = NULL;
int wxWebViewEdgeChromium::ms_isAvailable = -1; wxDynamicLibrary wxWebViewEdge::ms_loaderDll;
wxDynamicLibrary wxWebViewEdgeChromium::ms_loaderDll;
bool wxWebViewEdge::IsAvailable() bool wxWebViewEdge::IsAvailable()
{ {
@@ -655,6 +654,30 @@ bool wxWebViewEdge::IsContextMenuEnabled() const
return true; return true;
} }
void wxWebViewEdge::EnableDevTools(bool enable)
{
wxCOMPtr<IWebView2Settings> settings;
if (SUCCEEDED(m_webView->get_Settings(&settings)))
{
settings->put_AreDevToolsEnabled(enable);
}
}
bool wxWebViewEdge::IsDevToolsEnabled() const
{
wxCOMPtr<IWebView2Settings> settings;
if (SUCCEEDED(m_webView->get_Settings(&settings)))
{
BOOL devToolsEnabled = TRUE;
settings->get_AreDevToolsEnabled(&devToolsEnabled);
if (!devToolsEnabled)
return false;
}
return true;
}
bool wxWebViewEdge::RunScriptSync(const wxString& javascript, wxString* output) bool wxWebViewEdge::RunScriptSync(const wxString& javascript, wxString* output)
{ {
bool scriptExecuted = false; bool scriptExecuted = false;