Show emoji by first word after a delay.

This commit is contained in:
John Preston 2019-03-30 12:06:15 +04:00
parent 932ed20c4b
commit f76dc74040
2 changed files with 26 additions and 11 deletions

View File

@ -23,6 +23,7 @@ namespace Ui {
namespace Emoji { namespace Emoji {
namespace { namespace {
constexpr auto kShowExactDelay = crl::time(300);
constexpr auto kRowLimit = 5; constexpr auto kRowLimit = 5;
} // namespace } // namespace
@ -391,7 +392,8 @@ void SuggestionsWidget::leaveEventHook(QEvent *e) {
SuggestionsController::SuggestionsController( SuggestionsController::SuggestionsController(
not_null<QWidget*> outer, not_null<QWidget*> outer,
not_null<QTextEdit*> field) not_null<QTextEdit*> field)
: _field(field) { : _field(field)
, _showExactTimer([=] { showWithQuery(getEmojiQuery()); }) {
_container = base::make_unique_q<InnerDropdown>( _container = base::make_unique_q<InnerDropdown>(
outer, outer,
st::emojiSuggestionsDropdown); st::emojiSuggestionsDropdown);
@ -430,7 +432,10 @@ SuggestionsController::SuggestionsController(
}, _lifetime); }, _lifetime);
Core::App().emojiKeywords().refreshed( Core::App().emojiKeywords().refreshed(
) | rpl::start_with_next([=] { ) | rpl::start_with_next([=] {
showFromTextChange(getEmojiQuery(), true); _keywordsRefreshed = true;
if (!_showExactTimer.isActive()) {
showWithQuery(_lastShownQuery);
}
}, _lifetime); }, _lifetime);
updateForceHidden(); updateForceHidden();
@ -481,14 +486,20 @@ void SuggestionsController::handleTextChange() {
const auto query = getEmojiQuery(); const auto query = getEmojiQuery();
if (query.isEmpty() || _textChangeAfterKeyPress) { if (query.isEmpty() || _textChangeAfterKeyPress) {
showFromTextChange(query); const auto exact = (!query.isEmpty() && query[0] != ':');
if (exact && (_container->isHidden() || _container->isHiding())) {
_showExactTimer.callOnce(kShowExactDelay);
} else {
showWithQuery(query);
}
} }
} }
void SuggestionsController::showFromTextChange( void SuggestionsController::showWithQuery(const QString &query) {
const QString &query, _showExactTimer.cancel();
bool force) { const auto force = base::take(_keywordsRefreshed);
_suggestions->showWithQuery(query, force); _lastShownQuery = query;
_suggestions->showWithQuery(_lastShownQuery, force);
} }
QString SuggestionsController::getEmojiQuery() { QString SuggestionsController::getEmojiQuery() {
@ -565,7 +576,7 @@ QString SuggestionsController::getEmojiQuery() {
void SuggestionsController::replaceCurrent(const QString &replacement) { void SuggestionsController::replaceCurrent(const QString &replacement) {
const auto suggestion = getEmojiQuery(); const auto suggestion = getEmojiQuery();
if (suggestion.isEmpty()) { if (suggestion.isEmpty()) {
_suggestions->showWithQuery(QString()); showWithQuery(QString());
} else { } else {
const auto cursor = _field->textCursor(); const auto cursor = _field->textCursor();
const auto position = cursor.position(); const auto position = cursor.position();
@ -579,7 +590,7 @@ void SuggestionsController::handleCursorPositionChange() {
if (_ignoreCursorPositionChange) { if (_ignoreCursorPositionChange) {
return; return;
} }
_suggestions->showWithQuery(QString()); showWithQuery(QString());
}); });
} }
@ -676,7 +687,7 @@ bool SuggestionsController::fieldFilter(not_null<QEvent*> event) {
case Qt::Key_Escape: case Qt::Key_Escape:
if (_shown && !_forceHidden) { if (_shown && !_forceHidden) {
_suggestions->showWithQuery(QString()); showWithQuery(QString());
return true; return true;
} }
break; break;

View File

@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/effects/panel_animation.h" #include "ui/effects/panel_animation.h"
#include "base/unique_qptr.h" #include "base/unique_qptr.h"
#include "base/timer.h"
namespace Ui { namespace Ui {
@ -88,7 +89,7 @@ public:
private: private:
void handleCursorPositionChange(); void handleCursorPositionChange();
void handleTextChange(); void handleTextChange();
void showFromTextChange(const QString &query, bool force = false); void showWithQuery(const QString &query);
QString getEmojiQuery(); QString getEmojiQuery();
void suggestionsUpdated(bool visible); void suggestionsUpdated(bool visible);
void updateGeometry(); void updateGeometry();
@ -111,6 +112,9 @@ private:
QPointer<SuggestionsWidget> _suggestions; QPointer<SuggestionsWidget> _suggestions;
base::unique_qptr<QObject> _fieldFilter; base::unique_qptr<QObject> _fieldFilter;
base::unique_qptr<QObject> _outerFilter; base::unique_qptr<QObject> _outerFilter;
base::Timer _showExactTimer;
bool _keywordsRefreshed = false;
QString _lastShownQuery;
rpl::lifetime _lifetime; rpl::lifetime _lifetime;