Use QFontMetrics::elidedText over manual implementation (#4807)
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
## Unversioned
|
## Unversioned
|
||||||
|
|
||||||
|
- Bugfix: Fixed a performance issue when displaying replies to certain messages. (#4807)
|
||||||
- Bugfix: Fixed a data race when disconnecting from Twitch PubSub. (#4771)
|
- Bugfix: Fixed a data race when disconnecting from Twitch PubSub. (#4771)
|
||||||
- Bugfix: Fixed `/shoutout` command not working with usernames starting with @'s (e.g. `/shoutout @forsen`). (#4800)
|
- Bugfix: Fixed `/shoutout` command not working with usernames starting with @'s (e.g. `/shoutout @forsen`). (#4800)
|
||||||
- Dev: Fixed UTF16 encoding of `modes` file for the installer. (#4791)
|
- Dev: Fixed UTF16 encoding of `modes` file for the installer. (#4791)
|
||||||
|
|||||||
@@ -674,42 +674,16 @@ void SingleLineTextElement::addToContainer(MessageLayoutContainer &container,
|
|||||||
auto &parsedWord = parsedWords[0];
|
auto &parsedWord = parsedWords[0];
|
||||||
if (parsedWord.type() == typeid(QString))
|
if (parsedWord.type() == typeid(QString))
|
||||||
{
|
{
|
||||||
int nextWidth =
|
if (!currentText.isEmpty())
|
||||||
metrics.horizontalAdvance(currentText + word.text);
|
|
||||||
|
|
||||||
// see if the text fits in the current line
|
|
||||||
if (container.fitsInLine(nextWidth))
|
|
||||||
{
|
{
|
||||||
currentText += (word.text + " ");
|
currentText += ' ';
|
||||||
}
|
}
|
||||||
else
|
currentText += word.text;
|
||||||
|
QString prev = currentText; // only increments the ref-count
|
||||||
|
currentText = metrics.elidedText(currentText, Qt::ElideRight,
|
||||||
|
container.remainingWidth());
|
||||||
|
if (currentText != prev)
|
||||||
{
|
{
|
||||||
// word overflows, try minimum truncation
|
|
||||||
bool cutSuccess = false;
|
|
||||||
for (size_t cut = 1; cut < word.text.length(); ++cut)
|
|
||||||
{
|
|
||||||
// Cut off n characters and append the ellipsis.
|
|
||||||
// Try removing characters one by one until the word fits.
|
|
||||||
QString truncatedWord =
|
|
||||||
word.text.chopped(cut) + ellipsis;
|
|
||||||
int newSize = metrics.horizontalAdvance(currentText +
|
|
||||||
truncatedWord);
|
|
||||||
if (container.fitsInLine(newSize))
|
|
||||||
{
|
|
||||||
currentText += (truncatedWord);
|
|
||||||
|
|
||||||
cutSuccess = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!cutSuccess)
|
|
||||||
{
|
|
||||||
// We weren't able to show any part of the current word, so
|
|
||||||
// just append the ellipsis.
|
|
||||||
currentText += ellipsis;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -424,11 +424,16 @@ bool MessageLayoutContainer::atStartOfLine()
|
|||||||
|
|
||||||
bool MessageLayoutContainer::fitsInLine(int _width)
|
bool MessageLayoutContainer::fitsInLine(int _width)
|
||||||
{
|
{
|
||||||
return this->currentX_ + _width <=
|
return _width <= this->remainingWidth();
|
||||||
(this->width_ - int(this->margin.left * this->scale_) -
|
}
|
||||||
|
|
||||||
|
int MessageLayoutContainer::remainingWidth() const
|
||||||
|
{
|
||||||
|
return (this->width_ - int(this->margin.left * this->scale_) -
|
||||||
int(this->margin.right * this->scale_) -
|
int(this->margin.right * this->scale_) -
|
||||||
(this->line_ + 1 == MAX_UNCOLLAPSED_LINES ? this->dotdotdotWidth_
|
(this->line_ + 1 == MAX_UNCOLLAPSED_LINES ? this->dotdotdotWidth_
|
||||||
: 0));
|
: 0)) -
|
||||||
|
this->currentX_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MessageLayoutContainer::end()
|
void MessageLayoutContainer::end()
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ struct MessageLayoutContainer {
|
|||||||
void breakLine();
|
void breakLine();
|
||||||
bool atStartOfLine();
|
bool atStartOfLine();
|
||||||
bool fitsInLine(int width_);
|
bool fitsInLine(int width_);
|
||||||
|
int remainingWidth() const;
|
||||||
// this method is called when a message has an RTL word
|
// this method is called when a message has an RTL word
|
||||||
// we need to reorder the words to be shown properly
|
// we need to reorder the words to be shown properly
|
||||||
// however we don't we to reorder non-text elements like badges, timestamps, username
|
// however we don't we to reorder non-text elements like badges, timestamps, username
|
||||||
|
|||||||
Reference in New Issue
Block a user