hyprland-plugins/hyprbars/main.cpp

144 lines
5.6 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>
2024-03-20 04:02:10 +01:00
#include <hyprland/src/desktop/Window.hpp>
2023-05-01 03:57:48 +02:00
#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;
}
static void onNewWindow(void* self, std::any data) {
2023-02-28 21:30:51 +01:00
// data is guaranteed
2024-04-27 14:03:46 +02:00
const auto PWINDOW = std::any_cast<PHLWINDOW>(data);
2023-02-28 21:30:51 +01:00
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
}
static void onCloseWindow(void* self, std::any data) {
// data is guaranteed
2024-04-27 14:03:46 +02:00
const auto PWINDOW = std::any_cast<PHLWINDOW>(data);
const auto BARIT = std::find_if(g_pGlobalState->bars.begin(), g_pGlobalState->bars.end(), [PWINDOW](const auto& bar) { return bar->getOwner() == PWINDOW; });
if (BARIT == g_pGlobalState->bars.end())
return;
// we could use the API but this is faster + it doesn't matter here that much.
PWINDOW->removeWindowDeco(*BARIT);
}
static void onPreConfigReload() {
2023-10-29 18:33:32 +01:00
g_pGlobalState->buttons.clear();
}
static void onUpdateWindowRules(PHLWINDOW window) {
const auto BARIT = std::find_if(g_pGlobalState->bars.begin(), g_pGlobalState->bars.end(), [window](const auto& bar) { return bar->getOwner() == window; });
if (BARIT == g_pGlobalState->bars.end())
return;
(*BARIT)->updateRules();
window->updateWindowDecos();
}
2024-02-18 16:30:21 +01:00
Hyprlang::CParseResult onNewButton(const char* K, const char* V) {
std::string v = V;
CVarList vars(v);
Hyprlang::CParseResult result;
2023-10-29 18:33:32 +01:00
// hyprbars-button = color, size, icon, action
2023-10-29 18:33:32 +01:00
2024-02-18 16:30:21 +01:00
if (vars[0].empty() || vars[1].empty()) {
result.setError("var 1 and 2 cannot be empty");
return result;
}
2023-10-29 18:33:32 +01:00
float size = 10;
try {
size = std::stof(vars[1]);
2024-02-18 16:30:21 +01:00
} catch (std::exception& e) {
result.setError("failed parsing var 2 as int");
return result;
}
2023-10-29 18:33:32 +01:00
auto X = configStringToInt(vars[0]);
if (!X) {
result.setError("var2 is not a valid number");
return result;
}
g_pGlobalState->buttons.push_back(SHyprButton{vars[3], *X, size, vars[2]});
for (auto& b : g_pGlobalState->bars) {
b->m_bButtonsDirty = true;
}
2024-02-18 16:30:21 +01:00
return result;
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)",
2024-12-04 15:58:09 +01:00
CHyprColor{1.0, 0.2, 0.2, 1.0}, 5000);
2023-10-29 18:36:52 +01:00
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
2024-04-22 16:57:18 +02:00
static auto P = HyprlandAPI::registerCallbackDynamic(PHANDLE, "openWindow", [&](void* self, SCallbackInfo& info, std::any data) { onNewWindow(self, data); });
static auto P2 = HyprlandAPI::registerCallbackDynamic(PHANDLE, "closeWindow", [&](void* self, SCallbackInfo& info, std::any data) { onCloseWindow(self, data); });
static auto P3 = HyprlandAPI::registerCallbackDynamic(PHANDLE, "windowUpdateRules",
[&](void* self, SCallbackInfo& info, std::any data) { onUpdateWindowRules(std::any_cast<PHLWINDOW>(data)); });
2023-02-28 21:30:51 +01:00
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_color", Hyprlang::INT{*configStringToInt("rgba(33333388)")});
2024-02-18 16:30:21 +01:00
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_height", Hyprlang::INT{15});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:col.text", Hyprlang::INT{*configStringToInt("rgba(ffffffff)")});
2024-02-18 16:30:21 +01:00
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_text_size", Hyprlang::INT{10});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_title_enabled", Hyprlang::INT{1});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_text_font", Hyprlang::STRING{"Sans"});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_text_align", Hyprlang::STRING{"center"});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_part_of_window", Hyprlang::INT{1});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_precedence_over_border", Hyprlang::INT{0});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_buttons_alignment", Hyprlang::STRING{"right"});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_padding", Hyprlang::INT{7});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_button_padding", Hyprlang::INT{5});
HyprlandAPI::addConfigKeyword(PHANDLE, "hyprbars-button", onNewButton, Hyprlang::SHandlerOptions{});
static auto P4 = 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;
2024-04-27 14:03:46 +02:00
onNewWindow(nullptr /* unused */, std::any(w));
2023-02-28 21:30:51 +01:00
}
HyprlandAPI::reloadConfig();
2024-12-04 15:58:09 +01:00
HyprlandAPI::addNotification(PHANDLE, "[hyprbars] Initialized successfully!", CHyprColor{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;
}