Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

widgets: add progress-bar widget implementation #658

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/config/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,22 @@ void CConfigManager::init() {
m_config.addSpecialConfigValue("label", "zindex", Hyprlang::INT{0});
SHADOWABLE("label");

m_config.addSpecialCategory("progressbar", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true});
m_config.addSpecialConfigValue("progressbar", "monitor", Hyprlang::STRING{""});
m_config.addSpecialConfigValue("progressbar", "min", Hyprlang::INT{0});
m_config.addSpecialConfigValue("progressbar", "max", Hyprlang::INT{100});
m_config.addSpecialConfigValue("progressbar", "value", Hyprlang::STRING{""});
m_config.addSpecialConfigValue("progressbar", "border_size", Hyprlang::INT{0});
m_config.addSpecialConfigValue("progressbar", "border_color", Hyprlang::INT{0xFF000000});
m_config.addSpecialConfigValue("progressbar", "rounding", Hyprlang::INT{0});
m_config.addSpecialConfigValue("progressbar", "size", LAYOUTCONFIG("100,20"));
m_config.addSpecialConfigValue("progressbar", "position", LAYOUTCONFIG("0,0"));
m_config.addSpecialConfigValue("progressbar", "halign", Hyprlang::STRING{"center"});
m_config.addSpecialConfigValue("progressbar", "valign", Hyprlang::STRING{"center"});
m_config.addSpecialConfigValue("progressbar", "zindex", Hyprlang::INT{0});
m_config.addSpecialConfigValue("progressbar", "color", Hyprlang::INT{0xFF00FF00});
m_config.addSpecialConfigValue("progressbar", "background_color", Hyprlang::INT{0xFF000000});

m_config.registerHandler(&::handleSource, "source", {false});
m_config.registerHandler(&::handleBezier, "bezier", {false});
m_config.registerHandler(&::handleAnimation, "animation", {false});
Expand Down Expand Up @@ -515,6 +531,31 @@ std::vector<CConfigManager::SWidgetConfig> CConfigManager::getWidgetConfigs() {
// clang-format on
}

keys = m_config.listKeysForSpecialCategory("progressbar");
for (auto& k : keys) {
// clang-format off
result.push_back(CConfigManager::SWidgetConfig{
"progressbar",
std::any_cast<Hyprlang::STRING>(m_config.getSpecialConfigValue("progressbar", "monitor", k.c_str())),
{
{"min", m_config.getSpecialConfigValue("progressbar", "min", k.c_str())},
{"max", m_config.getSpecialConfigValue("progressbar", "max", k.c_str())},
{"value", m_config.getSpecialConfigValue("progressbar", "value", k.c_str())},
{"border_size", m_config.getSpecialConfigValue("progressbar", "border_size", k.c_str())},
{"border_color", m_config.getSpecialConfigValue("progressbar", "border_color", k.c_str())},
{"rounding", m_config.getSpecialConfigValue("progressbar", "rounding", k.c_str())},
{"size", m_config.getSpecialConfigValue("progressbar", "size", k.c_str())},
{"position", m_config.getSpecialConfigValue("progressbar", "position", k.c_str())},
{"halign", m_config.getSpecialConfigValue("progressbar", "halign", k.c_str())},
{"valign", m_config.getSpecialConfigValue("progressbar", "valign", k.c_str())},
{"zindex", m_config.getSpecialConfigValue("progressbar", "zindex", k.c_str())},
{"color", m_config.getSpecialConfigValue("progressbar", "color", k.c_str())},
{"background_color", m_config.getSpecialConfigValue("progressbar", "background_color", k.c_str())},
}
});
// clang-format on
}

return result;
}

Expand Down
3 changes: 3 additions & 0 deletions src/renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "widgets/Label.hpp"
#include "widgets/Image.hpp"
#include "widgets/Shape.hpp"
#include "widgets/ProgressBar.hpp"

inline const float fullVerts[] = {
1, 0, // top right
Expand Down Expand Up @@ -447,6 +448,8 @@ std::vector<std::unique_ptr<IWidget>>* CRenderer::getOrCreateWidgetsFor(const CS
resourceID = "image:" + PATH;

widgets[surf].emplace_back(std::make_unique<CImage>(surf->size, surf->output, resourceID, c.values));
} else if (c.type == "scale") {
widgets[surf].emplace_back(std::make_unique<CProgressBar>(surf->size, c.values, surf->output->stringPort));
}
}
}
Expand Down
105 changes: 105 additions & 0 deletions src/renderer/widgets/ProgressBar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "ProgressBar.hpp"
#include "../Renderer.hpp"
#include "../../core/AnimationManager.hpp"
#include "../../helpers/Log.hpp"
#include "../../config/ConfigDataValues.hpp"
#include "../../config/ConfigManager.hpp"
#include "../../core/hyprlock.hpp"
#include <hyprlang.hpp>

