From 766d47030890fa8f662ff7f72390b0c032bb0a74 Mon Sep 17 00:00:00 2001 From: Maximilian Seidler <78690852+PaideiaDilemma@users.noreply.github.com> Date: Sun, 10 Mar 2024 15:33:01 +0100 Subject: [PATCH] input-field: display utf-8 codepoint length (#164) --- src/core/hyprlock.cpp | 5 +++++ src/core/hyprlock.hpp | 1 + src/renderer/widgets/PasswordInputField.cpp | 6 +++--- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/hyprlock.cpp b/src/core/hyprlock.cpp index aedd86a..6b8ceb2 100644 --- a/src/core/hyprlock.cpp +++ b/src/core/hyprlock.cpp @@ -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; } diff --git a/src/core/hyprlock.hpp b/src/core/hyprlock.hpp index 06d9e08..9dc5425 100644 --- a/src/core/hyprlock.hpp +++ b/src/core/hyprlock.hpp @@ -54,6 +54,7 @@ class CHyprlock { std::optional passwordLastFailReason(); size_t getPasswordBufferLen(); + size_t getPasswordBufferDisplayLen(); size_t getPasswordFailedAttempts(); ext_session_lock_manager_v1* getSessionLockMgr(); diff --git a/src/renderer/widgets/PasswordInputField.cpp b/src/renderer/widgets/PasswordInputField.cpp index 0ccffc0..995e4ae 100644 --- a/src/renderer/widgets/PasswordInputField.cpp +++ b/src/renderer/widgets/PasswordInputField.cpp @@ -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;