Automatically scroll textboxes when the caret ends up outside. Fixes #10026 .

There was some code for this before but it didn't work.
This commit is contained in:
Henrik Rydgård 2017-10-24 23:05:21 +02:00
parent d1d1e1f742
commit 7b1e1cd5e0
2 changed files with 9 additions and 7 deletions

View File

@ -783,7 +783,7 @@ void TextEdit::Draw(UIContext &dc) {
float w, h;
Bounds textBounds = bounds_;
textBounds.x = textX;
textBounds.x = textX - scrollPos_;
if (text_.empty()) {
if (placeholderText_.size()) {
@ -797,13 +797,14 @@ void TextEdit::Draw(UIContext &dc) {
if (HasFocus()) {
// Hack to find the caret position. Might want to find a better way...
dc.MeasureTextCount(dc.theme->uiFont, 1.0f, 1.0f, text_.c_str(), caret_, &w, &h, ALIGN_VCENTER | ALIGN_LEFT);
float caretX = w;
caretX += textX;
float caretX = w - scrollPos_;
if (caretX > bounds_.w) {
// Scroll text to the left if the caret won't fit. Not ideal but looks better than not scrolling it.
textX -= caretX - bounds_.w;
scrollPos_ += caretX - bounds_.w;
}
if (caretX < 0) {
scrollPos_ += caretX;
}
caretX += textX;
dc.FillRect(UI::Drawable(textColor), Bounds(caretX - 1, bounds_.y + 2, 3, bounds_.h - 4));
}
dc.PopScissor();

View File

@ -780,7 +780,7 @@ private:
class TextEdit : public View {
public:
TextEdit(const std::string &text, const std::string &placeholderText, LayoutParams *layoutParams = 0);
void SetText(const std::string &text) { text_ = text; caret_ = (int)text_.size(); }
void SetText(const std::string &text) { text_ = text; scrollPos_ = 0; caret_ = (int)text_.size(); }
void SetTextColor(uint32_t color) { textColor_ = color; hasTextColor_ = true; }
const std::string &GetText() const { return text_; }
void SetMaxLen(size_t maxLen) { maxLen_ = maxLen; }
@ -802,6 +802,7 @@ private:
uint32_t textColor_;
bool hasTextColor_ = false;
int caret_;
int scrollPos_ = 0;
size_t maxLen_;
bool ctrlDown_ = false; // TODO: Make some global mechanism for this.
// TODO: Selections