Improve input field placeholders with IME.

Respect the IME preedit string when toggling the placeholder.
This commit is contained in:
John Preston 2017-05-26 18:50:25 +03:00
parent 57f0158ade
commit 7cea6ede1a
2 changed files with 39 additions and 6 deletions

View File

@ -1331,7 +1331,8 @@ void FlatTextarea::setPlaceholder(const QString &ph, int32 afterSymbols) {
} }
void FlatTextarea::updatePlaceholder() { void FlatTextarea::updatePlaceholder() {
auto placeholderVisible = (getTextWithTags().text.size() <= _phAfter); auto textSize = (getTextWithTags().text.size() + textCursor().block().layout()->preeditAreaText().size());
auto placeholderVisible = (textSize <= _phAfter);
if (_placeholderVisible != placeholderVisible) { if (_placeholderVisible != placeholderVisible) {
_placeholderVisible = placeholderVisible; _placeholderVisible = placeholderVisible;
_a_placeholderVisible.start([this] { update(); }, _placeholderVisible ? 0. : 1., _placeholderVisible ? 1. : 0., _st.phDuration); _a_placeholderVisible.start([this] { update(); }, _placeholderVisible ? 0. : 1., _placeholderVisible ? 1. : 0., _st.phDuration);
@ -1645,13 +1646,28 @@ QSize FlatInput::minimumSizeHint() const {
} }
void FlatInput::updatePlaceholder() { void FlatInput::updatePlaceholder() {
auto placeholderVisible = text().isEmpty(); auto hasText = !text().isEmpty();
if (!hasText) {
hasText = _lastPreEditTextNotEmpty;
} else {
_lastPreEditTextNotEmpty = false;
}
auto placeholderVisible = !hasText;
if (_placeholderVisible != placeholderVisible) { if (_placeholderVisible != placeholderVisible) {
_placeholderVisible = placeholderVisible; _placeholderVisible = placeholderVisible;
_a_placeholderVisible.start([this] { update(); }, _placeholderVisible ? 0. : 1., _placeholderVisible ? 1. : 0., _st.phDuration); _a_placeholderVisible.start([this] { update(); }, _placeholderVisible ? 0. : 1., _placeholderVisible ? 1. : 0., _st.phDuration);
} }
} }
void FlatInput::inputMethodEvent(QInputMethodEvent *e) {
QLineEdit::inputMethodEvent(e);
auto lastPreEditTextNotEmpty = !e->preeditString().isEmpty();
if (_lastPreEditTextNotEmpty != lastPreEditTextNotEmpty) {
_lastPreEditTextNotEmpty = lastPreEditTextNotEmpty;
updatePlaceholder();
}
}
const QString &FlatInput::placeholder() const { const QString &FlatInput::placeholder() const {
return _fullph; return _fullph;
} }
@ -2707,7 +2723,7 @@ void InputField::setFocused(bool focused) {
} }
void InputField::startPlaceholderAnimation() { void InputField::startPlaceholderAnimation() {
auto placeholderShifted = (_focused && _st.placeholderScale > 0.) || !getLastText().isEmpty() || _forcePlaceholderHidden; auto placeholderShifted = _forcePlaceholderHidden || (_focused && _st.placeholderScale > 0.) || !getLastText().isEmpty();
if (_placeholderShifted != placeholderShifted) { if (_placeholderShifted != placeholderShifted) {
_placeholderShifted = placeholderShifted; _placeholderShifted = placeholderShifted;
_a_placeholderShifted.start([this] { update(); }, _placeholderShifted ? 0. : 1., _placeholderShifted ? 1. : 0., _st.duration); _a_placeholderShifted.start([this] { update(); }, _placeholderShifted ? 0. : 1., _placeholderShifted ? 1. : 0., _st.duration);
@ -3495,6 +3511,12 @@ void MaskedInputField::contextMenuEvent(QContextMenuEvent *e) {
} }
} }
void MaskedInputField::inputMethodEvent(QInputMethodEvent *e) {
QLineEdit::inputMethodEvent(e);
_lastPreEditText = e->preeditString();
update();
}
void MaskedInputField::showError() { void MaskedInputField::showError() {
setErrorShown(true); setErrorShown(true);
if (!hasFocus()) { if (!hasFocus()) {
@ -3538,7 +3560,7 @@ void MaskedInputField::setPlaceholderHidden(bool forcePlaceholderHidden) {
} }
void MaskedInputField::startPlaceholderAnimation() { void MaskedInputField::startPlaceholderAnimation() {
auto placeholderShifted = (_focused && _st.placeholderScale > 0.) || !getLastText().isEmpty() || _forcePlaceholderHidden; auto placeholderShifted = _forcePlaceholderHidden || (_focused && _st.placeholderScale > 0.) || !getLastText().isEmpty();
if (_placeholderShifted != placeholderShifted) { if (_placeholderShifted != placeholderShifted) {
_placeholderShifted = placeholderShifted; _placeholderShifted = placeholderShifted;
_a_placeholderShifted.start([this] { update(); }, _placeholderShifted ? 0. : 1., _placeholderShifted ? 1. : 0., _st.duration); _a_placeholderShifted.start([this] { update(); }, _placeholderShifted ? 0. : 1., _placeholderShifted ? 1. : 0., _st.duration);
@ -3681,8 +3703,8 @@ PhonePartInput::PhonePartInput(QWidget *parent, const style::InputField &st) : M
} }
void PhonePartInput::paintAdditionalPlaceholder(Painter &p, TimeMs ms) { void PhonePartInput::paintAdditionalPlaceholder(Painter &p, TimeMs ms) {
auto t = getLastText();
if (!_pattern.isEmpty()) { if (!_pattern.isEmpty()) {
auto t = getDisplayedText();
auto ph = _additionalPlaceholder.mid(t.size()); auto ph = _additionalPlaceholder.mid(t.size());
if (!ph.isEmpty()) { if (!ph.isEmpty()) {
p.setClipRect(rect()); p.setClipRect(rect());
@ -3901,7 +3923,7 @@ void PhoneInput::clearText() {
void PhoneInput::paintAdditionalPlaceholder(Painter &p, TimeMs ms) { void PhoneInput::paintAdditionalPlaceholder(Painter &p, TimeMs ms) {
if (!_pattern.isEmpty()) { if (!_pattern.isEmpty()) {
auto t = getLastText(); auto t = getDisplayedText();
auto ph = _additionalPlaceholder.mid(t.size()); auto ph = _additionalPlaceholder.mid(t.size());
if (!ph.isEmpty()) { if (!ph.isEmpty()) {
p.setClipRect(rect()); p.setClipRect(rect());

View File

@ -273,6 +273,7 @@ protected:
void keyPressEvent(QKeyEvent *e) override; void keyPressEvent(QKeyEvent *e) override;
void resizeEvent(QResizeEvent *e) override; void resizeEvent(QResizeEvent *e) override;
void contextMenuEvent(QContextMenuEvent *e) override; void contextMenuEvent(QContextMenuEvent *e) override;
void inputMethodEvent(QInputMethodEvent *e) override;
virtual void correctValue(const QString &was, QString &now); virtual void correctValue(const QString &was, QString &now);
@ -294,6 +295,7 @@ private:
bool _placeholderVisible = true; bool _placeholderVisible = true;
Animation _a_placeholderFocused; Animation _a_placeholderFocused;
Animation _a_placeholderVisible; Animation _a_placeholderVisible;
bool _lastPreEditTextNotEmpty = false;
const style::FlatInput &_st; const style::FlatInput &_st;
@ -720,6 +722,13 @@ signals:
void blurred(); void blurred();
protected: protected:
QString getDisplayedText() const {
auto result = getLastText();
if (!_lastPreEditText.isEmpty()) {
result = result.mid(0, _oldcursor) + _lastPreEditText + result.mid(_oldcursor);
}
return result;
}
void startBorderAnimation(); void startBorderAnimation();
void startPlaceholderAnimation(); void startPlaceholderAnimation();
@ -731,6 +740,7 @@ protected:
void keyPressEvent(QKeyEvent *e) override; void keyPressEvent(QKeyEvent *e) override;
void resizeEvent(QResizeEvent *e) override; void resizeEvent(QResizeEvent *e) override;
void contextMenuEvent(QContextMenuEvent *e) override; void contextMenuEvent(QContextMenuEvent *e) override;
void inputMethodEvent(QInputMethodEvent *e) override;
virtual void correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor) { virtual void correctValue(const QString &was, int32 wasCursor, QString &now, int32 &nowCursor) {
} }
@ -762,6 +772,7 @@ private:
QString _oldtext; QString _oldtext;
int _oldcursor = 0; int _oldcursor = 0;
QString _lastPreEditText;
bool _undoAvailable = false; bool _undoAvailable = false;
bool _redoAvailable = false; bool _redoAvailable = false;