Slightly optimized replacement in send action text.

This commit is contained in:
23rd 2021-08-30 21:45:56 +03:00
parent 436212bb88
commit f7abd85761
3 changed files with 47 additions and 23 deletions

View file

@ -280,22 +280,24 @@ bool SendActionPainter::updateNeedsAnimating(crl::time now, bool force) {
return QString(); return QString();
}; };
for (const auto &[user, action] : _sendActions) { for (const auto &[user, action] : _sendActions) {
const auto isNamed = !_history->peer->isUser();
newTypingString = sendActionString( newTypingString = sendActionString(
action.type, action.type,
_history->peer->isUser() ? QString() : user->firstName); isNamed ? user->firstName : QString());
if (!newTypingString.isEmpty()) { if (!newTypingString.isEmpty()) {
_sendActionAnimation.start(action.type); _sendActionAnimation.start(action.type);
// Add an animation to the middle of text. // Add an animation to the middle of text.
using namespace Lang; const auto &lang = Lang::GetInstance();
if (GetInstance().supportChoosingStickerReplacement() if (lang.supportChoosingStickerReplacement()
&& (action.type == Type::ChooseSticker)) { && (action.type == Type::ChooseSticker)) {
const auto index = newTypingString.size()
const auto index = newTypingString.lastIndexOf( - lang.rightIndexChoosingStickerReplacement(
Lang::kChoosingStickerReplacement.utf8()); isNamed);
_animationLeft = (index == -1) _animationLeft = _st.font->width(
? 0 newTypingString,
: _st.font->width(newTypingString, 0, index); 0,
index);
if (!_spacesCount) { if (!_spacesCount) {
_spacesCount = std::ceil( _spacesCount = std::ceil(
@ -303,8 +305,10 @@ bool SendActionPainter::updateNeedsAnimating(crl::time now, bool force) {
/ _st.font->spacew); / _st.font->spacew);
} }
newTypingString = newTypingString.replace( newTypingString = newTypingString.replace(
Lang::kChoosingStickerReplacement.utf8(), index,
QString().fill(' ', _spacesCount)); Lang::kChoosingStickerReplacement.utf8().size(),
QString().fill(' ', _spacesCount).constData(),
_spacesCount);
} else { } else {
_animationLeft = 0; _animationLeft = 0;
} }

View file

@ -303,7 +303,7 @@ void Instance::reset(const Language &data) {
_values[i] = GetOriginalValue(ushort(i)); _values[i] = GetOriginalValue(ushort(i));
} }
ranges::fill(_nonDefaultSet, 0); ranges::fill(_nonDefaultSet, 0);
updateSupportChoosingStickerReplacement(); updateChoosingStickerReplacement();
_idChanges.fire_copy(_id); _idChanges.fire_copy(_id);
} }
@ -549,7 +549,7 @@ void Instance::fillFromSerialized(
applyValue(nonDefaultStrings[i], nonDefaultStrings[i + 1]); applyValue(nonDefaultStrings[i], nonDefaultStrings[i + 1]);
} }
updatePluralRules(); updatePluralRules();
updateSupportChoosingStickerReplacement(); updateChoosingStickerReplacement();
_idChanges.fire_copy(_id); _idChanges.fire_copy(_id);
} }
@ -574,7 +574,7 @@ void Instance::fillFromCustomContent(
_pluralId = PluralCodeForCustom(absolutePath, relativePath); _pluralId = PluralCodeForCustom(absolutePath, relativePath);
_name = _nativeName = QString(); _name = _nativeName = QString();
loadFromCustomContent(absolutePath, relativePath, content); loadFromCustomContent(absolutePath, relativePath, content);
updateSupportChoosingStickerReplacement(); updateChoosingStickerReplacement();
_idChanges.fire_copy(_id); _idChanges.fire_copy(_id);
} }
@ -605,18 +605,33 @@ bool Instance::loadFromCustomFile(const QString &filePath) {
return false; return false;
} }
void Instance::updateSupportChoosingStickerReplacement() { void Instance::updateChoosingStickerReplacement() {
// A language changing in the runtime is not supported. // A language changing in the runtime is not supported.
const auto replacement = kChoosingStickerReplacement.utf8();
const auto phrase = tr::lng_send_action_choose_sticker(tr::now); const auto phrase = tr::lng_send_action_choose_sticker(tr::now);
const auto first = phrase.indexOf(kChoosingStickerReplacement.utf8()); const auto first = phrase.indexOf(replacement);
const auto last = phrase.lastIndexOf(kChoosingStickerReplacement.utf8()); const auto support = (first != -1);
_supportChoosingStickerReplacement = (first == last) const auto phraseNamed = tr::lng_user_action_choose_sticker(
? (first != -1) tr::now,
: false; lt_user,
QString());
const auto firstNamed = phraseNamed.indexOf(replacement);
const auto supportNamed = (firstNamed != -1);
_choosingStickerReplacement.support = (supportNamed && support);
_choosingStickerReplacement.rightIndex = phrase.size() - first;
_choosingStickerReplacement.rightIndexNamed = phraseNamed.size()
- firstNamed;
} }
bool Instance::supportChoosingStickerReplacement() const { bool Instance::supportChoosingStickerReplacement() const {
return _supportChoosingStickerReplacement; return _choosingStickerReplacement.support;
}
int Instance::rightIndexChoosingStickerReplacement(bool named) const {
return named
? _choosingStickerReplacement.rightIndexNamed
: _choosingStickerReplacement.rightIndex;
} }
// SetCallback takes two QByteArrays: key, value. // SetCallback takes two QByteArrays: key, value.

View file

@ -77,6 +77,7 @@ public:
void fillFromSerialized(const QByteArray &data, int dataAppVersion); void fillFromSerialized(const QByteArray &data, int dataAppVersion);
bool supportChoosingStickerReplacement() const; bool supportChoosingStickerReplacement() const;
int rightIndexChoosingStickerReplacement(bool named) const;
void applyDifference( void applyDifference(
Pack pack, Pack pack,
@ -124,7 +125,7 @@ private:
const QString &relativePath, const QString &relativePath,
const QByteArray &content); const QByteArray &content);
void updatePluralRules(); void updatePluralRules();
void updateSupportChoosingStickerReplacement(); void updateChoosingStickerReplacement();
Instance *_derived = nullptr; Instance *_derived = nullptr;
@ -137,7 +138,11 @@ private:
int _version = 0; int _version = 0;
rpl::event_stream<> _updated; rpl::event_stream<> _updated;
bool _supportChoosingStickerReplacement; struct {
bool support = false;
int rightIndex = 0;
int rightIndexNamed = 0;
} _choosingStickerReplacement;
mutable QString _systemLanguage; mutable QString _systemLanguage;