Adding a tray icon.
This commit is contained in:
+227
-1
@@ -18,15 +18,36 @@
|
||||
#include <QFileDialog>
|
||||
#include <QMessageBox>
|
||||
#include <QAbstractButton>
|
||||
#include <QSystemTrayIcon>
|
||||
#include <QMenu>
|
||||
#include <QAction>
|
||||
#include "build_menu_from_json.h"
|
||||
|
||||
static inline char *copyString(const char *s)
|
||||
{
|
||||
int l = strlen(s);
|
||||
int l = static_cast<int>(strlen(s));
|
||||
char *cpy = static_cast<char *>(malloc(l + 1));
|
||||
memcpy(cpy, s, l + 1);
|
||||
return cpy;
|
||||
}
|
||||
|
||||
static QString activationReasonToString(QSystemTrayIcon::ActivationReason r)
|
||||
{
|
||||
switch (r) {
|
||||
case QSystemTrayIcon::Context:
|
||||
return "context-menu"; // rechtsklik / menu openen
|
||||
case QSystemTrayIcon::DoubleClick:
|
||||
return "double-click";
|
||||
case QSystemTrayIcon::Trigger:
|
||||
return "click"; // meestal linkerklik
|
||||
case QSystemTrayIcon::MiddleClick:
|
||||
return "middle-click";
|
||||
case QSystemTrayIcon::Unknown:
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
void Rktwebview_qt::processCommand(Command *cmd)
|
||||
{
|
||||
switch(cmd->cmd) {
|
||||
@@ -168,6 +189,19 @@ void Rktwebview_qt::processCommand(Command *cmd)
|
||||
}
|
||||
_view_js_callbacks.remove(wv);
|
||||
delete w;
|
||||
} else if (_trays.contains(wv)) {
|
||||
QSystemTrayIcon *tray = _trays[wv];
|
||||
_trays.remove(wv);
|
||||
|
||||
if (_tray_menus.contains(wv)) {
|
||||
QMenu *menu = _tray_menus[wv];
|
||||
_tray_menus.remove(wv);
|
||||
delete menu;
|
||||
}
|
||||
|
||||
tray->hide();
|
||||
delete tray;
|
||||
cmd->result = true;
|
||||
} else {
|
||||
cmd->result = false;
|
||||
}
|
||||
@@ -306,6 +340,18 @@ void Rktwebview_qt::processCommand(Command *cmd)
|
||||
w->activateWindow();
|
||||
}
|
||||
cmd->result = true;
|
||||
} else if (_trays.contains(wv)) {
|
||||
QSystemTrayIcon *tray = _trays[wv];
|
||||
|
||||
if (cmd->cmd == COMMAND_SHOW_WIN) {
|
||||
tray->show();
|
||||
cmd->result = true;
|
||||
} else if (cmd->cmd == COMMAND_HIDE_WIN) {
|
||||
tray->hide();
|
||||
cmd->result = true;
|
||||
} else {
|
||||
cmd->result = false;
|
||||
}
|
||||
} else {
|
||||
cmd->result = false;
|
||||
}
|
||||
@@ -449,6 +495,108 @@ void Rktwebview_qt::processCommand(Command *cmd)
|
||||
}
|
||||
cmd->done = true;
|
||||
}
|
||||
case COMMAND_CREATE_TRAY: {
|
||||
QString icon_file = cmd->args[0].toString();
|
||||
QString tooltip = cmd->args[1].toString();
|
||||
|
||||
int id = nextHandle();
|
||||
|
||||
QSystemTrayIcon *tray = new QSystemTrayIcon(this);
|
||||
tray->setIcon(QIcon(icon_file));
|
||||
tray->setToolTip(tooltip);
|
||||
|
||||
connect(tray, &QSystemTrayIcon::activated,
|
||||
this, [this, id](QSystemTrayIcon::ActivationReason reason) {
|
||||
EventContainer e("tray-activated");
|
||||
e["reason"] = activationReasonToString(reason);
|
||||
this->triggerEvent(id, e);
|
||||
});
|
||||
|
||||
connect(tray, &QSystemTrayIcon::messageClicked,
|
||||
this, [this, id]() {
|
||||
EventContainer e("tray-message-clicked");
|
||||
this->triggerEvent(id, e);
|
||||
});
|
||||
|
||||
_trays[id] = tray;
|
||||
|
||||
tray->show();
|
||||
|
||||
cmd->result = id;
|
||||
cmd->done = true;
|
||||
}
|
||||
break;
|
||||
case COMMAND_TRAY_SET_ICON: {
|
||||
int tray_id = cmd->args[0].toInt();
|
||||
QString icon_file = cmd->args[1].toString();
|
||||
|
||||
if (_trays.contains(tray_id)) {
|
||||
_trays[tray_id]->setIcon(QIcon(icon_file));
|
||||
cmd->result = true;
|
||||
} else {
|
||||
cmd->result = false;
|
||||
}
|
||||
|
||||
cmd->done = true;
|
||||
}
|
||||
break;
|
||||
case COMMAND_TRAY_SET_TOOLTIP: {
|
||||
int tray_id = cmd->args[0].toInt();
|
||||
QString tooltip = cmd->args[1].toString();
|
||||
|
||||
if (_trays.contains(tray_id)) {
|
||||
_trays[tray_id]->setToolTip(tooltip);
|
||||
cmd->result = true;
|
||||
} else {
|
||||
cmd->result = false;
|
||||
}
|
||||
|
||||
cmd->done = true;
|
||||
}
|
||||
break;
|
||||
case COMMAND_TRAY_SHOW_MESSAGE: {
|
||||
int tray_id = cmd->args[0].toInt();
|
||||
QString title = cmd->args[1].toString();
|
||||
QString message = cmd->args[2].toString();
|
||||
|
||||
if (_trays.contains(tray_id)) {
|
||||
_trays[tray_id]->showMessage(title, message);
|
||||
cmd->result = true;
|
||||
} else {
|
||||
cmd->result = false;
|
||||
}
|
||||
|
||||
cmd->done = true;
|
||||
}
|
||||
break;
|
||||
case COMMAND_TRAY_SET_MENU: {
|
||||
int tray_id = cmd->args[0].toInt();
|
||||
QString menu_json = cmd->args[1].toString();
|
||||
|
||||
if (!_trays.contains(tray_id)) {
|
||||
cmd->result = false;
|
||||
cmd->done = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (_tray_menus.contains(tray_id)) {
|
||||
QMenu *old = _tray_menus[tray_id];
|
||||
_tray_menus.remove(tray_id);
|
||||
delete old;
|
||||
}
|
||||
|
||||
QMenu *menu = buildMenuFromJson(this, menu_json, tray_id);
|
||||
if (menu == nullptr) {
|
||||
cmd->result = false;
|
||||
cmd->done = true;
|
||||
} else {
|
||||
_tray_menus[tray_id] = menu;
|
||||
_trays[tray_id]->setContextMenu(menu);
|
||||
}
|
||||
|
||||
cmd->result = true;
|
||||
cmd->done = true;
|
||||
}
|
||||
break;
|
||||
default: {
|
||||
cmd->result = false;
|
||||
@@ -888,6 +1036,71 @@ result_t Rktwebview_qt::rktMessageBox(rktwebview_t w, const char *title, const c
|
||||
return r ? result_t::oke : result_t::failed;
|
||||
}
|
||||
|
||||
rktwebview_t Rktwebview_qt::rktTrayCreate(const char *icon_file, const char *tooltip, event_cb_t evt_cb)
|
||||
{
|
||||
Command c(COMMAND_CREATE_TRAY);
|
||||
c.args.push_back(icon_file);
|
||||
c.args.push_back(tooltip);
|
||||
|
||||
void *function = reinterpret_cast<void *>(evt_cb);
|
||||
QVariant f(QVariant::fromValue(function));
|
||||
c.args.push_back(f);
|
||||
|
||||
postCommand(&c);
|
||||
while(!c.done) { doEvents(); }
|
||||
|
||||
return c.result.toInt();
|
||||
}
|
||||
|
||||
result_t Rktwebview_qt::rktTraySetIcon(rktwebview_t tray, const char *icon_file)
|
||||
{
|
||||
Command c(COMMAND_TRAY_SET_ICON);
|
||||
c.args.push_back(tray);
|
||||
c.args.push_back(icon_file);
|
||||
|
||||
postCommand(&c);
|
||||
while(!c.done) { doEvents(); }
|
||||
|
||||
return c.result.toBool() ? result_t::oke : result_t::failed;
|
||||
}
|
||||
|
||||
result_t Rktwebview_qt::rktTraySetTooltip(rktwebview_t tray, const char *tooltip)
|
||||
{
|
||||
Command c(COMMAND_TRAY_SET_TOOLTIP);
|
||||
c.args.push_back(tray);
|
||||
c.args.push_back(tooltip);
|
||||
|
||||
postCommand(&c);
|
||||
while(!c.done) { doEvents(); }
|
||||
|
||||
return c.result.toBool() ? result_t::oke : result_t::failed;
|
||||
}
|
||||
|
||||
result_t Rktwebview_qt::rktTrayShowMessage(rktwebview_t tray, const char *title, const char *message)
|
||||
{
|
||||
Command c(COMMAND_TRAY_SHOW_MESSAGE);
|
||||
c.args.push_back(tray);
|
||||
c.args.push_back(title);
|
||||
c.args.push_back(message);
|
||||
|
||||
postCommand(&c);
|
||||
while(!c.done) { doEvents(); }
|
||||
|
||||
return c.result.toBool() ? result_t::oke : result_t::failed;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
postCommand(&c);
|
||||
while(!c.done) { doEvents(); }
|
||||
|
||||
return c.result.toBool() ? result_t::oke : result_t::failed;
|
||||
}
|
||||
|
||||
result_t Rktwebview_qt::doWindow(rktwebview_t w, int cmd, result_t on_error)
|
||||
{
|
||||
Command c(cmd);
|
||||
@@ -1085,5 +1298,18 @@ Rktwebview_qt::~Rktwebview_qt()
|
||||
delete p;
|
||||
}
|
||||
|
||||
QList<int> tray_keys = _trays.keys();
|
||||
for(i = 0, N = tray_keys.size(); i < N; i++) {
|
||||
QSystemTrayIcon *tray = _trays[tray_keys[i]];
|
||||
tray->hide();
|
||||
delete tray;
|
||||
}
|
||||
|
||||
QList<int> menu_keys = _tray_menus.keys();
|
||||
for(i = 0, N = menu_keys.size(); i < N; i++) {
|
||||
QMenu *menu = _tray_menus[menu_keys[i]];
|
||||
delete menu;
|
||||
}
|
||||
|
||||
delete _app;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user