From 2f37e8696b25c60a9614a34dd2fb562efb1ff71d Mon Sep 17 00:00:00 2001 From: Derek Sauer Date: Wed, 21 Feb 2024 10:52:21 -0500 Subject: [PATCH] Fallback to background color when background image not found. --- src/renderer/AsyncResourceGatherer.cpp | 13 ++++++++++--- src/renderer/widgets/Background.cpp | 13 +++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/renderer/AsyncResourceGatherer.cpp b/src/renderer/AsyncResourceGatherer.cpp index 8ccf864f..47903131 100644 --- a/src/renderer/AsyncResourceGatherer.cpp +++ b/src/renderer/AsyncResourceGatherer.cpp @@ -5,6 +5,7 @@ #include #include #include "../core/hyprlock.hpp" +#include "../helpers/Log.hpp" std::mutex cvmtx; @@ -61,12 +62,18 @@ void CAsyncResourceGatherer::gather() { 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()); - const auto CAIRO = cairo_create(CAIROISURFACE); + // Make sure the img was actually loaded + if (cairo_surface_status(CAIROISURFACE) != CAIRO_STATUS_SUCCESS) { + Debug::log(WARN, "Background image could not be loaded: {}", path); + continue; + } + + std::string id = std::string{"background:"} + path; + + const auto CAIRO = cairo_create(CAIROISURFACE); cairo_scale(CAIRO, 1, 1); const auto TARGET = &preloadTargets.emplace_back(CAsyncResourceGatherer::SPreloadTarget{}); diff --git a/src/renderer/widgets/Background.cpp b/src/renderer/widgets/Background.cpp index fab24d3f..b5ab494e 100644 --- a/src/renderer/widgets/Background.cpp +++ b/src/renderer/widgets/Background.cpp @@ -6,8 +6,11 @@ CBackground::CBackground(const Vector2D& viewport_, const std::string& resourceI } bool CBackground::draw(const SRenderData& data) { + if (!asset) + asset = g_pRenderer->asyncResourceGatherer->getAssetByID(resourceID); - if (resourceID.empty()) { + // If background image does not exist, for whatever reason, fallback to color + if (!asset) { CBox monbox = {0, 0, viewport.x, viewport.y}; CColor col = color; col.a *= data.opacity; @@ -15,12 +18,6 @@ bool CBackground::draw(const SRenderData& data) { return data.opacity < 1.0; } - if (!asset) - asset = g_pRenderer->asyncResourceGatherer->getAssetByID(resourceID); - - if (!asset) - return false; - CBox texbox = {{}, asset->texture.m_vSize}; Vector2D size = asset->texture.m_vSize; @@ -38,4 +35,4 @@ bool CBackground::draw(const SRenderData& data) { g_pRenderer->renderTexture(texbox, asset->texture, data.opacity); return data.opacity < 1.0; -} \ No newline at end of file +}