Match text caret size with Entry widget

This commit is contained in:
David Capello 2025-10-07 11:08:41 -03:00
parent 0dc4bc9aa7
commit c781d82cf6
6 changed files with 26 additions and 15 deletions

View File

@ -1296,12 +1296,22 @@ int SkinTheme::getScrollbarSize()
return dimensions.scrollbarSize(); return dimensions.scrollbarSize();
} }
gfx::Size SkinTheme::getEntryCaretSize(Widget* widget) gfx::Size SkinTheme::getCaretSize(Widget* widget)
{ {
int caretHeight;
if (widget->type() == kTextEditWidget) {
// We cannot use the height of the widget text, because it
// includes the line height of every single line in the widget.
caretHeight = widget->font()->lineHeight();
}
else {
caretHeight = widget->textHeight();
}
if (widget->font()->type() == text::FontType::FreeType) if (widget->font()->type() == text::FontType::FreeType)
return gfx::Size(2 * guiscale(), widget->textHeight()); return gfx::Size(2 * guiscale(), caretHeight);
else else
return gfx::Size(2 * guiscale(), widget->textHeight() + 2 * guiscale()); return gfx::Size(2 * guiscale(), caretHeight + 2 * guiscale());
} }
void SkinTheme::paintEntry(PaintEvent& ev) void SkinTheme::paintEntry(PaintEvent& ev)
@ -1866,7 +1876,7 @@ void SkinTheme::drawEntryCaret(ui::Graphics* g, Entry* widget, int x, int y)
{ {
gfx::Color color = colors.text(); gfx::Color color = colors.text();
int textHeight = widget->textHeight(); int textHeight = widget->textHeight();
gfx::Size caretSize = getEntryCaretSize(widget); gfx::Size caretSize = getCaretSize(widget);
for (int u = x; u < x + caretSize.w; ++u) for (int u = x; u < x + caretSize.w; ++u)
g->drawVLine(color, u, y + textHeight / 2 - caretSize.h / 2, caretSize.h); g->drawVLine(color, u, y + textHeight / 2 - caretSize.h / 2, caretSize.h);

View File

@ -80,7 +80,7 @@ public:
void initWidget(ui::Widget* widget) override; void initWidget(ui::Widget* widget) override;
void getWindowMask(ui::Widget* widget, gfx::Region& region) override; void getWindowMask(ui::Widget* widget, gfx::Region& region) override;
int getScrollbarSize() override; int getScrollbarSize() override;
gfx::Size getEntryCaretSize(ui::Widget* widget) override; gfx::Size getCaretSize(ui::Widget* widget) override;
void paintEntry(ui::PaintEvent& ev) override; void paintEntry(ui::PaintEvent& ev) override;
void paintListBox(ui::PaintEvent& ev) override; void paintListBox(ui::PaintEvent& ev) override;

View File

@ -139,7 +139,7 @@ gfx::Point Entry::caretPosOnScreen() const
void Entry::setCaretPos(const int pos) void Entry::setCaretPos(const int pos)
{ {
gfx::Size caretSize = theme()->getEntryCaretSize(this); gfx::Size caretSize = theme()->getCaretSize(this);
int textlen = lastCaretPos(); int textlen = lastCaretPos();
m_caret = std::clamp(pos, 0, textlen); m_caret = std::clamp(pos, 0, textlen);
m_scroll = std::clamp(m_scroll, 0, textlen); m_scroll = std::clamp(m_scroll, 0, textlen);
@ -521,7 +521,7 @@ gfx::Size Entry::sizeHintWithText(Entry* entry, const std::string& text)
{ {
const auto& font = entry->font(); const auto& font = entry->font();
int w = font->textLength(text) + +2 * entry->theme()->getEntryCaretSize(entry).w + int w = font->textLength(text) + +2 * entry->theme()->getCaretSize(entry).w +
entry->border().width(); entry->border().width();
w = std::min(w, guiscale() * kMaxWidthHintForEntry); w = std::min(w, guiscale() * kMaxWidthHintForEntry);
@ -546,7 +546,7 @@ void Entry::onSizeHint(SizeHintEvent& ev)
const auto& font = this->font(); const auto& font = this->font();
int trailing = font->textLength(getSuffix()); int trailing = font->textLength(getSuffix());
trailing = std::max(trailing, 2 * theme()->getEntryCaretSize(this).w); trailing = std::max(trailing, 2 * theme()->getCaretSize(this).w);
int w = font->textLength("w") * std::min(m_maxsize, 6) + +trailing + border().width(); int w = font->textLength("w") * std::min(m_maxsize, 6) + +trailing + border().width();
@ -1012,7 +1012,7 @@ void Entry::recalcCharBoxes(const std::string& text)
box.codepoint = 0; box.codepoint = 0;
box.from = box.to = lastTextIndex; box.from = box.to = lastTextIndex;
box.x = lastX; box.x = lastX;
box.width = theme()->getEntryCaretSize(this).w; box.width = theme()->getCaretSize(this).w;
m_boxes.push_back(box); m_boxes.push_back(box);
} }

View File

@ -141,7 +141,7 @@ void IntEntry::onSizeHint(SizeHintEvent& ev)
{ {
const text::FontRef& font = this->font(); const text::FontRef& font = this->font();
int trailing = font->textLength(getSuffix()); int trailing = font->textLength(getSuffix());
trailing = std::max(trailing, 2 * theme()->getEntryCaretSize(this).w); trailing = std::max(trailing, 2 * theme()->getCaretSize(this).w);
int min_w = font->textLength(m_slider->convertValueToText(m_min)); int min_w = font->textLength(m_slider->convertValueToText(m_min));
int max_w = font->textLength(m_slider->convertValueToText(m_max)) + trailing; int max_w = font->textLength(m_slider->convertValueToText(m_max)) + trailing;

View File

@ -364,8 +364,8 @@ void TextEdit::onPaint(PaintEvent& ev)
gfx::PointF point(border().left(), border().top()); gfx::PointF point(border().left(), border().top());
point -= scroll; point -= scroll;
m_caretRect = m_caretRect = gfx::Rect(gfx::Point(border().left() - scroll.x, border().top() - scroll.y),
gfx::Rect(border().left() - scroll.x, border().top() - scroll.y, 2, font()->lineHeight()); theme()->getCaretSize(this));
const gfx::Rect clipBounds = g->getClipBounds(); const gfx::Rect clipBounds = g->getClipBounds();
@ -403,8 +403,9 @@ void TextEdit::onPaint(PaintEvent& ev)
m_caretRect.x += line.getBounds(m_caret.pos()).x; m_caretRect.x += line.getBounds(m_caret.pos()).x;
} }
m_caretRect.y = point.y; // Ensure the caret height corresponds with the tallest glyph
m_caretRect.h = line.height; // Ensure the caret height corresponds with the tallest glyph m_caretRect.h = std::max(m_caretRect.h, line.height);
m_caretRect.y = point.y + line.height / 2 - m_caretRect.h / 2;
} }
point.y += line.height; point.y += line.height;

View File

@ -74,7 +74,7 @@ public:
virtual void getWindowMask(Widget* widget, gfx::Region& region) {} virtual void getWindowMask(Widget* widget, gfx::Region& region) {}
virtual void setDecorativeWidgetBounds(Widget* widget); virtual void setDecorativeWidgetBounds(Widget* widget);
virtual int getScrollbarSize() { return kDefaultFontHeight; } virtual int getScrollbarSize() { return kDefaultFontHeight; }
virtual gfx::Size getEntryCaretSize(Widget* widget) { return gfx::Size(kDefaultFontHeight, 1); } virtual gfx::Size getCaretSize(Widget* widget) { return gfx::Size(kDefaultFontHeight, 1); }
virtual void paintEntry(PaintEvent& ev) {} virtual void paintEntry(PaintEvent& ev) {}
virtual void paintTextEdit(PaintEvent& ev) {} virtual void paintTextEdit(PaintEvent& ev) {}