mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Added support of primary username to FieldAutocomplete.
This commit is contained in:
parent
79f592a84f
commit
00df4625e2
5 changed files with 39 additions and 38 deletions
|
@ -51,6 +51,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
|
|
||||||
#include <QtWidgets/QApplication>
|
#include <QtWidgets/QApplication>
|
||||||
|
|
||||||
|
[[nodiscard]] QString PrimaryUsername(not_null<UserData*> user) {
|
||||||
|
const auto &usernames = user->usernames();
|
||||||
|
return usernames.empty() ? user->username() : usernames.front();
|
||||||
|
}
|
||||||
|
|
||||||
class FieldAutocomplete::Inner final : public Ui::RpWidget {
|
class FieldAutocomplete::Inner final : public Ui::RpWidget {
|
||||||
public:
|
public:
|
||||||
struct ScrollTo {
|
struct ScrollTo {
|
||||||
|
@ -396,9 +401,9 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) {
|
||||||
}
|
}
|
||||||
|
|
||||||
auto filterNotPassedByUsername = [this](UserData *user) -> bool {
|
auto filterNotPassedByUsername = [this](UserData *user) -> bool {
|
||||||
if (user->username().startsWith(_filter, Qt::CaseInsensitive)) {
|
if (PrimaryUsername(user).startsWith(_filter, Qt::CaseInsensitive)) {
|
||||||
const auto exactUsername =
|
const auto exactUsername =
|
||||||
(user->username().size() == _filter.size());
|
(PrimaryUsername(user).size() == _filter.size());
|
||||||
return exactUsername;
|
return exactUsername;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
@ -407,7 +412,7 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) {
|
||||||
for (const auto &nameWord : user->nameWords()) {
|
for (const auto &nameWord : user->nameWords()) {
|
||||||
if (nameWord.startsWith(_filter, Qt::CaseInsensitive)) {
|
if (nameWord.startsWith(_filter, Qt::CaseInsensitive)) {
|
||||||
const auto exactUsername =
|
const auto exactUsername =
|
||||||
(user->username().compare(_filter, Qt::CaseInsensitive) == 0);
|
(PrimaryUsername(user).compare(_filter, Qt::CaseInsensitive) == 0);
|
||||||
return exactUsername;
|
return exactUsername;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -556,7 +561,7 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) {
|
||||||
for (const auto &command : *i->second) {
|
for (const auto &command : *i->second) {
|
||||||
if (!listAllSuggestions) {
|
if (!listAllSuggestions) {
|
||||||
auto toFilter = (hasUsername || botStatus == 0 || botStatus == 2)
|
auto toFilter = (hasUsername || botStatus == 0 || botStatus == 2)
|
||||||
? command.command + '@' + user->username()
|
? command.command + '@' + PrimaryUsername(user)
|
||||||
: command.command;
|
: command.command;
|
||||||
if (!toFilter.startsWith(_filter, Qt::CaseInsensitive)/* || toFilter.size() == _filter.size()*/) {
|
if (!toFilter.startsWith(_filter, Qt::CaseInsensitive)/* || toFilter.size() == _filter.size()*/) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -575,7 +580,7 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) {
|
||||||
const auto toFilter = (hasUsername
|
const auto toFilter = (hasUsername
|
||||||
|| botStatus == 0
|
|| botStatus == 0
|
||||||
|| botStatus == 2)
|
|| botStatus == 2)
|
||||||
? command.command + '@' + user->username()
|
? command.command + '@' + PrimaryUsername(user)
|
||||||
: command.command;
|
: command.command;
|
||||||
if (!toFilter.startsWith(_filter, Qt::CaseInsensitive)/* || toFilter.size() == _filter.size()*/) continue;
|
if (!toFilter.startsWith(_filter, Qt::CaseInsensitive)/* || toFilter.size() == _filter.size()*/) continue;
|
||||||
}
|
}
|
||||||
|
@ -941,16 +946,16 @@ void FieldAutocomplete::Inner::paintEvent(QPaintEvent *e) {
|
||||||
auto &row = _mrows->at(i);
|
auto &row = _mrows->at(i);
|
||||||
const auto user = row.user;
|
const auto user = row.user;
|
||||||
auto first = (!filterIsEmpty
|
auto first = (!filterIsEmpty
|
||||||
&& user->username().startsWith(
|
&& PrimaryUsername(user).startsWith(
|
||||||
filter,
|
filter,
|
||||||
Qt::CaseInsensitive))
|
Qt::CaseInsensitive))
|
||||||
? ('@' + user->username().mid(0, filterSize))
|
? ('@' + PrimaryUsername(user).mid(0, filterSize))
|
||||||
: QString();
|
: QString();
|
||||||
auto second = first.isEmpty()
|
auto second = first.isEmpty()
|
||||||
? (user->username().isEmpty()
|
? (PrimaryUsername(user).isEmpty()
|
||||||
? QString()
|
? QString()
|
||||||
: ('@' + user->username()))
|
: ('@' + PrimaryUsername(user)))
|
||||||
: user->username().mid(filterSize);
|
: PrimaryUsername(user).mid(filterSize);
|
||||||
auto firstwidth = st::mentionFont->width(first);
|
auto firstwidth = st::mentionFont->width(first);
|
||||||
auto secondwidth = st::mentionFont->width(second);
|
auto secondwidth = st::mentionFont->width(second);
|
||||||
auto unamewidth = firstwidth + secondwidth;
|
auto unamewidth = firstwidth + secondwidth;
|
||||||
|
@ -1015,7 +1020,7 @@ void FieldAutocomplete::Inner::paintEvent(QPaintEvent *e) {
|
||||||
auto toHighlight = row.command;
|
auto toHighlight = row.command;
|
||||||
int32 botStatus = _parent->chat() ? _parent->chat()->botStatus : ((_parent->channel() && _parent->channel()->isMegagroup()) ? _parent->channel()->mgInfo->botStatus : -1);
|
int32 botStatus = _parent->chat() ? _parent->chat()->botStatus : ((_parent->channel() && _parent->channel()->isMegagroup()) ? _parent->channel()->mgInfo->botStatus : -1);
|
||||||
if (hasUsername || botStatus == 0 || botStatus == 2) {
|
if (hasUsername || botStatus == 0 || botStatus == 2) {
|
||||||
toHighlight += '@' + user->username();
|
toHighlight += '@' + PrimaryUsername(user);
|
||||||
}
|
}
|
||||||
user->loadUserpic();
|
user->loadUserpic();
|
||||||
user->paintUserpicLeft(p, row.userpic, st::mentionPadding.left(), i * st::mentionHeight + st::mentionPadding.top(), width(), st::mentionPhotoSize);
|
user->paintUserpicLeft(p, row.userpic, st::mentionPadding.left(), i * st::mentionHeight + st::mentionPadding.top(), width(), st::mentionPhotoSize);
|
||||||
|
@ -1149,7 +1154,8 @@ bool FieldAutocomplete::Inner::chooseAtIndex(
|
||||||
}
|
}
|
||||||
} else if (!_mrows->empty()) {
|
} else if (!_mrows->empty()) {
|
||||||
if (index < _mrows->size()) {
|
if (index < _mrows->size()) {
|
||||||
_mentionChosen.fire({ _mrows->at(index).user, method });
|
const auto user = _mrows->at(index).user;
|
||||||
|
_mentionChosen.fire({ user, PrimaryUsername(user), method });
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if (!_hrows->empty()) {
|
} else if (!_hrows->empty()) {
|
||||||
|
@ -1172,7 +1178,7 @@ bool FieldAutocomplete::Inner::chooseAtIndex(
|
||||||
|| _parent->filter().indexOf('@') > 0);
|
|| _parent->filter().indexOf('@') > 0);
|
||||||
const auto commandString = QString("/%1%2").arg(
|
const auto commandString = QString("/%1%2").arg(
|
||||||
command,
|
command,
|
||||||
insertUsername ? ('@' + user->username()) : QString());
|
insertUsername ? ('@' + PrimaryUsername(user)) : QString());
|
||||||
|
|
||||||
_botCommandChosen.fire({ commandString, method });
|
_botCommandChosen.fire({ commandString, method });
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -76,6 +76,7 @@ public:
|
||||||
};
|
};
|
||||||
struct MentionChosen {
|
struct MentionChosen {
|
||||||
not_null<UserData*> user;
|
not_null<UserData*> user;
|
||||||
|
QString mention;
|
||||||
ChooseMethod method = ChooseMethod::ByEnter;
|
ChooseMethod method = ChooseMethod::ByEnter;
|
||||||
};
|
};
|
||||||
struct HashtagChosen {
|
struct HashtagChosen {
|
||||||
|
|
|
@ -389,7 +389,18 @@ HistoryWidget::HistoryWidget(
|
||||||
|
|
||||||
_fieldAutocomplete->mentionChosen(
|
_fieldAutocomplete->mentionChosen(
|
||||||
) | rpl::start_with_next([=](FieldAutocomplete::MentionChosen data) {
|
) | rpl::start_with_next([=](FieldAutocomplete::MentionChosen data) {
|
||||||
insertMention(data.user);
|
auto replacement = QString();
|
||||||
|
auto entityTag = QString();
|
||||||
|
if (data.mention.isEmpty()) {
|
||||||
|
replacement = data.user->firstName;
|
||||||
|
if (replacement.isEmpty()) {
|
||||||
|
replacement = data.user->name();
|
||||||
|
}
|
||||||
|
entityTag = PrepareMentionTag(data.user);
|
||||||
|
} else {
|
||||||
|
replacement = '@' + data.mention;
|
||||||
|
}
|
||||||
|
_field->insertTag(replacement, entityTag);
|
||||||
}, lifetime());
|
}, lifetime());
|
||||||
|
|
||||||
_fieldAutocomplete->hashtagChosen(
|
_fieldAutocomplete->hashtagChosen(
|
||||||
|
@ -1331,20 +1342,6 @@ void HistoryWidget::start() {
|
||||||
session().data().stickers().notifySavedGifsUpdated();
|
session().data().stickers().notifySavedGifsUpdated();
|
||||||
}
|
}
|
||||||
|
|
||||||
void HistoryWidget::insertMention(UserData *user) {
|
|
||||||
QString replacement, entityTag;
|
|
||||||
if (user->username().isEmpty()) {
|
|
||||||
replacement = user->firstName;
|
|
||||||
if (replacement.isEmpty()) {
|
|
||||||
replacement = user->name();
|
|
||||||
}
|
|
||||||
entityTag = PrepareMentionTag(user);
|
|
||||||
} else {
|
|
||||||
replacement = '@' + user->username();
|
|
||||||
}
|
|
||||||
_field->insertTag(replacement, entityTag);
|
|
||||||
}
|
|
||||||
|
|
||||||
void HistoryWidget::insertHashtagOrBotCommand(
|
void HistoryWidget::insertHashtagOrBotCommand(
|
||||||
QString str,
|
QString str,
|
||||||
FieldAutocomplete::ChooseMethod method) {
|
FieldAutocomplete::ChooseMethod method) {
|
||||||
|
|
|
@ -348,7 +348,6 @@ private:
|
||||||
void insertHashtagOrBotCommand(
|
void insertHashtagOrBotCommand(
|
||||||
QString str,
|
QString str,
|
||||||
FieldAutocomplete::ChooseMethod method);
|
FieldAutocomplete::ChooseMethod method);
|
||||||
void insertMention(UserData *user);
|
|
||||||
void cancelInlineBot();
|
void cancelInlineBot();
|
||||||
void saveDraft(bool delayed = false);
|
void saveDraft(bool delayed = false);
|
||||||
void saveCloudDraft();
|
void saveCloudDraft();
|
||||||
|
|
|
@ -1496,19 +1496,17 @@ void ComposeControls::initAutocomplete() {
|
||||||
_field->insertTag(string);
|
_field->insertTag(string);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const auto insertMention = [=](not_null<UserData*> user) {
|
|
||||||
if (user->username().isEmpty()) {
|
_autocomplete->mentionChosen(
|
||||||
|
) | rpl::start_with_next([=](FieldAutocomplete::MentionChosen data) {
|
||||||
|
const auto user = data.user;
|
||||||
|
if (data.mention.isEmpty()) {
|
||||||
_field->insertTag(
|
_field->insertTag(
|
||||||
user->firstName.isEmpty() ? user->name() : user->firstName,
|
user->firstName.isEmpty() ? user->name() : user->firstName,
|
||||||
PrepareMentionTag(user));
|
PrepareMentionTag(user));
|
||||||
} else {
|
} else {
|
||||||
_field->insertTag('@' + user->username());
|
_field->insertTag('@' + data.mention);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
_autocomplete->mentionChosen(
|
|
||||||
) | rpl::start_with_next([=](FieldAutocomplete::MentionChosen data) {
|
|
||||||
insertMention(data.user);
|
|
||||||
}, _autocomplete->lifetime());
|
}, _autocomplete->lifetime());
|
||||||
|
|
||||||
_autocomplete->hashtagChosen(
|
_autocomplete->hashtagChosen(
|
||||||
|
|
Loading…
Add table
Reference in a new issue