Move common wxWebView code to base class

Removes duplicate code in the various webview backends
This commit is contained in:
Tobias Taschner
2021-02-04 14:49:26 +01:00
parent 0d82348328
commit b351e7762d
10 changed files with 200 additions and 485 deletions

View File

@@ -508,20 +508,6 @@ void wxWebViewEdge::Reload(wxWebViewReloadFlags WXUNUSED(flags))
m_impl->m_webView->Reload();
}
wxString wxWebViewEdge::GetPageSource() const
{
wxString text;
const_cast<wxWebViewEdge*>(this)->RunScript("document.documentElement.outerHTML;", &text);
return text;
}
wxString wxWebViewEdge::GetPageText() const
{
wxString text;
const_cast<wxWebViewEdge*>(this)->RunScript("document.body.innerText;", &text);
return text;
}
bool wxWebViewEdge::IsBusy() const
{
return m_impl->m_isBusy;
@@ -565,21 +551,6 @@ void wxWebViewEdge::Print()
RunScript("window.print();");
}
wxWebViewZoom wxWebViewEdge::GetZoom() const
{
double old_zoom_factor = 0.0;
m_impl->m_webViewController->get_ZoomFactor(&old_zoom_factor);
if (old_zoom_factor > 1.7)
return wxWEBVIEW_ZOOM_LARGEST;
if (old_zoom_factor > 1.3)
return wxWEBVIEW_ZOOM_LARGE;
if (old_zoom_factor > 0.8)
return wxWEBVIEW_ZOOM_MEDIUM;
if (old_zoom_factor > 0.6)
return wxWEBVIEW_ZOOM_SMALL;
return wxWEBVIEW_ZOOM_TINY;
}
float wxWebViewEdge::GetZoomFactor() const
{
double old_zoom_factor = 0.0;
@@ -587,69 +558,11 @@ float wxWebViewEdge::GetZoomFactor() const
return old_zoom_factor;
}
void wxWebViewEdge::SetZoom(wxWebViewZoom zoom)
{
double old_zoom_factor = 0.0;
m_impl->m_webViewController->get_ZoomFactor(&old_zoom_factor);
double zoom_factor = 1.0;
switch (zoom)
{
case wxWEBVIEW_ZOOM_LARGEST:
zoom_factor = 2.0;
break;
case wxWEBVIEW_ZOOM_LARGE:
zoom_factor = 1.5;
break;
case wxWEBVIEW_ZOOM_MEDIUM:
zoom_factor = 1.0;
break;
case wxWEBVIEW_ZOOM_SMALL:
zoom_factor = 0.75;
break;
case wxWEBVIEW_ZOOM_TINY:
zoom_factor = 0.5;
break;
default:
break;
}
SetZoomFactor(zoom_factor);
}
void wxWebViewEdge::SetZoomFactor(float zoom)
{
m_impl->m_webViewController->put_ZoomFactor(zoom);
}
bool wxWebViewEdge::CanCut() const
{
return QueryCommandEnabled("cut");
}
bool wxWebViewEdge::CanCopy() const
{
return QueryCommandEnabled("copy");
}
bool wxWebViewEdge::CanPaste() const
{
return QueryCommandEnabled("paste");
}
void wxWebViewEdge::Cut()
{
ExecCommand("cut");
}
void wxWebViewEdge::Copy()
{
ExecCommand("copy");
}
void wxWebViewEdge::Paste()
{
ExecCommand("paste");
}
bool wxWebViewEdge::CanUndo() const
{
return QueryCommandEnabled("undo");
@@ -670,12 +583,6 @@ void wxWebViewEdge::Redo()
ExecCommand("redo");
}
long wxWebViewEdge::Find(const wxString& WXUNUSED(text), int WXUNUSED(flags))
{
// TODO: not implemented in SDK (could probably be implemented by script)
return -1;
}
//Editing functions
void wxWebViewEdge::SetEditable(bool WXUNUSED(enable))
{
@@ -687,41 +594,6 @@ bool wxWebViewEdge::IsEditable() const
return false;
}
void wxWebViewEdge::SelectAll()
{
RunScript("window.getSelection().selectAllChildren(document);");
}
bool wxWebViewEdge::HasSelection() const
{
wxString rangeCountStr;
const_cast<wxWebViewEdge*>(this)->RunScript("window.getSelection().rangeCount;", &rangeCountStr);
return rangeCountStr != "0";
}
void wxWebViewEdge::DeleteSelection()
{
ExecCommand("delete");
}
wxString wxWebViewEdge::GetSelectedText() const
{
wxString selectedText;
const_cast<wxWebViewEdge*>(this)->RunScript("window.getSelection().toString();", &selectedText);
return selectedText;
}
wxString wxWebViewEdge::GetSelectedSource() const
{
// TODO: not implemented in SDK (could probably be implemented by script)
return wxString();
}
void wxWebViewEdge::ClearSelection()
{
RunScript("window.getSelection().empty();");
}
void wxWebViewEdge::EnableContextMenu(bool enable)
{
wxCOMPtr<ICoreWebView2Settings> settings(m_impl->GetSettings());
@@ -774,22 +646,9 @@ void* wxWebViewEdge::GetNativeBackend() const
return m_impl->m_webView;
}
bool wxWebViewEdge::QueryCommandEnabled(const wxString& command) const
void wxWebViewEdge::MSWSetBrowserExecutableDir(const wxString & path)
{
wxString resultStr;
const_cast<wxWebViewEdge*>(this)->RunScript(
wxString::Format("function f(){ return document.queryCommandEnabled('%s'); } f();", command), &resultStr);
return resultStr.IsSameAs("true", false);
}
void wxWebViewEdge::ExecCommand(const wxString& command)
{
RunScript(wxString::Format("document.execCommand('%s');", command));
}
void wxWebViewEdge::MSWSetBrowserExecutableDir(const wxString & path)
{
wxWebViewEdgeImpl::ms_browserExecutableDir = path;
wxWebViewEdgeImpl::ms_browserExecutableDir = path;
}
bool wxWebViewEdge::RunScriptSync(const wxString& javascript, wxString* output)