Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display all parsed elements when parsing emojis in replies #4875

Merged
merged 7 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Bugfix: Fixed too much text being copied when copying chat messages. (#4812, #4830, #4839)
- Bugfix: Fixed empty page being added when showing out of bounds dialog. (#4849)
- Bugfix: Fixed issue on Windows preventing the title bar from being dragged in the top left corner. (#4873)
- Bugfix: Fixed an issue where reply context didn't render correctly if an emoji was touching text. (#4875)
- Bugfix: Fixed the input completion popup from disappearing when clicking on it on Windows and macOS. (#4876)
- Dev: Fixed UTF16 encoding of `modes` file for the installer. (#4791)
- Dev: Temporarily disable High DPI scaling on Qt6 builds on Windows. (#4767)
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ set(SOURCE_FILES
util/Twitch.cpp
util/Twitch.hpp
util/TypeName.hpp
util/Variant.hpp
util/WidgetHelpers.cpp
util/WidgetHelpers.hpp
util/WindowsHelper.cpp
Expand Down
88 changes: 45 additions & 43 deletions src/messages/MessageElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "singletons/Settings.hpp"
#include "singletons/Theme.hpp"
#include "util/DebugCount.hpp"
#include "util/Variant.hpp"

namespace chatterino {

Expand Down Expand Up @@ -665,55 +666,56 @@ void SingleLineTextElement::addToContainer(MessageLayoutContainer &container,
container.first = FirstWord::Neutral;
for (Word &word : this->words_)
{
auto parsedWords = app->emotes->emojis.parse(word.text);
if (parsedWords.size() == 0)
for (const auto &parsedWord : app->emotes->emojis.parse(word.text))
{
continue; // sanity check
}

auto &parsedWord = parsedWords[0];
if (parsedWord.type() == typeid(QString))
{
if (!currentText.isEmpty())
{
currentText += ' ';
}
currentText += word.text;
QString prev = currentText; // only increments the ref-count
currentText = metrics.elidedText(currentText, Qt::ElideRight,
container.remainingWidth());
if (currentText != prev)
if (parsedWord.type() == typeid(QString))
{
break;
if (!currentText.isEmpty())
{
currentText += ' ';
}
currentText += boost::get<QString>(parsedWord);
QString prev =
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
currentText; // only increments the ref-count
currentText =
metrics.elidedText(currentText, Qt::ElideRight,
container.remainingWidth());
if (currentText != prev)
{
break;
}
}
}
else if (parsedWord.type() == typeid(EmotePtr))
{
auto emote = boost::get<EmotePtr>(parsedWord);
auto image =
emote->images.getImageOrLoaded(container.getScale());
if (!image->isEmpty())
else if (parsedWord.type() == typeid(EmotePtr))
{
auto emoteScale = getSettings()->emoteScale.getValue();

int currentWidth = metrics.horizontalAdvance(currentText);
auto emoteSize = QSize(image->width(), image->height()) *
(emoteScale * container.getScale());

if (!container.fitsInLine(currentWidth + emoteSize.width()))
auto emote = boost::get<EmotePtr>(parsedWord);
auto image =
emote->images.getImageOrLoaded(container.getScale());
if (!image->isEmpty())
{
currentText += ellipsis;
break;
auto emoteScale = getSettings()->emoteScale.getValue();

int currentWidth =
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
metrics.horizontalAdvance(currentText);
auto emoteSize =
QSize(image->width(), image->height()) *
(emoteScale * container.getScale());

if (!container.fitsInLine(currentWidth +
emoteSize.width()))
{
currentText += ellipsis;
break;
}

// Add currently pending text to container, then add the emote after.
container.addElementNoLineBreak(getTextLayoutElement(
currentText, currentWidth, false));
currentText.clear();

container.addElementNoLineBreak(
(new ImageLayoutElement(*this, image, emoteSize))
->setLink(this->getLink()));
}

// Add currently pending text to container, then add the emote after.
container.addElementNoLineBreak(
getTextLayoutElement(currentText, currentWidth, false));
currentText.clear();

container.addElementNoLineBreak(
(new ImageLayoutElement(*this, image, emoteSize))
->setLink(this->getLink()));
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/util/Variant.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

namespace chatterino::variant {

/// Compile-time safe visitor for std and boost variants.
///
/// From https://en.cppreference.com/w/cpp/utility/variant/visit
///
/// Usage:
///
/// ```
/// std::variant<int, double> v;
/// std::visit(variant::Overloaded{
/// [](double) { qDebug() << "double"; },
/// [](int) { qDebug() << "int"; }
/// }, v);
/// ```
template <class... Ts>
struct Overloaded : Ts... {
using Ts::operator()...;
};

// Technically, we shouldn't need this, as we're on C++ 20,
// but not all of our compilers support CTAD for aggregates yet.
template <class... Ts>
Overloaded(Ts...) -> Overloaded<Ts...>;

} // namespace chatterino::variant