hyprbars: support a dynamic nobar window rule

fixes #70

fixes #28

ref #74
This commit is contained in:
Vaxry 2024-05-02 02:31:33 +01:00
parent 97e9d252ff
commit 18daf37b7c
4 changed files with 38 additions and 7 deletions

View file

@ -53,3 +53,9 @@ Use the `hyprbars-button` keyword.
```ini ```ini
hyprbars-button = color, size, icon, on-click hyprbars-button = color, size, icon, on-click
``` ```
## Window rules
Hyprbars supports the following _dynamic_ window rules:
`plugin:hyprbars:nobar` -> disables the bar on matching windows.

View file

@ -31,11 +31,11 @@ SDecorationPositioningInfo CHyprBar::getPositioningInfo() {
static auto* const PPRECEDENCE = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprbars:bar_precedence_over_border")->getDataStaticPtr(); static auto* const PPRECEDENCE = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprbars:bar_precedence_over_border")->getDataStaticPtr();
SDecorationPositioningInfo info; SDecorationPositioningInfo info;
info.policy = DECORATION_POSITION_STICKY; info.policy = m_bHidden ? DECORATION_POSITION_ABSOLUTE : DECORATION_POSITION_STICKY;
info.edges = DECORATION_EDGE_TOP; info.edges = DECORATION_EDGE_TOP;
info.priority = **PPRECEDENCE ? 10005 : 5000; info.priority = **PPRECEDENCE ? 10005 : 5000;
info.reserved = true; info.reserved = true;
info.desiredExtents = {{0, **PHEIGHT}, {0, 0}}; info.desiredExtents = {{0, m_bHidden ? 0 : **PHEIGHT}, {0, 0}};
return info; return info;
} }
@ -381,7 +381,7 @@ void CHyprBar::renderBarButtonsText(CBox* barBox, const float scale, const float
} }
void CHyprBar::draw(CMonitor* pMonitor, float a) { void CHyprBar::draw(CMonitor* pMonitor, float a) {
if (!validMapped(m_pWindow)) if (m_bHidden || !validMapped(m_pWindow))
return; return;
const auto PWINDOW = m_pWindow.lock(); const auto PWINDOW = m_pWindow.lock();
@ -536,3 +536,12 @@ CBox CHyprBar::assignedBoxGlobal() {
PHLWINDOW CHyprBar::getOwner() { PHLWINDOW CHyprBar::getOwner() {
return m_pWindow.lock(); return m_pWindow.lock();
} }
void CHyprBar::setHidden(bool hidden) {
if (m_bHidden == hidden)
return;
m_bHidden = hidden;
g_pDecorationPositioner->repositionDeco(this);
}

View file

@ -33,6 +33,8 @@ class CHyprBar : public IHyprWindowDecoration {
PHLWINDOW getOwner(); PHLWINDOW getOwner();
void setHidden(bool hidden);
private: private:
SWindowDecorationExtents m_seExtents; SWindowDecorationExtents m_seExtents;
@ -44,6 +46,7 @@ class CHyprBar : public IHyprWindowDecoration {
CTexture m_tButtonsTex; CTexture m_tButtonsTex;
bool m_bWindowSizeChanged = false; bool m_bWindowSizeChanged = false;
bool m_bHidden = false;
Vector2D cursorRelativeToBar(); Vector2D cursorRelativeToBar();

View file

@ -30,7 +30,7 @@ static void onCloseWindow(void* self, std::any data) {
// data is guaranteed // data is guaranteed
const auto PWINDOW = std::any_cast<PHLWINDOW>(data); 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; }); 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()) if (BARIT == g_pGlobalState->bars.end())
return; return;
@ -39,10 +39,21 @@ static void onCloseWindow(void* self, std::any data) {
PWINDOW->removeWindowDeco(*BARIT); PWINDOW->removeWindowDeco(*BARIT);
} }
void onPreConfigReload() { static void onPreConfigReload() {
g_pGlobalState->buttons.clear(); 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;
const auto HASNOBAR = std::find_if(window->m_vMatchedRules.begin(), window->m_vMatchedRules.end(), [](const auto& rule) { return rule.szRule == "plugin:hyprbars:nobar"; }) != window->m_vMatchedRules.end();
(*BARIT)->setHidden(HASNOBAR);
}
Hyprlang::CParseResult onNewButton(const char* K, const char* V) { Hyprlang::CParseResult onNewButton(const char* K, const char* V) {
std::string v = V; std::string v = V;
CVarList vars(v); CVarList vars(v);
@ -88,6 +99,8 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
static auto P = HyprlandAPI::registerCallbackDynamic(PHANDLE, "openWindow", [&](void* self, SCallbackInfo& info, std::any data) { onNewWindow(self, data); }); 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 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)); });
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_color", Hyprlang::INT{configStringToInt("rgba(33333388)")}); HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_color", Hyprlang::INT{configStringToInt("rgba(33333388)")});
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_height", Hyprlang::INT{15}); HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_height", Hyprlang::INT{15});
@ -103,7 +116,7 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_button_padding", Hyprlang::INT{5}); HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprbars:bar_button_padding", Hyprlang::INT{5});
HyprlandAPI::addConfigKeyword(PHANDLE, "hyprbars-button", onNewButton, Hyprlang::SHandlerOptions{}); HyprlandAPI::addConfigKeyword(PHANDLE, "hyprbars-button", onNewButton, Hyprlang::SHandlerOptions{});
static auto P3 = HyprlandAPI::registerCallbackDynamic(PHANDLE, "preConfigReload", [&](void* self, SCallbackInfo& info, std::any data) { onPreConfigReload(); }); static auto P4 = HyprlandAPI::registerCallbackDynamic(PHANDLE, "preConfigReload", [&](void* self, SCallbackInfo& info, std::any data) { onPreConfigReload(); });
// add deco to existing windows // add deco to existing windows
for (auto& w : g_pCompositor->m_vWindows) { for (auto& w : g_pCompositor->m_vWindows) {