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(); 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() { size_t CHyprlock::getPasswordFailedAttempts() {
return m_sPasswordState.failedAttempts; return m_sPasswordState.failedAttempts;
} }

View file

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

View file

@ -121,7 +121,7 @@ void CPasswordInputField::updateFade() {
} }
void CPasswordInputField::updateDots() { void CPasswordInputField::updateDots() {
const auto PASSLEN = g_pHyprlock->getPasswordBufferLen(); const auto PASSLEN = g_pHyprlock->getPasswordBufferDisplayLen();
if (PASSLEN == dots.currentAmount) if (PASSLEN == dots.currentAmount)
return; return;
@ -320,11 +320,11 @@ void CPasswordInputField::updateFailTex() {
} }
void CPasswordInputField::updateHiddenInputState() { void CPasswordInputField::updateHiddenInputState() {
if (!hiddenInputState.enabled || (size_t)hiddenInputState.lastPasswordLength == g_pHyprlock->getPasswordBufferLen()) if (!hiddenInputState.enabled || (size_t)hiddenInputState.lastPasswordLength == g_pHyprlock->getPasswordBufferDisplayLen())
return; return;
// randomize new thang // randomize new thang
hiddenInputState.lastPasswordLength = g_pHyprlock->getPasswordBufferLen(); hiddenInputState.lastPasswordLength = g_pHyprlock->getPasswordBufferDisplayLen();
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;