Permalink
Browse files

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

…10026 .

There was some code for this before but it didn't work.
  • Loading branch information...
hrydgard committed Oct 24, 2017
1 parent d1d1e1f commit 7b1e1cd5e0a47a2133a75ad7d9ffe88a9f5b53e2
Showing with 9 additions and 7 deletions.
  1. +7 −6 ext/native/ui/view.cpp
  2. +2 −1 ext/native/ui/view.h
View
@@ -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
@@ -780,7 +780,7 @@ class TextView : public InertView {
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 @@ class TextEdit : public View {
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

0 comments on commit 7b1e1cd

Please sign in to comment.