diff --git a/src/widgets/chatwidgetview.cpp b/src/widgets/chatwidgetview.cpp index 14cbbf989..10b41ef1d 100644 --- a/src/widgets/chatwidgetview.cpp +++ b/src/widgets/chatwidgetview.cpp @@ -8,10 +8,11 @@ #include "util/distancebetweenpoints.h" #include "widgets/chatwidget.h" -#include #include #include #include + +#include #include #include @@ -19,7 +20,7 @@ namespace chatterino { namespace widgets { ChatWidgetView::ChatWidgetView(ChatWidget *parent) - : QWidget() + : QWidget(parent) , _chatWidget(parent) , _scrollbar(this) , _userPopupWidget(_chatWidget->getChannelRef()) @@ -34,7 +35,10 @@ ChatWidgetView::ChatWidgetView(ChatWidget *parent) QObject::connect(&SettingsManager::getInstance(), &SettingsManager::wordTypeMaskChanged, this, &ChatWidgetView::wordTypeMaskChanged); - _scrollbar.getCurrentValueChanged().connect([this] { update(); }); + _scrollbar.getCurrentValueChanged().connect([this] { + // Whenever the scrollbar value has been changed, re-render the ChatWidgetView + this->update(); + }); } ChatWidgetView::~ChatWidgetView() @@ -55,6 +59,15 @@ bool ChatWidgetView::layoutMessages() bool showScrollbar = false, redraw = false; + // Bool indicating whether or not we were showing all messages + // True if one of the following statements are true: + // The scrollbar was not visible + // The scrollbar was visible and at the bottom + bool showingLatestMessages = false; + if (this->_scrollbar.isAtBottom() || !this->_scrollbar.isVisible()) { + showingLatestMessages = true; + } + int start = _scrollbar.getCurrentValue(); int layoutWidth = _scrollbar.isVisible() ? width() - _scrollbar.width() : width(); @@ -102,13 +115,22 @@ bool ChatWidgetView::layoutMessages() _scrollbar.setMaximum(messages.getSize()); + if (showingLatestMessages && showScrollbar) { + // If we were showing the latest messages and the scrollbar now wants to be rendered, scroll + // to bottom + // TODO: Do we want to check if the user is currently moving the scrollbar? + // Perhaps also if the user scrolled with the scrollwheel in this ChatWidget in the last 0.2 + // seconds or something + this->_scrollbar.scrollToBottom(); + } + return redraw; } void ChatWidgetView::updateGifEmotes() { _onlyUpdateEmotes = true; - update(); + this->update(); } ScrollBar *ChatWidgetView::getScrollbar() @@ -123,7 +145,7 @@ void ChatWidgetView::resizeEvent(QResizeEvent *) layoutMessages(); - update(); + this->update(); } void ChatWidgetView::paintEvent(QPaintEvent *event) @@ -403,5 +425,6 @@ bool ChatWidgetView::tryGetMessageAt(QPoint p, std::shared_ptr #include #include @@ -88,6 +89,16 @@ void ScrollBar::addHighlight(ScrollBarHighlight *highlight) _mutex.unlock(); } +void ScrollBar::scrollToBottom() +{ + this->setDesiredValue(this->_maximum - this->getLargeChange()); +} + +bool ScrollBar::isAtBottom() const +{ + return this->getCurrentValue() == this->getMaximum() - this->getLargeChange(); +} + void ScrollBar::setMaximum(qreal value) { _maximum = value; @@ -188,6 +199,15 @@ void ScrollBar::setCurrentValue(qreal value) } } +void ScrollBar::printCurrentState(const QString &prefix) const +{ + qDebug() << prefix // + << "Current value: " << this->getCurrentValue() // + << ". Maximum: " << this->getMaximum() // + << ". Minimum: " << this->getMinimum() // + << ". Large change: " << this->getLargeChange(); // +} + void ScrollBar::paintEvent(QPaintEvent *) { QPainter painter(this); @@ -301,5 +321,6 @@ void ScrollBar::updateScroll() update(); } -} -} + +} // namespace widgets +} // namespace chatterino diff --git a/src/widgets/scrollbar.h b/src/widgets/scrollbar.h index 5f3094e0b..217a41835 100644 --- a/src/widgets/scrollbar.h +++ b/src/widgets/scrollbar.h @@ -1,5 +1,4 @@ -#ifndef SCROLLBAR_H -#define SCROLLBAR_H +#pragma once #include "widgets/scrollbarhighlight.h" @@ -7,7 +6,6 @@ #include #include #include -#include namespace chatterino { namespace widgets { @@ -25,6 +23,10 @@ public: Q_PROPERTY(qreal _desiredValue READ getDesiredValue WRITE setDesiredValue) + void scrollToBottom(); + + bool isAtBottom() const; + void setMaximum(qreal value); void setMinimum(qreal value); void setLargeChange(qreal value); @@ -39,6 +41,8 @@ public: boost::signals2::signal &getCurrentValueChanged(); void setCurrentValue(qreal value); + void printCurrentState(const QString &prefix = QString()) const; + private: Q_PROPERTY(qreal _currentValue READ getCurrentValue WRITE setCurrentValue) @@ -74,7 +78,6 @@ private: void updateScroll(); }; -} -} -#endif // SCROLLBAR_H +} // namespace widgets +} // namespace chatterino