Skip to content

Commit

Permalink
refactor: remove singletons from message rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed Apr 11, 2023
1 parent 610394d commit 7189e77
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 105 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ set(SOURCE_FILES
messages/layouts/MessageLayout.hpp
messages/layouts/MessageLayoutContainer.cpp
messages/layouts/MessageLayoutContainer.hpp
messages/layouts/MessageLayoutContext.cpp
messages/layouts/MessageLayoutContext.hpp
messages/layouts/MessageLayoutElement.cpp
messages/layouts/MessageLayoutElement.hpp
messages/search/AuthorPredicate.cpp
Expand Down
117 changes: 52 additions & 65 deletions src/messages/layouts/MessageLayout.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#include "messages/layouts/MessageLayout.hpp"

#include "Application.hpp"
#include "debug/Benchmark.hpp"
#include "messages/layouts/MessageLayoutContainer.hpp"
#include "messages/layouts/MessageLayoutContext.hpp"
#include "messages/layouts/MessageLayoutElement.hpp"
#include "messages/Message.hpp"
#include "messages/MessageElement.hpp"
#include "messages/Selection.hpp"
#include "providers/colors/ColorProvider.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/Settings.hpp"
#include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp"
#include "util/DebugCount.hpp"
#include "util/StreamerMode.hpp"
Expand Down Expand Up @@ -198,84 +196,77 @@ void MessageLayout::actuallyLayout(int width, MessageElementFlags flags)
}

