Commit 210b00c5 authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Reserve 1 DIP for the caret in Textfield default/min width calculations.

This matches what the underlying RenderText already does.  It probably
doesn't matter much, since the width itself is "average character
width * n", which isn't going to result in a reliable pixel-precise
value, but it does mean that, on Windows, sizing a Textfield to n
characters (which are in theory based on the width of 'x'), then typing
n 'x's, no longer results in the Textfield scrolling slightly.

Bug: none
Change-Id: Idea1473438037836f8a50e260c5b2ac261830a84
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2421028
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarPeter Boström <pbos@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809942}
parent 90735fc9
...@@ -709,8 +709,7 @@ int Textfield::GetBaseline() const { ...@@ -709,8 +709,7 @@ int Textfield::GetBaseline() const {
gfx::Size Textfield::CalculatePreferredSize() const { gfx::Size Textfield::CalculatePreferredSize() const {
DCHECK_GE(default_width_in_chars_, minimum_width_in_chars_); DCHECK_GE(default_width_in_chars_, minimum_width_in_chars_);
return gfx::Size( return gfx::Size(
GetFontList().GetExpectedTextWidth(default_width_in_chars_) + CharsToDips(default_width_in_chars_),
GetInsets().width(),
LayoutProvider::GetControlHeightForFont(style::CONTEXT_TEXTFIELD, LayoutProvider::GetControlHeightForFont(style::CONTEXT_TEXTFIELD,
GetTextStyle(), GetFontList())); GetTextStyle(), GetFontList()));
} }
...@@ -719,9 +718,7 @@ gfx::Size Textfield::GetMinimumSize() const { ...@@ -719,9 +718,7 @@ gfx::Size Textfield::GetMinimumSize() const {
DCHECK_LE(minimum_width_in_chars_, default_width_in_chars_); DCHECK_LE(minimum_width_in_chars_, default_width_in_chars_);
gfx::Size minimum_size = View::GetMinimumSize(); gfx::Size minimum_size = View::GetMinimumSize();
if (minimum_width_in_chars_ >= 0) if (minimum_width_in_chars_ >= 0)
minimum_size.set_width( minimum_size.set_width(CharsToDips(minimum_width_in_chars_));
GetFontList().GetExpectedTextWidth(minimum_width_in_chars_) +
GetInsets().width());
return minimum_size; return minimum_size;
} }
...@@ -2506,6 +2503,16 @@ bool Textfield::ShouldShowCursor() const { ...@@ -2506,6 +2503,16 @@ bool Textfield::ShouldShowCursor() const {
!drop_cursor_visible_ && GetRenderText()->cursor_enabled(); !drop_cursor_visible_ && GetRenderText()->cursor_enabled();
} }
int Textfield::CharsToDips(int width_in_chars) const {
// Use a subset of the conditions in ShouldShowCursor() that are unlikely to
// change dynamically. Dynamic changes can result in glitchy-looking visual
// effects like find boxes on different tabs being 1 DIP different width.
const int cursor_width =
(!GetReadOnly() && GetRenderText()->cursor_enabled()) ? 1 : 0;
return GetFontList().GetExpectedTextWidth(default_width_in_chars_) +
cursor_width + GetInsets().width();
}
bool Textfield::ShouldBlinkCursor() const { bool Textfield::ShouldBlinkCursor() const {
return ShouldShowCursor() && !Textfield::GetCaretBlinkInterval().is_zero(); return ShouldShowCursor() && !Textfield::GetCaretBlinkInterval().is_zero();
} }
......
...@@ -571,6 +571,10 @@ class VIEWS_EXPORT Textfield : public View, ...@@ -571,6 +571,10 @@ class VIEWS_EXPORT Textfield : public View,
// placed at the point new text will be inserted). // placed at the point new text will be inserted).
bool ShouldShowCursor() const; bool ShouldShowCursor() const;
// Converts a textfield width in "average characters" to the required number
// of DIPs, accounting for insets and cursor.
int CharsToDips(int width_in_chars) const;
// Returns true if an insertion cursor should be visible and blinking. // Returns true if an insertion cursor should be visible and blinking.
bool ShouldBlinkCursor() const; bool ShouldBlinkCursor() const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment