From 9c812753e2b1f01b2e0c85be9904ff7694f0e6ec Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 1 May 2025 23:41:54 +0530 Subject: Implement NotificationReceived event --- src/events/Event.hpp | 9 +++++ src/events/NotificationReceivedEvent.hpp | 47 ++++++++++++++++++++++++++ src/events/PermissionRequestedEvent.hpp | 58 ++++++++++++-------------------- 3 files changed, 78 insertions(+), 36 deletions(-) create mode 100644 src/events/NotificationReceivedEvent.hpp (limited to 'src/events') diff --git a/src/events/Event.hpp b/src/events/Event.hpp index a0853d6..11f1141 100644 --- a/src/events/Event.hpp +++ b/src/events/Event.hpp @@ -8,6 +8,15 @@ lua_push##TYPE(state, VALUE); \ lua_settable(state, -3); +// Not a huge fan but looks nice +#define SET_FIELD_CLOSURE_WITH_SELF(NAME, CLOSURE) \ + lua_pushstring(state, NAME); \ + lua_pushlightuserdata(state, (void *)this); \ + lua_pushcclosure(state, CLOSURE, 1); \ + lua_settable(state, -3); + +#define GET_CLOSURE_SELF(TYPE) static_cast(lua_touserdata(state, lua_upvalueindex(1))); + class Event { public: QString kind = "-"; diff --git a/src/events/NotificationReceivedEvent.hpp b/src/events/NotificationReceivedEvent.hpp new file mode 100644 index 0000000..ade2132 --- /dev/null +++ b/src/events/NotificationReceivedEvent.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include +#include +#include + +#include "events/Event.hpp" + +class NotificationReceivedEvent : public Event { +public: + std::unique_ptr notification; + + NotificationReceivedEvent(std::unique_ptr notification) + : notification(std::move(notification)) { + kind = "NotificationReceived"; + } + + void lua_push(lua_State *state) const override { + lua_newtable(state); + SET_FIELD("type", string, kind.toStdString().c_str()) + SET_FIELD("title", string, notification->title().toStdString().c_str()) + SET_FIELD("message", string, notification->message().toStdString().c_str()) + SET_FIELD("tag", string, notification->tag().toStdString().c_str()) + SET_FIELD("origin", string, notification->origin().toString().toStdString().c_str()) + + SET_FIELD_CLOSURE_WITH_SELF("click", [](lua_State *state) { + auto *event = GET_CLOSURE_SELF(NotificationReceivedEvent *); + event->notification->click(); + lua_pushnil(state); + return 1; + }); + + SET_FIELD_CLOSURE_WITH_SELF("show", [](lua_State *state) { + auto *event = GET_CLOSURE_SELF(NotificationReceivedEvent *); + event->notification->show(); + lua_pushnil(state); + return 1; + }); + + SET_FIELD_CLOSURE_WITH_SELF("close", [](lua_State *state) { + auto *event = GET_CLOSURE_SELF(NotificationReceivedEvent *); + event->notification->close(); + lua_pushnil(state); + return 1; + }); + } +}; diff --git a/src/events/PermissionRequestedEvent.hpp b/src/events/PermissionRequestedEvent.hpp index 089cc99..ec03ea5 100644 --- a/src/events/PermissionRequestedEvent.hpp +++ b/src/events/PermissionRequestedEvent.hpp @@ -39,6 +39,28 @@ public: win_id); } + void lua_push(lua_State *state) const override { + lua_newtable(state); + SET_FIELD("type", string, kind.toStdString().c_str()) + SET_FIELD("permission_type", string, permission_type()) + SET_FIELD("view_id", integer, webview_id) + SET_FIELD("win_id", integer, win_id) + + SET_FIELD_CLOSURE_WITH_SELF("accept", [](lua_State *state) { + auto *event = GET_CLOSURE_SELF(PermissionRequestedEvent *); + event->accept_request(); + lua_pushnil(state); + return 1; + }); + + SET_FIELD_CLOSURE_WITH_SELF("reject", [](lua_State *state) { + auto *event = GET_CLOSURE_SELF(PermissionRequestedEvent *); + event->reject_request(); + lua_pushnil(state); + return 1; + }); + } + [[nodiscard]] const char *permission_type() const { switch (premission_type) { case QWebEnginePermission::PermissionType::MediaAudioCapture: @@ -65,40 +87,4 @@ public: return "Unsupported"; } } - - void lua_push(lua_State *state) const override { - lua_newtable(state); - SET_FIELD("type", string, kind.toStdString().c_str()) - SET_FIELD("permission_type", string, permission_type()) - SET_FIELD("view_id", integer, webview_id) - SET_FIELD("win_id", integer, win_id) - - lua_pushstring(state, "accept"); - lua_pushlightuserdata(state, (void *)this); - lua_pushcclosure( - state, - [](lua_State *state) { - void *userdata = lua_touserdata(state, lua_upvalueindex(1)); - auto *event = static_cast(userdata); - event->accept_request(); - lua_pushnil(state); - return 1; - }, - 1); - lua_settable(state, -3); - - lua_pushstring(state, "reject"); - lua_pushlightuserdata(state, (void *)this); - lua_pushcclosure( - state, - [](lua_State *state) { - void *userdata = lua_touserdata(state, lua_upvalueindex(1)); - auto *event = static_cast(userdata); - event->reject_request(); - lua_pushnil(state); - return 1; - }, - 1); - lua_settable(state, -3); - } }; -- cgit v1.3.1