mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 03:24:00 +02:00
Update chat window to only scroll when at the bottom
It can be frustrating when scrolling up to read a message to have the window forcibly scrolled to the bottom when a new message appears.
This commit is contained in:
parent
de475ee46c
commit
16a2367b29
2 changed files with 28 additions and 2 deletions
|
@ -154,10 +154,18 @@ void ChatWindow::addTimeStamp() {
|
|||
"padding: 4px;");
|
||||
timeLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
|
||||
timeLabel->setAlignment(Qt::AlignHCenter);
|
||||
|
||||
bool atBottom = isAtBottom();
|
||||
|
||||
ui->messagesGridLayout->addWidget(timeLabel, ui->messagesGridLayout->rowCount(), 0, 1, 2);
|
||||
ui->messagesGridLayout->parentWidget()->updateGeometry();
|
||||
|
||||
Application::processEvents();
|
||||
numMessagesAfterLastTimeStamp = 0;
|
||||
|
||||
if (atBottom) {
|
||||
scrollToBottom();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -245,12 +253,15 @@ void ChatWindow::messageReceived(const QXmppMessage& message) {
|
|||
messageLabel->setStyleSheet(messageLabel->styleSheet() + "; background-color: #e1e8ea");
|
||||
}
|
||||
|
||||
bool atBottom = isAtBottom();
|
||||
ui->messagesGridLayout->addWidget(userLabel, ui->messagesGridLayout->rowCount(), 0);
|
||||
ui->messagesGridLayout->addWidget(messageLabel, ui->messagesGridLayout->rowCount() - 1, 1);
|
||||
ui->messagesGridLayout->parentWidget()->updateGeometry();
|
||||
Application::processEvents();
|
||||
QScrollBar* verticalScrollBar = ui->messagesScrollArea->verticalScrollBar();
|
||||
verticalScrollBar->setSliderPosition(verticalScrollBar->maximum());
|
||||
|
||||
if (atBottom) {
|
||||
scrollToBottom();
|
||||
}
|
||||
|
||||
++numMessagesAfterLastTimeStamp;
|
||||
if (message.stamp().isValid()) {
|
||||
|
@ -260,4 +271,17 @@ void ChatWindow::messageReceived(const QXmppMessage& message) {
|
|||
}
|
||||
}
|
||||
|
||||
bool ChatWindow::isAtBottom() {
|
||||
QScrollBar* verticalScrollBar = ui->messagesScrollArea->verticalScrollBar();
|
||||
qDebug() << "Checking for bottom " << verticalScrollBar->sliderPosition() << " " << verticalScrollBar->maximum();
|
||||
return verticalScrollBar->sliderPosition() == verticalScrollBar->maximum();
|
||||
}
|
||||
|
||||
// Scroll chat message area to bottom.
|
||||
void ChatWindow::scrollToBottom() {
|
||||
QScrollBar* verticalScrollBar = ui->messagesScrollArea->verticalScrollBar();
|
||||
qDebug() << "Scrolling to " << verticalScrollBar->maximum();
|
||||
verticalScrollBar->setSliderPosition(verticalScrollBar->maximum());
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -48,6 +48,8 @@ private:
|
|||
#endif
|
||||
void startTimerForTimeStamps();
|
||||
void addTimeStamp();
|
||||
bool isAtBottom();
|
||||
void scrollToBottom();
|
||||
|
||||
Ui::ChatWindow* ui;
|
||||
int numMessagesAfterLastTimeStamp;
|
||||
|
|
Loading…
Reference in a new issue