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

Remove getApp and getSettings calls from Message-Rendering #4535

Merged
merged 13 commits into from
Jul 30, 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 .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Checks: "-*,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-avoid-const-or-ref-data-members,
-readability-magic-numbers,
-performance-noexcept-move-constructor,
-misc-non-private-member-variables-in-classes,
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- Dev: Moved preprocessor Git and date definitions to executables only. (#4681)
- Dev: Refactored tests to be able to use `ctest` and run in debug builds. (#4700)
- Dev: Added the ability to use an alternate linker using the `-DUSE_ALTERNATE_LINKER=...` CMake parameter. (#4711)
- Dev: Removed `getApp` and `getSettings` calls from message rendering. (#4535)

## 2.4.4

Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,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
2 changes: 0 additions & 2 deletions src/common/Literals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace chatterino::literals {
namespace detail {
// NOLINTBEGIN(modernize-avoid-c-arrays)
// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays)
// NOLINTBEGIN(cppcoreguidelines-avoid-const-or-ref-data-members)

template <size_t N>
struct LiteralResolver {
Expand Down Expand Up @@ -95,7 +94,6 @@ namespace detail {
}
};

// NOLINTEND(cppcoreguidelines-avoid-const-or-ref-data-members)
// NOLINTEND(cppcoreguidelines-avoid-c-arrays)
// NOLINTEND(modernize-avoid-c-arrays)

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.canvasWidth);

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
Loading
Loading