Fix text box cursor offset with hidden text

This commit is contained in:
Kae 2023-06-28 01:54:37 +10:00
parent cd497bbcf3
commit 326bc7266d
3 changed files with 18 additions and 4 deletions

View File

@ -1,5 +1,6 @@
{ {
"password" : { "password" : {
"hidden" : true "hidden" : true,
"hAnchor" : "right"
} }
} }

View File

@ -385,6 +385,7 @@ int GuiContext::stringWidth(String const& s) {
return textPainter()->stringWidth(s); return textPainter()->stringWidth(s);
} }
//TODO: Make this use StringView
int GuiContext::stringInterfaceWidth(String const& s) { int GuiContext::stringInterfaceWidth(String const& s) {
if (interfaceScale()) { if (interfaceScale()) {
// font size is already adjusted UP by interfaceScale, so we have to adjust // font size is already adjusted UP by interfaceScale, so we have to adjust

View File

@ -102,14 +102,26 @@ int TextBoxWidget::getCursorOffset() { // horizontal only
scale = 0.5; scale = 0.5;
} else if (m_hAnchor == HorizontalAnchor::RightAnchor) { } else if (m_hAnchor == HorizontalAnchor::RightAnchor) {
scale = -1.0; scale = -1.0;
if (m_textHidden) {
int width = context()->stringInterfaceWidth("*");
size_t chars = m_text.size();
return (width * chars) * scale + (width * (chars - m_cursorOffset));
} else {
return context()->stringInterfaceWidth(m_text) * scale return context()->stringInterfaceWidth(m_text) * scale
+ context()->stringInterfaceWidth(m_text.substr(m_cursorOffset, m_text.size())); + context()->stringInterfaceWidth(m_text.substr(m_cursorOffset, m_text.size()));
}
} else { } else {
throw GuiException("Somehow, the value of m_hAnchor became bad"); throw GuiException("Somehow, the value of m_hAnchor became bad");
} }
if (m_textHidden) {
int width = context()->stringInterfaceWidth("*");
size_t chars = m_text.size();
return (int)std::ceil((width * chars) * scale - (width * (chars - m_cursorOffset)));
} else {
return (int)std::ceil(context()->stringInterfaceWidth(m_text) * scale return (int)std::ceil(context()->stringInterfaceWidth(m_text) * scale
- context()->stringInterfaceWidth(m_text.substr(m_cursorOffset, m_text.size()))); - context()->stringInterfaceWidth(m_text.substr(m_cursorOffset, m_text.size())));
}
} }
void TextBoxWidget::update() { void TextBoxWidget::update() {