From 1606ea648bb69c23988509853f81ab4163f031b0 Mon Sep 17 00:00:00 2001 From: fourtf Date: Fri, 5 Jan 2018 03:14:46 +0100 Subject: [PATCH] added smoothscrolling on new message --- src/singletons/settingsmanager.hpp | 1 + src/widgets/helper/channelview.cpp | 28 ++++++++++++++++++---------- src/widgets/helper/channelview.hpp | 1 + src/widgets/scrollbar.cpp | 4 ++-- src/widgets/scrollbar.hpp | 2 +- src/widgets/settingsdialog.cpp | 4 ++++ 6 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/singletons/settingsmanager.hpp b/src/singletons/settingsmanager.hpp index 44ab5275c..1ab00ab92 100644 --- a/src/singletons/settingsmanager.hpp +++ b/src/singletons/settingsmanager.hpp @@ -40,6 +40,7 @@ public: BoolSetting hidePreferencesButton = {"/appearance/hidePreferencesButton", false}; BoolSetting hideUserButton = {"/appearance/hideUserButton", false}; BoolSetting enableSmoothScrolling = {"/appearance/smoothScrolling", true}; + BoolSetting enableSmoothScrollingNewMessages = {"/appearance/smoothScrolling", true}; // BoolSetting useCustomWindowFrame = {"/appearance/useCustomWindowFrame", false}; /// Behaviour diff --git a/src/widgets/helper/channelview.cpp b/src/widgets/helper/channelview.cpp index 9dae85ec5..ce8baab1a 100644 --- a/src/widgets/helper/channelview.cpp +++ b/src/widgets/helper/channelview.cpp @@ -72,8 +72,12 @@ ChannelView::ChannelView(BaseWidget *parent) this->layoutMessages(); // })); - connect(goToBottom, &RippleEffectLabel::clicked, this, - [this] { QTimer::singleShot(180, [this] { this->scrollBar.scrollToBottom(); }); }); + connect(goToBottom, &RippleEffectLabel::clicked, this, [this] { + QTimer::singleShot(180, [this] { + this->scrollBar.scrollToBottom(singletons::SettingManager::getInstance() + .enableSmoothScrollingNewMessages.getValue()); + }); + }); this->updateTimer.setInterval(1000 / 60); this->updateTimer.setSingleShot(true); @@ -186,13 +190,15 @@ void ChannelView::actuallyLayoutMessages() this->scrollBar.setMaximum(messagesSnapshot.getLength()); + // 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 if (this->enableScrollingToBottom && this->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(); + this->scrollBar.scrollToBottom( + this->messageWasAdded && + singletons::SettingManager::getInstance().enableSmoothScrollingNewMessages.getValue()); } // MARK(timer); @@ -377,7 +383,8 @@ void ChannelView::setChannel(std::shared_ptr newChannel) this->highlightedMessageReceived.invoke(); } - layoutMessages(); + this->messageWasAdded = true; + this->layoutMessages(); }); this->messageAddedAtStartConnection = @@ -397,7 +404,8 @@ void ChannelView::setChannel(std::shared_ptr newChannel) } } - layoutMessages(); + this->messageWasAdded = true; + this->layoutMessages(); }); // on message removed diff --git a/src/widgets/helper/channelview.hpp b/src/widgets/helper/channelview.hpp index 9e73f3c50..5d50565ec 100644 --- a/src/widgets/helper/channelview.hpp +++ b/src/widgets/helper/channelview.hpp @@ -71,6 +71,7 @@ private: QTimer updateTimer; bool updateQueued = false; + bool messageWasAdded = false; void detachChannel(); void actuallyLayoutMessages(); diff --git a/src/widgets/scrollbar.cpp b/src/widgets/scrollbar.cpp index f34d06153..e8cd9d8c3 100644 --- a/src/widgets/scrollbar.cpp +++ b/src/widgets/scrollbar.cpp @@ -89,9 +89,9 @@ void ScrollBar::addHighlight(ScrollBarHighlight *highlight) this->mutex.unlock(); } -void ScrollBar::scrollToBottom() +void ScrollBar::scrollToBottom(bool animate) { - this->setDesiredValue(this->maximum - this->getLargeChange()); + this->setDesiredValue(this->maximum - this->getLargeChange(), animate); } bool ScrollBar::isAtBottom() const diff --git a/src/widgets/scrollbar.hpp b/src/widgets/scrollbar.hpp index 37b1ae361..c5151711e 100644 --- a/src/widgets/scrollbar.hpp +++ b/src/widgets/scrollbar.hpp @@ -28,7 +28,7 @@ public: Q_PROPERTY(qreal desiredValue READ getDesiredValue WRITE setDesiredValue) - void scrollToBottom(); + void scrollToBottom(bool animate = false); bool isAtBottom() const; diff --git a/src/widgets/settingsdialog.cpp b/src/widgets/settingsdialog.cpp index 5788162b2..9846e6145 100644 --- a/src/widgets/settingsdialog.cpp +++ b/src/widgets/settingsdialog.cpp @@ -280,6 +280,10 @@ QVBoxLayout *SettingsDialog::createAppearanceTab() createCheckbox("Enable smooth scrolling", settings.enableSmoothScrolling); form->addRow("Scrolling:", enableSmoothScrolling); + auto enableSmoothScrollingNewMessages = createCheckbox( + "Enable smooth scrolling for new messages", settings.enableSmoothScrolling); + form->addRow("", enableSmoothScrollingNewMessages); + group->setLayout(form); layout->addWidget(group);