CProgressBar::~CProgressBar() {
if (valueTimer) {
valueTimer->cancel();
valueTimer.reset();
}
}

static void onTimer(std::shared_ptr<CTimer> self, void* data) {
if (data == nullptr)
return;

const auto PSCALE = (CProgressBar*)data;

PSCALE->onTimerUpdate();
PSCALE->plantTimer();
}

void CProgressBar::onTimerUpdate() {
updateValue();
g_pHyprlock->renderOutput(outputStringPort);
}

void CProgressBar::plantTimer() {
if (value.updateEveryMs != 0)
valueTimer = g_pHyprlock->addTimer(std::chrono::milliseconds((int)value.updateEveryMs), onTimer, this, value.allowForceUpdate);
else if (value.updateEveryMs == 0 && value.allowForceUpdate)
valueTimer = g_pHyprlock->addTimer(std::chrono::hours(1), onTimer, this, true);
}

CProgressBar::CProgressBar(const Vector2D& viewport_, const std::unordered_map<std::string, std::any>& props, const std::string& output) : viewport(viewport_), outputStringPort(output) {
try {
min = std::any_cast<Hyprlang::INT>(props.at("min"));
max = std::any_cast<Hyprlang::INT>(props.at("max"));
valuePreFormat = std::any_cast<Hyprlang::STRING>(props.at("value"));
borderSize = std::any_cast<Hyprlang::INT>(props.at("border_size"));
borderColor = std::any_cast<Hyprlang::INT>(props.at("border_color"));
rounding = std::any_cast<Hyprlang::INT>(props.at("rounding"));
configSize = CLayoutValueData::fromAnyPv(props.at("size"))->getAbsolute(viewport_);
configPos = CLayoutValueData::fromAnyPv(props.at("position"))->getAbsolute(viewport_);
halign = std::any_cast<Hyprlang::STRING>(props.at("halign"));
valign = std::any_cast<Hyprlang::STRING>(props.at("valign"));
zindex = std::any_cast<Hyprlang::INT>(props.at("zindex"));
color = std::any_cast<Hyprlang::INT>(props.at("color"));

value = formatString(valuePreFormat);

backgroundColor = std::any_cast<Hyprlang::INT>(props.at("background_color"));

} catch (const std::bad_any_cast& e) { RASSERT(false, "Failed to construct CProgressBar: {}", e.what()); } catch (const std::out_of_range& e) {
RASSERT(false, "Missing property for CProgressBar: {}", e.what());
}

pos = posFromHVAlign(viewport, size, configPos, halign, valign);
size = configSize;

g_pAnimationManager->createAnimation(0.f, animatedValue, g_pConfigManager->m_AnimationTree.getConfig("inputFieldWidth"));

updateValue();

plantTimer();
}

void CProgressBar::updateValue() {
int value = getValue();
if (value < min)
value = min;
else if (value > max)
value = max;

*animatedValue = static_cast<float>(value - min) / (max - min);
}

bool CProgressBar::draw(const SRenderData& data) {
CBox box = {pos.x, pos.y, size.x, size.y};
g_pRenderer->renderRect(box, backgroundColor, rounding);

CBox progress = {pos.x, pos.y, size.x * animatedValue->value(), size.y};
g_pRenderer->renderRect(progress, color, rounding);

if (borderSize > 0)
g_pRenderer->renderBorder(box, borderColor, borderSize, rounding, data.opacity);

return false;
}

int CProgressBar::getValue() {
if (value.cmd) {
const auto _value = g_pHyprlock->spawnSync(value.formatted);
if (_value.empty())
return 0;
else
return std::stoi(_value);
}

return std::stoi(value.formatted);
}
44 changes: 44 additions & 0 deletions src/renderer/widgets/ProgressBar.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include "IWidget.hpp"
#include "../../helpers/Color.hpp"
#include "../../helpers/Math.hpp"
#include "../../helpers/AnimatedVariable.hpp"
#include "../../core/Timer.hpp"
#include <string>
#include <unordered_map>
#include <any>

class CProgressBar : public IWidget {
public:
CProgressBar(const Vector2D& viewport, const std::unordered_map<std::string, std::any>& props, const std::string& output);
~CProgressBar();

virtual bool draw(const SRenderData& data);

void onTimerUpdate();
void plantTimer();

private:
void updateValue();
int getValue();

std::string valuePreFormat;
IWidget::SFormatResult value;

std::shared_ptr<CTimer> valueTimer = nullptr;

Vector2D viewport;
Vector2D pos;
Vector2D size;
Vector2D configPos;
Vector2D configSize;

std::string halign, valign;
int min, max, borderSize, rounding, zindex;
CHyprColor borderColor, color, backgroundColor;

std::string outputStringPort;

PHLANIMVAR<float> animatedValue;
};