hyprlock/src/renderer/widgets/Label.cpp

126 lines
4 KiB
C++
Raw Normal View History

2024-02-19 17:26:08 +01:00
#include "Label.hpp"
#include "../../helpers/Color.hpp"
#include <hyprlang.hpp>
#include "../Renderer.hpp"
#include "../../helpers/Log.hpp"
2024-02-20 01:11:19 +01:00
#include "../../core/hyprlock.hpp"
2024-02-19 17:26:08 +01:00
2024-02-20 01:11:19 +01:00
CLabel::~CLabel() {
labelTimer->cancel();
labelTimer.reset();
}
static void onTimer(std::shared_ptr<CTimer> self, void* data) {
const auto PLABEL = (CLabel*)data;
// update label
PLABEL->onTimerUpdate();
// plant new timer
2024-02-20 01:11:19 +01:00
PLABEL->plantTimer();
}
static void onAssetCallback(void* data) {
const auto PLABEL = (CLabel*)data;
PLABEL->renderSuper();
}
std::string CLabel::getUniqueResourceId() {
return std::string{"label:"} + std::to_string((uintptr_t)this) + ",time:" + std::to_string(std::chrono::system_clock::now().time_since_epoch().count());
}
2024-02-20 01:11:19 +01:00
void CLabel::onTimerUpdate() {
std::string oldFormatted = label.formatted;
label = formatString(labelPreFormat);
2024-02-21 15:08:40 +01:00
if (label.formatted == oldFormatted && !label.alwaysUpdate)
2024-02-19 17:26:08 +01:00
return;
2024-02-20 01:11:19 +01:00
if (!pendingResourceID.empty())
return; // too many updates, we'll miss some. Shouldn't happen tbh
// request new
request.id = getUniqueResourceId();
2024-02-20 01:11:19 +01:00
pendingResourceID = request.id;
request.asset = label.formatted;
request.callback = onAssetCallback;
request.callbackData = this;
2024-02-20 01:11:19 +01:00
g_pRenderer->asyncResourceGatherer->requestAsyncAssetPreload(request);
2024-02-19 17:26:08 +01:00
}
2024-02-20 01:11:19 +01:00
void CLabel::plantTimer() {
if (label.updateEveryMs != 0)
labelTimer = g_pHyprlock->addTimer(std::chrono::milliseconds((int)label.updateEveryMs), onTimer, this);
2024-02-19 17:26:08 +01:00
}
2024-03-05 21:24:11 +01:00
CLabel::CLabel(const Vector2D& viewport_, const std::unordered_map<std::string, std::any>& props, CSessionLockSurface* surface_) :
surface(surface_), shadow(this, props, viewport_) {
2024-02-20 01:11:19 +01:00
labelPreFormat = std::any_cast<Hyprlang::STRING>(props.at("text"));
std::string fontFamily = std::any_cast<Hyprlang::STRING>(props.at("font_family"));
CColor labelColor = std::any_cast<Hyprlang::INT>(props.at("color"));
int fontSize = std::any_cast<Hyprlang::INT>(props.at("font_size"));
2024-02-19 17:26:08 +01:00
2024-02-20 01:11:19 +01:00
label = formatString(labelPreFormat);
request.id = getUniqueResourceId();
2024-02-19 17:26:08 +01:00
resourceID = request.id;
2024-02-20 01:11:19 +01:00
request.asset = label.formatted;
2024-02-19 17:26:08 +01:00
request.type = CAsyncResourceGatherer::eTargetType::TARGET_TEXT;
request.props["font_family"] = fontFamily;
request.props["color"] = labelColor;
request.props["font_size"] = fontSize;
2024-02-21 15:08:40 +01:00
request.props["cmd"] = label.cmd;
2024-02-19 17:26:08 +01:00
g_pRenderer->asyncResourceGatherer->requestAsyncAssetPreload(request);
auto POS__ = std::any_cast<Hyprlang::VEC2>(props.at("position"));
pos = {POS__.x, POS__.y};
2024-02-20 01:11:19 +01:00
configPos = pos;
2024-02-19 17:26:08 +01:00
viewport = viewport_;
halign = std::any_cast<Hyprlang::STRING>(props.at("halign"));
valign = std::any_cast<Hyprlang::STRING>(props.at("valign"));
2024-02-20 01:11:19 +01:00
plantTimer();
2024-02-19 17:26:08 +01:00
}
bool CLabel::draw(const SRenderData& data) {
if (!asset) {
asset = g_pRenderer->asyncResourceGatherer->getAssetByID(resourceID);
if (!asset)
return true;
// calc pos
2024-02-20 01:11:19 +01:00
pos = posFromHVAlign(viewport, asset->texture.m_vSize, configPos, halign, valign);
2024-03-05 21:24:11 +01:00
shadow.markShadowDirty();
2024-02-20 01:11:19 +01:00
}
if (!pendingResourceID.empty()) {
// new asset is pending
auto newAsset = g_pRenderer->asyncResourceGatherer->getAssetByID(pendingResourceID);
if (newAsset) {
// new asset is ready :D
g_pRenderer->asyncResourceGatherer->unloadAsset(asset);
asset = newAsset;
resourceID = pendingResourceID;
pendingResourceID = "";
pos = posFromHVAlign(viewport, asset->texture.m_vSize, configPos, halign, valign);
2024-03-05 21:24:11 +01:00
shadow.markShadowDirty();
2024-02-20 01:11:19 +01:00
}
2024-02-19 17:26:08 +01:00
}
CBox box = {pos.x, pos.y, asset->texture.m_vSize.x, asset->texture.m_vSize.y};
2024-03-05 21:24:11 +01:00
shadow.draw(data);
2024-02-19 17:26:08 +01:00
g_pRenderer->renderTexture(box, asset->texture, data.opacity);
return !pendingResourceID.empty();
2024-02-20 01:11:19 +01:00
}
void CLabel::renderSuper() {
surface->render();
2024-02-19 17:26:08 +01:00
}