From 07a910b455b84413620282a5dce4e31fab3c3922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 18 Jan 2019 10:54:21 +0100 Subject: [PATCH] [escher] Clean text_input --- escher/include/escher/text_input.h | 20 ++++++++++----- escher/src/text_input.cpp | 40 ++++++++---------------------- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/escher/include/escher/text_input.h b/escher/include/escher/text_input.h index 826b92d21..f4a705c77 100644 --- a/escher/include/escher/text_input.h +++ b/escher/include/escher/text_input.h @@ -1,14 +1,14 @@ #ifndef ESCHER_TEXT_INPUT_H #define ESCHER_TEXT_INPUT_H -#include -#include #include #include +#include +#include class TextInput : public ScrollableView, public ScrollViewDataSource { public: - TextInput(Responder * parentResponder, View * contentView); + TextInput(Responder * parentResponder, View * contentView) : ScrollableView(parentResponder, contentView, this) {} void setFont(const KDFont * font) { contentView()->setFont(font); } const char * text() const { return nonEditableContentView()->text(); } bool removeChar(); @@ -18,7 +18,12 @@ public: protected: class ContentView : public View { public: - ContentView(const KDFont * font); + ContentView(const KDFont * font) : + View(), + m_cursorView(), + m_font(font), + m_cursorIndex(0) + {} void setFont(const KDFont * font); const KDFont * font() const { return m_font; } size_t cursorLocation() const { return m_cursorIndex; } @@ -37,8 +42,11 @@ protected: const KDFont * m_font; size_t m_cursorIndex; private: - int numberOfSubviews() const override; - View * subviewAtIndex(int index) override; + int numberOfSubviews() const override { return 1; } + View * subviewAtIndex(int index) override { + assert(index == 1); + return &m_cursorView; + } virtual size_t editedTextLength() const = 0; }; protected: diff --git a/escher/src/text_input.cpp b/escher/src/text_input.cpp index 51c7d6dfd..dee7c1f79 100644 --- a/escher/src/text_input.cpp +++ b/escher/src/text_input.cpp @@ -3,14 +3,6 @@ /* TextInput::ContentView */ -TextInput::ContentView::ContentView(const KDFont * font) : - View(), - m_cursorView(), - m_font(font), - m_cursorIndex(0) -{ -} - void TextInput::ContentView::setCursorLocation(int location) { assert(location >= 0); int adjustedLocation = location > (signed int)editedTextLength() ? (signed int)editedTextLength() : location; @@ -27,14 +19,6 @@ KDRect TextInput::ContentView::cursorRect() { return characterFrameAtIndex(m_cursorIndex); } -int TextInput::ContentView::numberOfSubviews() const { - return 1; -} - -View * TextInput::ContentView::subviewAtIndex(int index) { - return &m_cursorView; -} - void TextInput::ContentView::layoutSubviews() { m_cursorView.setFrame(cursorRect()); } @@ -43,7 +27,7 @@ KDRect TextInput::ContentView::dirtyRectFromCursorPosition(size_t index, bool li KDRect charRect = characterFrameAtIndex(index); KDRect dirtyRect = KDRect(charRect.x(), charRect.y(), bounds().width() - charRect.x(), charRect.height()); if (lineBreak) { - dirtyRect = dirtyRect.unionedWith(KDRect(0, charRect.bottom()+1, bounds().width(), bounds().height()-charRect.bottom()-1)); + dirtyRect = dirtyRect.unionedWith(KDRect(0, charRect.bottom()+1, bounds().width(), bounds().height()-charRect.bottom()-1)); } return dirtyRect; } @@ -54,11 +38,6 @@ void TextInput::ContentView::reloadRectFromCursorPosition(size_t index, bool lin /* TextInput */ -TextInput::TextInput(Responder * parentResponder, View * contentView) : - ScrollableView(parentResponder, contentView, this) -{ -} - bool TextInput::removeChar() { contentView()->removeChar(); scrollToCursor(); @@ -66,13 +45,14 @@ bool TextInput::removeChar() { } void TextInput::scrollToCursor() { - /* Technically, we do not need to overscroll in text input. However, - * logically, we should layout the scroll view before calling - * scrollToContentRect in case the size of the scroll view has changed and - * then call scrollToContentRect which call another layout of the scroll view - * if the offset has evolved. In order to avoid requiring two layouts, we - * allow overscrolling in scrollToContentRect and the last layout of the - * scroll view corrects the size of the scroll view only once. */ + /* Technically, we do not need to overscroll in text input. However, we should + * layout the scroll view before calling scrollToContentRect (in case the size + * of the scroll view has changed) and then call scrollToContentRect which + * calls another layout of the scroll view if the offset has evolved. + * + * In order to avoid requiring two layouts, we allow overscrolling in + * scrollToContentRect, and the last layout of the scroll view corrects the + * size of the scroll view only once. */ scrollToContentRect(contentView()->cursorRect(), true); } @@ -87,7 +67,7 @@ bool TextInput::setCursorLocation(int location) { bool TextInput::insertTextAtLocation(const char * text, int location) { if (contentView()->insertTextAtLocation(text, location)) { /* We layout the scrollable view before scrolling to cursor because the - * content size might have changed. */ + * content size might have changed. */ layoutSubviews(); scrollToCursor(); return true;