make ArticleView::hasSound() async

This commit is contained in:
ngn999 2022-03-27 22:22:42 +08:00
parent dc19f417db
commit 95be606a9e
4 changed files with 30 additions and 14 deletions

View file

@ -2,6 +2,7 @@
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "articleview.hh" #include "articleview.hh"
#include "QtCore/qvariant.h"
#include "folding.hh" #include "folding.hh"
#include "fulltextsearch.hh" #include "fulltextsearch.hh"
#include "gddebug.hh" #include "gddebug.hh"
@ -829,6 +830,7 @@ bool ArticleView::handleF3( QObject * /*obj*/, QEvent * ev )
bool ArticleView::eventFilter( QObject * obj, QEvent * ev ) bool ArticleView::eventFilter( QObject * obj, QEvent * ev )
{ {
if( ev->type() == QEvent::Gesture ) if( ev->type() == QEvent::Gesture )
{ {
Gestures::GestureResult result; Gestures::GestureResult result;
@ -1644,12 +1646,14 @@ void ArticleView::forward()
ui.definition->forward(); ui.definition->forward();
} }
bool ArticleView::hasSound() void ArticleView::hasSound(const std::function< void (bool) > &callback)
{ {
QVariant v = runJavaScriptSync( ui.definition->page(),"gdAudioLinks.first" ); ui.definition->page()->runJavaScript("gdAudioLinks.first", [callback](const QVariant &v) {
bool has = false;
if ( v.type() == QVariant::String ) if ( v.type() == QVariant::String )
return !v.toString().isEmpty(); has = !v.toString().isEmpty();
return false; callback(has);
});
} }
//use webengine javascript to playsound //use webengine javascript to playsound
@ -1661,10 +1665,14 @@ void ArticleView::playSound()
" } " " } "
" return link;})(); "; " return link;})(); ";
QString soundScript = runJavaScriptSync(ui.definition->page(), variable); ui.definition->page()->runJavaScript(variable,[this](const QVariant & result){
if (result.type() == QVariant::String) {
QString soundScript = result.toString();
if (!soundScript.isEmpty()) if (!soundScript.isEmpty())
openLink(QUrl::fromEncoded(soundScript.toUtf8()), ui.definition->url()); openLink(QUrl::fromEncoded(soundScript.toUtf8()), ui.definition->url());
} }
});
}
// use eventloop to turn the async callback to sync execution. // use eventloop to turn the async callback to sync execution.
QString ArticleView::toHtml() QString ArticleView::toHtml()

View file

@ -179,7 +179,7 @@ public:
{ ui.definition->reload(); } { ui.definition->reload(); }
/// Returns true if there's an audio reference on the page, false otherwise. /// Returns true if there's an audio reference on the page, false otherwise.
bool hasSound(); void hasSound(const std::function< void (bool has) > &callback);
/// Plays the first audio reference on the page, if any. /// Plays the first audio reference on the page, if any.
void playSound(); void playSound();

View file

@ -2033,10 +2033,14 @@ void MainWindow::updateBackForwardButtons()
void MainWindow::updatePronounceAvailability() void MainWindow::updatePronounceAvailability()
{ {
bool pronounceEnabled = ui.tabWidget->count() > 0 && if (ui.tabWidget->count() > 0) {
getCurrentArticleView()->hasSound(); getCurrentArticleView()->hasSound([this](bool has) {
navPronounce->setEnabled( has );
navPronounce->setEnabled( pronounceEnabled ); });
}
else {
navPronounce->setEnabled( false );
}
} }
void MainWindow::editDictionaries( unsigned editDictionaryGroup ) void MainWindow::editDictionaries( unsigned editDictionaryGroup )

View file

@ -1141,7 +1141,11 @@ void ScanPopup::altModePoll()
void ScanPopup::pageLoaded( ArticleView * ) void ScanPopup::pageLoaded( ArticleView * )
{ {
ui.pronounceButton->setVisible( definition->hasSound() );
definition->hasSound([this](bool has){
ui.pronounceButton->setVisible( has );
});
updateBackForwardButtons(); updateBackForwardButtons();