experimental/editor: scroll on cursor movement, Editor::getLocation

Bug: skia:9020
Change-Id: Iab91402790077c969af8e553b03525e63dab44ba
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/230496
Auto-Submit: Hal Canary <halcanary@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
This commit is contained in:
Hal Canary 2019-07-29 14:44:04 -04:00 committed by Skia Commit-Bot
parent eecc687376
commit 9ef1b771af
3 changed files with 31 additions and 13 deletions

View File

@ -16,6 +16,11 @@
using namespace editor;
static inline SkRect offset(SkRect r, SkIPoint p) {
r.offset((float)p.x(), (float)p.y());
return r;
}
static constexpr SkRect kUnsetRect{-FLT_MAX, -FLT_MAX, -FLT_MAX, -FLT_MAX};
static SkRect selection_box(const SkFontMetrics& metrics,
@ -237,6 +242,21 @@ static const char* prev_utf8(const char* p, const char* begin) {
return p > begin ? align_utf8(p - 1, begin) : begin;
}
SkRect Editor::getLocation(Editor::TextPosition cursor) {
this->reshapeAll();
if (fLines.size() > 0) {
const TextLine& cLine = fLines[cursor.fParagraphIndex];
SkRect pos = fSpaceBounds;
if (cursor.fTextByteIndex < cLine.fCursorPos.size()) {
pos = cLine.fCursorPos[cursor.fTextByteIndex];
}
pos.fRight = pos.fLeft + 1;
pos.fLeft -= 1;
return offset(pos, cLine.fOrigin);
}
return SkRect{0, 0, 0, 0};
}
static size_t count_char(const StringSlice& string, char value) {
size_t count = 0;
for (char c : string) { if (c == value) { ++count; } }
@ -456,11 +476,6 @@ Editor::TextPosition Editor::move(Editor::Movement move, Editor::TextPosition po
return pos;
}
static inline SkRect offset(SkRect r, SkIPoint p) {
r.offset((float)p.x(), (float)p.y());
return r;
}
void Editor::paint(SkCanvas* c, PaintOpts options) {
this->reshapeAll();
if (!c) {
@ -482,14 +497,7 @@ void Editor::paint(SkCanvas* c, PaintOpts options) {
}
if (fLines.size() > 0) {
const TextLine& cLine = fLines[options.fCursor.fParagraphIndex];
SkRect pos = fSpaceBounds;
if (options.fCursor.fTextByteIndex < cLine.fCursorPos.size()) {
pos = cLine.fCursorPos[options.fCursor.fTextByteIndex];
}
pos.fRight = pos.fLeft + 1;
pos.fLeft -= 1;
c->drawRect(offset(pos, cLine.fOrigin), SkPaint(options.fCursorColor));
c->drawRect(Editor::getLocation(options.fCursor), SkPaint(options.fCursorColor));
}
SkPaint foreground = SkPaint(options.fForegroundColor);

View File

@ -72,6 +72,7 @@ public:
};
TextPosition move(Editor::Movement move, Editor::TextPosition pos) const;
TextPosition getPosition(SkIPoint);
SkRect getLocation(TextPosition);
// insert into current text.
TextPosition insert(TextPosition, const char* utf8Text, size_t byteLen);
// remove text between two positions

View File

@ -228,9 +228,18 @@ struct EditorLayer : public sk_app::Window::Layer {
fShiftDown = shift;
}
fTextPos = fEditor.move(m, fTextPos);
// scroll if needed.
SkIRect cursor = fEditor.getLocation(fTextPos).roundOut();
if (cursor.bottom() > fPos + fHeight) {
fPos = cursor.bottom() - fHeight + fEditor.getMargin();
} else if (cursor.top() < fPos) {
fPos = cursor.top() - fEditor.getMargin();
}
this->inval();
return true;
}
bool onKey(sk_app::Window::Key key,
InputState state,
ModifierKey modifiers) override {