[Improvement] Allow to mention by name from autocomplete

This commit is contained in:
Eric Kotato 2022-09-11 05:46:53 +03:00 committed by Eric Kotato
parent 6c6535a1bb
commit 9c1bf86958
5 changed files with 40 additions and 11 deletions

View file

@ -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) {

View file

@ -70,6 +70,9 @@ public:
ByEnter,
ByTab,
ByClick,
ByRightClick,
ByCtrlEnter,
ByCtrlClick,
};
struct MentionChosen {
not_null<UserData*> user;

View file

@ -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 {

View file

@ -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();

View file

@ -1494,8 +1494,11 @@ void ComposeControls::initAutocomplete() {
_field->insertTag(string);
}
};
const auto insertMention = [=](not_null<UserData*> user) {
if (user->username.isEmpty()) {
const auto insertMention = [=](not_null<UserData*> 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(