From 6a31b2f1825b253f21ad1c1fa6e0534351899f40 Mon Sep 17 00:00:00 2001 From: Vaxry Date: Mon, 19 Feb 2024 20:50:58 +0000 Subject: [PATCH] widgets: allow background to take a color --- src/config/ConfigManager.cpp | 2 ++ src/renderer/AsyncResourceGatherer.cpp | 6 +++++- src/renderer/Renderer.cpp | 8 +++++--- src/renderer/widgets/Background.cpp | 11 ++++++++++- src/renderer/widgets/Background.hpp | 4 +++- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 102a2f5..5aa63ae 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -24,6 +24,7 @@ void CConfigManager::init() { m_config.addSpecialCategory("background", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true}); m_config.addSpecialConfigValue("background", "monitor", Hyprlang::STRING{""}); m_config.addSpecialConfigValue("background", "path", Hyprlang::STRING{""}); + m_config.addSpecialConfigValue("background", "color", Hyprlang::INT{0xFF111111}); m_config.addSpecialCategory("input-field", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true}); m_config.addSpecialConfigValue("input-field", "monitor", Hyprlang::STRING{""}); @@ -70,6 +71,7 @@ std::vector CConfigManager::getWidgetConfigs() { std::any_cast(m_config.getSpecialConfigValue("background", "monitor", k.c_str())), { {"path", m_config.getSpecialConfigValue("background", "path", k.c_str())}, + {"color", m_config.getSpecialConfigValue("background", "color", k.c_str())}, } }); // clang-format on diff --git a/src/renderer/AsyncResourceGatherer.cpp b/src/renderer/AsyncResourceGatherer.cpp index 167e67d..7e42528 100644 --- a/src/renderer/AsyncResourceGatherer.cpp +++ b/src/renderer/AsyncResourceGatherer.cpp @@ -57,7 +57,11 @@ void CAsyncResourceGatherer::gather() { progress += 1.0 / (preloads + 1.0); std::string path = std::any_cast(c.values.at("path")); - std::string id = std::string{"background:"} + path; + + if (path.empty()) + continue; + + std::string id = std::string{"background:"} + path; // preload bg img const auto CAIROISURFACE = cairo_image_surface_create_from_png(path.c_str()); diff --git a/src/renderer/Renderer.cpp b/src/renderer/Renderer.cpp index 0a6728e..7884802 100644 --- a/src/renderer/Renderer.cpp +++ b/src/renderer/Renderer.cpp @@ -251,9 +251,11 @@ std::vector>* CRenderer::getOrCreateWidgetsFor(const CS continue; // by type - if (c.type == "background") - widgets[surf].emplace_back(std::make_unique(surf->size, std::string{"background:"} + std::any_cast(c.values.at("path")))); - else if (c.type == "input-field") { + if (c.type == "background") { + const std::string PATH = std::any_cast(c.values.at("path")); + widgets[surf].emplace_back( + std::make_unique(surf->size, PATH.empty() ? "" : std::string{"background:"} + PATH, std::any_cast(c.values.at("color")))); + } else if (c.type == "input-field") { const auto SIZE = std::any_cast(c.values.at("size")); widgets[surf].emplace_back(std::make_unique( surf->size, Vector2D{SIZE.x, SIZE.y}, std::any_cast(c.values.at("outer_color")), std::any_cast(c.values.at("inner_color")), diff --git a/src/renderer/widgets/Background.cpp b/src/renderer/widgets/Background.cpp index 86f43f1..4c72980 100644 --- a/src/renderer/widgets/Background.cpp +++ b/src/renderer/widgets/Background.cpp @@ -1,11 +1,20 @@ #include "Background.hpp" #include "../Renderer.hpp" -CBackground::CBackground(const Vector2D& viewport_, const std::string& resourceID_) : viewport(viewport_), resourceID(resourceID_) { +CBackground::CBackground(const Vector2D& viewport_, const std::string& resourceID_, const CColor& color_) : viewport(viewport_), resourceID(resourceID_), color(color_) { ; } bool CBackground::draw(const SRenderData& data) { + + if (resourceID.empty()) { + CBox monbox = {0, 0, viewport.x, viewport.y}; + CColor col = color; + col.a *= data.opacity; + g_pRenderer->renderRect(monbox, col, 0); + return data.opacity < 1.0; + } + if (!asset) asset = g_pRenderer->asyncResourceGatherer->getAssetByID(resourceID); diff --git a/src/renderer/widgets/Background.hpp b/src/renderer/widgets/Background.hpp index 9845c41..b736aa9 100644 --- a/src/renderer/widgets/Background.hpp +++ b/src/renderer/widgets/Background.hpp @@ -2,18 +2,20 @@ #include "IWidget.hpp" #include "../../helpers/Vector2D.hpp" +#include "../../helpers/Color.hpp" #include struct SPreloadedAsset; class CBackground : public IWidget { public: - CBackground(const Vector2D& viewport, const std::string& resourceID); + CBackground(const Vector2D& viewport, const std::string& resourceID, const CColor& color); virtual bool draw(const SRenderData& data); private: Vector2D viewport; std::string resourceID; + CColor color; SPreloadedAsset* asset = nullptr; }; \ No newline at end of file