input-field: various fixes (#198)

* input-field: some fixes

* simplify; correctly check passlen(utf8) in draw return

* correctly erase utf-8 on backspace

* render on up events too

* omglul

me dumb

* revert fade speed change
This commit is contained in:
bvr-yr 2024-03-17 03:32:43 +03:00 committed by GitHub
parent 3e38762e84
commit 23224d40e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 88 additions and 79 deletions

View file

@ -773,9 +773,6 @@ void CHyprlock::onKey(uint32_t key, bool down) {
else else
std::erase(m_vPressedKeys, key); std::erase(m_vPressedKeys, key);
if (!down) // we dont care about up events
return;
if (m_sPasswordState.result) { if (m_sPasswordState.result) {
for (auto& o : m_vOutputs) { for (auto& o : m_vOutputs) {
o->sessionLockSurface->render(); o->sessionLockSurface->render();
@ -783,30 +780,36 @@ void CHyprlock::onKey(uint32_t key, bool down) {
return; return;
} }
const auto SYM = xkb_state_key_get_one_sym(m_pXKBState, key + 8); if (down) {
const auto SYM = xkb_state_key_get_one_sym(m_pXKBState, key + 8);
m_bCapsLock = xkb_state_mod_name_is_active(g_pHyprlock->m_pXKBState, XKB_MOD_NAME_CAPS, XKB_STATE_MODS_LOCKED); m_bCapsLock = xkb_state_mod_name_is_active(g_pHyprlock->m_pXKBState, XKB_MOD_NAME_CAPS, XKB_STATE_MODS_LOCKED);
m_bNumLock = xkb_state_mod_name_is_active(g_pHyprlock->m_pXKBState, XKB_MOD_NAME_NUM, XKB_STATE_MODS_LOCKED); m_bNumLock = xkb_state_mod_name_is_active(g_pHyprlock->m_pXKBState, XKB_MOD_NAME_NUM, XKB_STATE_MODS_LOCKED);
if (SYM == XKB_KEY_BackSpace) { if (SYM == XKB_KEY_BackSpace) {
if (m_sPasswordState.passBuffer.length() > 0) if (m_sPasswordState.passBuffer.length() > 0) {
m_sPasswordState.passBuffer = m_sPasswordState.passBuffer.substr(0, m_sPasswordState.passBuffer.length() - 1); // handle utf-8
} else if (SYM == XKB_KEY_Return || SYM == XKB_KEY_KP_Enter) { while ((m_sPasswordState.passBuffer.back() & 0xc0) == 0x80)
Debug::log(LOG, "Authenticating"); m_sPasswordState.passBuffer.pop_back();
m_sPasswordState.result = g_pPassword->verify(m_sPasswordState.passBuffer); m_sPasswordState.passBuffer = m_sPasswordState.passBuffer.substr(0, m_sPasswordState.passBuffer.length() - 1);
} else if (SYM == XKB_KEY_Escape) { }
Debug::log(LOG, "Clearing password buffer"); } else if (SYM == XKB_KEY_Return || SYM == XKB_KEY_KP_Enter) {
Debug::log(LOG, "Authenticating");
m_sPasswordState.result = g_pPassword->verify(m_sPasswordState.passBuffer);
} else if (SYM == XKB_KEY_Escape) {
Debug::log(LOG, "Clearing password buffer");
m_sPasswordState.passBuffer = ""; m_sPasswordState.passBuffer = "";
} else if (SYM == XKB_KEY_Caps_Lock) { } else if (SYM == XKB_KEY_Caps_Lock) {
m_bCapsLock = !m_bCapsLock; m_bCapsLock = !m_bCapsLock;
} else if (SYM == XKB_KEY_Num_Lock) { } else if (SYM == XKB_KEY_Num_Lock) {
m_bNumLock = !m_bNumLock; m_bNumLock = !m_bNumLock;
} else { } else {
char buf[16] = {0}; char buf[16] = {0};
int len = xkb_keysym_to_utf8(SYM, buf, 16); int len = xkb_keysym_to_utf8(SYM, buf, 16);
if (len > 1) if (len > 1)
m_sPasswordState.passBuffer += std::string{buf, len - 1}; m_sPasswordState.passBuffer += std::string{buf, len - 1};
}
} }
for (auto& o : m_vOutputs) { for (auto& o : m_vOutputs) {

View file

@ -89,22 +89,20 @@ void CPasswordInputField::onFadeOutTimer() {
} }
void CPasswordInputField::updateFade() { void CPasswordInputField::updateFade() {
const auto PASSLEN = g_pHyprlock->getPasswordBufferLen();
if (!fadeOnEmpty) { if (!fadeOnEmpty) {
fade.a = 1.0; fade.a = 1.0;
return; return;
} }
if (PASSLEN > 0 && fade.allowFadeOut) if (passwordLength > 0 && fade.allowFadeOut)
fade.allowFadeOut = false; fade.allowFadeOut = false;
if (PASSLEN > 0 && fade.fadeOutTimer.get()) { if (passwordLength > 0 && fade.fadeOutTimer.get()) {
fade.fadeOutTimer->cancel(); fade.fadeOutTimer->cancel();
fade.fadeOutTimer.reset(); fade.fadeOutTimer.reset();
} }
if (PASSLEN == 0 && fade.a != 0.0 && (!fade.animated || fade.appearing)) { if (passwordLength == 0 && fade.a != 0.0 && (!fade.animated || fade.appearing)) {
if (fade.allowFadeOut || fadeTimeoutMs == 0) { if (fade.allowFadeOut || fadeTimeoutMs == 0) {
fade.a = 1.0; fade.a = 1.0;
fade.animated = true; fade.animated = true;
@ -115,7 +113,7 @@ void CPasswordInputField::updateFade() {
fade.fadeOutTimer = g_pHyprlock->addTimer(std::chrono::milliseconds(fadeTimeoutMs), fadeOutCallback, this); fade.fadeOutTimer = g_pHyprlock->addTimer(std::chrono::milliseconds(fadeTimeoutMs), fadeOutCallback, this);
} }
if (PASSLEN > 0 && fade.a != 1.0 && (!fade.animated || !fade.appearing)) { if (passwordLength > 0 && fade.a != 1.0 && (!fade.animated || !fade.appearing)) {
fade.a = 0.0; fade.a = 0.0;
fade.animated = true; fade.animated = true;
fade.appearing = true; fade.appearing = true;
@ -136,13 +134,11 @@ void CPasswordInputField::updateFade() {
} }
void CPasswordInputField::updateDots() { void CPasswordInputField::updateDots() {
const auto PASSLEN = g_pHyprlock->getPasswordBufferDisplayLen(); if (passwordLength == dots.currentAmount)
if (PASSLEN == dots.currentAmount)
return; return;
if (std::abs(PASSLEN - dots.currentAmount) > 1) { if (std::abs(passwordLength - dots.currentAmount) > 1) {
dots.currentAmount = std::clamp(dots.currentAmount, PASSLEN - 1.f, PASSLEN + 1.f); dots.currentAmount = std::clamp(dots.currentAmount, passwordLength - 1.f, passwordLength + 1.f);
dots.lastFrame = std::chrono::system_clock::now(); dots.lastFrame = std::chrono::system_clock::now();
} }
@ -150,14 +146,14 @@ void CPasswordInputField::updateDots() {
const float TOADD = DELTA / 1000000.0 * dots.speedPerSecond; const float TOADD = DELTA / 1000000.0 * dots.speedPerSecond;
if (PASSLEN > dots.currentAmount) { if (passwordLength > dots.currentAmount) {
dots.currentAmount += TOADD; dots.currentAmount += TOADD;
if (dots.currentAmount > PASSLEN) if (dots.currentAmount > passwordLength)
dots.currentAmount = PASSLEN; dots.currentAmount = passwordLength;
} else if (PASSLEN < dots.currentAmount) { } else if (passwordLength < dots.currentAmount) {
dots.currentAmount -= TOADD; dots.currentAmount -= TOADD;
if (dots.currentAmount < PASSLEN) if (dots.currentAmount < passwordLength)
dots.currentAmount = PASSLEN; dots.currentAmount = passwordLength;
} }
dots.lastFrame = std::chrono::system_clock::now(); dots.lastFrame = std::chrono::system_clock::now();
@ -175,6 +171,9 @@ bool CPasswordInputField::draw(const SRenderData& data) {
bool forceReload = false; bool forceReload = false;
passwordLength = g_pHyprlock->getPasswordBufferDisplayLen();
checkWaiting = g_pHyprlock->passwordCheckWaiting();
updateFade(); updateFade();
updateDots(); updateDots();
updateFailTex(); updateFailTex();
@ -209,7 +208,7 @@ bool CPasswordInputField::draw(const SRenderData& data) {
shadowData.opacity *= fade.a; shadowData.opacity *= fade.a;
shadow.draw(shadowData); shadow.draw(shadowData);
float passAlpha = g_pHyprlock->passwordCheckWaiting() ? 0.5 : 1.0; float passAlpha = checkWaiting ? 0.5 : 1.0;
CColor outerCol = outerColor.main; CColor outerCol = outerColor.main;
outerCol.a *= fade.a * data.opacity; outerCol.a *= fade.a * data.opacity;
@ -218,38 +217,39 @@ bool CPasswordInputField::draw(const SRenderData& data) {
CColor fontCol = font; CColor fontCol = font;
fontCol.a *= fade.a * data.opacity * passAlpha; fontCol.a *= fade.a * data.opacity * passAlpha;
g_pRenderer->renderRect(outerBox, outerCol, rounding == -1 ? outerBox.h / 2.0 : rounding); if (outThick > 0) {
g_pRenderer->renderRect(outerBox, outerCol, rounding == -1 ? outerBox.h / 2.0 : rounding);
const auto PASSLEN = g_pHyprlock->getPasswordBufferLen(); if (passwordLength != 0 && hiddenInputState.enabled && !fade.animated && data.opacity == 1.0) {
CBox outerBoxScaled = outerBox;
if (PASSLEN != 0 && hiddenInputState.enabled) { Vector2D p = outerBox.pos();
CBox outerBoxScaled = outerBox; outerBoxScaled.translate(-p).scale(0.5).translate(p);
Vector2D p = outerBox.pos(); if (hiddenInputState.lastQuadrant > 1)
outerBoxScaled.translate(-p).scale(0.5).translate(p); outerBoxScaled.y += outerBoxScaled.h;
if (hiddenInputState.lastQuadrant > 1) if (hiddenInputState.lastQuadrant % 2 == 1)
outerBoxScaled.y += outerBoxScaled.h; outerBoxScaled.x += outerBoxScaled.w;
if (hiddenInputState.lastQuadrant % 2 == 1) glEnable(GL_SCISSOR_TEST);
outerBoxScaled.x += outerBoxScaled.w; glScissor(outerBoxScaled.x, outerBoxScaled.y, outerBoxScaled.w, outerBoxScaled.h);
glEnable(GL_SCISSOR_TEST); g_pRenderer->renderRect(outerBox, hiddenInputState.lastColor, rounding == -1 ? outerBox.h / 2.0 : rounding);
glScissor(outerBoxScaled.x, outerBoxScaled.y, outerBoxScaled.w, outerBoxScaled.h); glScissor(0, 0, viewport.x, viewport.y);
g_pRenderer->renderRect(outerBox, hiddenInputState.lastColor, rounding == -1 ? outerBox.h / 2.0 : rounding); glDisable(GL_SCISSOR_TEST);
glScissor(0, 0, viewport.x, viewport.y); }
glDisable(GL_SCISSOR_TEST);
} }
g_pRenderer->renderRect(inputFieldBox, innerCol, rounding == -1 ? inputFieldBox.h / 2.0 : rounding - outThick); g_pRenderer->renderRect(inputFieldBox, innerCol, rounding == -1 ? inputFieldBox.h / 2.0 : rounding - outThick);
const int PASS_SIZE = std::nearbyint(inputFieldBox.h * dots.size * 0.5f) * 2.f; if (!hiddenInputState.enabled && !g_pHyprlock->m_bFadeStarted) {
const int PASS_SPACING = std::floor(PASS_SIZE * dots.spacing); const int PASS_SIZE = std::nearbyint(inputFieldBox.h * dots.size * 0.5f) * 2.f;
const int DOT_PAD = (inputFieldBox.h - PASS_SIZE) / 2; const int PASS_SPACING = std::floor(PASS_SIZE * dots.spacing);
const int DOT_AREA_WIDTH = inputFieldBox.w - DOT_PAD * 2; // avail width for dots const int DOT_PAD = (inputFieldBox.h - PASS_SIZE) / 2;
const int MAX_DOTS = std::round(DOT_AREA_WIDTH * 1.0 / (PASS_SIZE + PASS_SPACING)); // max amount of dots that can fit in the area const int DOT_AREA_WIDTH = inputFieldBox.w - DOT_PAD * 2; // avail width for dots
const int DOT_FLOORED = std::floor(dots.currentAmount); const int MAX_DOTS = std::round(DOT_AREA_WIDTH * 1.0 / (PASS_SIZE + PASS_SPACING)); // max amount of dots that can fit in the area
const float DOT_ALPHA = fontCol.a; const int DOT_FLOORED = std::floor(dots.currentAmount);
// Calculate the total width required for all dots including spaces between them const float DOT_ALPHA = fontCol.a;
const int TOTAL_DOTS_WIDTH = (PASS_SIZE + PASS_SPACING) * dots.currentAmount - PASS_SPACING;
// Calculate the total width required for all dots including spaces between them
const int TOTAL_DOTS_WIDTH = (PASS_SIZE + PASS_SPACING) * dots.currentAmount - PASS_SPACING;
if (!hiddenInputState.enabled) {
// Calculate starting x-position to ensure dots stay centered within the input field // Calculate starting x-position to ensure dots stay centered within the input field
int xstart = dots.center ? (DOT_AREA_WIDTH - TOTAL_DOTS_WIDTH) / 2 + DOT_PAD : DOT_PAD; int xstart = dots.center ? (DOT_AREA_WIDTH - TOTAL_DOTS_WIDTH) / 2 + DOT_PAD : DOT_PAD;
@ -280,7 +280,7 @@ bool CPasswordInputField::draw(const SRenderData& data) {
} }
} }
if (PASSLEN == 0 && !placeholder.resourceID.empty()) { if (passwordLength == 0 && !placeholder.resourceID.empty()) {
SPreloadedAsset* currAsset = nullptr; SPreloadedAsset* currAsset = nullptr;
if (!placeholder.failID.empty()) { if (!placeholder.failID.empty()) {
@ -304,18 +304,16 @@ bool CPasswordInputField::draw(const SRenderData& data) {
forceReload = true; forceReload = true;
} }
return dots.currentAmount != PASSLEN || fade.animated || outerColor.animated || redrawShadow || data.opacity < 1.0 || forceReload; return dots.currentAmount != passwordLength || fade.animated || outerColor.animated || redrawShadow || data.opacity < 1.0 || forceReload;
} }
void CPasswordInputField::updateFailTex() { void CPasswordInputField::updateFailTex() {
const auto FAIL = g_pHyprlock->passwordLastFailReason(); const auto FAIL = g_pHyprlock->passwordLastFailReason();
const auto WAITING = g_pHyprlock->passwordCheckWaiting();
const auto PASSLEN = g_pHyprlock->getPasswordBufferLen();
if (WAITING) if (checkWaiting)
placeholder.canGetNewFail = true; placeholder.canGetNewFail = true;
if (PASSLEN != 0 || (WAITING && PASSLEN == 0)) { if (passwordLength != 0 || (checkWaiting && passwordLength == 0)) {
if (placeholder.failAsset) { if (placeholder.failAsset) {
g_pRenderer->asyncResourceGatherer->unloadAsset(placeholder.failAsset); g_pRenderer->asyncResourceGatherer->unloadAsset(placeholder.failAsset);
placeholder.failAsset = nullptr; placeholder.failAsset = nullptr;
@ -347,11 +345,11 @@ void CPasswordInputField::updateFailTex() {
} }
void CPasswordInputField::updateHiddenInputState() { void CPasswordInputField::updateHiddenInputState() {
if (!hiddenInputState.enabled || (size_t)hiddenInputState.lastPasswordLength == g_pHyprlock->getPasswordBufferDisplayLen()) if (!hiddenInputState.enabled || (size_t)hiddenInputState.lastPasswordLength == passwordLength)
return; return;
// randomize new thang // randomize new thang
hiddenInputState.lastPasswordLength = g_pHyprlock->getPasswordBufferDisplayLen(); hiddenInputState.lastPasswordLength = passwordLength;
srand(std::chrono::system_clock::now().time_since_epoch().count()); srand(std::chrono::system_clock::now().time_since_epoch().count());
float r1 = (rand() % 100) / 255.0; float r1 = (rand() % 100) / 255.0;
@ -381,7 +379,7 @@ void CPasswordInputField::updateOuter() {
static auto TIMER = std::chrono::system_clock::now(); static auto TIMER = std::chrono::system_clock::now();
if (outerColor.animated) { if (outerColor.animated) {
if (outerColor.stateNum != outerColor.invertNum ? !g_pHyprlock->m_bNumLock : g_pHyprlock->m_bNumLock || outerColor.stateCaps != g_pHyprlock->m_bCapsLock) if (outerColor.stateNum != (outerColor.invertNum ? !g_pHyprlock->m_bNumLock : g_pHyprlock->m_bNumLock) || outerColor.stateCaps != g_pHyprlock->m_bCapsLock)
SOURCE = outerColor.main; SOURCE = outerColor.main;
} else } else
SOURCE = outerColor.main; SOURCE = outerColor.main;
@ -391,7 +389,12 @@ void CPasswordInputField::updateOuter() {
outerColor.stateCaps = g_pHyprlock->m_bCapsLock; outerColor.stateCaps = g_pHyprlock->m_bCapsLock;
if (placeholder.failID.empty()) { if (placeholder.failID.empty()) {
if (g_pHyprlock->passwordCheckWaiting()) if (g_pHyprlock->m_bFadeStarted) {
if (TARGET == outerColor.check)
SOURCE = outerColor.main;
outerColor.transitionMs = 100;
TARGET = OUTER;
} else if (checkWaiting)
TARGET = outerColor.check; TARGET = outerColor.check;
else if (outerColor.both != OUTER && outerColor.stateCaps && outerColor.stateNum) else if (outerColor.both != OUTER && outerColor.stateCaps && outerColor.stateNum)
TARGET = outerColor.both; TARGET = outerColor.both;

View file

@ -28,6 +28,9 @@ class CPasswordInputField : public IWidget {
bool firstRender = true; bool firstRender = true;
bool redrawShadow = false; bool redrawShadow = false;
bool checkWaiting = false;
size_t passwordLength = 0;
Vector2D size; Vector2D size;
Vector2D pos; Vector2D pos;