hyprland-plugins/hyprbars/main.cpp

97 lines
3.5 KiB
C++
Raw Normal View History

2023-02-28 21:30:51 +01:00
#define WLR_USE_UNSTABLE
#include <unistd.h>
#include <any>
2023-05-01 03:57:48 +02:00
#include <hyprland/src/Compositor.hpp>
#include <hyprland/src/Window.hpp>
#include <hyprland/src/config/ConfigManager.hpp>
2023-02-28 21:30:51 +01:00
#include "barDeco.hpp"
#include "globals.hpp"
// Do NOT change this function.
APICALL EXPORT std::string PLUGIN_API_VERSION() {
return HYPRLAND_API_VERSION;
}
void onNewWindow(void* self, std::any data) {
// data is guaranteed
auto* const PWINDOW = std::any_cast<CWindow*>(data);
if (!PWINDOW->m_bX11DoesntWantBorders) {
2023-11-11 15:39:46 +01:00
std::unique_ptr<CHyprBar> bar = std::make_unique<CHyprBar>(PWINDOW);
g_pGlobalState->bars.push_back(bar.get());
HyprlandAPI::addWindowDecoration(PHANDLE, PWINDOW, std::move(bar));
}
2023-02-28 21:30:51 +01:00
}
2023-10-29 18:33:32 +01:00
void onPreConfigReload() {
g_pGlobalState->buttons.clear();
}
void onNewButton(const std::string& k, const std::string& v) {
CVarList vars(v);
// hyprbars-button = color, size, icon, action
2023-10-29 18:33:32 +01:00
if (vars[0].empty() || vars[1].empty())
return;
float size = 10;
try {
size = std::stof(vars[1]);
} catch (std::exception& e) { return; }
g_pGlobalState->buttons.push_back(SHyprButton{vars[3], configStringToInt(vars[0]), size, vars[2]});
for (auto& b : g_pGlobalState->bars) {
b->m_bButtonsDirty = true;
}
2023-10-29 18:33:32 +01:00
}
2023-02-28 21:30:51 +01:00
APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
2023-10-29 18:36:52 +01:00
PHANDLE = handle;
const std::string HASH = __hyprland_api_get_hash();
if (HASH != GIT_COMMIT_HASH) {
HyprlandAPI::addNotification(PHANDLE, "[hyprbars] Failure in initialization: Version mismatch (headers ver is not equal to running hyprland ver)",
CColor{1.0, 0.2, 0.2, 1.0}, 5000);
throw std::runtime_error("[hb] Version mismatch");
}
2023-10-29 18:33:32 +01:00
g_pGlobalState = std::make_unique<SGlobalState>();
2023-02-28 21:30:51 +01:00
2023-10-21 15:55:42 +02:00
HyprlandAPI::registerCallbackDynamic(PHANDLE, "openWindow", [&](void* self, SCallbackInfo& info, std::any data) { onNewWindow(self, data); });
2023-02-28 21:30:51 +01:00
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_color", SConfigValue{.intValue = configStringToInt("rgba(33333388)")});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_height", SConfigValue{.intValue = 15});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:col.text", SConfigValue{.intValue = configStringToInt("rgba(ffffffff)")});
2023-02-28 21:30:51 +01:00
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_text_size", SConfigValue{.intValue = 10});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_text_font", SConfigValue{.strValue = "Sans"});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_part_of_window", SConfigValue{.intValue = 1});
2023-10-29 18:33:32 +01:00
HyprlandAPI::addConfigKeyword(PHANDLE, "hyprbars-button", [&](const std::string& k, const std::string& v) { onNewButton(k, v); });
HyprlandAPI::registerCallbackDynamic(PHANDLE, "preConfigReload", [&](void* self, SCallbackInfo& info, std::any data) { onPreConfigReload(); });
2023-02-28 21:30:51 +01:00
// add deco to existing windows
for (auto& w : g_pCompositor->m_vWindows) {
if (w->isHidden() || !w->m_bIsMapped)
continue;
onNewWindow(nullptr /* unused */, std::any(w.get()));
2023-02-28 21:30:51 +01:00
}
HyprlandAPI::reloadConfig();
2023-04-05 12:20:21 +02:00
HyprlandAPI::addNotification(PHANDLE, "[hyprbars] Initialized successfully!", CColor{0.2, 1.0, 0.2, 1.0}, 5000);
2023-02-28 21:30:51 +01:00
return {"hyprbars", "A plugin to add title bars to windows.", "Vaxry", "1.0"};
}
APICALL EXPORT void PLUGIN_EXIT() {
2023-02-28 23:03:04 +01:00
for (auto& m : g_pCompositor->m_vMonitors)
m->scheduledRecalc = true;
}