From c56407863ccde03623c15eeae661c969972f2cd5 Mon Sep 17 00:00:00 2001 From: "yifang.xiao" Date: Tue, 30 Nov 2021 10:53:18 +0800 Subject: [PATCH] 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 --- articleview.cc | 61 +++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/articleview.cc b/articleview.cc index 647e0ea4..995a35f7 100644 --- a/articleview.cc +++ b/articleview.cc @@ -2384,15 +2384,16 @@ void ArticleView::performFindOperation( bool restart, bool backwards, bool check bool ArticleView::findText(QString& text, const QWebEnginePage::FindFlags& f) { - bool r; - QSemaphore sem(1); - sem.acquire(1); - ui.definition->findText(text, f, [&sem,&r](bool result) { - r = result; - sem.release(1); - }); - sem.acquire(1); - return r; + bool r; + //turn async to sync invoke. + QEventLoop loop; + ui.definition->findText(text, f, [&](bool result) + { + r = result; + loop.quit(); + }); + loop.exec(); + return r; } void ArticleView::reloadStyleSheet() @@ -2591,14 +2592,15 @@ void ArticleView::highlightFTSResults() } QString ArticleView::getWebPageTextSync(QWebEnginePage * page){ - QSemaphore sem(1); - sem.acquire(1); QString planText; + QEventLoop loop; page->toPlainText([&](const QString & result){ + if(result.valid()) planText = result; - sem.release(1); + loop.quit(); }); - sem.acquire(1); + + loop.exec(); return planText; } @@ -2769,29 +2771,18 @@ QString ArticleView::insertSpans( QString const & html ) return newContent; } -QString ArticleView::checkElement( QWebEnginePage & elem, QPoint const & pt ) +QString ArticleView::checkElement( QWebEnginePage & page, QPoint const & pt ) { - - - QSemaphore semaphore(1); - semaphore.acquire(1); - QString nodeValue; - elem.runJavaScript(QString( - " var a= document.elementFromPoint(%1,%2);" - "var nodename=a.nodeName.toLowerCase();" - "if(nodename==\"body\"||nodename==\"html\"||nodename==\"head\")" - "{" - " 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; + return runJavaScriptSync(page, QString( + " var a= document.elementFromPoint(%1,%2);" + "var nodename=a.nodeName.toLowerCase();" + "if(nodename==\"body\"||nodename==\"html\"||nodename==\"head\")" + "{" + " return '';" + "}" + "return a.textContent;") + .arg(pt.x()) + .arg(pt.y())); } QString ArticleView::wordAtPoint( int x, int y )