From af0f22fc9102e931a0d4cee43f642d084e5e539f Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sat, 5 Aug 2017 15:59:24 +0300 Subject: [PATCH] Keep fetching history until the scrollbar gets activated --- include/RoomInfoListItem.h | 3 ++- include/TimelineView.h | 4 +++- src/ChatPage.cc | 2 +- src/RoomInfoListItem.cc | 10 ++++++++++ src/TimelineView.cc | 19 ++++++++++++------- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/include/RoomInfoListItem.h b/include/RoomInfoListItem.h index 12d5abb1..8ead930b 100644 --- a/include/RoomInfoListItem.h +++ b/include/RoomInfoListItem.h @@ -56,13 +56,14 @@ public slots: protected: void mousePressEvent(QMouseEvent *event) override; void paintEvent(QPaintEvent *event) override; + void resizeEvent(QResizeEvent *event) override; void contextMenuEvent(QContextMenuEvent *event) override; private: QString notificationText(); const int Padding = 7; - const int IconSize = 46; + const int IconSize = 48; RippleOverlay *ripple_overlay_; diff --git a/include/TimelineView.h b/include/TimelineView.h index ea3e5fb3..f1860dbe 100644 --- a/include/TimelineView.h +++ b/include/TimelineView.h @@ -73,12 +73,12 @@ public: int addEvents(const Timeline &timeline); void addUserTextMessage(const QString &msg, int txn_id); void updatePendingMessage(int txn_id, QString event_id); - void fetchHistory(); void scrollDown(); public slots: void sliderRangeChanged(int min, int max); void sliderMoved(int position); + void fetchHistory(); // Add old events at the top of the timeline. void addBackwardsEvents(const QString &room_id, const RoomMessages &msgs); @@ -118,6 +118,8 @@ private: const int SCROLL_BAR_GAP = 400; + QTimer *paginationTimer_; + int scroll_height_ = 0; int previous_max_height_ = 0; diff --git a/src/ChatPage.cc b/src/ChatPage.cc index 5a5a497e..dbfa7c5c 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -102,7 +102,7 @@ ChatPage::ChatPage(QSharedPointer client, QWidget *parent) splitter->addWidget(sideBar_); splitter->addWidget(content_); - room_list_ = new RoomList(client, this); + room_list_ = new RoomList(client, sideBar_); sideBarMainLayout_->addWidget(room_list_); top_bar_ = new TopRoomBar(this); diff --git a/src/RoomInfoListItem.cc b/src/RoomInfoListItem.cc index 11d4da57..379e4000 100644 --- a/src/RoomInfoListItem.cc +++ b/src/RoomInfoListItem.cc @@ -69,6 +69,16 @@ QString RoomInfoListItem::notificationText() return tr("Enable notifications"); } +void RoomInfoListItem::resizeEvent(QResizeEvent *) +{ + // Update ripple's clipping path. + QPainterPath path; + path.addRect(0, 0, width(), height()); + + ripple_overlay_->setClipPath(path); + ripple_overlay_->setClipping(true); +} + void RoomInfoListItem::paintEvent(QPaintEvent *event) { Q_UNUSED(event); diff --git a/src/TimelineView.cc b/src/TimelineView.cc index 731e7db5..5968f9cf 100644 --- a/src/TimelineView.cc +++ b/src/TimelineView.cc @@ -65,8 +65,10 @@ void TimelineView::sliderRangeChanged(int min, int max) { Q_UNUSED(min); - if (!scroll_area_->verticalScrollBar()->isVisible()) + if (!scroll_area_->verticalScrollBar()->isVisible()) { + scroll_area_->verticalScrollBar()->setValue(max); return; + } if (max - scroll_area_->verticalScrollBar()->value() < SCROLL_BAR_GAP) scroll_area_->verticalScrollBar()->setValue(max); @@ -84,7 +86,6 @@ void TimelineView::sliderRangeChanged(int min, int max) newPosition = max; scroll_area_->verticalScrollBar()->setValue(newPosition); - fetchHistory(); } } @@ -92,11 +93,14 @@ void TimelineView::fetchHistory() { bool hasEnoughMessages = scroll_area_->verticalScrollBar()->value() != 0; - if (!hasEnoughMessages && !isTimelineFinished && !isPaginationInProgress_) { + if (!hasEnoughMessages && !isTimelineFinished) { isPaginationInProgress_ = true; client_->messages(room_id_, prev_batch_token_); - scroll_area_->verticalScrollBar()->setValue(scroll_area_->verticalScrollBar()->maximum()); + paginationTimer_->start(500); + return; } + + paginationTimer_->stop(); } void TimelineView::scrollDown() @@ -169,10 +173,8 @@ void TimelineView::addBackwardsEvents(const QString &room_id, const RoomMessages oldPosition_ = scroll_area_->verticalScrollBar()->value(); oldHeight_ = scroll_widget_->size().height(); - for (const auto &item : items) { - item->adjustSize(); + for (const auto &item : items) addTimelineItem(item, TimelineDirection::Top); - } prev_batch_token_ = msgs.end(); isPaginationInProgress_ = false; @@ -323,6 +325,9 @@ void TimelineView::init() setLayout(top_layout_); + paginationTimer_ = new QTimer(this); + connect(paginationTimer_, &QTimer::timeout, this, &TimelineView::fetchHistory); + connect(client_.data(), &MatrixClient::messagesRetrieved, this, &TimelineView::addBackwardsEvents); connect(scroll_area_->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(sliderMoved(int)));