Fix #8971: Resize QueryStrings with interface scale change. (#10281)

* Fix: Use width of caret symbol '_' for text entry.

This replaces an arbitrary pixel width with the space actually required.

* Fix #8971: Update QueryString sizes with interface scale change.
This commit is contained in:
PeterN 2022-12-25 00:40:55 +00:00 committed by GitHub
parent 3451c0a82c
commit 23eec0b7b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 5 deletions

View File

@ -789,6 +789,11 @@ void QueryString::HandleEditBox(Window *w, int wid)
}
}
static int GetCaretWidth()
{
return GetCharacterWidth(FS_NORMAL, '_');
}
void QueryString::DrawEditBox(const Window *w, int wid) const
{
const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
@ -821,7 +826,7 @@ void QueryString::DrawEditBox(const Window *w, int wid) const
/* We will take the current widget length as maximum width, with a small
* space reserved at the end for the caret to show */
const Textbuf *tb = &this->text;
int delta = std::min(0, (fr.right - fr.left) - tb->pixels - 10);
int delta = std::min(0, (fr.right - fr.left) - tb->pixels - GetCaretWidth());
if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
@ -858,7 +863,7 @@ Point QueryString::GetCaretPosition(const Window *w, int wid) const
/* Clamp caret position to be inside out current width. */
const Textbuf *tb = &this->text;
int delta = std::min(0, (r.right - r.left) - tb->pixels - 10);
int delta = std::min(0, (r.right - r.left) - tb->pixels - GetCaretWidth());
if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
Point pt = {r.left + tb->caretxoffs + delta, r.top};
@ -887,7 +892,7 @@ Rect QueryString::GetBoundingRect(const Window *w, int wid, const char *from, co
/* Clamp caret position to be inside our current width. */
const Textbuf *tb = &this->text;
int delta = std::min(0, r.Width() - tb->pixels - 10);
int delta = std::min(0, r.Width() - tb->pixels - GetCaretWidth());
if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
/* Get location of first and last character. */
@ -920,7 +925,7 @@ const char *QueryString::GetCharAtPosition(const Window *w, int wid, const Point
/* Clamp caret position to be inside our current width. */
const Textbuf *tb = &this->text;
int delta = std::min(0, r.Width() - tb->pixels - 10);
int delta = std::min(0, r.Width() - tb->pixels - GetCaretWidth());
if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
return ::GetCharAtPosition(tb->buf, pt.x - delta - r.left);

View File

@ -354,6 +354,17 @@ QueryString *Window::GetQueryString(uint widnum)
return query != this->querystrings.End() ? query->second : nullptr;
}
/**
* Update size of all QueryStrings of this window.
*/
void Window::UpdateQueryStringSize()
{
for (auto &qs : this->querystrings)
{
qs.second->text.UpdateSize();
}
}
/**
* Get the current input text if an edit box has the focus.
* @return The currently focused input text or nullptr if no input focused.
@ -3353,7 +3364,10 @@ void HideVitalWindows()
void ReInitWindow(Window *w, bool zoom_changed)
{
if (w == nullptr) return;
if (zoom_changed) w->nested_root->AdjustPaddingForZoom();
if (zoom_changed) {
w->nested_root->AdjustPaddingForZoom();
w->UpdateQueryStringSize();
}
w->ReInit();
}

View File

@ -276,6 +276,7 @@ public:
const QueryString *GetQueryString(uint widnum) const;
QueryString *GetQueryString(uint widnum);
void UpdateQueryStringSize();
virtual const char *GetFocusedText() const;
virtual const char *GetCaret() const;