widgets: allow background to take a color

This commit is contained in:
Vaxry 2024-02-19 20:50:58 +00:00
parent 81ca6f068b
commit 6a31b2f182
5 changed files with 25 additions and 6 deletions

View File

@ -24,6 +24,7 @@ void CConfigManager::init() {
m_config.addSpecialCategory("background", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true}); m_config.addSpecialCategory("background", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true});
m_config.addSpecialConfigValue("background", "monitor", Hyprlang::STRING{""}); m_config.addSpecialConfigValue("background", "monitor", Hyprlang::STRING{""});
m_config.addSpecialConfigValue("background", "path", 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.addSpecialCategory("input-field", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true});
m_config.addSpecialConfigValue("input-field", "monitor", Hyprlang::STRING{""}); m_config.addSpecialConfigValue("input-field", "monitor", Hyprlang::STRING{""});
@ -70,6 +71,7 @@ std::vector<CConfigManager::SWidgetConfig> CConfigManager::getWidgetConfigs() {
std::any_cast<Hyprlang::STRING>(m_config.getSpecialConfigValue("background", "monitor", k.c_str())), std::any_cast<Hyprlang::STRING>(m_config.getSpecialConfigValue("background", "monitor", k.c_str())),
{ {
{"path", m_config.getSpecialConfigValue("background", "path", k.c_str())}, {"path", m_config.getSpecialConfigValue("background", "path", k.c_str())},
{"color", m_config.getSpecialConfigValue("background", "color", k.c_str())},
} }
}); });
// clang-format on // clang-format on

View File

@ -57,6 +57,10 @@ void CAsyncResourceGatherer::gather() {
progress += 1.0 / (preloads + 1.0); progress += 1.0 / (preloads + 1.0);
std::string path = std::any_cast<Hyprlang::STRING>(c.values.at("path")); std::string path = std::any_cast<Hyprlang::STRING>(c.values.at("path"));
if (path.empty())
continue;
std::string id = std::string{"background:"} + path; std::string id = std::string{"background:"} + path;
// preload bg img // preload bg img

View File

@ -251,9 +251,11 @@ std::vector<std::unique_ptr<IWidget>>* CRenderer::getOrCreateWidgetsFor(const CS
continue; continue;
// by type // by type
if (c.type == "background") if (c.type == "background") {
widgets[surf].emplace_back(std::make_unique<CBackground>(surf->size, std::string{"background:"} + std::any_cast<Hyprlang::STRING>(c.values.at("path")))); const std::string PATH = std::any_cast<Hyprlang::STRING>(c.values.at("path"));
else if (c.type == "input-field") { widgets[surf].emplace_back(
std::make_unique<CBackground>(surf->size, PATH.empty() ? "" : std::string{"background:"} + PATH, std::any_cast<Hyprlang::INT>(c.values.at("color"))));
} else if (c.type == "input-field") {
const auto SIZE = std::any_cast<Hyprlang::VEC2>(c.values.at("size")); const auto SIZE = std::any_cast<Hyprlang::VEC2>(c.values.at("size"));
widgets[surf].emplace_back(std::make_unique<CPasswordInputField>( widgets[surf].emplace_back(std::make_unique<CPasswordInputField>(
surf->size, Vector2D{SIZE.x, SIZE.y}, std::any_cast<Hyprlang::INT>(c.values.at("outer_color")), std::any_cast<Hyprlang::INT>(c.values.at("inner_color")), surf->size, Vector2D{SIZE.x, SIZE.y}, std::any_cast<Hyprlang::INT>(c.values.at("outer_color")), std::any_cast<Hyprlang::INT>(c.values.at("inner_color")),

View File

@ -1,11 +1,20 @@
#include "Background.hpp" #include "Background.hpp"
#include "../Renderer.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) { 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) if (!asset)
asset = g_pRenderer->asyncResourceGatherer->getAssetByID(resourceID); asset = g_pRenderer->asyncResourceGatherer->getAssetByID(resourceID);

View File

@ -2,18 +2,20 @@
#include "IWidget.hpp" #include "IWidget.hpp"
#include "../../helpers/Vector2D.hpp" #include "../../helpers/Vector2D.hpp"
#include "../../helpers/Color.hpp"
#include <string> #include <string>
struct SPreloadedAsset; struct SPreloadedAsset;
class CBackground : public IWidget { class CBackground : public IWidget {
public: 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); virtual bool draw(const SRenderData& data);
private: private:
Vector2D viewport; Vector2D viewport;
std::string resourceID; std::string resourceID;
CColor color;
SPreloadedAsset* asset = nullptr; SPreloadedAsset* asset = nullptr;
}; };