Remove getApp and getSettings calls from Message-Rendering (#4535)

* refactor: remove singletons from message rendering

* chore: add changelog entry

* Disable the `cppcoreguidelines-avoid-const-or-ref-data-members` clang-tidy check

* auto *app

* Selection is a struct, not a class

* Use ChannelView's `signalHolder_` instead of `channelConnections_`

* Remove `applySettings` step, instead just connect & set each setting individually

* rename & constify some context values

* Handle empty "last message color" setting value better (as it was
        originally in this pr before I removed that change :-)

* unrelated mini refactor cleanup

* let painSelection handle size_t instead of int

* Add some more comments to the MessageLayoutContext structs

---------

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
nerix
2023-07-30 14:52:39 +02:00
committed by GitHub
parent 95aee044e2
commit 71594ad0d8
14 changed files with 312 additions and 131 deletions
+38 -17
View File
@@ -11,12 +11,14 @@
#include "messages/Emote.hpp"
#include "messages/Image.hpp"
#include "messages/layouts/MessageLayout.hpp"
#include "messages/layouts/MessageLayoutContext.hpp"
#include "messages/layouts/MessageLayoutElement.hpp"
#include "messages/LimitedQueueSnapshot.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "messages/MessageElement.hpp"
#include "messages/MessageThread.hpp"
#include "providers/colors/ColorProvider.hpp"
#include "providers/LinkResolver.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
@@ -61,7 +63,6 @@
#include <functional>
#include <memory>
#define DRAW_WIDTH (this->width())
#define SELECTION_RESUME_SCROLLING_MSG_THRESHOLD 3
#define CHAT_HOVER_PAUSE_DURATION 1000
#define TOOLTIP_EMOTE_ENTRIES_LIMIT 7
@@ -201,6 +202,10 @@ ChannelView::ChannelView(BaseWidget *parent, Split *split, Context context,
auto curve = QEasingCurve();
curve.setCustomType(highlightEasingFunction);
this->highlightAnimation_.setEasingCurve(curve);
this->messageColors_.applyTheme(getTheme());
this->messagePreferences_.connectSettings(getSettings(),
this->signalHolder_);
}
void ChannelView::initializeLayout()
@@ -378,6 +383,7 @@ void ChannelView::themeChangedEvent()
this->setupHighlightAnimationColors();
this->queueLayout();
this->messageColors_.applyTheme(getTheme());
}
void ChannelView::setupHighlightAnimationColors()
@@ -1247,32 +1253,47 @@ void ChannelView::drawMessages(QPainter &painter)
return;
}
int y = int(-(messagesSnapshot[start].get()->getHeight() *
(fmod(this->scrollBar_->getRelativeCurrentValue(), 1))));
MessageLayout *end = nullptr;
bool windowFocused = this->window() == QApplication::activeWindow();
auto app = getApp();
bool isMentions = this->underlyingChannel_ == app->twitch->mentionsChannel;
MessagePaintContext ctx = {
.painter = painter,
.selection = this->selection_,
.colorProvider = ColorProvider::instance(),
.messageColors = this->messageColors_,
.preferences = this->messagePreferences_,
for (size_t i = start; i < messagesSnapshot.size(); ++i)
.canvasWidth = this->width(),
.isWindowFocused = this->window() == QApplication::activeWindow(),
.isMentions =
this->underlyingChannel_ == getApp()->twitch->mentionsChannel,
.y = int(-(messagesSnapshot[start]->getHeight() *
(fmod(this->scrollBar_->getRelativeCurrentValue(), 1)))),
.messageIndex = start,
.isLastReadMessage = false,
};
bool showLastMessageIndicator = getSettings()->showLastMessageIndicator;
for (; ctx.messageIndex < messagesSnapshot.size(); ++ctx.messageIndex)
{
MessageLayout *layout = messagesSnapshot[i].get();
MessageLayout *layout = messagesSnapshot[ctx.messageIndex].get();
bool isLastMessage = false;
if (getSettings()->showLastMessageIndicator)
if (showLastMessageIndicator)
{
isLastMessage = this->lastReadMessage_.get() == layout;
ctx.isLastReadMessage = this->lastReadMessage_.get() == layout;
}
else
{
ctx.isLastReadMessage = false;
}
layout->paint(painter, DRAW_WIDTH, y, i, this->selection_,
isLastMessage, windowFocused, isMentions);
layout->paint(ctx);
if (this->highlightedMessage_ == layout)
{
painter.fillRect(
0, y, layout->getWidth(), layout->getHeight(),
0, ctx.y, layout->getWidth(), layout->getHeight(),
this->highlightAnimation_.currentValue().value<QColor>());
if (this->highlightAnimation_.state() == QVariantAnimation::Stopped)
{
@@ -1280,10 +1301,10 @@ void ChannelView::drawMessages(QPainter &painter)
}
}
y += layout->getHeight();
ctx.y += layout->getHeight();
end = layout;
if (y > this->height())
if (ctx.y > this->height())
{
break;
}