mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-09-28 20:08:39 -05:00
[Chat] Scroll chat view to bottom when loading chat history (#7079)
Some checks failed
Build Desktop / Configure (push) Has been cancelled
Build Desktop / Debian 13 (push) Has been cancelled
Build Desktop / Debian 12 (push) Has been cancelled
Build Desktop / Fedora 44 (push) Has been cancelled
Build Desktop / Fedora 43 (push) Has been cancelled
Build Desktop / Servatrice_Debian 12 (push) Has been cancelled
Build Desktop / Ubuntu 26.04 (push) Has been cancelled
Build Desktop / Ubuntu 24.04 (push) Has been cancelled
Build Desktop / Arch (push) Has been cancelled
Build Desktop / macOS 14 (push) Has been cancelled
Build Desktop / macOS 15 (push) Has been cancelled
Build Desktop / macOS 13 Intel (push) Has been cancelled
Build Desktop / macOS 15 Debug (push) Has been cancelled
Build Desktop / Windows 10 (push) Has been cancelled
Build Docker Image / amd64 & arm64 (push) Has been cancelled
Some checks failed
Build Desktop / Configure (push) Has been cancelled
Build Desktop / Debian 13 (push) Has been cancelled
Build Desktop / Debian 12 (push) Has been cancelled
Build Desktop / Fedora 44 (push) Has been cancelled
Build Desktop / Fedora 43 (push) Has been cancelled
Build Desktop / Servatrice_Debian 12 (push) Has been cancelled
Build Desktop / Ubuntu 26.04 (push) Has been cancelled
Build Desktop / Ubuntu 24.04 (push) Has been cancelled
Build Desktop / Arch (push) Has been cancelled
Build Desktop / macOS 14 (push) Has been cancelled
Build Desktop / macOS 15 (push) Has been cancelled
Build Desktop / macOS 13 Intel (push) Has been cancelled
Build Desktop / macOS 15 Debug (push) Has been cancelled
Build Desktop / Windows 10 (push) Has been cancelled
Build Docker Image / amd64 & arm64 (push) Has been cancelled
* [Chat] Scroll chat view to bottom when loading chat history (#2725) Took 4 minutes Took 38 seconds * Harden scrolling to bottom Took 11 minutes * Implement stick-to-bottom flag --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include <QDesktopServices>
|
||||
#include <QMouseEvent>
|
||||
#include <QScrollBar>
|
||||
#include <QTimer>
|
||||
#include <libcockatrice/card/database/card_database_manager.h>
|
||||
#include <libcockatrice/network/server/remote/user_level.h>
|
||||
#include <libcockatrice/settings/chat_settings.h>
|
||||
@@ -51,6 +52,9 @@ ChatView::ChatView(TabSupervisor *_tabSupervisor, AbstractGame *_game, bool _sho
|
||||
setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
|
||||
setOpenLinks(false);
|
||||
connect(this, &ChatView::anchorClicked, this, &ChatView::openLink);
|
||||
|
||||
connect(verticalScrollBar(), &QScrollBar::rangeChanged, this, &ChatView::onScrollBarRangeChanged);
|
||||
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &ChatView::onScrollBarValueChanged);
|
||||
}
|
||||
|
||||
void ChatView::adjustColorsToPalette()
|
||||
@@ -151,7 +155,7 @@ void ChatView::appendHtml(const QString &html)
|
||||
bool atBottom = verticalScrollBar()->value() >= verticalScrollBar()->maximum();
|
||||
prepareBlock().insertHtml(html);
|
||||
if (atBottom) {
|
||||
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
|
||||
scrollToBottom();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,7 +173,7 @@ void ChatView::appendHtmlServerMessage(const QString &html, bool optionalIsBold,
|
||||
|
||||
prepareBlock().insertHtml(htmlText);
|
||||
if (atBottom) {
|
||||
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
|
||||
scrollToBottom();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,11 +342,36 @@ void ChatView::appendMessage(QString message,
|
||||
}
|
||||
}
|
||||
|
||||
if (atBottom) {
|
||||
// ChatHistory messages are only ever sent once per room, right after joining, before the user can
|
||||
// interact with the view. Always scroll to the bottom so the whole history is visible on join.
|
||||
if (atBottom || messageType.testFlag(Event_RoomSay::ChatHistory)) {
|
||||
scrollToBottom();
|
||||
}
|
||||
}
|
||||
|
||||
void ChatView::scrollToBottom()
|
||||
{
|
||||
// The document layout, and therefore the scrollbar range, may be updated asynchronously (e.g. while
|
||||
// the chat history is loaded into a view that has not been laid out yet). Setting the value once is
|
||||
// not enough: keep stickToBottom set so any later range change scrolls to the new maximum as well.
|
||||
stickToBottom = true;
|
||||
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
|
||||
}
|
||||
|
||||
void ChatView::onScrollBarRangeChanged()
|
||||
{
|
||||
if (stickToBottom) {
|
||||
verticalScrollBar()->setValue(verticalScrollBar()->maximum());
|
||||
}
|
||||
}
|
||||
|
||||
void ChatView::onScrollBarValueChanged(int value)
|
||||
{
|
||||
if (value < verticalScrollBar()->maximum()) {
|
||||
stickToBottom = false;
|
||||
}
|
||||
}
|
||||
|
||||
void ChatView::checkTag(QTextCursor &cursor, QString &message)
|
||||
{
|
||||
if (message.startsWith("[card]")) {
|
||||
|
||||
@@ -60,6 +60,7 @@ private:
|
||||
QStringList highlightedWords;
|
||||
bool evenNumber;
|
||||
bool showTimestamps;
|
||||
bool stickToBottom = false;
|
||||
HoveredItemType hoveredItemType;
|
||||
QString hoveredContent;
|
||||
QAction *messageClicked;
|
||||
@@ -67,6 +68,7 @@ private:
|
||||
|
||||
[[nodiscard]] QTextFragment getFragmentUnderMouse(const QPoint &pos) const;
|
||||
QTextCursor prepareBlock(bool same = false);
|
||||
void scrollToBottom();
|
||||
void appendCardTag(QTextCursor &cursor, const QString &cardName);
|
||||
void appendUrlTag(QTextCursor &cursor, QString url);
|
||||
static QColor getCustomMentionColor();
|
||||
@@ -88,6 +90,8 @@ private slots:
|
||||
void actMessageClicked();
|
||||
void adjustColorsToPalette();
|
||||
void refreshBlockColors();
|
||||
void onScrollBarRangeChanged();
|
||||
void onScrollBarValueChanged(int value);
|
||||
|
||||
public:
|
||||
ChatView(TabSupervisor *_tabSupervisor, AbstractGame *_game, bool _showTimestamps, QWidget *parent = nullptr);
|
||||
|
||||
Reference in New Issue
Block a user