[escher] Add textArea line number limit

Change-Id: Ia0d9a9136282efc87c0996c141c0852ed75892f9
This commit is contained in:
Hugo Saint-Vignes
2020-08-11 16:24:49 +02:00
committed by Émilie Feral
parent 52f83eb682
commit aba135bc8b
4 changed files with 22 additions and 3 deletions

View File

@@ -67,11 +67,19 @@ void EditorView::GutterView::drawRect(KDContext * ctx, KDRect rect) const {
KDCoordinate firstLine = m_offset / glyphSize.height();
KDCoordinate firstLinePixelOffset = m_offset - firstLine * glyphSize.height();
char lineNumber[4];
char lineNumber[k_lineNumberCharLength];
int numberOfLines = bounds().height() / glyphSize.height() + 1;
for (int i=0; i<numberOfLines; i++) {
Poincare::Integer line(i + firstLine + 1);
line.serialize(lineNumber, 4);
// Only the first two digits are displayed
int lineNumberValue = (i + firstLine + 1) % 100;
Poincare::Integer line(lineNumberValue);
if (firstLine < 10 || lineNumberValue >= 10) {
line.serialize(lineNumber, k_lineNumberCharLength);
} else {
// Add a leading "0"
lineNumber[0] = '0';
line.serialize(lineNumber + 1, k_lineNumberCharLength - 1);
}
KDCoordinate leftPadding = (2 - strlen(lineNumber)) * glyphSize.width();
ctx->drawString(
lineNumber,

View File

@@ -42,6 +42,7 @@ private:
KDSize minimalSizeForOptimalDisplay() const override;
private:
static constexpr KDCoordinate k_margin = 2;
static constexpr int k_lineNumberCharLength = 3;
const KDFont * m_font;
KDCoordinate m_offset;
};

View File

@@ -94,6 +94,9 @@ protected:
size_t textLength() const {
return strlen(m_buffer);
}
int textLineTotal() const {
return positionAtPointer(m_buffer+textLength()).line();
}
private:
char * m_buffer;
size_t m_bufferSize;
@@ -133,6 +136,8 @@ protected:
private:
void selectUpDown(bool up, int step);
TextAreaDelegate * m_delegate;
// Due to rect size limitation, the editor cannot display more than 1800 lines
constexpr static int k_maxLines = 999;
};
#endif

View File

@@ -63,6 +63,11 @@ bool TextArea::handleEventWithText(const char * text, bool indentation, bool for
}
}
// Check the text will not overflow the max number of lines
if (contentView()->getText()->textLineTotal() + UTF8Helper::CountOccurrences(text, '\n') >= k_maxLines) {
return false;
}
// Insert the text
if (!insertTextAtLocation(text, insertionPosition)) {
return true;