Skip to content

Commit

Permalink
Merge chatterino2 into chatterino7
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed Dec 25, 2023
2 parents bfe118c + 1006bf9 commit 020d7ed
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
- Dev: BREAKING: Replace custom `import()` with normal Lua `require()`. (#5014)
- Dev: Fixed most compiler warnings. (#5028)
- Dev: Added the ability to show `ChannelView`s without a `Split`. (#4747)
- Dev: Channels without any animated elements on screen will skip updates from the GIF timer. (#5042)

## 2.4.6

Expand Down
9 changes: 7 additions & 2 deletions src/messages/layouts/MessageLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ void MessageLayout::actuallyLayout(int width, MessageElementFlags flags)
}

// Painting
void MessageLayout::paint(const MessagePaintContext &ctx)
MessagePaintResult MessageLayout::paint(const MessagePaintContext &ctx)
{
MessagePaintResult result;

QPixmap *pixmap = this->ensureBuffer(ctx.painter, ctx.canvasWidth);

if (!this->bufferValid_)
Expand All @@ -209,7 +211,8 @@ void MessageLayout::paint(const MessagePaintContext &ctx)
ctx.painter.drawPixmap(0, ctx.y, *pixmap);

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

// draw disabled
if (this->message_->flags.has(MessageFlag::Disabled))
Expand Down Expand Up @@ -270,6 +273,8 @@ void MessageLayout::paint(const MessagePaintContext &ctx)
}

this->bufferValid_ = true;

return result;
}

QPixmap *MessageLayout::ensureBuffer(QPainter &painter, int width)
Expand Down
6 changes: 5 additions & 1 deletion src/messages/layouts/MessageLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ enum class MessageLayoutFlag : uint8_t {
};
using MessageLayoutFlags = FlagsEnum<MessageLayoutFlag>;

struct MessagePaintResult {
bool hasAnimatedElements = false;
};

class MessageLayout
{
public:
Expand All @@ -55,7 +59,7 @@ class MessageLayout
bool layout(int width, float scale_, MessageElementFlags flags);

// Painting
void paint(const MessagePaintContext &ctx);
MessagePaintResult paint(const MessagePaintContext &ctx);
void invalidateBuffer();
void deleteBuffer();
void deleteCache();
Expand Down
6 changes: 4 additions & 2 deletions src/messages/layouts/MessageLayoutContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,15 @@ void MessageLayoutContainer::paintElements(QPainter &painter,
}
}

void MessageLayoutContainer::paintAnimatedElements(QPainter &painter,
bool MessageLayoutContainer::paintAnimatedElements(QPainter &painter,
int yOffset) const
{
bool anyAnimatedElement = false;
for (const auto &element : this->elements_)
{
element->paintAnimated(painter, yOffset);
anyAnimatedElement |= element->paintAnimated(painter, yOffset);
}
return anyAnimatedElement;
}

void MessageLayoutContainer::paintSelection(QPainter &painter,
Expand Down
3 changes: 2 additions & 1 deletion src/messages/layouts/MessageLayoutContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ struct MessageLayoutContainer {

/**
* Paint the animated elements in this message
* @returns true if this container contains at least one animated element
*/
void paintAnimatedElements(QPainter &painter, int yOffset) const;
bool paintAnimatedElements(QPainter &painter, int yOffset) const;

/**
* Paint the selection for this container
Expand Down
26 changes: 19 additions & 7 deletions src/messages/layouts/MessageLayoutElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ void ImageLayoutElement::paint(QPainter &painter,
}
}

void ImageLayoutElement::paintAnimated(QPainter &painter, int yOffset)
bool ImageLayoutElement::paintAnimated(QPainter &painter, int yOffset)
{
if (this->image_ == nullptr)
{
return;
return false;
}

if (this->image_->animated())
Expand All @@ -174,8 +174,10 @@ void ImageLayoutElement::paintAnimated(QPainter &painter, int yOffset)
auto rect = this->getRect();
rect.moveTop(rect.y() + yOffset);
painter.drawPixmap(QRectF(rect), *pixmap, QRectF());
return true;
}
}
return false;
}

int ImageLayoutElement::getMouseOverIndex(const QPoint &abs) const
Expand Down Expand Up @@ -272,7 +274,7 @@ void LayeredImageLayoutElement::paint(QPainter &painter,
}
}

void LayeredImageLayoutElement::paintAnimated(QPainter &painter, int yOffset)
bool LayeredImageLayoutElement::paintAnimated(QPainter &painter, int yOffset)
{
auto fullRect = QRectF(this->getRect());
fullRect.moveTop(fullRect.y() + yOffset);
Expand Down Expand Up @@ -304,6 +306,7 @@ void LayeredImageLayoutElement::paintAnimated(QPainter &painter, int yOffset)
}
}
}
return animatedFlag;
}

int LayeredImageLayoutElement::getMouseOverIndex(const QPoint &abs) const
Expand Down Expand Up @@ -479,10 +482,12 @@ void TextLayoutElement::paint(QPainter &painter,
}
}

void TextLayoutElement::paintAnimated(QPainter &painter, const int yOffset)
bool TextLayoutElement::paintAnimated(QPainter &painter, const int yOffset)
{
if (this->getRect().isEmpty())
return;
{
return false;
}

const auto font = getApp()->getFonts()->getFont(this->style_, this->scale_);

Expand All @@ -505,7 +510,10 @@ void TextLayoutElement::paintAnimated(QPainter &painter, const int yOffset)
auto rect = this->getRect();
rect.moveTop(rect.y() + yOffset);
painter.drawPixmap(rect, paintPixmap, QRectF());
return true;
}

return false;
}

int TextLayoutElement::getMouseOverIndex(const QPoint &abs) const
Expand Down Expand Up @@ -625,8 +633,10 @@ void TextIconLayoutElement::paint(QPainter &painter,
}
}

void TextIconLayoutElement::paintAnimated(QPainter &painter, int yOffset)
bool TextIconLayoutElement::paintAnimated(QPainter & /*painter*/,
int /*yOffset*/)
{
return false;
}

int TextIconLayoutElement::getMouseOverIndex(const QPoint &abs) const
Expand Down Expand Up @@ -698,8 +708,10 @@ void ReplyCurveLayoutElement::paint(QPainter &painter,
painter.drawPath(path);
}

void ReplyCurveLayoutElement::paintAnimated(QPainter &painter, int yOffset)
bool ReplyCurveLayoutElement::paintAnimated(QPainter & /*painter*/,
int /*yOffset*/)
{
return false;
}

int ReplyCurveLayoutElement::getMouseOverIndex(const QPoint &abs) const
Expand Down
13 changes: 7 additions & 6 deletions src/messages/layouts/MessageLayoutElement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class MessageLayoutElement
virtual size_t getSelectionIndexCount() const = 0;
virtual void paint(QPainter &painter,
const MessageColors &messageColors) = 0;
virtual void paintAnimated(QPainter &painter, int yOffset) = 0;
/// @returns true if anything was painted
virtual bool paintAnimated(QPainter &painter, int yOffset) = 0;
virtual int getMouseOverIndex(const QPoint &abs) const = 0;
virtual int getXFromIndex(size_t index) = 0;

Expand Down Expand Up @@ -86,7 +87,7 @@ class ImageLayoutElement : public MessageLayoutElement
uint32_t to = UINT32_MAX) const override;
size_t getSelectionIndexCount() const override;
void paint(QPainter &painter, const MessageColors &messageColors) override;
void paintAnimated(QPainter &painter, int yOffset) override;
bool paintAnimated(QPainter &painter, int yOffset) override;
int getMouseOverIndex(const QPoint &abs) const override;
int getXFromIndex(size_t index) override;

Expand All @@ -105,7 +106,7 @@ class LayeredImageLayoutElement : public MessageLayoutElement
uint32_t to = UINT32_MAX) const override;
size_t getSelectionIndexCount() const override;
void paint(QPainter &painter, const MessageColors &messageColors) override;
void paintAnimated(QPainter &painter, int yOffset) override;
bool paintAnimated(QPainter &painter, int yOffset) override;
int getMouseOverIndex(const QPoint &abs) const override;
int getXFromIndex(size_t index) override;

Expand Down Expand Up @@ -158,7 +159,7 @@ class TextLayoutElement : public MessageLayoutElement
uint32_t to = UINT32_MAX) const override;
size_t getSelectionIndexCount() const override;
void paint(QPainter &painter, const MessageColors &messageColors) override;
void paintAnimated(QPainter &painter, int yOffset) override;
bool paintAnimated(QPainter &painter, int yOffset) override;
int getMouseOverIndex(const QPoint &abs) const override;
int getXFromIndex(size_t index) override;

Expand All @@ -182,7 +183,7 @@ class TextIconLayoutElement : public MessageLayoutElement
uint32_t to = UINT32_MAX) const override;
size_t getSelectionIndexCount() const override;
void paint(QPainter &painter, const MessageColors &messageColors) override;
void paintAnimated(QPainter &painter, int yOffset) override;
bool paintAnimated(QPainter &painter, int yOffset) override;
int getMouseOverIndex(const QPoint &abs) const override;
int getXFromIndex(size_t index) override;

