use Eventloop to convert async function to sync function.based on current situation ,I think it would be better to keep the sync mode. webengine use aysnc callback,while webkit use sync function

This commit is contained in:
yifang.xiao 2021-11-30 10:53:18 +08:00 committed by yifang
parent 303179b410
commit c56407863c

View file

@ -2384,15 +2384,16 @@ void ArticleView::performFindOperation( bool restart, bool backwards, bool check
bool ArticleView::findText(QString& text, const QWebEnginePage::FindFlags& f) bool ArticleView::findText(QString& text, const QWebEnginePage::FindFlags& f)
{ {
bool r; bool r;
QSemaphore sem(1); //turn async to sync invoke.
sem.acquire(1); QEventLoop loop;
ui.definition->findText(text, f, [&sem,&r](bool result) { ui.definition->findText(text, f, [&](bool result)
r = result; {
sem.release(1); r = result;
}); loop.quit();
sem.acquire(1); });
return r; loop.exec();
return r;
} }
void ArticleView::reloadStyleSheet() void ArticleView::reloadStyleSheet()
@ -2591,14 +2592,15 @@ void ArticleView::highlightFTSResults()
} }
QString ArticleView::getWebPageTextSync(QWebEnginePage * page){ QString ArticleView::getWebPageTextSync(QWebEnginePage * page){
QSemaphore sem(1);
sem.acquire(1);
QString planText; QString planText;
QEventLoop loop;
page->toPlainText([&](const QString & result){ page->toPlainText([&](const QString & result){
if(result.valid())
planText = result; planText = result;
sem.release(1); loop.quit();
}); });
sem.acquire(1);
loop.exec();
return planText; return planText;
} }
@ -2769,29 +2771,18 @@ QString ArticleView::insertSpans( QString const & html )
return newContent; return newContent;
} }
QString ArticleView::checkElement( QWebEnginePage & elem, QPoint const & pt ) QString ArticleView::checkElement( QWebEnginePage & page, QPoint const & pt )
{ {
return runJavaScriptSync(page, QString(
" var a= document.elementFromPoint(%1,%2);"
QSemaphore semaphore(1); "var nodename=a.nodeName.toLowerCase();"
semaphore.acquire(1); "if(nodename==\"body\"||nodename==\"html\"||nodename==\"head\")"
QString nodeValue; "{"
elem.runJavaScript(QString( " return '';"
" var a= document.elementFromPoint(%1,%2);" "}"
"var nodename=a.nodeName.toLowerCase();" "return a.textContent;")
"if(nodename==\"body\"||nodename==\"html\"||nodename==\"head\")" .arg(pt.x())
"{" .arg(pt.y()));
" return '';"
"}"
"return a.textContent;")
.arg(pt.x()).arg(pt.y()),[&semaphore,&nodeValue](const QVariant & result){
semaphore.release();
nodeValue=result.toString();
});
semaphore.acquire(1);
return nodeValue;
} }
QString ArticleView::wordAtPoint( int x, int y ) QString ArticleView::wordAtPoint( int x, int y )