Skip to content

Commit

Permalink
Fallback to background color when background image not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekSauer committed Feb 21, 2024
1 parent 06b07c5 commit 2f37e86
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/renderer/AsyncResourceGatherer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <pango/pangocairo.h>
#include <algorithm>
#include "../core/hyprlock.hpp"
#include "../helpers/Log.hpp"

std::mutex cvmtx;

Expand Down Expand Up @@ -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{});
Expand Down
13 changes: 5 additions & 8 deletions src/renderer/widgets/Background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ 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;
g_pRenderer->renderRect(monbox, col, 0);
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;
Expand All @@ -38,4 +35,4 @@ bool CBackground::draw(const SRenderData& data) {
g_pRenderer->renderTexture(texbox, asset->texture, data.opacity);

return data.opacity < 1.0;
}
}

0 comments on commit 2f37e86

Please sign in to comment.