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.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::SWidgetConfig> CConfigManager::getWidgetConfigs() {
std::any_cast<Hyprlang::STRING>(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

View file

@ -57,7 +57,11 @@ void CAsyncResourceGatherer::gather() {
progress += 1.0 / (preloads + 1.0);
std::string path = std::any_cast<Hyprlang::STRING>(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());

View file

@ -251,9 +251,11 @@ std::vector<std::unique_ptr<IWidget>>* CRenderer::getOrCreateWidgetsFor(const CS
continue;
// by type
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"))));
else if (c.type == "input-field") {
if (c.type == "background") {
const std::string PATH = std::any_cast<Hyprlang::STRING>(c.values.at("path"));
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"));
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")),

View file

@ -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);

View file

@ -2,18 +2,20 @@
#include "IWidget.hpp"
#include "../../helpers/Vector2D.hpp"
#include "../../helpers/Color.hpp"
#include <string>
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;
};