From b53597396c3494fb0ed5c1f14b95896b959f67ee Mon Sep 17 00:00:00 2001 From: Hans Dijkema Date: Thu, 30 Apr 2026 11:53:08 +0200 Subject: [PATCH] trying to clear the context menu, but that does not work --- build_menu_from_json.cpp | 2 ++ main.cpp | 6 ++++++ rkt_protocol.h | 1 + rktwebview.cpp | 6 +++++- rktwebview_qt.cpp | 33 ++++++++++++++++++++++++++++----- 5 files changed, 42 insertions(+), 6 deletions(-) diff --git a/build_menu_from_json.cpp b/build_menu_from_json.cpp index 141e344..43f9f0d 100644 --- a/build_menu_from_json.cpp +++ b/build_menu_from_json.cpp @@ -3,6 +3,7 @@ #include #include #include "rktwebview_qt.h" +#include "utils.h" static QAction *addMenuItemFromJson(Rktwebview_qt *owner, QMenu *menu, @@ -87,6 +88,7 @@ QMenu *buildMenuFromJson(Rktwebview_qt *self, const QString &menu_json, int sour QJsonDocument doc = QJsonDocument::fromJson(menu_json.toUtf8(), &err); if (err.error != QJsonParseError::NoError || !doc.isObject()) { + ERROR2("Json parse error for menu: %d, '%s'\n", err.error, err.errorString().toUtf8().constData()); return nullptr; } diff --git a/main.cpp b/main.cpp index 0230e2a..b1454fb 100644 --- a/main.cpp +++ b/main.cpp @@ -384,6 +384,12 @@ void Handler::run() result_queue->enqueue(r); } break; + case CMD_TRAY_CLEAR_MENU: { + int tray = data_obj["wv"].toInt(); + result_t r = webview_handler->rktTraySetMenu(tray, nullptr); + result_queue->enqueue(r); + } + break; case CMD_TRAY_SET_MENU: { int tray = data_obj["wv"].toInt(); QString menu_json = data_obj["menu_json"].toString(); diff --git a/rkt_protocol.h b/rkt_protocol.h index fe579a3..d8f636e 100644 --- a/rkt_protocol.h +++ b/rkt_protocol.h @@ -34,6 +34,7 @@ #define CMD_TRAY_SET_TOOLTIP 31 // arguments: tray: int, tooltip: string -> result_t: int #define CMD_TRAY_SHOW_MESSAGE 32 // arguments: tray: int, title: string, message: string -> result_t: int #define CMD_TRAY_SET_MENU 33 // arguments: tray: int, menu_json: string -> result_t: int +#define CMD_TRAY_CLEAR_MENU 34 // arguments: tray: int #define CMD_NOOP 33621 diff --git a/rktwebview.cpp b/rktwebview.cpp index 65298fa..b867fd7 100644 --- a/rktwebview.cpp +++ b/rktwebview.cpp @@ -966,6 +966,10 @@ result_t rkt_webview_tray_show_message(rktwebview_t tray, const char *title, con result_t rkt_webview_tray_set_menu(rktwebview_t tray, const char *menu_json) { - CMDRES(CMD_TRAY_SET_MENU, tray, "menu", menu_json) + if (menu_json == nullptr) { + CMDRES0(CMD_TRAY_CLEAR_MENU, tray) + } else { + CMDRES(CMD_TRAY_SET_MENU, tray, "menu_json", menu_json) + } } diff --git a/rktwebview_qt.cpp b/rktwebview_qt.cpp index 5dca7eb..f3b9db9 100644 --- a/rktwebview_qt.cpp +++ b/rktwebview_qt.cpp @@ -567,7 +567,8 @@ void Rktwebview_qt::processCommand(Command *cmd) break; case COMMAND_TRAY_SET_MENU: { int tray_id = cmd->args[0].toInt(); - QString menu_json = cmd->args[1].toString(); + int has_menu = cmd->args[1].toInt(); + QString menu_json = cmd->args[2].toString(); if (!_trays.contains(tray_id)) { cmd->result = false; @@ -581,10 +582,20 @@ void Rktwebview_qt::processCommand(Command *cmd) delete old; } - QMenu *menu = buildMenuFromJson(this, menu_json, tray_id); + QMenu *menu = (has_menu) ? buildMenuFromJson(this, menu_json, tray_id) : nullptr; if (menu == nullptr) { - cmd->result = false; - cmd->done = true; + if (!has_menu) { + INFO1("null menu, setting null menu on tray %d\n", tray_id); + _trays[tray_id]->setContextMenu(nullptr); + INFO0("Done setting tray menu to null menu\n"); + WARN0("Note: on Linux, this will not clear the context menu\n"); + cmd->result = true; + cmd->done = true; + } else { + ERROR2("menu json '%s' for tray %d is not oke\n", menu_json.toUtf8().constData(), tray_id); + cmd->result = false; + cmd->done = true; + } } else { _tray_menus[tray_id] = menu; _trays[tray_id]->setContextMenu(menu); @@ -606,6 +617,17 @@ void Rktwebview_qt::handleTrayActivation(QSystemTrayIcon::ActivationReason reaso { QObject *obj = sender(); + QSystemTrayIcon *icn = qobject_cast(obj); + + // If a context menu has been set, pop it up and leave. + /*if (reason == QSystemTrayIcon::ActivationReason::Context) { + QMenu *mnu = icn->contextMenu(); + if (mnu != nullptr) { + mnu->popup(QCursor::pos()); + return; + } + }*/ + int tray_wv = obj->property("tray-id").toInt(); QString evt = "tray-activated"; QString reason_str = activationReasonToString(reason); @@ -1123,7 +1145,8 @@ result_t Rktwebview_qt::rktTraySetMenu(rktwebview_t tray, const char *menu_json) { Command c(COMMAND_TRAY_SET_MENU); c.args.push_back(tray); - c.args.push_back(menu_json); + c.args.push_back(menu_json != nullptr); + c.args.push_back((menu_json == nullptr) ? "" : menu_json); postCommand(&c); while(!c.done) { doEvents(); }