implemented pausing on hover

This commit is contained in:
fourtf 2018-11-03 21:26:57 +01:00
parent 22cf4368bd
commit 5453c65f0f
10 changed files with 395 additions and 304 deletions

View file

@ -88,7 +88,7 @@ void Channel::addMessage(MessagePtr message,
void Channel::addOrReplaceTimeout(MessagePtr message) void Channel::addOrReplaceTimeout(MessagePtr message)
{ {
LimitedQueueSnapshot<MessagePtr> snapshot = this->getMessageSnapshot(); LimitedQueueSnapshot<MessagePtr> snapshot = this->getMessageSnapshot();
int snapshotLength = snapshot.getLength(); int snapshotLength = snapshot.size();
int end = std::max(0, snapshotLength - 20); int end = std::max(0, snapshotLength - 20);
@ -169,7 +169,7 @@ void Channel::addOrReplaceTimeout(MessagePtr message)
void Channel::disableAllMessages() void Channel::disableAllMessages()
{ {
LimitedQueueSnapshot<MessagePtr> snapshot = this->getMessageSnapshot(); LimitedQueueSnapshot<MessagePtr> snapshot = this->getMessageSnapshot();
int snapshotLength = snapshot.getLength(); int snapshotLength = snapshot.size();
for (int i = 0; i < snapshotLength; i++) for (int i = 0; i < snapshotLength; i++)
{ {
auto &message = snapshot[i]; auto &message = snapshot[i];

View file

@ -22,7 +22,7 @@ public:
{ {
} }
std::size_t getLength() std::size_t size()
{ {
return this->length_; return this->length_;
} }

View file

@ -244,13 +244,13 @@ void AbstractIrcServer::onConnected()
LimitedQueueSnapshot<MessagePtr> snapshot = chan->getMessageSnapshot(); LimitedQueueSnapshot<MessagePtr> snapshot = chan->getMessageSnapshot();
bool replaceMessage = snapshot.getLength() > 0 && bool replaceMessage = snapshot.size() > 0 &&
snapshot[snapshot.getLength() - 1]->flags.has( snapshot[snapshot.size() - 1]->flags.has(
MessageFlag::DisconnectedMessage); MessageFlag::DisconnectedMessage);
if (replaceMessage) if (replaceMessage)
{ {
chan->replaceMessage(snapshot[snapshot.getLength() - 1], chan->replaceMessage(snapshot[snapshot.size() - 1],
reconnected); reconnected);
continue; continue;
} }

View file

@ -4,6 +4,8 @@
#include "common/Common.hpp" #include "common/Common.hpp"
#include "controllers/accounts/AccountController.hpp" #include "controllers/accounts/AccountController.hpp"
#include "controllers/highlights/HighlightController.hpp" #include "controllers/highlights/HighlightController.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "providers/twitch/IrcMessageHandler.hpp" #include "providers/twitch/IrcMessageHandler.hpp"
#include "providers/twitch/PubsubClient.hpp" #include "providers/twitch/PubsubClient.hpp"
#include "providers/twitch/TwitchAccount.hpp" #include "providers/twitch/TwitchAccount.hpp"
@ -182,6 +184,23 @@ std::shared_ptr<Channel> TwitchServer::getCustomChannel(
return this->mentionsChannel; return this->mentionsChannel;
} }
if (channelName == "$$$")
{
static auto channel =
std::make_shared<Channel>("$$$", chatterino::Channel::Type::Misc);
static auto timer = [&] {
auto timer = new QTimer;
QObject::connect(timer, &QTimer::timeout, [] {
channel->addMessage(
makeSystemMessage(QTime::currentTime().toString()));
});
timer->start(500);
return timer;
}();
return channel;
}
return nullptr; return nullptr;
} }

View file

@ -264,7 +264,7 @@ void Scrollbar::paintEvent(QPaintEvent *)
// draw highlights // draw highlights
auto snapshot = this->getHighlightSnapshot(); auto snapshot = this->getHighlightSnapshot();
size_t snapshotLength = snapshot.getLength(); size_t snapshotLength = snapshot.size();
if (snapshotLength == 0) if (snapshotLength == 0)
{ {

View file

@ -172,7 +172,7 @@ void EmotePopup::loadChannel(ChannelPtr _channel)
this->subEmotesView_->setChannel(subChannel); this->subEmotesView_->setChannel(subChannel);
this->channelEmotesView_->setChannel(channelChannel); this->channelEmotesView_->setChannel(channelChannel);
if (subChannel->getMessageSnapshot().getLength() == 0) if (subChannel->getMessageSnapshot().size() == 0)
{ {
MessageBuilder builder; MessageBuilder builder;
builder->flags.set(MessageFlag::Centered); builder->flags.set(MessageFlag::Centered);

View file

@ -114,11 +114,13 @@ ChannelView::ChannelView(BaseWidget *parent)
this->initializeScrollbar(); this->initializeScrollbar();
this->initializeSignals(); this->initializeSignals();
this->pauseTimeout_.setSingleShot(true); this->pauseTimer_.setSingleShot(true);
QObject::connect(&this->pauseTimeout_, &QTimer::timeout, [this] { QObject::connect(&this->pauseTimer_, &QTimer::timeout, this, [this] {
this->pausedTemporarily_ = false; /// remove elements that are finite
this->updatePauseStatus(); for (auto it = this->pauses_.begin(); it != this->pauses_.end();)
this->layoutMessages(); it = it->second ? this->pauses_.erase(it) : ++it;
this->updatePauseTimer();
}); });
auto shortcut = new QShortcut(QKeySequence("Ctrl+C"), this); auto shortcut = new QShortcut(QKeySequence("Ctrl+C"), this);
@ -150,7 +152,7 @@ void ChannelView::initializeLayout()
void ChannelView::initializeScrollbar() void ChannelView::initializeScrollbar()
{ {
this->scrollBar_->getCurrentValueChanged().connect([this] { this->scrollBar_->getCurrentValueChanged().connect([this] {
this->actuallyLayoutMessages(true); this->performLayout(true);
this->goToBottom_->setVisible(this->enableScrollingToBottom_ && this->goToBottom_->setVisible(this->enableScrollingToBottom_ &&
this->scrollBar_->isVisible() && this->scrollBar_->isVisible() &&
@ -158,17 +160,13 @@ void ChannelView::initializeScrollbar()
this->queueUpdate(); this->queueUpdate();
}); });
this->scrollBar_->getDesiredValueChanged().connect([this] {
this->pausedByScrollingUp_ = !this->scrollBar_->isAtBottom();
});
} }
void ChannelView::initializeSignals() void ChannelView::initializeSignals()
{ {
this->connections_.push_back( this->connections_.push_back(
getApp()->windows->wordFlagsChanged.connect([this] { getApp()->windows->wordFlagsChanged.connect([this] {
this->layoutMessages(); this->queueLayout();
this->update(); this->update();
})); }));
@ -181,18 +179,100 @@ void ChannelView::initializeSignals()
connections_.push_back( connections_.push_back(
getApp()->windows->layout.connect([&](Channel *channel) { getApp()->windows->layout.connect([&](Channel *channel) {
if (channel == nullptr || this->channel_.get() == channel) if (channel == nullptr || this->channel_.get() == channel)
this->layoutMessages(); this->queueLayout();
})); }));
connections_.push_back(getApp()->fonts->fontChanged.connect( connections_.push_back(
[this] { this->layoutMessages(); })); getApp()->fonts->fontChanged.connect([this] { this->queueLayout(); }));
}
bool ChannelView::paused() const
{
/// No elements in the map -> not paused
return !this->pauses_.empty();
}
void ChannelView::pause(PauseReason reason, boost::optional<uint> msecs)
{
if (msecs)
{
/// Msecs has a value
auto timePoint =
SteadyClock::now() + std::chrono::milliseconds(msecs.get());
auto it = this->pauses_.find(reason);
if (it == this->pauses_.end())
{
/// No value found so we insert a new one.
this->pauses_[reason] = timePoint;
}
else
{
/// If the new time point is newer then we override.
if (it->second && it->second.get() < timePoint)
it->second = timePoint;
}
}
else
{
/// Msecs is none -> pause is infinite.
/// We just override the value.
this->pauses_[reason] = boost::none;
}
this->updatePauseTimer();
}
void ChannelView::unpause(PauseReason reason)
{
/// Remove the value from the map
this->pauses_.erase(reason);
this->updatePauseTimer();
}
void ChannelView::updatePauseTimer()
{
using namespace std::chrono;
if (this->pauses_.empty())
{
/// No pauses so we can stop the timer
this->pauseEnd = boost::none;
this->pauseTimer_.stop();
this->queueLayout();
}
else if (std::any_of(this->pauses_.begin(), this->pauses_.end(),
[](auto &&value) { return !value.second; }))
{
/// Some of the pauses are infinite
this->pauseEnd = boost::none;
this->pauseTimer_.stop();
}
else
{
/// Get the maximum pause
auto max = std::max_element(
this->pauses_.begin(), this->pauses_.end(),
[](auto &&a, auto &&b) { return a.second > b.second; })
->second.get();
if (max != this->pauseEnd)
{
/// Start the timer
this->pauseEnd = max;
this->pauseTimer_.start(
duration_cast<milliseconds>(max - SteadyClock::now()));
}
}
} }
void ChannelView::themeChangedEvent() void ChannelView::themeChangedEvent()
{ {
BaseWidget::themeChangedEvent(); BaseWidget::themeChangedEvent();
this->layoutMessages(); this->queueLayout();
} }
void ChannelView::queueUpdate() void ChannelView::queueUpdate()
@ -208,10 +288,10 @@ void ChannelView::queueUpdate()
// this->updateTimer.start(); // this->updateTimer.start();
} }
void ChannelView::layoutMessages() void ChannelView::queueLayout()
{ {
// if (!this->layoutCooldown->isActive()) { // if (!this->layoutCooldown->isActive()) {
this->actuallyLayoutMessages(); this->performLayout();
// this->layoutCooldown->start(); // this->layoutCooldown->start();
// } else { // } else {
@ -219,79 +299,86 @@ void ChannelView::layoutMessages()
// } // }
} }
void ChannelView::actuallyLayoutMessages(bool causedByScrollbar) void ChannelView::performLayout(bool causedByScrollbar)
{ {
// BenchmarkGuard benchmark("layout"); // BenchmarkGuard benchmark("layout");
auto messagesSnapshot = this->getMessagesSnapshot(); /// Get messages and check if there are at least 1
auto messages = this->getMessagesSnapshot();
if (messagesSnapshot.getLength() == 0)
{
this->scrollBar_->setVisible(false);
return;
}
bool redrawRequired = false;
bool showScrollbar = 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
this->showingLatestMessages_ = this->showingLatestMessages_ =
this->scrollBar_->isAtBottom() || !this->scrollBar_->isVisible(); this->scrollBar_->isAtBottom() || !this->scrollBar_->isVisible();
size_t start = size_t(this->scrollBar_->getCurrentValue()); /// Layout visible messages
int layoutWidth = this->getLayoutWidth(); this->layoutVisibleMessages(messages);
MessageElementFlags flags = this->getFlags(); /// Update scrollbar
this->updateScrollbar(messages, causedByScrollbar);
}
// layout the visible messages in the view void ChannelView::layoutVisibleMessages(
if (messagesSnapshot.getLength() > start) LimitedQueueSnapshot<MessageLayoutPtr> &messages)
{
const auto start = size_t(this->scrollBar_->getCurrentValue());
const auto layoutWidth = this->getLayoutWidth();
const auto flags = this->getFlags();
auto redrawRequired = false;
if (messages.size() > start)
{ {
int y = int(-(messagesSnapshot[start]->getHeight() * auto y = int(-(messages[start]->getHeight() *
(fmod(this->scrollBar_->getCurrentValue(), 1)))); (fmod(this->scrollBar_->getCurrentValue(), 1))));
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) for (auto i = start; i < messages.size() && y >= this->height(); i++)
{ {
auto message = messagesSnapshot[i]; auto message = messages[i];
redrawRequired |= redrawRequired |=
message->layout(layoutWidth, this->getScale(), flags); message->layout(layoutWidth, this->getScale(), flags);
y += message->getHeight(); y += message->getHeight();
}
}
if (y >= this->height()) if (redrawRequired)
this->queueUpdate();
}
void ChannelView::updateScrollbar(
LimitedQueueSnapshot<MessageLayoutPtr> &messages, bool causedByScrollbar)
{
if (messages.size() == 0)
{ {
break; this->scrollBar_->setVisible(false);
} return;
}
} }
// layout the messages at the bottom to determine the scrollbar thumb size /// Layout the messages at the bottom
int h = this->height() - 8; auto h = this->height() - 8;
auto flags = this->getFlags();
auto layoutWidth = this->getLayoutWidth();
auto showScrollbar = false;
for (int i = int(messagesSnapshot.getLength()) - 1; i >= 0; i--) // convert i to int since it checks >= 0
for (auto i = int(messages.size()) - 1; i >= 0; i--)
{ {
auto *message = messagesSnapshot[i].get(); auto *message = messages[i].get();
message->layout(layoutWidth, this->getScale(), flags); message->layout(layoutWidth, this->getScale(), flags);
h -= message->getHeight(); h -= message->getHeight();
if (h < 0) if (h < 0) // break condition
{ {
this->scrollBar_->setLargeChange( this->scrollBar_->setLargeChange((messages.size() - i) +
(messagesSnapshot.getLength() - i) +
qreal(h) / message->getHeight()); qreal(h) / message->getHeight());
// this->scrollBar.setDesiredValue(this->scrollBar.getDesiredValue());
showScrollbar = true; showScrollbar = true;
break; break;
} }
} }
/// Update scrollbar values
this->scrollBar_->setVisible(showScrollbar); this->scrollBar_->setVisible(showScrollbar);
if (!showScrollbar && !causedByScrollbar) if (!showScrollbar && !causedByScrollbar)
@ -299,26 +386,18 @@ void ChannelView::actuallyLayoutMessages(bool causedByScrollbar)
this->scrollBar_->setDesiredValue(0); this->scrollBar_->setDesiredValue(0);
} }
this->scrollBar_->setMaximum(messagesSnapshot.getLength()); this->scrollBar_->setMaximum(messages.size());
// If we were showing the latest messages and the scrollbar now wants to be // If we were showing the latest messages and the scrollbar now wants to be
// rendered, scroll to bottom // rendered, scroll to bottom
if (this->enableScrollingToBottom_ && this->showingLatestMessages_ && if (this->enableScrollingToBottom_ && this->showingLatestMessages_ &&
showScrollbar) showScrollbar)
{
if (!this->isPaused())
{ {
this->scrollBar_->scrollToBottom( this->scrollBar_->scrollToBottom(
// this->messageWasAdded && // this->messageWasAdded &&
getSettings()->enableSmoothScrollingNewMessages.getValue()); getSettings()->enableSmoothScrollingNewMessages.getValue());
}
this->messageWasAdded_ = false; this->messageWasAdded_ = false;
} }
if (redrawRequired)
{
this->queueUpdate();
}
} }
void ChannelView::clearMessages() void ChannelView::clearMessages()
@ -329,7 +408,7 @@ void ChannelView::clearMessages()
// Layout chat widget messages, and force an update regardless if there are // Layout chat widget messages, and force an update regardless if there are
// no messages // no messages
this->layoutMessages(); this->queueLayout();
this->queueUpdate(); this->queueUpdate();
} }
@ -363,11 +442,8 @@ QString ChannelView::getSelectedText()
? _selection.selectionMax.charIndex ? _selection.selectionMax.charIndex
: layout->getLastCharacterIndex() + 1; : layout->getLastCharacterIndex() + 1;
qDebug() << "from:" << from << ", to:" << to;
layout->addSelectionText(result, from, to); layout->addSelectionText(result, from, to);
} }
qDebug() << "xd <";
return result; return result;
} }
@ -380,7 +456,7 @@ bool ChannelView::hasSelection()
void ChannelView::clearSelection() void ChannelView::clearSelection()
{ {
this->selection_ = Selection(); this->selection_ = Selection();
layoutMessages(); queueLayout();
} }
void ChannelView::setEnableScrollingToBottom(bool value) void ChannelView::setEnableScrollingToBottom(bool value)
@ -406,7 +482,7 @@ const boost::optional<MessageElementFlags> &ChannelView::getOverrideFlags()
LimitedQueueSnapshot<MessageLayoutPtr> ChannelView::getMessagesSnapshot() LimitedQueueSnapshot<MessageLayoutPtr> ChannelView::getMessagesSnapshot()
{ {
if (!this->isPaused() /*|| this->scrollBar_->isVisible()*/) if (!this->paused() /*|| this->scrollBar_->isVisible()*/)
{ {
this->snapshot_ = this->messages.getSnapshot(); this->snapshot_ = this->messages.getSnapshot();
} }
@ -427,6 +503,63 @@ void ChannelView::setChannel(ChannelPtr newChannel)
this->channelConnections_.push_back(newChannel->messageAppended.connect( this->channelConnections_.push_back(newChannel->messageAppended.connect(
[this](MessagePtr &message, [this](MessagePtr &message,
boost::optional<MessageFlags> overridingFlags) { boost::optional<MessageFlags> overridingFlags) {
this->messageAppended(message, overridingFlags);
}));
this->channelConnections_.push_back(
newChannel->messagesAddedAtStart.connect(
[this](std::vector<MessagePtr> &messages) {
this->messageAddedAtStart(messages);
}));
// on message removed
this->channelConnections_.push_back(
newChannel->messageRemovedFromStart.connect(
[this](MessagePtr &message) {
this->messageRemoveFromStart(message);
}));
// on message replaced
this->channelConnections_.push_back(newChannel->messageReplaced.connect(
[this](size_t index, MessagePtr replacement) {
this->messageReplaced(index, replacement);
}));
auto snapshot = newChannel->getMessageSnapshot();
for (size_t i = 0; i < snapshot.size(); i++)
{
MessageLayoutPtr deleted;
auto messageRef = new MessageLayout(snapshot[i]);
if (this->lastMessageHasAlternateBackground_)
{
messageRef->flags.set(MessageLayoutFlag::AlternateBackground);
}
this->lastMessageHasAlternateBackground_ =
!this->lastMessageHasAlternateBackground_;
this->messages.pushBack(MessageLayoutPtr(messageRef), deleted);
}
this->channel_ = newChannel;
this->queueLayout();
this->queueUpdate();
// Notifications
if (auto tc = dynamic_cast<TwitchChannel *>(newChannel.get()))
{
tc->liveStatusChanged.connect([this]() {
this->liveStatusChanged.invoke(); //
});
}
}
void ChannelView::messageAppended(MessagePtr &message,
boost::optional<MessageFlags> overridingFlags)
{
MessageLayoutPtr deleted; MessageLayoutPtr deleted;
auto *messageFlags = &message->flags; auto *messageFlags = &message->flags;
@ -448,11 +581,6 @@ void ChannelView::setChannel(ChannelPtr newChannel)
this->lastMessageHasAlternateBackground_ = this->lastMessageHasAlternateBackground_ =
!this->lastMessageHasAlternateBackground_; !this->lastMessageHasAlternateBackground_;
if (this->isPaused())
{
this->messagesAddedSinceSelectionPause_++;
}
if (this->messages.pushBack(MessageLayoutPtr(messageRef), deleted)) if (this->messages.pushBack(MessageLayoutPtr(messageRef), deleted))
{ {
// if (!this->isPaused()) { // if (!this->isPaused()) {
@ -471,38 +599,33 @@ void ChannelView::setChannel(ChannelPtr newChannel)
{ {
if (messageFlags->has(MessageFlag::Highlighted)) if (messageFlags->has(MessageFlag::Highlighted))
{ {
this->tabHighlightRequested.invoke( this->tabHighlightRequested.invoke(HighlightState::Highlighted);
HighlightState::Highlighted);
} }
else else
{ {
this->tabHighlightRequested.invoke( this->tabHighlightRequested.invoke(HighlightState::NewMessage);
HighlightState::NewMessage);
} }
} }
if (this->channel_->getType() != Channel::Type::TwitchMentions) if (this->channel_->getType() != Channel::Type::TwitchMentions)
{ {
this->scrollBar_->addHighlight( this->scrollBar_->addHighlight(message->getScrollBarHighlight());
message->getScrollBarHighlight());
} }
this->messageWasAdded_ = true; this->messageWasAdded_ = true;
this->layoutMessages(); this->queueLayout();
})); }
this->channelConnections_.push_back( void ChannelView::messageAddedAtStart(std::vector<MessagePtr> &messages)
newChannel->messagesAddedAtStart.connect( {
[this](std::vector<MessagePtr> &messages) {
std::vector<MessageLayoutPtr> messageRefs; std::vector<MessageLayoutPtr> messageRefs;
messageRefs.resize(messages.size()); messageRefs.resize(messages.size());
for (size_t i = 0; i < messages.size(); i++) for (size_t i = 0; i < messages.size(); i++)
{ {
messageRefs.at(i) = messageRefs.at(i) = MessageLayoutPtr(new MessageLayout(messages.at(i)));
MessageLayoutPtr(new MessageLayout(messages.at(i)));
} }
if (!this->isPaused()) // if (!this->isPaused())
{ {
if (this->messages.pushFront(messageRefs).size() > 0) if (this->messages.pushFront(messageRefs).size() > 0)
{ {
@ -521,42 +644,39 @@ void ChannelView::setChannel(ChannelPtr newChannel)
highlights.reserve(messages.size()); highlights.reserve(messages.size());
for (size_t i = 0; i < messages.size(); i++) for (size_t i = 0; i < messages.size(); i++)
{ {
highlights.push_back( highlights.push_back(messages.at(i)->getScrollBarHighlight());
messages.at(i)->getScrollBarHighlight());
} }
this->scrollBar_->addHighlightsAtStart(highlights); this->scrollBar_->addHighlightsAtStart(highlights);
this->messageWasAdded_ = true; this->messageWasAdded_ = true;
this->layoutMessages(); this->queueLayout();
})); }
// on message removed void ChannelView::messageRemoveFromStart(MessagePtr &message)
this->channelConnections_.push_back( {
newChannel->messageRemovedFromStart.connect([this](MessagePtr &) {
this->selection_.selectionMin.messageIndex--; this->selection_.selectionMin.messageIndex--;
this->selection_.selectionMax.messageIndex--; this->selection_.selectionMax.messageIndex--;
this->selection_.start.messageIndex--; this->selection_.start.messageIndex--;
this->selection_.end.messageIndex--; this->selection_.end.messageIndex--;
this->layoutMessages(); this->queueLayout();
})); }
// on message replaced void ChannelView::messageReplaced(size_t index, MessagePtr &replacement)
this->channelConnections_.push_back(newChannel->messageReplaced.connect( {
[this](size_t index, MessagePtr replacement) { if (index >= this->messages.getSnapshot().size() || index < 0)
if (index >= this->messages.getSnapshot().getLength() || index < 0)
{ {
return; return;
} }
MessageLayoutPtr newItem(new MessageLayout(replacement)); MessageLayoutPtr newItem(new MessageLayout(replacement));
auto snapshot = this->messages.getSnapshot(); auto snapshot = this->messages.getSnapshot();
if (index >= snapshot.getLength()) if (index >= snapshot.size())
{ {
log("Tried to replace out of bounds message. Index: {}. " log("Tried to replace out of bounds message. Index: {}. "
"Length: {}", "Length: {}",
index, snapshot.getLength()); index, snapshot.size());
return; return;
} }
@ -566,43 +686,11 @@ void ChannelView::setChannel(ChannelPtr newChannel)
newItem->flags.set(MessageLayoutFlag::AlternateBackground); newItem->flags.set(MessageLayoutFlag::AlternateBackground);
} }
this->scrollBar_->replaceHighlight( this->scrollBar_->replaceHighlight(index,
index, replacement->getScrollBarHighlight()); replacement->getScrollBarHighlight());
this->messages.replaceItem(message, newItem); this->messages.replaceItem(message, newItem);
this->layoutMessages(); this->queueLayout();
}));
auto snapshot = newChannel->getMessageSnapshot();
for (size_t i = 0; i < snapshot.getLength(); i++)
{
MessageLayoutPtr deleted;
auto messageRef = new MessageLayout(snapshot[i]);
if (this->lastMessageHasAlternateBackground_)
{
messageRef->flags.set(MessageLayoutFlag::AlternateBackground);
}
this->lastMessageHasAlternateBackground_ =
!this->lastMessageHasAlternateBackground_;
this->messages.pushBack(MessageLayoutPtr(messageRef), deleted);
}
this->channel_ = newChannel;
this->layoutMessages();
this->queueUpdate();
// Notifications
if (auto tc = dynamic_cast<TwitchChannel *>(newChannel.get()))
{
tc->liveStatusChanged.connect([this]() {
this->liveStatusChanged.invoke(); //
});
}
} }
void ChannelView::detachChannel() void ChannelView::detachChannel()
@ -610,27 +698,13 @@ void ChannelView::detachChannel()
this->channelConnections_.clear(); this->channelConnections_.clear();
} }
void ChannelView::pause(int msecTimeout)
{
this->pausedTemporarily_ = true;
this->updatePauseStatus();
if (this->pauseTimeout_.remainingTime() < msecTimeout)
{
this->pauseTimeout_.stop();
this->pauseTimeout_.start(msecTimeout);
// qDebug() << "pause" << msecTimeout;
}
}
void ChannelView::updateLastReadMessage() void ChannelView::updateLastReadMessage()
{ {
auto _snapshot = this->getMessagesSnapshot(); auto _snapshot = this->getMessagesSnapshot();
if (_snapshot.getLength() > 0) if (_snapshot.size() > 0)
{ {
this->lastReadMessage_ = _snapshot[_snapshot.getLength() - 1]; this->lastReadMessage_ = _snapshot[_snapshot.size() - 1];
} }
this->update(); this->update();
@ -645,7 +719,7 @@ void ChannelView::resizeEvent(QResizeEvent *)
this->scrollBar_->raise(); this->scrollBar_->raise();
this->layoutMessages(); this->queueLayout();
this->update(); this->update();
} }
@ -656,10 +730,10 @@ void ChannelView::setSelection(const SelectionItem &start,
// selections // selections
if (!this->selecting_ && start != end) if (!this->selecting_ && start != end)
{ {
this->messagesAddedSinceSelectionPause_ = 0; // this->messagesAddedSinceSelectionPause_ = 0;
this->selecting_ = true; this->selecting_ = true;
this->pausedBySelection_ = true; // this->pausedBySelection_ = true;
} }
this->selection_ = Selection(start, end); this->selection_ = Selection(start, end);
@ -695,25 +769,6 @@ MessageElementFlags ChannelView::getFlags() const
return flags; return flags;
} }
bool ChannelView::isPaused()
{
// return false;
return this->pausedTemporarily_;
// || this->pausedBySelection_ || this->pausedByScrollingUp_;
}
void ChannelView::updatePauseStatus()
{
if (this->isPaused())
{
this->scrollBar_->pauseHighlights();
}
else
{
this->scrollBar_->unpauseHighlights();
}
}
void ChannelView::paintEvent(QPaintEvent * /*event*/) void ChannelView::paintEvent(QPaintEvent * /*event*/)
{ {
// BenchmarkGuard benchmark("paint"); // BenchmarkGuard benchmark("paint");
@ -724,6 +779,15 @@ void ChannelView::paintEvent(QPaintEvent * /*event*/)
// draw messages // draw messages
this->drawMessages(painter); this->drawMessages(painter);
// draw paused sign
if (this->paused())
{
auto a = this->getScale() * 16;
auto brush = QBrush(QColor(127, 127, 127, 63));
painter.fillRect(QRectF(this->width() - a, a / 4, a / 4, a), brush);
painter.fillRect(QRectF(this->width() - a / 2, a / 4, a / 4, a), brush);
}
} }
// if overlays is false then it draws the message, if true then it draws things // if overlays is false then it draws the message, if true then it draws things
@ -734,7 +798,7 @@ void ChannelView::drawMessages(QPainter &painter)
size_t start = size_t(this->scrollBar_->getCurrentValue()); size_t start = size_t(this->scrollBar_->getCurrentValue());
if (start >= messagesSnapshot.getLength()) if (start >= messagesSnapshot.size())
{ {
return; return;
} }
@ -745,7 +809,7 @@ void ChannelView::drawMessages(QPainter &painter)
MessageLayout *end = nullptr; MessageLayout *end = nullptr;
bool windowFocused = this->window() == QApplication::activeWindow(); bool windowFocused = this->window() == QApplication::activeWindow();
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) for (size_t i = start; i < messagesSnapshot.size(); ++i)
{ {
MessageLayout *layout = messagesSnapshot[i].get(); MessageLayout *layout = messagesSnapshot[i].get();
@ -774,7 +838,7 @@ void ChannelView::drawMessages(QPainter &painter)
// remove messages that are on screen // remove messages that are on screen
// the messages that are left at the end get their buffers reset // the messages that are left at the end get their buffers reset
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) for (size_t i = start; i < messagesSnapshot.size(); ++i)
{ {
auto it = this->messagesOnScreen_.find(messagesSnapshot[i]); auto it = this->messagesOnScreen_.find(messagesSnapshot[i]);
if (it != this->messagesOnScreen_.end()) if (it != this->messagesOnScreen_.end())
@ -792,7 +856,7 @@ void ChannelView::drawMessages(QPainter &painter)
this->messagesOnScreen_.clear(); this->messagesOnScreen_.clear();
// add all messages on screen to the map // add all messages on screen to the map
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) for (size_t i = start; i < messagesSnapshot.size(); ++i)
{ {
std::shared_ptr<MessageLayout> layout = messagesSnapshot[i]; std::shared_ptr<MessageLayout> layout = messagesSnapshot[i];
@ -808,13 +872,7 @@ void ChannelView::drawMessages(QPainter &painter)
void ChannelView::wheelEvent(QWheelEvent *event) void ChannelView::wheelEvent(QWheelEvent *event)
{ {
if (event->orientation() != Qt::Vertical) if (event->orientation() != Qt::Vertical)
{
return; return;
}
this->pausedBySelection_ = false;
this->pausedTemporarily_ = false;
this->updatePauseStatus();
if (event->modifiers() & Qt::ControlModifier) if (event->modifiers() & Qt::ControlModifier)
{ {
@ -830,7 +888,7 @@ void ChannelView::wheelEvent(QWheelEvent *event)
qreal delta = event->delta() * qreal(1.5) * mouseMultiplier; qreal delta = event->delta() * qreal(1.5) * mouseMultiplier;
auto snapshot = this->getMessagesSnapshot(); auto snapshot = this->getMessagesSnapshot();
int snapshotLength = int(snapshot.getLength()); int snapshotLength = int(snapshot.size());
int i = std::min<int>(int(desired), snapshotLength); int i = std::min<int>(int(desired), snapshotLength);
if (delta > 0) if (delta > 0)
@ -888,7 +946,7 @@ void ChannelView::wheelEvent(QWheelEvent *event)
if (i == snapshotLength - 1) if (i == snapshotLength - 1)
{ {
desired = snapshot.getLength(); desired = snapshot.size();
} }
else else
{ {
@ -912,9 +970,7 @@ void ChannelView::enterEvent(QEvent *)
void ChannelView::leaveEvent(QEvent *) void ChannelView::leaveEvent(QEvent *)
{ {
this->pausedTemporarily_ = false; this->queueLayout();
this->updatePauseStatus();
this->layoutMessages();
} }
void ChannelView::mouseMoveEvent(QMouseEvent *event) void ChannelView::mouseMoveEvent(QMouseEvent *event)
@ -927,9 +983,10 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
return; return;
} }
/// Pause on hover
if (getSettings()->pauseChatOnHover.getValue()) if (getSettings()->pauseChatOnHover.getValue())
{ {
this->pause(CHAT_HOVER_PAUSE_DURATION); this->pause(PauseReason::Mouse, 500);
} }
auto tooltipWidget = TooltipWidget::getInstance(); auto tooltipWidget = TooltipWidget::getInstance();
@ -948,7 +1005,7 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
// is selecting // is selecting
if (this->isMouseDown_) if (this->isMouseDown_)
{ {
this->pause(300); // this->pause(PauseReason::Selecting, 300);
int index = layout->getSelectionIndex(relativePos); int index = layout->getSelectionIndex(relativePos);
this->setSelection(this->selection_.start, this->setSelection(this->selection_.start,
@ -1154,7 +1211,7 @@ void ChannelView::mousePressEvent(QMouseEvent *event)
{ {
setCursor(Qt::ArrowCursor); setCursor(Qt::ArrowCursor);
auto messagesSnapshot = this->getMessagesSnapshot(); auto messagesSnapshot = this->getMessagesSnapshot();
if (messagesSnapshot.getLength() == 0) if (messagesSnapshot.size() == 0)
{ {
return; return;
} }
@ -1162,7 +1219,7 @@ void ChannelView::mousePressEvent(QMouseEvent *event)
// Start selection at the last message at its last index // Start selection at the last message at its last index
if (event->button() == Qt::LeftButton) if (event->button() == Qt::LeftButton)
{ {
auto lastMessageIndex = messagesSnapshot.getLength() - 1; auto lastMessageIndex = messagesSnapshot.size() - 1;
auto lastMessage = messagesSnapshot[lastMessageIndex]; auto lastMessage = messagesSnapshot[lastMessageIndex];
auto lastCharacterIndex = lastMessage->getLastCharacterIndex(); auto lastCharacterIndex = lastMessage->getLastCharacterIndex();
@ -1181,13 +1238,11 @@ void ChannelView::mousePressEvent(QMouseEvent *event)
this->isMouseDown_ = true; this->isMouseDown_ = true;
if (layout->flags.has(MessageLayoutFlag::Collapsed)) if (layout->flags.has(MessageLayoutFlag::Collapsed))
{
return; return;
}
if (getSettings()->linksDoubleClickOnly.getValue()) if (getSettings()->linksDoubleClickOnly.getValue())
{ {
this->pause(200); this->pause(PauseReason::DoubleClick, 200);
} }
int index = layout->getSelectionIndex(relativePos); int index = layout->getSelectionIndex(relativePos);
@ -1265,7 +1320,7 @@ void ChannelView::mouseReleaseEvent(QMouseEvent *event)
return; return;
} }
// find message // find message
this->layoutMessages(); this->queueLayout();
std::shared_ptr<MessageLayout> layout; std::shared_ptr<MessageLayout> layout;
QPoint relativePos; QPoint relativePos;
@ -1284,7 +1339,7 @@ void ChannelView::mouseReleaseEvent(QMouseEvent *event)
layout->flags.set(MessageLayoutFlag::Expanded); layout->flags.set(MessageLayoutFlag::Expanded);
layout->flags.set(MessageLayoutFlag::RequiresLayout); layout->flags.set(MessageLayoutFlag::RequiresLayout);
this->layoutMessages(); this->queueLayout();
return; return;
} }
@ -1319,18 +1374,12 @@ void ChannelView::handleMouseClick(QMouseEvent *event,
{ {
if (this->selecting_) if (this->selecting_)
{ {
if (this->messagesAddedSinceSelectionPause_ >
SELECTION_RESUME_SCROLLING_MSG_THRESHOLD)
{
this->showingLatestMessages_ = false;
}
// this->pausedBySelection = false; // this->pausedBySelection = false;
this->selecting_ = false; this->selecting_ = false;
// this->pauseTimeout.stop(); // this->pauseTimeout.stop();
// this->pausedTemporarily = false; // this->pausedTemporarily = false;
this->layoutMessages(); this->queueLayout();
} }
auto &link = hoveredElement->getLink(); auto &link = hoveredElement->getLink();
@ -1601,7 +1650,7 @@ bool ChannelView::tryGetMessageAt(QPoint p,
size_t start = this->scrollBar_->getCurrentValue(); size_t start = this->scrollBar_->getCurrentValue();
if (start >= messagesSnapshot.getLength()) if (start >= messagesSnapshot.size())
{ {
return false; return false;
} }
@ -1609,7 +1658,7 @@ bool ChannelView::tryGetMessageAt(QPoint p,
int y = -(messagesSnapshot[start]->getHeight() * int y = -(messagesSnapshot[start]->getHeight() *
(fmod(this->scrollBar_->getCurrentValue(), 1))); (fmod(this->scrollBar_->getCurrentValue(), 1)));
for (size_t i = start; i < messagesSnapshot.getLength(); ++i) for (size_t i = start; i < messagesSnapshot.size(); ++i)
{ {
auto message = messagesSnapshot[i]; auto message = messagesSnapshot[i];

View file

@ -13,6 +13,7 @@
#include <QWidget> #include <QWidget>
#include <pajlada/signals/signal.hpp> #include <pajlada/signals/signal.hpp>
#include <unordered_map>
#include <unordered_set> #include <unordered_set>
namespace chatterino { namespace chatterino {
@ -21,6 +22,12 @@ enum class HighlightState;
class Channel; class Channel;
using ChannelPtr = std::shared_ptr<Channel>; using ChannelPtr = std::shared_ptr<Channel>;
struct Message;
using MessagePtr = std::shared_ptr<const Message>;
enum class MessageFlag : uint16_t;
using MessageFlags = FlagsEnum<MessageFlag>;
class MessageLayout; class MessageLayout;
using MessageLayoutPtr = std::shared_ptr<MessageLayout>; using MessageLayoutPtr = std::shared_ptr<MessageLayout>;
@ -32,6 +39,14 @@ class EffectLabel;
struct Link; struct Link;
class MessageLayoutElement; class MessageLayoutElement;
enum class PauseReason {
Mouse,
Selection,
DoubleClick,
};
using SteadyClock = std::chrono::steady_clock;
class ChannelView final : public BaseWidget class ChannelView final : public BaseWidget
{ {
Q_OBJECT Q_OBJECT
@ -48,12 +63,15 @@ public:
bool getEnableScrollingToBottom() const; bool getEnableScrollingToBottom() const;
void setOverrideFlags(boost::optional<MessageElementFlags> value); void setOverrideFlags(boost::optional<MessageElementFlags> value);
const boost::optional<MessageElementFlags> &getOverrideFlags() const; const boost::optional<MessageElementFlags> &getOverrideFlags() const;
void pause(int msecTimeout);
void updateLastReadMessage(); void updateLastReadMessage();
bool paused() const;
void pause(PauseReason reason, boost::optional<uint> msecs = boost::none);
void unpause(PauseReason reason);
void setChannel(ChannelPtr channel_); void setChannel(ChannelPtr channel_);
LimitedQueueSnapshot<MessageLayoutPtr> getMessagesSnapshot(); LimitedQueueSnapshot<MessageLayoutPtr> getMessagesSnapshot();
void layoutMessages(); void queueLayout();
void clearMessages(); void clearMessages();
void showUserInfoPopup(const QString &userName); void showUserInfoPopup(const QString &userName);
@ -94,18 +112,23 @@ private:
void initializeScrollbar(); void initializeScrollbar();
void initializeSignals(); void initializeSignals();
// void messageAppended(MessagePtr &message); void messageAppended(MessagePtr &message,
// void messageAddedAtStart(std::vector<MessagePtr> &messages); boost::optional<MessageFlags> overridingFlags);
// void messageRemoveFromStart(MessagePtr &message); void messageAddedAtStart(std::vector<MessagePtr> &messages);
void messageRemoveFromStart(MessagePtr &message);
void messageReplaced(size_t index, MessagePtr &replacement);
void updatePauseStatus();
void detachChannel(); void detachChannel();
void actuallyLayoutMessages(bool causedByScollbar = false);
void performLayout(bool causedByScollbar = false);
void layoutVisibleMessages(
LimitedQueueSnapshot<MessageLayoutPtr> &messages);
void updateScrollbar(LimitedQueueSnapshot<MessageLayoutPtr> &messages,
bool causedByScrollbar);
void drawMessages(QPainter &painter); void drawMessages(QPainter &painter);
void setSelection(const SelectionItem &start, const SelectionItem &end); void setSelection(const SelectionItem &start, const SelectionItem &end);
MessageElementFlags getFlags() const; MessageElementFlags getFlags() const;
bool isPaused();
void selectWholeMessage(MessageLayout *layout, int &messageIndex); void selectWholeMessage(MessageLayout *layout, int &messageIndex);
void getWordBounds(MessageLayout *layout, void getWordBounds(MessageLayout *layout,
const MessageLayoutElement *element, const MessageLayoutElement *element,
@ -117,6 +140,7 @@ private:
void addContextMenuItems(const MessageLayoutElement *hoveredElement, void addContextMenuItems(const MessageLayoutElement *hoveredElement,
MessageLayout *layout); MessageLayout *layout);
int getLayoutWidth() const; int getLayoutWidth() const;
void updatePauseTimer();
QTimer *layoutCooldown_; QTimer *layoutCooldown_;
bool layoutQueued_; bool layoutQueued_;
@ -126,12 +150,11 @@ private:
bool messageWasAdded_ = false; bool messageWasAdded_ = false;
bool lastMessageHasAlternateBackground_ = false; bool lastMessageHasAlternateBackground_ = false;
bool pausedTemporarily_ = false; QTimer pauseTimer_;
bool pausedBySelection_ = false; std::unordered_map<PauseReason, boost::optional<SteadyClock::time_point>>
bool pausedByScrollingUp_ = false; pauses_;
int messagesAddedSinceSelectionPause_ = 0; boost::optional<SteadyClock::time_point> pauseEnd;
QTimer pauseTimeout_;
boost::optional<MessageElementFlags> overrideFlags_; boost::optional<MessageElementFlags> overrideFlags_;
MessageLayoutPtr lastReadMessage_; MessageLayoutPtr lastReadMessage_;
@ -179,7 +202,7 @@ private:
private slots: private slots:
void wordFlagsChanged() void wordFlagsChanged()
{ {
layoutMessages(); queueLayout();
update(); update();
} }
}; };

View file

@ -74,7 +74,7 @@ void SearchPopup::performSearch()
ChannelPtr channel(new Channel("search", Channel::Type::None)); ChannelPtr channel(new Channel("search", Channel::Type::None));
for (size_t i = 0; i < this->snapshot_.getLength(); i++) for (size_t i = 0; i < this->snapshot_.size(); i++)
{ {
MessagePtr message = this->snapshot_[i]; MessagePtr message = this->snapshot_[i];

View file

@ -274,7 +274,7 @@ void Split::setModerationMode(bool value)
{ {
this->moderationMode_ = value; this->moderationMode_ = value;
this->header_->updateModerationModeIcon(); this->header_->updateModerationModeIcon();
this->view_->layoutMessages(); this->view_->queueLayout();
} }
bool Split::getModerationMode() const bool Split::getModerationMode() const
@ -322,7 +322,7 @@ void Split::showChangeChannelPopup(const char *dialogTitle, bool empty,
void Split::layoutMessages() void Split::layoutMessages()
{ {
this->view_->layoutMessages(); this->view_->queueLayout();
} }
void Split::updateGifEmotes() void Split::updateGifEmotes()