Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix(chatlog): fix stick to bottom behavior
Browse files Browse the repository at this point in the history
This commit fixes the behavior when a message is received while the
chatlog is scrolled to the bottom. With this change, the chatlog will
stick to the bottom when it is scrolled all the way down. If it is
somewhere in the middle (e.g. for search) the chatlog will not change
its position.
  • Loading branch information
sudden6 committed Mar 15, 2020
1 parent 0b5f751 commit f2fa601
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
28 changes: 14 additions & 14 deletions src/chatlog/chatlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,9 @@ void ChatLog::insertChatlineAtBottom(const QList<ChatLine::Ptr>& newLines)

layout(lines.last()->getRow(), lines.size(), useableWidth());

if (visibleLines.size() > 1) {
startResizeWorker(visibleLines[1]);
} else {
startResizeWorker();
// redo layout only when scrolled down
if(stickToBottom()) {
startResizeWorker(true);
}
}

Expand Down Expand Up @@ -463,9 +462,9 @@ void ChatLog::insertChatlinesOnTop(const QList<ChatLine::Ptr>& newLines)

// redo layout
if (visibleLines.size() > 1) {
startResizeWorker(visibleLines[1]);
startResizeWorker(stickToBottom(), visibleLines[1]);
} else {
startResizeWorker();
startResizeWorker(stickToBottom());
}

}
Expand All @@ -481,7 +480,7 @@ void ChatLog::scrollToBottom()
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
}

void ChatLog::startResizeWorker(ChatLine::Ptr anchorLine)
void ChatLog::startResizeWorker(bool stick, ChatLine::Ptr anchorLine)
{
if (lines.empty()) {
isScroll = true;
Expand All @@ -491,11 +490,11 @@ void ChatLog::startResizeWorker(ChatLine::Ptr anchorLine)
// (re)start the worker
if (!workerTimer->isActive()) {
// these values must not be reevaluated while the worker is running
if (anchorLine) {
workerAnchorLine = anchorLine;
workerStb = false;
workerStb = stick;
if (stick) {
workerAnchorLine = ChatLine::Ptr();
} else {
workerStb = stickToBottom();
workerAnchorLine = anchorLine;
}
}

Expand Down Expand Up @@ -765,7 +764,7 @@ int ChatLog::getNumRemove() const

void ChatLog::forceRelayout()
{
startResizeWorker();
startResizeWorker(stickToBottom());
}

void ChatLog::checkVisibility(bool causedWheelEvent)
Expand Down Expand Up @@ -834,7 +833,7 @@ void ChatLog::resizeEvent(QResizeEvent* ev)
bool stb = stickToBottom();

if (ev->size().width() != ev->oldSize().width()) {
startResizeWorker();
startResizeWorker(stb);
stb = false; // let the resize worker handle it
}

Expand Down Expand Up @@ -901,8 +900,9 @@ QRectF ChatLog::calculateSceneRect() const
{
qreal bottom = (lines.empty() ? 0.0 : lines.last()->sceneBoundingRect().bottom());

if (typingNotification.get() != nullptr)
if (typingNotification.get() != nullptr) {
bottom += typingNotification->sceneBoundingRect().height() + lineSpacing;
}

return QRectF(-margins.left(), -margins.top(), useableWidth(),
bottom + margins.bottom() + margins.top());
Expand Down
2 changes: 1 addition & 1 deletion src/chatlog/chatlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private slots:
void updateSceneRect();
void checkVisibility(bool causedWheelEvent = false);
void scrollToBottom();
void startResizeWorker(ChatLine::Ptr anchorLine = nullptr);
void startResizeWorker(bool stick, ChatLine::Ptr anchorLine = nullptr);

virtual void mouseDoubleClickEvent(QMouseEvent* ev) final override;
virtual void mousePressEvent(QMouseEvent* ev) final override;
Expand Down

0 comments on commit f2fa601

Please sign in to comment.