// Painting
void MessageLayout::paint(QPainter &painter, int width, int y, int messageIndex,
Selection &selection, bool isLastReadMessage,
bool isWindowFocused, bool isMentions)
void MessageLayout::paint(const MessagePaintContext &ctx)
{
auto app = getApp();
QPixmap *pixmap = this->ensureBuffer(painter, width);
QPixmap *pixmap = this->ensureBuffer(ctx.painter, ctx.width);

if (!this->bufferValid_ || !selection.isEmpty())
if (!this->bufferValid_ || !ctx.selection.isEmpty())
{
this->updateBuffer(pixmap, messageIndex, selection);
this->updateBuffer(pixmap, ctx);
}

// draw on buffer
painter.drawPixmap(0, y, *pixmap);
// painter.drawPixmap(0, y, this->container.width,
// this->container.getHeight(), *pixmap);
ctx.painter.drawPixmap(0, ctx.y, *pixmap);

// draw gif emotes
this->container_.paintAnimatedElements(painter, y);
this->container_.paintAnimatedElements(ctx.painter, ctx.y);

// draw disabled
if (this->message_->flags.has(MessageFlag::Disabled))
{
painter.fillRect(0, y, pixmap->width(), pixmap->height(),
app->themes->messages.disabled);
// painter.fillRect(0, y, pixmap->width(), pixmap->height(),
// QBrush(QColor(64, 64, 64, 64)));
ctx.painter.fillRect(0, ctx.y, pixmap->width(), pixmap->height(),
ctx.messageColors.disabled);
}

if (this->message_->flags.has(MessageFlag::RecentMessage))
{
painter.fillRect(0, y, pixmap->width(), pixmap->height(),
app->themes->messages.disabled);
ctx.painter.fillRect(0, ctx.y, pixmap->width(), pixmap->height(),
ctx.messageColors.disabled);
}

if (!isMentions &&
if (!ctx.isMentions &&
(this->message_->flags.has(MessageFlag::RedeemedChannelPointReward) ||
this->message_->flags.has(MessageFlag::RedeemedHighlight)) &&
getSettings()->enableRedeemedHighlight.getValue())
ctx.preferences.enableRedeemedHighlight)
{
painter.fillRect(
0, y, this->scale_ * 4, pixmap->height(),
ctx.painter.fillRect(
0, ctx.y, int(this->scale_ * 4), pixmap->height(),
*ColorProvider::instance().color(ColorType::RedeemedHighlight));
}

// draw selection
if (!selection.isEmpty())
if (!ctx.selection.isEmpty())
{
this->container_.paintSelection(painter, messageIndex, selection, y);
this->container_.paintSelection(ctx.painter, ctx.messageIndex,
ctx.selection, ctx.y);
}

// draw message seperation line
if (getSettings()->separateMessages.getValue())
if (ctx.preferences.separateMessages)
{
painter.fillRect(0, y, this->container_.getWidth() + 64, 1,
app->themes->splits.messageSeperator);
ctx.painter.fillRect(0, ctx.y, this->container_.getWidth() + 64, 1,
ctx.messageColors.messageSeperator);
}

// draw last read message line
if (isLastReadMessage)
if (ctx.isLastReadMessage)
{
QColor color;
if (getSettings()->lastMessageColor != QStringLiteral(""))
if (ctx.preferences.lastMessageColor.isValid())
{
color = QColor(getSettings()->lastMessageColor.getValue());
color = ctx.preferences.lastMessageColor;
}
else
{
color = isWindowFocused
? app->themes->tabs.selected.backgrounds.regular
: app->themes->tabs.selected.backgrounds.unfocused;
color = ctx.isWindowFocused
? ctx.messageColors.focusedLastMessageLine
: ctx.messageColors.unfocusedLastMessageLine;
}

QBrush brush(color, static_cast<Qt::BrushStyle>(
getSettings()->lastMessagePattern.getValue()));
QBrush brush(color, ctx.preferences.lastMessagePattern);

painter.fillRect(0, y + this->container_.getHeight() - 1,
pixmap->width(), 1, brush);
ctx.painter.fillRect(0, ctx.y + this->container_.getHeight() - 1,
pixmap->width(), 1, brush);
}

this->bufferValid_ = true;
Expand Down Expand Up @@ -305,45 +296,42 @@ QPixmap *MessageLayout::ensureBuffer(QPainter &painter, int width)
return this->buffer_.get();
}

void MessageLayout::updateBuffer(QPixmap *buffer, int /*messageIndex*/,
Selection & /*selection*/)
void MessageLayout::updateBuffer(QPixmap *buffer,
const MessagePaintContext &ctx)
{
if (buffer->isNull())
{
return;

auto app = getApp();
auto settings = getSettings();
}

QPainter painter(buffer);
painter.setRenderHint(QPainter::SmoothPixmapTransform);

// draw background
QColor backgroundColor = [this, &app] {
if (getSettings()->alternateMessages.getValue() &&
QColor backgroundColor = [&] {
if (ctx.preferences.alternateMessages &&
this->flags.has(MessageLayoutFlag::AlternateBackground))
{
return app->themes->messages.backgrounds.alternate;
}
else
{
return app->themes->messages.backgrounds.regular;
return ctx.messageColors.alternate;
}

return ctx.messageColors.regular;
}();

if (this->message_->flags.has(MessageFlag::ElevatedMessage) &&
getSettings()->enableElevatedMessageHighlight.getValue())
ctx.preferences.enableElevatedMessageHighlight)
{
backgroundColor = blendColors(backgroundColor,
*ColorProvider::instance().color(
ColorType::ElevatedMessageHighlight));
backgroundColor = blendColors(
backgroundColor,
*ctx.colorProvider.color(ColorType::ElevatedMessageHighlight));
}

else if (this->message_->flags.has(MessageFlag::FirstMessage) &&
getSettings()->enableFirstMessageHighlight.getValue())
ctx.preferences.enableFirstMessageHighlight)
{
backgroundColor = blendColors(
backgroundColor,
*ColorProvider::instance().color(ColorType::FirstMessageHighlight));
*ctx.colorProvider.color(ColorType::FirstMessageHighlight));
}
else if ((this->message_->flags.has(MessageFlag::Highlighted) ||
this->message_->flags.has(MessageFlag::HighlightedWhisper)) &&
Expand All @@ -354,22 +342,21 @@ void MessageLayout::updateBuffer(QPixmap *buffer, int /*messageIndex*/,
blendColors(backgroundColor, *this->message_->highlightColor);
}
else if (this->message_->flags.has(MessageFlag::Subscription) &&
getSettings()->enableSubHighlight)
ctx.preferences.enableSubHighlight)
{
// Blend highlight color with usual background color
backgroundColor = blendColors(
backgroundColor,
*ColorProvider::instance().color(ColorType::Subscription));
backgroundColor, *ctx.colorProvider.color(ColorType::Subscription));
}
else if ((this->message_->flags.has(MessageFlag::RedeemedHighlight) ||
this->message_->flags.has(
MessageFlag::RedeemedChannelPointReward)) &&
settings->enableRedeemedHighlight.getValue())
ctx.preferences.enableRedeemedHighlight)
{
// Blend highlight color with usual background color
backgroundColor = blendColors(
backgroundColor,
*ColorProvider::instance().color(ColorType::RedeemedHighlight));
backgroundColor =
blendColors(backgroundColor,
*ctx.colorProvider.color(ColorType::RedeemedHighlight));
}
else if (this->message_->flags.has(MessageFlag::AutoMod))
{
Expand All @@ -383,7 +370,7 @@ void MessageLayout::updateBuffer(QPixmap *buffer, int /*messageIndex*/,
painter.fillRect(buffer->rect(), backgroundColor);

// draw message
this->container_.paintElements(painter);
this->container_.paintElements(painter, ctx);

#ifdef FOURTF
// debug
Expand Down
7 changes: 3 additions & 4 deletions src/messages/layouts/MessageLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using MessagePtr = std::shared_ptr<const Message>;
struct Selection;
struct MessageLayoutContainer;
class MessageLayoutElement;
struct MessagePaintContext;

enum class MessageElementFlag : int64_t;
using MessageElementFlags = FlagsEnum<MessageElementFlag>;
Expand Down Expand Up @@ -49,9 +50,7 @@ class MessageLayout : boost::noncopyable
bool layout(int width, float scale_, MessageElementFlags flags);

// Painting
void paint(QPainter &painter, int width, int y, int messageIndex,
Selection &selection, bool isLastReadMessage,
bool isWindowFocused, bool isMentions);
void paint(const MessagePaintContext &ctx);
void invalidateBuffer();
void deleteBuffer();
void deleteCache();
Expand All @@ -72,7 +71,7 @@ class MessageLayout : boost::noncopyable
private:
// methods
void actuallyLayout(int width, MessageElementFlags flags);
void updateBuffer(QPixmap *pixmap, int messageIndex, Selection &selection);
void updateBuffer(QPixmap *buffer, const MessagePaintContext &ctx);

// Create new buffer if required, returning the buffer
QPixmap *ensureBuffer(QPainter &painter, int width);
Expand Down
9 changes: 6 additions & 3 deletions src/messages/layouts/MessageLayoutContainer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "MessageLayoutContainer.hpp"

#include "Application.hpp"
#include "messages/layouts/MessageLayoutContext.hpp"
#include "messages/layouts/MessageLayoutElement.hpp"
#include "messages/Message.hpp"
#include "messages/MessageElement.hpp"
Expand Down Expand Up @@ -492,7 +493,8 @@ MessageLayoutElement *MessageLayoutContainer::getElementAt(QPoint point)
}

// painting
void MessageLayoutContainer::paintElements(QPainter &painter)
void MessageLayoutContainer::paintElements(QPainter &painter,
const MessagePaintContext &ctx)
{
for (const std::unique_ptr<MessageLayoutElement> &element : this->elements_)
{
Expand All @@ -501,7 +503,7 @@ void MessageLayoutContainer::paintElements(QPainter &painter)
painter.drawRect(element->getRect());
#endif

element->paint(painter);
element->paint(painter, ctx.messageColors);
}
}

Expand All @@ -515,7 +517,8 @@ void MessageLayoutContainer::paintAnimatedElements(QPainter &painter,
}

void MessageLayoutContainer::paintSelection(QPainter &painter, int messageIndex,
Selection &selection, int yOffset)
const Selection &selection,
int yOffset)
{
auto app = getApp();
QColor selectionColor = app->themes->messages.selection;
Expand Down
5 changes: 3 additions & 2 deletions src/messages/layouts/MessageLayoutContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum class FirstWord { Neutral, RTL, LTR };
using MessageFlags = FlagsEnum<MessageFlag>;
class MessageLayoutElement;
struct Selection;
struct MessagePaintContext;

struct Margin {
int top;
Expand Down Expand Up @@ -73,10 +74,10 @@ struct MessageLayoutContainer {
MessageLayoutElement *getElementAt(QPoint point);

// painting
void paintElements(QPainter &painter);
void paintElements(QPainter &painter, const MessagePaintContext &ctx);
void paintAnimatedElements(QPainter &painter, int yOffset);
void paintSelection(QPainter &painter, int messageIndex,
Selection &selection, int yOffset);
const Selection &selection, int yOffset);

// selection
int getSelectionIndex(QPoint point);
Expand Down
60 changes: 60 additions & 0 deletions src/messages/layouts/MessageLayoutContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "messages/layouts/MessageLayoutContext.hpp"

#include "singletons/Settings.hpp"
#include "singletons/Theme.hpp"

namespace chatterino {

void MessageColors::applyTheme(Theme *theme)
{
this->regular = theme->messages.backgrounds.regular;
this->alternate = theme->messages.backgrounds.alternate;

this->disabled = theme->messages.disabled;
this->selection = theme->messages.selection;
this->system = theme->messages.textColors.system;

this->messageSeperator = theme->splits.messageSeperator;

this->focusedLastMessageLine = theme->tabs.selected.backgrounds.regular;
this->unfocusedLastMessageLine = theme->tabs.selected.backgrounds.unfocused;
}

void MessagePreferences::applySettings(Settings *settings)
{
this->enableRedeemedHighlight = settings->enableRedeemedHighlight;
this->enableElevatedMessageHighlight =
settings->enableElevatedMessageHighlight;
this->enableFirstMessageHighlight = settings->enableFirstMessageHighlight;
this->enableSubHighlight = settings->enableSubHighlight;

this->alternateMessages = settings->alternateMessages;
this->separateMessages = settings->separateMessages;

this->lastMessageColor = settings->lastMessageColor.getValue().isEmpty()
? QColor()
: QColor(settings->lastMessageColor);
this->lastMessagePattern =
static_cast<Qt::BrushStyle>(settings->lastMessagePattern);
}

void MessagePreferences::connectSettings(Settings *settings,
pajlada::Signals::SignalHolder &holder)
{
const auto apply = [this, settings]() {
this->applySettings(settings);
};
const auto connect = [&](auto &setting) {
setting.connect(apply, holder, false);
};
connect(settings->enableRedeemedHighlight);
connect(settings->enableElevatedMessageHighlight);
connect(settings->enableFirstMessageHighlight);
connect(settings->enableSubHighlight);
connect(settings->alternateMessages);
connect(settings->separateMessages);
connect(settings->lastMessageColor);
connect(settings->lastMessagePattern);
}

} // namespace chatterino
Loading

0 comments on commit 7189e77

Please sign in to comment.