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 "widgets/chatwidget.h"
#include <math.h>
#include <QDebug>
#include <QGraphicsBlurEffect>
#include <QPainter>
#include <math.h>
#include <chrono>
#include <functional>
@ -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<messages::Message
return false;
}
} // namespace widgets
} // namespace chatterino

View file

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

View file

@ -1,6 +1,7 @@
#include "widgets/scrollbar.h"
#include "colorscheme.h"
#include <QDebug>
#include <QMouseEvent>
#include <QPainter>
@ -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

View file

@ -1,5 +1,4 @@
#ifndef SCROLLBAR_H
#define SCROLLBAR_H
#pragma once
#include "widgets/scrollbarhighlight.h"
@ -7,7 +6,6 @@
#include <QPropertyAnimation>
#include <QWidget>
#include <boost/signals2.hpp>
#include <functional>
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<void()> &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