diff --git a/apps/code/editor_view.cpp b/apps/code/editor_view.cpp index 855db6ab0..1e3c34af9 100644 --- a/apps/code/editor_view.cpp +++ b/apps/code/editor_view.cpp @@ -8,8 +8,6 @@ namespace Code { /* EditorView */ -constexpr char Code::EditorView::k_eol; - EditorView::EditorView(Responder * parentResponder, App * pythonDelegate) : Responder(parentResponder), View(), @@ -28,9 +26,8 @@ void EditorView::resetSelection() { } void EditorView::scrollViewDidChangeOffset(ScrollViewDataSource * scrollViewDataSource) { - m_gutterView.setOffset(scrollViewDataSource->offset().y()); - if (m_gutterView.isEditorReloadNeeded()) { - redrawSubviews(); + if (m_gutterView.setOffsetAndNeedResize(scrollViewDataSource->offset().y())) { + internalLayoutSubviews(true); } } @@ -47,8 +44,12 @@ void EditorView::didBecomeFirstResponder() { } void EditorView::layoutSubviews(bool force) { - m_gutterView.setOffset(0); - KDCoordinate gutterWidth = m_gutterView.widthComputed().width(); + m_gutterView.setOffsetAndNeedResize(0); // Whatever the return is, we layout the editor view + internalLayoutSubviews(force); +} + +void EditorView::internalLayoutSubviews(bool force) { + KDCoordinate gutterWidth = m_gutterView.computeWidth(); m_gutterView.setFrame(KDRect(0, 0, gutterWidth, bounds().height()), force); m_textArea.setFrame(KDRect( @@ -57,19 +58,6 @@ void EditorView::layoutSubviews(bool force) { bounds().width()-gutterWidth, bounds().height()), force); - m_gutterView.loadMaxDigits(); -} - -void EditorView::redrawSubviews() { - KDCoordinate gutterWidth = m_gutterView.widthComputed().width(); - m_gutterView.setFrame(KDRect(0, 0, gutterWidth, bounds().height()), true); - m_textArea.setFrame(KDRect( - gutterWidth, - 0, - bounds().width()-gutterWidth, - bounds().height()), - true); - markRectAsDirty(bounds()); } /* EditorView::GutterView */ @@ -85,57 +73,60 @@ void EditorView::GutterView::drawRect(KDContext * ctx, KDRect rect) const { KDCoordinate firstLine = m_offset / glyphSize.height(); KDCoordinate firstLinePixelOffset = m_offset - firstLine * glyphSize.height(); - char lineNumber[m_digits]; + char lineNumberBuffer[m_numberOfDigits + 1]; int numberOfLines = bounds().height() / glyphSize.height() + 1; for (int i=0; idrawString( - lineNumber, - KDPoint(k_margin + leftPadding, i*glyphSize.height() - firstLinePixelOffset), - m_font, - textColor, - backgroundColor + lineNumberBuffer, + KDPoint(k_margin, i*glyphSize.height() - firstLinePixelOffset), + m_font, + textColor, + backgroundColor ); } } -void EditorView::GutterView::loadMaxDigits() { - m_digits = getDigits((bounds().height() / m_font->glyphSize().height() + 1) + (m_offset / m_font->glyphSize().height())) + 1; -} - -void EditorView::GutterView::setOffset(KDCoordinate offset) { +bool EditorView::GutterView::setOffsetAndNeedResize(KDCoordinate offset) { if (m_offset == offset) { - return; + return false; } m_offset = offset; - m_previousDigits = m_digits; - loadMaxDigits(); + + int numberOfDigits = computeMaxNumberOfDigits(); + if (numberOfDigits != m_numberOfDigits) { + m_numberOfDigits = numberOfDigits; + return true; + } + markRectAsDirty(bounds()); + return false; } -KDSize EditorView::GutterView::widthComputed() { - return KDSize(2 * k_margin + (m_digits - 1) * m_font->glyphSize().width(), 0); +int EditorView::GutterView::computeWidth() { + return 2 * k_margin + (m_numberOfDigits) * m_font->glyphSize().width(); } -int EditorView::GutterView::getDigits(int value) { - int digits = 0; - while (value >= pow(10, digits)) {digits++;} +int EditorView::GutterView::computeMaxNumberOfDigits() { + return computeNumberOfDigitsFor((bounds().height() / m_font->glyphSize().height() + 1) + (m_offset / m_font->glyphSize().height())); +} + +int EditorView::GutterView::computeNumberOfDigitsFor(int value) { + int digits = 1; + while (value >= pow(10, digits)) { + digits++; + } return digits; } -bool EditorView::GutterView::isEditorReloadNeeded() { - return m_previousDigits != m_digits; -} - } diff --git a/apps/code/editor_view.h b/apps/code/editor_view.h index 55ae800df..6c91fdde6 100644 --- a/apps/code/editor_view.h +++ b/apps/code/editor_view.h @@ -8,8 +8,6 @@ namespace Code { class EditorView : public Responder, public View, public ScrollViewDelegate { public: - static constexpr char k_eol = '\n'; - EditorView(Responder * parentResponder, App * pythonDelegate); PythonTextArea::AutocompletionType autocompletionType(const char ** autocompletionBeginning, const char ** autocompletionEnd) const { return m_textArea.autocompletionType(nullptr, autocompletionBeginning, autocompletionEnd); } bool isAutocompleting() const; @@ -31,7 +29,7 @@ public: void unloadSyntaxHighlighter() { m_textArea.unloadSyntaxHighlighter(); }; void scrollViewDidChangeOffset(ScrollViewDataSource * scrollViewDataSource) override; void didBecomeFirstResponder() override; - void redrawSubviews(); + void internalLayoutSubviews(bool force); private: int numberOfSubviews() const override { return 2; } View * subviewAtIndex(int index) override; @@ -39,24 +37,21 @@ private: class GutterView : public View { public: - GutterView(const KDFont * font) : View(), m_font(font), m_offset(0), m_digits(3), m_previousDigits(0) {} + GutterView(const KDFont * font) : View(), m_font(font), m_offset(0), m_numberOfDigits(2) {} void drawRect(KDContext * ctx, KDRect rect) const override; - void setOffset(KDCoordinate offset); + bool setOffsetAndNeedResize(KDCoordinate offset); // Return true if the gutter view need to be resized - KDSize widthComputed(); - void loadMaxDigits(); - bool isEditorReloadNeeded(); - - static int getDigits(int value); + int computeWidth(); + int computeMaxNumberOfDigits(); + static int computeNumberOfDigitsFor(int value); private: static constexpr KDCoordinate k_margin = 2; const KDFont * m_font; KDCoordinate m_offset; - int m_digits; - int m_previousDigits; + int m_numberOfDigits; }; PythonTextArea m_textArea;