[apps] TextFieldWithExtension blocks cursor on the rightmost text

This commit is contained in:
Léa Saviot
2018-10-16 18:19:52 +02:00
committed by Émilie Feral
parent df542f2f84
commit 885f953d52
5 changed files with 53 additions and 3 deletions

View File

@@ -76,6 +76,7 @@ app_objs += $(addprefix apps/shared/,\
tab_table_controller.o\
text_field_delegate.o\
text_field_delegate_app.o\
text_field_with_extension.o\
toolbox_helpers.o\
values_function_parameter_controller.o\
values_parameter_controller.o\

View File

@@ -0,0 +1,14 @@
#include "text_field_with_extension.h"
namespace Shared {
void TextFieldWithExtension::willSetCursorLocation(int * location) {
size_t textLength = strlen(text());
assert(textLength >= m_extensionLength);
size_t maxLocation = textLength - m_extensionLength;
if (*location > (int)maxLocation) {
*location = maxLocation;
}
}
}

View File

@@ -0,0 +1,32 @@
#ifndef SHARED_TEXT_FIELD_WITH_EXTENSION_H
#define SHARED_TEXT_FIELD_WITH_EXTENSION_H
#include <escher/text_field.h>
namespace Shared {
class TextFieldWithExtension : public TextField {
public:
TextFieldWithExtension(size_t extensionLength,
Responder * parentResponder,
char * textBuffer,
char * draftTextBuffer,
size_t textBufferSize,
TextFieldDelegate * delegate = nullptr,
bool hasTwoBuffers = true,
KDText::FontSize size = KDText::FontSize::Large,
float horizontalAlignment = 0.0f,
float verticalAlignment = 0.5f,
KDColor textColor = KDColorBlack,
KDColor backgroundColor = KDColorWhite) :
TextField(parentResponder, textBuffer, draftTextBuffer, textBufferSize, delegate, hasTwoBuffers, size, horizontalAlignment, verticalAlignment, textColor, backgroundColor),
m_extensionLength(extensionLength)
{}
private:
void willSetCursorLocation(int * location) override;
size_t m_extensionLength;
};
}
#endif

View File

@@ -55,6 +55,7 @@ protected:
virtual const ContentView * nonEditableContentView() const = 0;
private:
virtual TextInputDelegate * delegate() = 0;
virtual void willSetCursorLocation(int * location) {}
};
#endif

View File

@@ -12,8 +12,8 @@ TextInput::ContentView::ContentView(const KDFont * font) :
}
void TextInput::ContentView::setCursorLocation(int location) {
int adjustedLocation = location < 0 ? 0 : location;
adjustedLocation = adjustedLocation > (signed int)editedTextLength() ? (signed int)editedTextLength() : adjustedLocation;
assert(location >= 0);
int adjustedLocation = location > (signed int)editedTextLength() ? (signed int)editedTextLength() : location;
m_cursorIndex = adjustedLocation;
layoutSubviews();
}
@@ -84,7 +84,9 @@ void TextInput::scrollToCursor() {
}
bool TextInput::setCursorLocation(int location) {
contentView()->setCursorLocation(location);
int adjustedLocation = location < 0 ? 0 : location;
willSetCursorLocation(&adjustedLocation);
contentView()->setCursorLocation(adjustedLocation);
scrollToCursor();
return true;
}