perf: Only update regions with animated elements (#5043)

This commit is contained in:
nerix
2023-12-27 01:12:14 +01:00
committed by GitHub
parent eb12cfa50b
commit 9612eac966
3 changed files with 72 additions and 24 deletions
+66 -20
View File
@@ -350,6 +350,10 @@ ChannelView::ChannelView(InternalCtor /*tag*/, QWidget *parent, Split *split,
auto curve = QEasingCurve();
curve.setCustomType(highlightEasingFunction);
this->highlightAnimation_.setEasingCurve(curve);
QObject::connect(&this->highlightAnimation_,
&QVariantAnimation::valueChanged, this, [this] {
this->queueUpdate();
});
this->messageColors_.applyTheme(getTheme());
this->messagePreferences_.connectSettings(getSettings(),
@@ -404,13 +408,13 @@ void ChannelView::initializeSignals()
},
this->signalHolder_);
this->signalHolder_.managedConnect(getApp()->windows->gifRepaintRequested,
[&] {
if (this->anyAnimationShown_)
{
this->queueUpdate();
}
});
this->signalHolder_.managedConnect(
getApp()->windows->gifRepaintRequested, [&] {
if (!this->animationArea_.isEmpty())
{
this->queueUpdate(this->animationArea_);
}
});
this->signalHolder_.managedConnect(
getApp()->windows->layoutRequested, [&](Channel *channel) {
@@ -580,6 +584,11 @@ void ChannelView::queueUpdate()
this->update();
}
void ChannelView::queueUpdate(const QRect &area)
{
this->update(area);
}
void ChannelView::queueLayout()
{
if (this->isVisible())
@@ -1418,7 +1427,7 @@ void ChannelView::scrollToMessageLayout(MessageLayout *layout,
}
}
void ChannelView::paintEvent(QPaintEvent * /*event*/)
void ChannelView::paintEvent(QPaintEvent *event)
{
// BenchmarkGuard benchmark("paint");
@@ -1427,7 +1436,7 @@ void ChannelView::paintEvent(QPaintEvent * /*event*/)
painter.fillRect(rect(), this->theme->splits.background);
// draw messages
this->drawMessages(painter);
this->drawMessages(painter, event->rect());
// draw paused sign
if (this->paused())
@@ -1441,7 +1450,7 @@ void ChannelView::paintEvent(QPaintEvent * /*event*/)
// if overlays is false then it draws the message, if true then it draws things
// such as the grey overlay when a message is disabled
void ChannelView::drawMessages(QPainter &painter)
void ChannelView::drawMessages(QPainter &painter, const QRect &area)
{
auto &messagesSnapshot = this->getMessagesSnapshot();
@@ -1474,7 +1483,10 @@ void ChannelView::drawMessages(QPainter &painter)
};
bool showLastMessageIndicator = getSettings()->showLastMessageIndicator;
bool anyAnimation = false;
QRect animationArea;
auto areaContainsY = [&area](auto y) {
return y >= area.y() && y < area.y() + area.height();
};
for (; ctx.messageIndex < messagesSnapshot.size(); ++ctx.messageIndex)
{
@@ -1489,16 +1501,34 @@ void ChannelView::drawMessages(QPainter &painter)
ctx.isLastReadMessage = false;
}
anyAnimation |= layout->paint(ctx).hasAnimatedElements;
if (this->highlightedMessage_ == layout)
if (areaContainsY(ctx.y) || areaContainsY(ctx.y + layout->getHeight()))
{
painter.fillRect(
0, ctx.y, layout->getWidth(), layout->getHeight(),
this->highlightAnimation_.currentValue().value<QColor>());
if (this->highlightAnimation_.state() == QVariantAnimation::Stopped)
auto paintResult = layout->paint(ctx);
if (paintResult.hasAnimatedElements)
{
this->highlightedMessage_ = nullptr;
if (animationArea.isNull())
{
animationArea = QRect{0, ctx.y, layout->getWidth(),
layout->getHeight()};
}
else
{
animationArea.setBottom(ctx.y + layout->getHeight());
animationArea.setWidth(
std::max(layout->getWidth(), animationArea.width()));
}
}
if (this->highlightedMessage_ == layout)
{
painter.fillRect(
0, ctx.y, layout->getWidth(), layout->getHeight(),
this->highlightAnimation_.currentValue().value<QColor>());
if (this->highlightAnimation_.state() ==
QVariantAnimation::Stopped)
{
this->highlightedMessage_ = nullptr;
}
}
}
@@ -1510,7 +1540,23 @@ void ChannelView::drawMessages(QPainter &painter)
break;
}
}
this->anyAnimationShown_ = anyAnimation;
// Only update on a full repaint as some messages with animated elements
// might get left out in partial repaints.
// This happens for example when hovering over the go-to-bottom button.
if (this->height() <= area.height())
{
this->animationArea_ = animationArea;
}
#ifdef FOURTF
else
{
// shows the updated area on partial repaints
painter.setPen(Qt::red);
painter.drawRect(area.x(), area.y(), area.width() - 1,
area.height() - 1);
}
#endif
if (end == nullptr)
{