This commit is contained in:
2026-03-03 09:39:15 +01:00
parent 769d5681d4
commit 35aae3b707
9 changed files with 136 additions and 40 deletions

View File

@@ -5,6 +5,8 @@
#include <QApplication>
#include <QTimer>
#include <QSemaphore>
#include <QJsonDocument>
#include <QJsonObject>
#define COMMAND_QUIT 1
#define COMMAND_CLOSE 2
@@ -25,9 +27,15 @@ public:
}
};
static QString jsonString(QJsonObject obj)
{
QJsonDocument doc(obj);
QString j = QString::fromUtf8(doc.toJson(QJsonDocument::JsonFormat::Compact));
return j;
}
void Rktwebview_qt::processCommands()
{
printf("COmmand processing\n");
while(!_command_queue.empty()) {
Command *cmd = _command_queue.dequeue();
@@ -39,6 +47,10 @@ void Rktwebview_qt::processCommands()
break;
case COMMAND_CREATE: {
int parent = cmd->args[0].toInt();
void *f = cmd->args[1].value<void *>();
void (*js_event_cb)(const char *msg) = reinterpret_cast <void(*)(const char *)>(f);
QWidget *p;
if (_views.contains(parent)) {
p = _views[parent];
@@ -53,6 +65,7 @@ void Rktwebview_qt::processCommands()
int id = view->id();
_views[id] = w;
_view_js_callbacks[id] = js_event_cb;
w->show();
@@ -98,10 +111,9 @@ void Rktwebview_qt::processCommands()
}
}
void Rktwebview_qt::interruptEventLoop()
void Rktwebview_qt::processJsEventQueue()
{
printf("Exeting application event loop\n");
//_app->quit();
}
void Rktwebview_qt::removeView(int id)
@@ -115,10 +127,13 @@ int Rktwebview_qt::nextHandle()
return h;
}
int Rktwebview_qt::rktWebViewCreate(int parent)
int Rktwebview_qt::rktWebViewCreate(int parent, void (*js_evt_cb)(const char *))
{
Command c(COMMAND_CREATE);
c.args.push_back(parent);
void *function = reinterpret_cast<void *>(js_evt_cb);
QVariant f(QVariant::fromValue(function));
c.args.push_back(f);
_command_queue.enqueue(&c);
@@ -153,6 +168,30 @@ result_t Rktwebview_qt::rktSetUrl(rktwebview_t wv, const char *url)
return r ? result_t::oke : result_t::set_navigate_failed;
}
void Rktwebview_qt::pageLoaded(rktwebview_t w, bool ok)
{
// Inject event handling code for the javascript side
runJs(w, "window.rkt_event_queue = [];\n"
"window.rkt_send_event = function(obj) { window.rkt_event_queue.push(obj); };\n"
"window.rkt_get_events = function() { "
" let q = window.rkt_event_queue; "
" window.rkt_event_queue = [];"
" let json_q = JSON.stringify(q);"
" return json_q;"
"};");
// trigger page loaded.
QJsonObject obj;
obj["event"] = "page-loaded";
obj["oke"] = ok;
triggerEvent(w, jsonString(obj));
}
void Rktwebview_qt::triggerEvent(rktwebview_t wv, const QString &msg)
{
}
void Rktwebview_qt::rktQuit()
{
QList<int> keys = _views.keys();
@@ -168,13 +207,18 @@ void Rktwebview_qt::rktQuit()
while(!c.done) { doEvents(); }
}
void Rktwebview_qt::runJs(rktwebview_t wv, const char *js)
{
if (_views.contains(wv)) {
QString _js(js);
WebViewQt *view = _views[wv]->view();
view->runJs(_js);
}
}
void Rktwebview_qt::doEvents()
{
//_quit_event_loop.start(2000);
//_app->exec();
//processCommands();
_app->processEvents();
}
Rktwebview_qt::Rktwebview_qt(Rktwebview_qt **handler) :
@@ -189,8 +233,7 @@ Rktwebview_qt::Rktwebview_qt(Rktwebview_qt **handler) :
_app = new QApplication(_argc, _argv);
connect(&_process_commands, &QTimer::timeout, this, &Rktwebview_qt::processCommands);
connect(&_quit_event_loop, &QTimer::timeout, this, &Rktwebview_qt::interruptEventLoop);
_process_commands.start(1000);
_process_commands.start(10);
*_handler = nullptr;
}