plugin api: add addNotificationV2

Allows for issuing fancy notifs via api
This commit is contained in:
vaxerski 2023-03-20 15:23:25 +00:00
parent 71a95a581f
commit 34da16b7e6
5 changed files with 79 additions and 10 deletions

View file

@ -77,6 +77,9 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
g_pMouseDownHook = HyprlandAPI::createFunctionHook( g_pMouseDownHook = HyprlandAPI::createFunctionHook(
PHANDLE, HyprlandAPI::getFunctionAddressFromSignature(PHANDLE, "_ZN13CInputManager22processMouseDownNormalEP24wlr_pointer_button_event"), (void*)&hkProcessMouseDownNormal); PHANDLE, HyprlandAPI::getFunctionAddressFromSignature(PHANDLE, "_ZN13CInputManager22processMouseDownNormalEP24wlr_pointer_button_event"), (void*)&hkProcessMouseDownNormal);
// fancy notifications
HyprlandAPI::addNotificationV2(PHANDLE, {{"text", "Example hint"}, {"time", (uint64_t)10000}, {"color", CColor(0.2, 0.2, 0.9, 1.0)}, {"icon", ICON_HINT}});
// Enable our hooks // Enable our hooks
g_pFocusHook->hook(); g_pFocusHook->hook();
g_pMotionHook->hook(); g_pMotionHook->hook();

11
src/SharedDefs.hpp Normal file
View file

@ -0,0 +1,11 @@
#pragma once
enum eIcons
{
ICON_WARNING = 0,
ICON_INFO,
ICON_HINT,
ICON_ERROR,
ICON_CONFUSED,
ICON_NONE
};

View file

@ -4,6 +4,7 @@
#include "../helpers/Timer.hpp" #include "../helpers/Timer.hpp"
#include "../helpers/Monitor.hpp" #include "../helpers/Monitor.hpp"
#include "../render/Texture.hpp" #include "../render/Texture.hpp"
#include "../SharedDefs.hpp"
#include <deque> #include <deque>
@ -16,16 +17,6 @@ enum eIconBackend
ICONS_BACKEND_FA ICONS_BACKEND_FA
}; };
enum eIcons
{
ICON_WARNING = 0,
ICON_INFO,
ICON_HINT,
ICON_ERROR,
ICON_CONFUSED,
ICON_NONE
};
static const std::array<std::array<std::string, ICON_NONE + 1>, 3 /* backends */> ICONS_ARRAY = {std::array<std::string, ICON_NONE + 1>{"[!]", "[i]", "[Hint]", "[Err]", "[?]", ""}, static const std::array<std::array<std::string, ICON_NONE + 1>, 3 /* backends */> ICONS_ARRAY = {std::array<std::string, ICON_NONE + 1>{"[!]", "[i]", "[Hint]", "[Err]", "[?]", ""},
std::array<std::string, ICON_NONE + 1>{"", "", "", "", "", ""}, std::array<std::string, ICON_NONE + 1>{"", "", "", "", "", ""},
std::array<std::string, ICON_NONE + 1>{"", "", "", "", ""}}; std::array<std::string, ICON_NONE + 1>{"", "", "", "", ""}};

View file

@ -193,3 +193,51 @@ APICALL bool HyprlandAPI::removeDispatcher(HANDLE handle, const std::string& nam
return true; return true;
} }
APICALL bool addNotificationV2(HANDLE handle, const std::unordered_map<std::string, std::any>& data) {
auto* const PLUGIN = g_pPluginSystem->getPluginByHandle(handle);
if (!PLUGIN)
return false;
try {
auto iterator = data.find("text");
if (iterator == data.end())
return false;
// mandatory
std::string text;
try {
text = std::any_cast<std::string>(iterator->second);
} catch (std::exception& e) {
// attempt const char*
text = std::any_cast<const char*>(iterator->second);
}
iterator = data.find("time");
if (iterator == data.end())
return false;
const auto TIME = std::any_cast<uint64_t>(iterator->second);
iterator = data.find("color");
if (iterator == data.end())
return false;
const auto COLOR = std::any_cast<CColor>(iterator->second);
// optional
eIcons icon = ICON_NONE;
iterator = data.find("icon");
if (iterator != data.end())
icon = std::any_cast<eIcons>(iterator->second);
g_pHyprNotificationOverlay->addNotification(text, COLOR, TIME, icon);
} catch (std::exception& e) {
// bad any_cast most likely, plugin error
return false;
}
return true;
}

View file

@ -19,6 +19,7 @@ See examples/examplePlugin for an example plugin
#include "../helpers/Color.hpp" #include "../helpers/Color.hpp"
#include "HookSystem.hpp" #include "HookSystem.hpp"
#include "../SharedDefs.hpp"
#include <any> #include <any>
#include <functional> #include <functional>
@ -214,4 +215,19 @@ namespace HyprlandAPI {
returns: true on success. False otherwise. returns: true on success. False otherwise.
*/ */
APICALL bool removeDispatcher(HANDLE handle, const std::string& name); APICALL bool removeDispatcher(HANDLE handle, const std::string& name);
/*
Adds a notification.
data has to contain:
- text: std::string or const char*
- time: uint64_t
- color: CColor
data may contain:
- icon: eIcons
returns: true on success. False otherwise.
*/
APICALL bool addNotificationV2(HANDLE handle, const std::unordered_map<std::string, std::any>& data);
}; };