Fix scrollbar

This commit is contained in:
Rasmus Karlsson 2017-06-06 17:18:23 +02:00
parent 2d2d6dad17
commit 814fc4bbae
4 changed files with 62 additions and 17 deletions

View file

@ -8,10 +8,11 @@
#include "util/distancebetweenpoints.h" #include "util/distancebetweenpoints.h"
#include "widgets/chatwidget.h" #include "widgets/chatwidget.h"
#include <math.h>
#include <QDebug> #include <QDebug>
#include <QGraphicsBlurEffect> #include <QGraphicsBlurEffect>
#include <QPainter> #include <QPainter>
#include <math.h>
#include <chrono> #include <chrono>
#include <functional> #include <functional>
@ -19,7 +20,7 @@ namespace chatterino {
namespace widgets { namespace widgets {
ChatWidgetView::ChatWidgetView(ChatWidget *parent) ChatWidgetView::ChatWidgetView(ChatWidget *parent)
: QWidget() : QWidget(parent)
, _chatWidget(parent) , _chatWidget(parent)
, _scrollbar(this) , _scrollbar(this)
, _userPopupWidget(_chatWidget->getChannelRef()) , _userPopupWidget(_chatWidget->getChannelRef())
@ -34,7 +35,10 @@ ChatWidgetView::ChatWidgetView(ChatWidget *parent)
QObject::connect(&SettingsManager::getInstance(), &SettingsManager::wordTypeMaskChanged, this, QObject::connect(&SettingsManager::getInstance(), &SettingsManager::wordTypeMaskChanged, this,
&ChatWidgetView::wordTypeMaskChanged); &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() ChatWidgetView::~ChatWidgetView()
@ -55,6 +59,15 @@ bool ChatWidgetView::layoutMessages()
bool showScrollbar = false, redraw = false; 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 start = _scrollbar.getCurrentValue();
int layoutWidth = _scrollbar.isVisible() ? width() - _scrollbar.width() : width(); int layoutWidth = _scrollbar.isVisible() ? width() - _scrollbar.width() : width();
@ -102,13 +115,22 @@ bool ChatWidgetView::layoutMessages()
_scrollbar.setMaximum(messages.getSize()); _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; return redraw;
} }
void ChatWidgetView::updateGifEmotes() void ChatWidgetView::updateGifEmotes()
{ {
_onlyUpdateEmotes = true; _onlyUpdateEmotes = true;
update(); this->update();
} }
ScrollBar *ChatWidgetView::getScrollbar() ScrollBar *ChatWidgetView::getScrollbar()
@ -123,7 +145,7 @@ void ChatWidgetView::resizeEvent(QResizeEvent *)
layoutMessages(); layoutMessages();
update(); this->update();
} }
void ChatWidgetView::paintEvent(QPaintEvent *event) void ChatWidgetView::paintEvent(QPaintEvent *event)
@ -403,5 +425,6 @@ bool ChatWidgetView::tryGetMessageAt(QPoint p, std::shared_ptr<messages::Message
return false; return false;
} }
} // namespace widgets } // namespace widgets
} // namespace chatterino } // namespace chatterino

View file

@ -1,5 +1,4 @@
#ifndef CHATVIEW_H #pragma once
#define CHATVIEW_H
#include "channel.h" #include "channel.h"
#include "messages/lazyloadedimage.h" #include "messages/lazyloadedimage.h"
@ -67,7 +66,6 @@ private slots:
update(); update();
} }
}; };
} // namespace widgets } // namespace widgets
} // namespace chatterino } // namespace chatterino
#endif // CHATVIEW_H

View file

@ -1,6 +1,7 @@
#include "widgets/scrollbar.h" #include "widgets/scrollbar.h"
#include "colorscheme.h" #include "colorscheme.h"
#include <QDebug>
#include <QMouseEvent> #include <QMouseEvent>
#include <QPainter> #include <QPainter>
@ -88,6 +89,16 @@ void ScrollBar::addHighlight(ScrollBarHighlight *highlight)
_mutex.unlock(); _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) void ScrollBar::setMaximum(qreal value)
{ {
_maximum = 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 *) void ScrollBar::paintEvent(QPaintEvent *)
{ {
QPainter painter(this); QPainter painter(this);
@ -301,5 +321,6 @@ void ScrollBar::updateScroll()
update(); update();
} }
}
} } // namespace widgets
} // namespace chatterino

View file

@ -1,5 +1,4 @@
#ifndef SCROLLBAR_H #pragma once
#define SCROLLBAR_H
#include "widgets/scrollbarhighlight.h" #include "widgets/scrollbarhighlight.h"
@ -7,7 +6,6 @@
#include <QPropertyAnimation> #include <QPropertyAnimation>
#include <QWidget> #include <QWidget>
#include <boost/signals2.hpp> #include <boost/signals2.hpp>
#include <functional>
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
@ -25,6 +23,10 @@ public:
Q_PROPERTY(qreal _desiredValue READ getDesiredValue WRITE setDesiredValue) Q_PROPERTY(qreal _desiredValue READ getDesiredValue WRITE setDesiredValue)
void scrollToBottom();
bool isAtBottom() const;
void setMaximum(qreal value); void setMaximum(qreal value);
void setMinimum(qreal value); void setMinimum(qreal value);
void setLargeChange(qreal value); void setLargeChange(qreal value);
@ -39,6 +41,8 @@ public:
boost::signals2::signal<void()> &getCurrentValueChanged(); boost::signals2::signal<void()> &getCurrentValueChanged();
void setCurrentValue(qreal value); void setCurrentValue(qreal value);
void printCurrentState(const QString &prefix = QString()) const;
private: private:
Q_PROPERTY(qreal _currentValue READ getCurrentValue WRITE setCurrentValue) Q_PROPERTY(qreal _currentValue READ getCurrentValue WRITE setCurrentValue)
@ -74,7 +78,6 @@ private:
void updateScroll(); void updateScroll();
}; };
}
}
#endif // SCROLLBAR_H } // namespace widgets
} // namespace chatterino