trying to clear the context menu, but that does not work

This commit is contained in:
2026-04-30 11:53:08 +02:00
parent be980dbffd
commit b53597396c
5 changed files with 42 additions and 6 deletions
+2
View File
@@ -3,6 +3,7 @@
#include <QJsonObject> #include <QJsonObject>
#include <QMenu> #include <QMenu>
#include "rktwebview_qt.h" #include "rktwebview_qt.h"
#include "utils.h"
static QAction *addMenuItemFromJson(Rktwebview_qt *owner, static QAction *addMenuItemFromJson(Rktwebview_qt *owner,
QMenu *menu, 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); QJsonDocument doc = QJsonDocument::fromJson(menu_json.toUtf8(), &err);
if (err.error != QJsonParseError::NoError || !doc.isObject()) { if (err.error != QJsonParseError::NoError || !doc.isObject()) {
ERROR2("Json parse error for menu: %d, '%s'\n", err.error, err.errorString().toUtf8().constData());
return nullptr; return nullptr;
} }
+6
View File
@@ -384,6 +384,12 @@ void Handler::run()
result_queue->enqueue(r); result_queue->enqueue(r);
} }
break; 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: { case CMD_TRAY_SET_MENU: {
int tray = data_obj["wv"].toInt(); int tray = data_obj["wv"].toInt();
QString menu_json = data_obj["menu_json"].toString(); QString menu_json = data_obj["menu_json"].toString();
+1
View File
@@ -34,6 +34,7 @@
#define CMD_TRAY_SET_TOOLTIP 31 // arguments: tray: int, tooltip: string -> result_t: int #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_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_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 #define CMD_NOOP 33621
+5 -1
View File
@@ -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) 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)
}
} }
+26 -3
View File
@@ -567,7 +567,8 @@ void Rktwebview_qt::processCommand(Command *cmd)
break; break;
case COMMAND_TRAY_SET_MENU: { case COMMAND_TRAY_SET_MENU: {
int tray_id = cmd->args[0].toInt(); 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)) { if (!_trays.contains(tray_id)) {
cmd->result = false; cmd->result = false;
@@ -581,10 +582,20 @@ void Rktwebview_qt::processCommand(Command *cmd)
delete old; delete old;
} }
QMenu *menu = buildMenuFromJson(this, menu_json, tray_id); QMenu *menu = (has_menu) ? buildMenuFromJson(this, menu_json, tray_id) : nullptr;
if (menu == nullptr) { if (menu == nullptr) {
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->result = false;
cmd->done = true; cmd->done = true;
}
} else { } else {
_tray_menus[tray_id] = menu; _tray_menus[tray_id] = menu;
_trays[tray_id]->setContextMenu(menu); _trays[tray_id]->setContextMenu(menu);
@@ -606,6 +617,17 @@ void Rktwebview_qt::handleTrayActivation(QSystemTrayIcon::ActivationReason reaso
{ {
QObject *obj = sender(); QObject *obj = sender();
QSystemTrayIcon *icn = qobject_cast<QSystemTrayIcon *>(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(); int tray_wv = obj->property("tray-id").toInt();
QString evt = "tray-activated"; QString evt = "tray-activated";
QString reason_str = activationReasonToString(reason); 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); Command c(COMMAND_TRAY_SET_MENU);
c.args.push_back(tray); 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); postCommand(&c);
while(!c.done) { doEvents(); } while(!c.done) { doEvents(); }