Skip to content

Commit

Permalink
input-field: display utf-8 codepoint length
Browse files Browse the repository at this point in the history
  • Loading branch information
PaideiaDilemma committed Mar 10, 2024
1 parent 160fe35 commit b10018c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/core/hyprlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/core/hyprlock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/widgets/PasswordInputField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b10018c

Please sign in to comment.