From 6ff808cebf4db80f69b786637c3a3b21e0dc0cdd Mon Sep 17 00:00:00 2001 From: Vaxry Date: Tue, 20 Feb 2024 14:42:04 +0000 Subject: [PATCH] input-field: add hide_input --- src/config/ConfigManager.cpp | 2 + src/renderer/widgets/PasswordInputField.cpp | 89 ++++++++++++++++----- src/renderer/widgets/PasswordInputField.hpp | 9 +++ 3 files changed, 79 insertions(+), 21 deletions(-) diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 7be3c8b..ac7c459 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -39,6 +39,7 @@ void CConfigManager::init() { m_config.addSpecialConfigValue("input-field", "valign", Hyprlang::STRING{"center"}); m_config.addSpecialConfigValue("input-field", "position", Hyprlang::VEC2{0, -20}); m_config.addSpecialConfigValue("input-field", "placeholder_text", Hyprlang::STRING{"Input Password"}); + m_config.addSpecialConfigValue("input-field", "hide_input", Hyprlang::INT{0}); m_config.addSpecialCategory("label", Hyprlang::SSpecialCategoryOptions{.key = nullptr, .anonymousKeyBased = true}); m_config.addSpecialConfigValue("label", "monitor", Hyprlang::STRING{""}); @@ -100,6 +101,7 @@ std::vector CConfigManager::getWidgetConfigs() { {"valign", m_config.getSpecialConfigValue("input-field", "valign", k.c_str())}, {"position", m_config.getSpecialConfigValue("input-field", "position", k.c_str())}, {"placeholder_text", m_config.getSpecialConfigValue("input-field", "placeholder_text", k.c_str())}, + {"hide_input", m_config.getSpecialConfigValue("input-field", "hide_input", k.c_str())}, } }); // clang-format on diff --git a/src/renderer/widgets/PasswordInputField.cpp b/src/renderer/widgets/PasswordInputField.cpp index 5214c89..30fcafe 100644 --- a/src/renderer/widgets/PasswordInputField.cpp +++ b/src/renderer/widgets/PasswordInputField.cpp @@ -3,14 +3,16 @@ #include "../../core/hyprlock.hpp" #include -CPasswordInputField::CPasswordInputField(const Vector2D& viewport, const std::unordered_map& props) { - size = std::any_cast(props.at("size")); - inner = std::any_cast(props.at("inner_color")); - outer = std::any_cast(props.at("outer_color")); - out_thick = std::any_cast(props.at("outline_thickness")); - fadeOnEmpty = std::any_cast(props.at("fade_on_empty")); - font = std::any_cast(props.at("font_color")); - pos = std::any_cast(props.at("position")); +CPasswordInputField::CPasswordInputField(const Vector2D& viewport_, const std::unordered_map& props) { + size = std::any_cast(props.at("size")); + inner = std::any_cast(props.at("inner_color")); + outer = std::any_cast(props.at("outer_color")); + out_thick = std::any_cast(props.at("outline_thickness")); + fadeOnEmpty = std::any_cast(props.at("fade_on_empty")); + font = std::any_cast(props.at("font_color")); + pos = std::any_cast(props.at("position")); + hiddenInputState.enabled = std::any_cast(props.at("hide_input")); + viewport = viewport_; pos = posFromHVAlign(viewport, size, pos, std::any_cast(props.at("halign")), std::any_cast(props.at("valign"))); @@ -96,6 +98,7 @@ bool CPasswordInputField::draw(const SRenderData& data) { updateFade(); updateDots(); updateFailTex(); + updateHiddenInputState(); float passAlpha = g_pHyprlock->passwordCheckWaiting() ? 0.5 : 1.0; @@ -107,27 +110,45 @@ bool CPasswordInputField::draw(const SRenderData& data) { fontCol.a *= fade.a * data.opacity * passAlpha; g_pRenderer->renderRect(outerBox, outerCol, outerBox.h / 2.0); + + const auto PASSLEN = g_pHyprlock->getPasswordBufferLen(); + + if (PASSLEN != 0 && hiddenInputState.enabled) { + CBox outerBoxScaled = outerBox; + Vector2D p = outerBox.pos(); + outerBoxScaled.translate(-p).scale(0.5).translate(p); + if (hiddenInputState.lastQuadrant > 1) + outerBoxScaled.y += outerBoxScaled.h; + if (hiddenInputState.lastQuadrant % 2 == 1) + outerBoxScaled.x += outerBoxScaled.w; + glEnable(GL_SCISSOR_TEST); + glScissor(outerBoxScaled.x, outerBoxScaled.y, outerBoxScaled.w, outerBoxScaled.h); + g_pRenderer->renderRect(outerBox, hiddenInputState.lastColor, outerBox.h / 2.0); + glScissor(0, 0, viewport.x, viewport.y); + glDisable(GL_SCISSOR_TEST); + } + g_pRenderer->renderRect(inputFieldBox, innerCol, inputFieldBox.h / 2.0); constexpr int PASS_SPACING = 3; constexpr int PASS_SIZE = 8; - for (size_t i = 0; i < std::floor(dots.currentAmount); ++i) { - Vector2D currentPos = inputFieldBox.pos() + Vector2D{PASS_SPACING * 4, inputFieldBox.h / 2.f - PASS_SIZE / 2.f} + Vector2D{(PASS_SIZE + PASS_SPACING) * i, 0}; - CBox box{currentPos, Vector2D{PASS_SIZE, PASS_SIZE}}; - g_pRenderer->renderRect(box, fontCol, PASS_SIZE / 2.0); - } + if (!hiddenInputState.enabled) { + for (size_t i = 0; i < std::floor(dots.currentAmount); ++i) { + Vector2D currentPos = inputFieldBox.pos() + Vector2D{PASS_SPACING * 4, inputFieldBox.h / 2.f - PASS_SIZE / 2.f} + Vector2D{(PASS_SIZE + PASS_SPACING) * i, 0}; + CBox box{currentPos, Vector2D{PASS_SIZE, PASS_SIZE}}; + g_pRenderer->renderRect(box, fontCol, PASS_SIZE / 2.0); + } - if (dots.currentAmount != std::floor(dots.currentAmount)) { - Vector2D currentPos = - inputFieldBox.pos() + Vector2D{PASS_SPACING * 4, inputFieldBox.h / 2.f - PASS_SIZE / 2.f} + Vector2D{(PASS_SIZE + PASS_SPACING) * std::floor(dots.currentAmount), 0}; - CBox box{currentPos, Vector2D{PASS_SIZE, PASS_SIZE}}; - fontCol.a = (dots.currentAmount - std::floor(dots.currentAmount)) * data.opacity; - g_pRenderer->renderRect(box, fontCol, PASS_SIZE / 2.0); + if (dots.currentAmount != std::floor(dots.currentAmount)) { + Vector2D currentPos = inputFieldBox.pos() + Vector2D{PASS_SPACING * 4, inputFieldBox.h / 2.f - PASS_SIZE / 2.f} + + Vector2D{(PASS_SIZE + PASS_SPACING) * std::floor(dots.currentAmount), 0}; + CBox box{currentPos, Vector2D{PASS_SIZE, PASS_SIZE}}; + fontCol.a = (dots.currentAmount - std::floor(dots.currentAmount)) * data.opacity; + g_pRenderer->renderRect(box, fontCol, PASS_SIZE / 2.0); + } } - const auto PASSLEN = g_pHyprlock->getPasswordBufferLen(); - if (PASSLEN == 0 && !placeholder.resourceID.empty()) { SPreloadedAsset* currAsset = nullptr; @@ -186,3 +207,29 @@ void CPasswordInputField::updateFailTex() { placeholder.canGetNewFail = false; } + +void CPasswordInputField::updateHiddenInputState() { + if (!hiddenInputState.enabled || (size_t)hiddenInputState.lastPasswordLength == g_pHyprlock->getPasswordBufferLen()) + return; + + // randomize new thang + hiddenInputState.lastPasswordLength = g_pHyprlock->getPasswordBufferLen(); + + float r1 = (rand() % 100) / 255.0; + float r2 = (rand() % 100) / 255.0; + int r3 = rand() % 3; + int r4 = rand() % 2; + int r5 = rand() % 2; + + ((float*)&hiddenInputState.lastColor.r)[r3] = r1 + 155 / 255.0; + ((float*)&hiddenInputState.lastColor.r)[(r3 + r4) % 3] = r2 + 155 / 255.0; + + for (int i = 0; i < 3; ++i) { + if (i != r3 && i != ((r3 + r4) % 3)) { + ((float*)&hiddenInputState.lastColor.r)[i] = 1.0 - ((float*)&hiddenInputState.lastColor.r)[r5 ? r3 : ((r3 + r4) % 3)]; + } + } + + hiddenInputState.lastColor.a = 1.0; + hiddenInputState.lastQuadrant = (hiddenInputState.lastQuadrant + rand() % 3 + 1) % 4; +} diff --git a/src/renderer/widgets/PasswordInputField.hpp b/src/renderer/widgets/PasswordInputField.hpp index 2fa51ee..a471167 100644 --- a/src/renderer/widgets/PasswordInputField.hpp +++ b/src/renderer/widgets/PasswordInputField.hpp @@ -20,9 +20,11 @@ class CPasswordInputField : public IWidget { void updateDots(); void updateFade(); void updateFailTex(); + void updateHiddenInputState(); Vector2D size; Vector2D pos; + Vector2D viewport; int out_thick; @@ -50,5 +52,12 @@ class CPasswordInputField : public IWidget { bool canGetNewFail = true; } placeholder; + struct { + CColor lastColor; + int lastQuadrant = 0; + int lastPasswordLength = 0; + bool enabled = false; + } hiddenInputState; + bool fadeOnEmpty; }; \ No newline at end of file