input-field: display utf-8 codepoint length (#164)

This commit is contained in:
Maximilian Seidler 2024-03-10 15:33:01 +01:00 committed by GitHub
parent 160fe3553f
commit 766d470308
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 3 deletions

View File

@ -825,6 +825,11 @@ size_t CHyprlock::getPasswordBufferLen() {
return m_sPasswordState.passBuffer.length();
}
size_t CHyprlock::getPasswordBufferDisplayLen() {
// Counts utf-8 codepoints in the buffer. A byte is counted if it does not match 0b10xxxxxx.
return std::count_if(m_sPasswordState.passBuffer.begin(), m_sPasswordState.passBuffer.end(), [](char c) { return (c & 0xc0) != 0x80; });
}
size_t CHyprlock::getPasswordFailedAttempts() {
return m_sPasswordState.failedAttempts;
}

View File

@ -54,6 +54,7 @@ class CHyprlock {
std::optional<std::string> passwordLastFailReason();
size_t getPasswordBufferLen();
size_t getPasswordBufferDisplayLen();
size_t getPasswordFailedAttempts();
ext_session_lock_manager_v1* getSessionLockMgr();

View File

@ -121,7 +121,7 @@ void CPasswordInputField::updateFade() {
}
void CPasswordInputField::updateDots() {
const auto PASSLEN = g_pHyprlock->getPasswordBufferLen();
const auto PASSLEN = g_pHyprlock->getPasswordBufferDisplayLen();
if (PASSLEN == dots.currentAmount)
return;
@ -320,11 +320,11 @@ void CPasswordInputField::updateFailTex() {
}
void CPasswordInputField::updateHiddenInputState() {
if (!hiddenInputState.enabled || (size_t)hiddenInputState.lastPasswordLength == g_pHyprlock->getPasswordBufferLen())
if (!hiddenInputState.enabled || (size_t)hiddenInputState.lastPasswordLength == g_pHyprlock->getPasswordBufferDisplayLen())
return;
// randomize new thang
hiddenInputState.lastPasswordLength = g_pHyprlock->getPasswordBufferLen();
hiddenInputState.lastPasswordLength = g_pHyprlock->getPasswordBufferDisplayLen();
srand(std::chrono::system_clock::now().time_since_epoch().count());
float r1 = (rand() % 100) / 255.0;