From aba135bc8b34a328065d5613fb85321234820679 Mon Sep 17 00:00:00 2001 From: Hugo Saint-Vignes Date: Tue, 11 Aug 2020 16:24:49 +0200 Subject: [PATCH] [escher] Add textArea line number limit Change-Id: Ia0d9a9136282efc87c0996c141c0852ed75892f9 --- apps/code/editor_view.cpp | 14 +++++++++++--- apps/code/editor_view.h | 1 + escher/include/escher/text_area.h | 5 +++++ escher/src/text_area.cpp | 5 +++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/apps/code/editor_view.cpp b/apps/code/editor_view.cpp index 86a29ba66..9ec48d7eb 100644 --- a/apps/code/editor_view.cpp +++ b/apps/code/editor_view.cpp @@ -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= 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, diff --git a/apps/code/editor_view.h b/apps/code/editor_view.h index ab3038c94..547f7340b 100644 --- a/apps/code/editor_view.h +++ b/apps/code/editor_view.h @@ -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; }; diff --git a/escher/include/escher/text_area.h b/escher/include/escher/text_area.h index 51881daeb..ffc65b4bc 100644 --- a/escher/include/escher/text_area.h +++ b/escher/include/escher/text_area.h @@ -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 diff --git a/escher/src/text_area.cpp b/escher/src/text_area.cpp index 76e7c0386..bbddcb700 100644 --- a/escher/src/text_area.cpp +++ b/escher/src/text_area.cpp @@ -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;