Expand All @@ -200,7 +201,7 @@ class ReplyCurveLayoutElement : public MessageLayoutElement

protected:
void paint(QPainter &painter, const MessageColors &messageColors) override;
void paintAnimated(QPainter &painter, int yOffset) override;
bool paintAnimated(QPainter &painter, int yOffset) override;
int getMouseOverIndex(const QPoint &abs) const override;
int getXFromIndex(size_t index) override;
void addCopyTextToString(QString &str, uint32_t from = 0,
Expand Down
10 changes: 8 additions & 2 deletions src/widgets/helper/ChannelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,10 @@ void ChannelView::initializeSignals()

this->signalHolder_.managedConnect(getApp()->windows->gifRepaintRequested,
[&] {
this->queueUpdate();
if (this->anyAnimationShown_)
{
this->queueUpdate();
}
});

this->signalHolder_.managedConnect(
Expand Down Expand Up @@ -1471,6 +1474,8 @@ void ChannelView::drawMessages(QPainter &painter)
};
bool showLastMessageIndicator = getSettings()->showLastMessageIndicator;

bool anyAnimation = false;

for (; ctx.messageIndex < messagesSnapshot.size(); ++ctx.messageIndex)
{
MessageLayout *layout = messagesSnapshot[ctx.messageIndex].get();
Expand All @@ -1484,7 +1489,7 @@ void ChannelView::drawMessages(QPainter &painter)
ctx.isLastReadMessage = false;
}

layout->paint(ctx);
anyAnimation |= layout->paint(ctx).hasAnimatedElements;

if (this->highlightedMessage_ == layout)
{
Expand All @@ -1505,6 +1510,7 @@ void ChannelView::drawMessages(QPainter &painter)
break;
}
}
this->anyAnimationShown_ = anyAnimation;

if (end == nullptr)
{
Expand Down
3 changes: 3 additions & 0 deletions src/widgets/helper/ChannelView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ class ChannelView final : public BaseWidget
bool lastMessageHasAlternateBackground_ = false;
bool lastMessageHasAlternateBackgroundReverse_ = true;

/// Tracks if this view has painted any animated element in the last #paintEvent().
bool anyAnimationShown_ = false;

bool pausable_ = false;
QTimer pauseTimer_;
std::unordered_map<PauseReason, std::optional<SteadyClock::time_point>>
Expand Down

0 comments on commit 020d7ed

Please sign in to comment.