From 9c1bf8695864cbdbdc88a5d979613128c3a695ba Mon Sep 17 00:00:00 2001 From: RadRussianRus Date: Sun, 11 Sep 2022 05:46:53 +0300 Subject: [PATCH] [Improvement] Allow to mention by name from autocomplete --- .../chat_helpers/field_autocomplete.cpp | 26 ++++++++++++++++--- .../chat_helpers/field_autocomplete.h | 3 +++ .../SourceFiles/history/history_widget.cpp | 11 +++++--- Telegram/SourceFiles/history/history_widget.h | 2 +- .../history_view_compose_controls.cpp | 9 ++++--- 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp index 8930c8ca0..bd185bbf9 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp @@ -771,6 +771,10 @@ bool FieldAutocomplete::eventFilter(QObject *obj, QEvent *e) { ? _moderateKeyActivateCallback(key) : false; } + } else if (ev->modifiers() & Qt::ControlModifier) { + if (ev->key() == Qt::Key_Enter || ev->key() == Qt::Key_Return) { + return _inner->chooseSelected(ChooseMethod::ByCtrlEnter); + } } } return QWidget::eventFilter(obj, e); @@ -1170,7 +1174,7 @@ void FieldAutocomplete::Inner::setRecentInlineBotsInRows(int32 bots) { void FieldAutocomplete::Inner::mousePressEvent(QMouseEvent *e) { selectByMouse(e->globalPos()); - if (e->button() == Qt::LeftButton) { + if (e->button() == Qt::LeftButton || e->button() == Qt::RightButton) { if (_overDelete && _sel >= 0 && _sel < (_mrows->empty() ? _hrows->size() : _recentInlineBotsInRows)) { bool removed = false; if (_mrows->empty()) { @@ -1200,7 +1204,15 @@ void FieldAutocomplete::Inner::mousePressEvent(QMouseEvent *e) { selectByMouse(e->globalPos()); } else if (_srows->empty()) { - chooseSelected(FieldAutocomplete::ChooseMethod::ByClick); + if (e->button() == Qt::LeftButton) { + if (e->modifiers() & Qt::ControlModifier) { + chooseSelected(FieldAutocomplete::ChooseMethod::ByCtrlClick); + } else { + chooseSelected(FieldAutocomplete::ChooseMethod::ByClick); + } + } else if (e->button() == Qt::RightButton) { + chooseSelected(FieldAutocomplete::ChooseMethod::ByRightClick); + } } else { _down = _sel; _previewTimer.callOnce(QApplication::startDragTime()); @@ -1223,7 +1235,15 @@ void FieldAutocomplete::Inner::mouseReleaseEvent(QMouseEvent *e) { if (_sel < 0 || _sel != pressed || _srows->empty()) return; - chooseSelected(FieldAutocomplete::ChooseMethod::ByClick); + if (e->button() == Qt::LeftButton) { + if (e->modifiers() & Qt::ControlModifier) { + chooseSelected(FieldAutocomplete::ChooseMethod::ByCtrlClick); + } else { + chooseSelected(FieldAutocomplete::ChooseMethod::ByClick); + } + } else if (e->button() == Qt::RightButton) { + chooseSelected(FieldAutocomplete::ChooseMethod::ByRightClick); + } } void FieldAutocomplete::Inner::contextMenuEvent(QContextMenuEvent *e) { diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.h b/Telegram/SourceFiles/chat_helpers/field_autocomplete.h index aff798543..e859be52c 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.h +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.h @@ -70,6 +70,9 @@ public: ByEnter, ByTab, ByClick, + ByRightClick, + ByCtrlEnter, + ByCtrlClick, }; struct MentionChosen { not_null user; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 917406dec..8f315cd6d 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -413,7 +413,7 @@ HistoryWidget::HistoryWidget( _fieldAutocomplete->mentionChosen( ) | rpl::start_with_next([=](FieldAutocomplete::MentionChosen data) { - insertMention(data.user); + insertMention(data.user, data.method); }, lifetime()); _fieldAutocomplete->hashtagChosen( @@ -1443,9 +1443,12 @@ void HistoryWidget::start() { session().data().stickers().notifySavedGifsUpdated(); } -void HistoryWidget::insertMention(UserData *user) { +void HistoryWidget::insertMention(UserData *user, FieldAutocomplete::ChooseMethod method) { QString replacement, entityTag; - if (user->username.isEmpty()) { + if (user->username.isEmpty() + || method == FieldAutocomplete::ChooseMethod::ByRightClick + || method == FieldAutocomplete::ChooseMethod::ByCtrlEnter + || method == FieldAutocomplete::ChooseMethod::ByCtrlClick) { replacement = user->firstName; if (replacement.isEmpty()) { replacement = user->name(); @@ -6938,7 +6941,7 @@ void HistoryWidget::mentionUser(PeerData *peer) { if (user->username.isEmpty()) { replacement = user->firstName; if (replacement.isEmpty()) { - replacement = user->name; + replacement = user->name(); } entityTag = PrepareMentionTag(user); } else { diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index b5b5d151d..be306e12e 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -350,7 +350,7 @@ private: void insertHashtagOrBotCommand( QString str, FieldAutocomplete::ChooseMethod method); - void insertMention(UserData *user); + void insertMention(UserData *user, FieldAutocomplete::ChooseMethod method); void cancelInlineBot(); void saveDraft(bool delayed = false); void saveCloudDraft(); diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp index c826a7e50..93d2befad 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp @@ -1494,8 +1494,11 @@ void ComposeControls::initAutocomplete() { _field->insertTag(string); } }; - const auto insertMention = [=](not_null user) { - if (user->username.isEmpty()) { + const auto insertMention = [=](not_null user, FieldAutocomplete::ChooseMethod method) { + if (user->username.isEmpty() + || method == FieldAutocomplete::ChooseMethod::ByRightClick + || method == FieldAutocomplete::ChooseMethod::ByCtrlEnter + || method == FieldAutocomplete::ChooseMethod::ByCtrlClick) { _field->insertTag( user->firstName.isEmpty() ? user->name() : user->firstName, PrepareMentionTag(user)); @@ -1506,7 +1509,7 @@ void ComposeControls::initAutocomplete() { _autocomplete->mentionChosen( ) | rpl::start_with_next([=](FieldAutocomplete::MentionChosen data) { - insertMention(data.user); + insertMention(data.user, data.method); }, _autocomplete->lifetime()); _autocomplete->hashtagChosen(