mirror-chatterino2/src/widgets/helper/ChannelView.cpp

2901 lines
86 KiB
C++
Raw Normal View History

2018-06-26 14:09:39 +02:00
#include "ChannelView.hpp"
#include "Application.hpp"
#include "common/Common.hpp"
#include "common/QLogging.hpp"
2019-01-20 16:07:31 +01:00
#include "controllers/accounts/AccountController.hpp"
#include "controllers/commands/Command.hpp"
2020-04-11 12:13:38 +02:00
#include "controllers/commands/CommandController.hpp"
#include "controllers/filters/FilterSet.hpp"
2018-06-26 17:20:03 +02:00
#include "debug/Benchmark.hpp"
#include "messages/Emote.hpp"
#include "messages/Image.hpp"
#include "messages/layouts/MessageLayout.hpp"
#include "messages/layouts/MessageLayoutElement.hpp"
2018-06-26 14:09:39 +02:00
#include "messages/LimitedQueueSnapshot.hpp"
#include "messages/Message.hpp"
#include "messages/MessageBuilder.hpp"
#include "messages/MessageElement.hpp"
#include "messages/MessageThread.hpp"
#include "providers/LinkResolver.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchIrcServer.hpp"
#include "singletons/Resources.hpp"
2018-06-28 19:46:45 +02:00
#include "singletons/Settings.hpp"
2018-06-28 20:03:04 +02:00
#include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp"
#include "util/Clipboard.hpp"
2018-06-26 14:09:39 +02:00
#include "util/DistanceBetweenPoints.hpp"
#include "util/Helpers.hpp"
#include "util/IncognitoBrowser.hpp"
#include "util/StreamerMode.hpp"
#include "util/Twitch.hpp"
#include "widgets/dialogs/ReplyThreadPopup.hpp"
#include "widgets/dialogs/SettingsDialog.hpp"
2018-06-26 15:11:45 +02:00
#include "widgets/dialogs/UserInfoPopup.hpp"
#include "widgets/helper/EffectLabel.hpp"
#include "widgets/helper/ScrollbarHighlight.hpp"
#include "widgets/helper/SearchPopup.hpp"
#include "widgets/Scrollbar.hpp"
2018-06-26 17:20:03 +02:00
#include "widgets/splits/Split.hpp"
#include "widgets/splits/SplitInput.hpp"
#include "widgets/TooltipWidget.hpp"
#include "widgets/Window.hpp"
#include <QClipboard>
#include <QColor>
#include <QDate>
#include <QDebug>
#include <QDesktopServices>
#include <QEasingCurve>
#include <QGraphicsBlurEffect>
#include <QMessageBox>
#include <QPainter>
#include <QScreen>
#include <QVariantAnimation>
#include <algorithm>
#include <chrono>
#include <cmath>
#include <functional>
#include <memory>
2017-01-11 01:08:20 +01:00
#define DRAW_WIDTH (this->width())
#define SELECTION_RESUME_SCROLLING_MSG_THRESHOLD 3
2018-06-13 13:27:10 +02:00
#define CHAT_HOVER_PAUSE_DURATION 1000
2017-01-18 21:30:23 +01:00
namespace chatterino {
2018-08-02 14:23:27 +02:00
namespace {
2018-08-15 22:46:20 +02:00
void addEmoteContextMenuItems(const Emote &emote,
MessageElementFlags creatorFlags, QMenu &menu)
{
auto openAction = menu.addAction("Open");
auto openMenu = new QMenu;
openAction->setMenu(openMenu);
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
auto copyAction = menu.addAction("Copy");
auto copyMenu = new QMenu;
copyAction->setMenu(copyMenu);
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
// see if the QMenu actually gets destroyed
QObject::connect(openMenu, &QMenu::destroyed, [] {
QMessageBox(QMessageBox::Information, "xD", "the menu got deleted")
.exec();
});
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
// Add copy and open links for 1x, 2x, 3x
auto addImageLink = [&](const ImagePtr &image, char scale) {
2018-10-21 13:43:02 +02:00
if (!image->isEmpty())
{
copyMenu->addAction(QString(scale) + "x link",
[url = image->url()] {
crossPlatformCopy(url.string);
});
2018-08-15 22:46:20 +02:00
openMenu->addAction(
QString(scale) + "x link", [url = image->url()] {
QDesktopServices::openUrl(QUrl(url.string));
});
}
};
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
addImageLink(emote.images.getImage1(), '1');
addImageLink(emote.images.getImage2(), '2');
addImageLink(emote.images.getImage3(), '3');
2018-08-02 14:23:27 +02:00
2018-08-15 22:46:20 +02:00
// Copy and open emote page link
auto addPageLink = [&](const QString &name) {
copyMenu->addSeparator();
openMenu->addSeparator();
2018-08-02 14:23:27 +02:00
copyMenu->addAction("Copy " + name + " emote link",
[url = emote.homePage] {
crossPlatformCopy(url.string);
});
openMenu->addAction("Open " + name + " emote link",
[url = emote.homePage] {
QDesktopServices::openUrl(QUrl(url.string));
});
2018-08-15 22:46:20 +02:00
};
2018-08-02 14:23:27 +02:00
if (creatorFlags.has(MessageElementFlag::BttvEmote))
2018-10-21 13:43:02 +02:00
{
2018-08-15 22:46:20 +02:00
addPageLink("BTTV");
2018-10-21 13:43:02 +02:00
}
else if (creatorFlags.has(MessageElementFlag::FfzEmote))
{
2018-08-15 22:46:20 +02:00
addPageLink("FFZ");
2018-08-02 14:23:27 +02:00
}
else if (creatorFlags.has(MessageElementFlag::SevenTVEmote))
{
addPageLink("7TV");
}
2018-08-02 14:23:27 +02:00
}
// Current function: https://www.desmos.com/calculator/vdyamchjwh
qreal highlightEasingFunction(qreal progress)
{
if (progress <= 0.1)
{
return 1.0 - pow(10.0 * progress, 3.0);
}
return 1.0 + pow((20.0 / 9.0) * (0.5 * progress - 0.5), 3.0);
}
2018-08-02 14:23:27 +02:00
} // namespace
2017-01-18 21:30:23 +01:00
ChannelView::ChannelView(BaseWidget *parent, Split *split, Context context,
size_t messagesLimit)
: BaseWidget(parent)
, split_(split)
, scrollBar_(new Scrollbar(messagesLimit, this))
, highlightAnimation_(this)
, context_(context)
, messages_(messagesLimit)
2017-01-01 02:30:42 +01:00
{
this->setMouseTracking(true);
2017-01-22 12:46:35 +01:00
2018-08-08 20:06:20 +02:00
this->initializeLayout();
this->initializeScrollbar();
this->initializeSignals();
this->cursors_.neutral = QCursor(getResources().scrolling.neutralScroll);
this->cursors_.up = QCursor(getResources().scrolling.upScroll);
this->cursors_.down = QCursor(getResources().scrolling.downScroll);
2018-11-03 21:26:57 +01:00
this->pauseTimer_.setSingleShot(true);
QObject::connect(&this->pauseTimer_, &QTimer::timeout, this, [this] {
/// remove elements that are finite
for (auto it = this->pauses_.begin(); it != this->pauses_.end();)
it = it->second ? this->pauses_.erase(it) : ++it;
this->updatePauses();
2017-06-06 17:18:23 +02:00
});
2017-09-17 02:13:57 +02:00
// This shortcut is not used in splits, it's used in views that
// don't have a SplitInput like the SearchPopup or EmotePopup.
// See SplitInput::installKeyPressedEvent for the copy event
// from views with a SplitInput.
2020-09-26 01:19:47 +02:00
auto shortcut = new QShortcut(QKeySequence::StandardKey::Copy, this);
QObject::connect(shortcut, &QShortcut::activated, [this] {
this->copySelectedText();
});
this->clickTimer_ = new QTimer(this);
this->clickTimer_->setSingleShot(true);
this->clickTimer_->setInterval(500);
this->scrollTimer_.setInterval(20);
QObject::connect(&this->scrollTimer_, &QTimer::timeout, this,
&ChannelView::scrollUpdateRequested);
// TODO: Figure out if we need this, and if so, why
// StrongFocus means we can focus this event through clicking it
// and tabbing to it from another widget. I don't currently know
// of any place where you can, or where it would make sense,
// to tab to a ChannelVieChannelView
this->setFocusPolicy(Qt::FocusPolicy::ClickFocus);
this->setupHighlightAnimationColors();
this->highlightAnimation_.setDuration(1500);
auto curve = QEasingCurve();
curve.setCustomType(highlightEasingFunction);
this->highlightAnimation_.setEasingCurve(curve);
2018-08-08 20:06:20 +02:00
}
2018-06-13 03:58:52 +02:00
2018-08-08 20:06:20 +02:00
void ChannelView::initializeLayout()
{
2018-08-08 15:35:54 +02:00
this->goToBottom_ = new EffectLabel(this, 0);
2018-08-06 21:17:03 +02:00
this->goToBottom_->setStyleSheet(
"background-color: rgba(0,0,0,0.66); color: #FFF;");
2018-06-13 13:27:10 +02:00
this->goToBottom_->getLabel().setText("More messages below");
this->goToBottom_->setVisible(false);
QObject::connect(
this->goToBottom_, &EffectLabel::leftClicked, this, [this] {
QTimer::singleShot(180, [this] {
this->scrollBar_->scrollToBottom(
getSettings()->enableSmoothScrollingNewMessages.getValue());
});
2018-01-05 03:14:46 +01:00
});
2018-08-08 20:06:20 +02:00
}
2018-08-08 20:06:20 +02:00
void ChannelView::initializeScrollbar()
{
this->scrollBar_->getCurrentValueChanged().connect([this] {
2018-11-03 21:26:57 +01:00
this->performLayout(true);
2018-08-08 20:06:20 +02:00
this->queueUpdate();
});
2018-05-23 13:54:42 +02:00
}
2017-01-22 12:46:35 +01:00
2018-08-08 20:06:20 +02:00
void ChannelView::initializeSignals()
2017-01-22 12:46:35 +01:00
{
this->signalHolder_.managedConnect(getApp()->windows->wordFlagsChanged,
[this] {
this->queueLayout();
this->update();
});
2018-08-08 20:06:20 +02:00
getSettings()->showLastMessageIndicator.connect(
[this](auto, auto) {
this->update();
},
this->signalHolder_);
2018-08-08 20:06:20 +02:00
this->signalHolder_.managedConnect(getApp()->windows->gifRepaintRequested,
[&] {
this->queueUpdate();
});
2018-08-08 20:06:20 +02:00
this->signalHolder_.managedConnect(
getApp()->windows->layoutRequested, [&](Channel *channel) {
2018-11-14 17:26:08 +01:00
if (this->isVisible() &&
(channel == nullptr || this->channel_.get() == channel))
{
2018-11-03 21:26:57 +01:00
this->queueLayout();
2018-11-14 17:26:08 +01:00
}
});
2018-08-08 20:06:20 +02:00
this->signalHolder_.managedConnect(getApp()->fonts->fontChanged, [this] {
this->queueLayout();
});
2018-11-03 21:26:57 +01:00
}
bool ChannelView::pausable() const
{
return pausable_;
}
void ChannelView::setPausable(bool value)
{
this->pausable_ = value;
}
2018-11-03 21:26:57 +01:00
bool ChannelView::paused() const
{
/// No elements in the map -> not paused
return this->pausable() && !this->pauses_.empty();
2018-11-03 21:26:57 +01:00
}
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->updatePauses();
2018-11-03 21:26:57 +01:00
}
void ChannelView::unpause(PauseReason reason)
{
/// Remove the value from the map
this->pauses_.erase(reason);
this->updatePauses();
2018-11-03 21:26:57 +01:00
}
void ChannelView::updatePauses()
2018-11-03 21:26:57 +01:00
{
using namespace std::chrono;
if (this->pauses_.empty())
{
this->unpaused();
2018-11-03 21:26:57 +01:00
/// No pauses so we can stop the timer
this->pauseEnd_ = boost::none;
2018-11-03 21:26:57 +01:00
this->pauseTimer_.stop();
this->scrollBar_->offset(this->pauseScrollOffset_);
this->pauseScrollOffset_ = 0;
2018-11-03 21:26:57 +01:00
this->queueLayout();
}
else if (std::any_of(this->pauses_.begin(), this->pauses_.end(),
[](auto &&value) {
return !value.second;
}))
2018-11-03 21:26:57 +01:00
{
/// Some of the pauses are infinite
this->pauseEnd_ = boost::none;
2018-11-03 21:26:57 +01:00
this->pauseTimer_.stop();
}
else
{
/// Get the maximum pause
auto pauseEnd =
std::max_element(this->pauses_.begin(), this->pauses_.end(),
[](auto &&a, auto &&b) {
return a.second > b.second;
})
->second.get();
2018-11-03 21:26:57 +01:00
if (pauseEnd != this->pauseEnd_)
2018-11-03 21:26:57 +01:00
{
/// Start the timer
this->pauseEnd_ = pauseEnd;
2018-11-03 21:26:57 +01:00
this->pauseTimer_.start(
duration_cast<milliseconds>(pauseEnd - SteadyClock::now()));
2018-11-03 21:26:57 +01:00
}
}
2017-01-01 02:30:42 +01:00
}
2017-01-03 21:19:33 +01:00
void ChannelView::unpaused()
{
/// Move selection
this->selection_.shiftMessageIndex(this->pauseSelectionOffset_);
this->pauseSelectionOffset_ = 0;
}
2018-07-06 17:11:37 +02:00
void ChannelView::themeChangedEvent()
2018-01-24 20:35:26 +01:00
{
2018-07-06 17:11:37 +02:00
BaseWidget::themeChangedEvent();
2018-01-24 20:35:26 +01:00
this->setupHighlightAnimationColors();
2018-11-03 21:26:57 +01:00
this->queueLayout();
2018-01-24 20:35:26 +01:00
}
void ChannelView::setupHighlightAnimationColors()
{
this->highlightAnimation_.setStartValue(
this->theme->messages.highlightAnimationStart);
this->highlightAnimation_.setEndValue(
this->theme->messages.highlightAnimationEnd);
}
2018-12-02 18:26:21 +01:00
void ChannelView::scaleChangedEvent(float scale)
{
BaseWidget::scaleChangedEvent(scale);
if (this->goToBottom_)
{
auto factor = this->qtFontScale();
#ifdef Q_OS_MACOS
factor = scale * 80.f /
std::max<float>(
0.01, this->logicalDpiX() * this->devicePixelRatioF());
#endif
2018-12-02 18:26:21 +01:00
this->goToBottom_->getLabel().setFont(
getFonts()->getFont(FontStyle::UiMedium, factor));
2018-12-02 18:26:21 +01:00
}
}
void ChannelView::queueUpdate()
2017-01-03 21:19:33 +01:00
{
// if (this->updateTimer.isActive()) {
// this->updateQueued = true;
// return;
// }
2018-01-19 23:41:02 +01:00
// this->repaint();
2019-06-04 19:59:08 +02:00
2018-01-19 23:41:02 +01:00
this->update();
// this->updateTimer.start();
}
2018-11-03 21:26:57 +01:00
void ChannelView::queueLayout()
{
2018-04-10 02:07:25 +02:00
// if (!this->layoutCooldown->isActive()) {
2018-11-03 21:26:57 +01:00
this->performLayout();
2018-04-10 02:07:25 +02:00
// this->layoutCooldown->start();
// } else {
// this->layoutQueued = true;
// }
}
2018-11-03 21:26:57 +01:00
void ChannelView::performLayout(bool causedByScrollbar)
{
2018-11-03 21:26:57 +01:00
// BenchmarkGuard benchmark("layout");
2018-11-03 21:26:57 +01:00
/// Get messages and check if there are at least 1
const auto &messages = this->getMessagesSnapshot();
2018-08-06 21:17:03 +02:00
this->showingLatestMessages_ =
this->scrollBar_->isAtBottom() ||
(!this->scrollBar_->isVisible() && !causedByScrollbar);
2017-06-06 17:18:23 +02:00
2018-11-03 21:26:57 +01:00
/// Layout visible messages
this->layoutVisibleMessages(messages);
/// Update scrollbar
this->updateScrollbar(messages, causedByScrollbar);
this->goToBottom_->setVisible(this->enableScrollingToBottom_ &&
this->scrollBar_->isVisible() &&
!this->scrollBar_->isAtBottom());
2018-11-03 21:26:57 +01:00
}
2018-11-03 21:26:57 +01:00
void ChannelView::layoutVisibleMessages(
const LimitedQueueSnapshot<MessageLayoutPtr> &messages)
2018-11-03 21:26:57 +01:00
{
const auto start = size_t(this->scrollBar_->getCurrentValue());
const auto layoutWidth = this->getLayoutWidth();
const auto flags = this->getFlags();
auto redrawRequired = false;
2018-01-17 16:52:51 +01:00
2018-11-03 21:26:57 +01:00
if (messages.size() > start)
2018-10-21 13:43:02 +02:00
{
2018-11-03 21:26:57 +01:00
auto y = int(-(messages[start]->getHeight() *
(fmod(this->scrollBar_->getCurrentValue(), 1))));
2018-12-02 19:20:14 +01:00
for (auto i = start; i < messages.size() && y <= this->height(); i++)
2018-10-21 13:43:02 +02:00
{
const auto &message = messages[i];
2018-08-06 21:17:03 +02:00
redrawRequired |=
2018-11-21 21:37:41 +01:00
message->layout(layoutWidth, this->scale(), flags);
2017-02-02 20:35:12 +01:00
y += message->getHeight();
}
}
2018-11-03 21:26:57 +01:00
if (redrawRequired)
{
2018-11-03 21:26:57 +01:00
this->queueUpdate();
}
2018-11-03 21:26:57 +01:00
}
void ChannelView::updateScrollbar(
const LimitedQueueSnapshot<MessageLayoutPtr> &messages,
bool causedByScrollbar)
2018-11-03 21:26:57 +01:00
{
if (messages.size() == 0)
{
this->scrollBar_->setVisible(false);
return;
}
2017-01-26 04:26:40 +01:00
2018-11-03 21:26:57 +01:00
/// Layout the messages at the bottom
auto h = this->height() - 8;
auto flags = this->getFlags();
auto layoutWidth = this->getLayoutWidth();
auto showScrollbar = false;
// convert i to int since it checks >= 0
for (auto i = int(messages.size()) - 1; i >= 0; i--)
2018-10-21 13:43:02 +02:00
{
2018-11-03 21:26:57 +01:00
auto *message = messages[i].get();
2018-11-21 21:37:41 +01:00
message->layout(layoutWidth, this->scale(), flags);
2017-01-26 04:26:40 +01:00
2017-01-26 07:10:46 +01:00
h -= message->getHeight();
2018-11-03 21:26:57 +01:00
if (h < 0) // break condition
2018-10-21 13:43:02 +02:00
{
this->scrollBar_->setLargeChange(
(messages.size() - i) +
qreal(h) / std::max<int>(1, message->getHeight()));
2017-01-26 07:10:46 +01:00
showScrollbar = true;
2017-02-01 16:28:28 +01:00
break;
2017-01-26 07:10:46 +01:00
}
2017-01-26 04:26:40 +01:00
}
2018-11-03 21:26:57 +01:00
/// Update scrollbar values
this->scrollBar_->setVisible(showScrollbar);
2017-01-26 07:10:46 +01:00
2018-10-21 13:43:02 +02:00
if (!showScrollbar && !causedByScrollbar)
{
this->scrollBar_->setDesiredValue(0);
2017-02-07 00:03:15 +01:00
}
this->showScrollBar_ = showScrollbar;
2017-02-07 00:03:15 +01:00
2018-11-03 21:26:57 +01:00
this->scrollBar_->setMaximum(messages.size());
2017-01-26 07:10:46 +01:00
2018-08-06 21:17:03 +02:00
// If we were showing the latest messages and the scrollbar now wants to be
// rendered, scroll to bottom
if (this->enableScrollingToBottom_ && this->showingLatestMessages_ &&
2018-10-21 13:43:02 +02:00
showScrollbar)
{
2018-11-03 21:26:57 +01:00
this->scrollBar_->scrollToBottom(
// this->messageWasAdded &&
getSettings()->enableSmoothScrollingNewMessages.getValue());
2018-06-13 13:27:10 +02:00
this->messageWasAdded_ = false;
2017-06-06 17:18:23 +02:00
}
2017-01-26 04:26:40 +01:00
}
void ChannelView::clearMessages()
{
// Clear all stored messages in this chat widget
2018-11-14 17:26:08 +01:00
this->messages_.clear();
this->scrollBar_->clearHighlights();
2018-11-03 21:26:57 +01:00
this->queueLayout();
this->lastMessageHasAlternateBackground_ = false;
this->lastMessageHasAlternateBackgroundReverse_ = true;
}
2018-01-06 03:48:56 +01:00
Scrollbar &ChannelView::getScrollBar()
2017-04-12 17:46:44 +02:00
{
return *this->scrollBar_;
2017-04-12 17:46:44 +02:00
}
QString ChannelView::getSelectedText()
2017-09-12 19:06:16 +02:00
{
2018-01-16 02:39:31 +01:00
QString result = "";
2017-09-12 19:06:16 +02:00
LimitedQueueSnapshot<MessageLayoutPtr> &messagesSnapshot =
2018-08-06 21:17:03 +02:00
this->getMessagesSnapshot();
2017-09-12 19:06:16 +02:00
Selection selection = this->selection_;
2017-09-12 19:06:16 +02:00
if (selection.isEmpty())
2018-10-21 13:43:02 +02:00
{
2018-01-16 02:39:31 +01:00
return result;
}
2017-09-12 19:06:16 +02:00
const auto numMessages = messagesSnapshot.size();
const auto indexStart = selection.selectionMin.messageIndex;
const auto indexEnd = selection.selectionMax.messageIndex;
if (indexEnd >= numMessages || indexStart >= numMessages)
{
// One of our messages is out of bounds
return result;
}
for (auto msg = indexStart; msg <= indexEnd; msg++)
2018-10-21 13:43:02 +02:00
{
2018-01-16 02:39:31 +01:00
MessageLayoutPtr layout = messagesSnapshot[msg];
auto from = msg == selection.selectionMin.messageIndex
? selection.selectionMin.charIndex
: 0;
auto to = msg == selection.selectionMax.messageIndex
? selection.selectionMax.charIndex
: layout->getLastCharacterIndex() + 1;
2018-04-10 15:48:56 +02:00
2018-01-16 02:39:31 +01:00
layout->addSelectionText(result, from, to);
}
2017-09-12 19:06:16 +02:00
2018-01-16 02:39:31 +01:00
return result;
2017-09-12 19:06:16 +02:00
}
2017-09-21 02:20:02 +02:00
bool ChannelView::hasSelection()
{
2018-06-13 13:27:10 +02:00
return !this->selection_.isEmpty();
2017-09-21 02:20:02 +02:00
}
void ChannelView::clearSelection()
{
2018-06-13 13:27:10 +02:00
this->selection_ = Selection();
2018-11-03 21:26:57 +01:00
queueLayout();
2017-09-21 02:20:02 +02:00
}
void ChannelView::copySelectedText()
{
crossPlatformCopy(this->getSelectedText());
}
void ChannelView::setEnableScrollingToBottom(bool value)
{
2018-06-13 13:27:10 +02:00
this->enableScrollingToBottom_ = value;
}
bool ChannelView::getEnableScrollingToBottom() const
{
2018-06-13 13:27:10 +02:00
return this->enableScrollingToBottom_;
}
2018-08-07 07:55:31 +02:00
void ChannelView::setOverrideFlags(boost::optional<MessageElementFlags> value)
2018-01-27 21:13:22 +01:00
{
this->overrideFlags_ = std::move(value);
2018-01-27 21:13:22 +01:00
}
2018-08-07 07:55:31 +02:00
const boost::optional<MessageElementFlags> &ChannelView::getOverrideFlags()
2018-08-06 21:17:03 +02:00
const
2018-01-27 21:13:22 +01:00
{
2018-06-13 13:27:10 +02:00
return this->overrideFlags_;
2018-01-27 21:13:22 +01:00
}
LimitedQueueSnapshot<MessageLayoutPtr> &ChannelView::getMessagesSnapshot()
{
this->snapshotGuard_.guard();
2018-11-03 21:26:57 +01:00
if (!this->paused() /*|| this->scrollBar_->isVisible()*/)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
this->snapshot_ = this->messages_.getSnapshot();
2018-06-13 13:27:10 +02:00
}
2018-06-13 13:27:10 +02:00
return this->snapshot_;
}
2018-11-14 17:26:08 +01:00
ChannelPtr ChannelView::channel()
{
return this->channel_;
}
bool ChannelView::showScrollbarHighlights() const
{
return this->channel_->getType() != Channel::Type::TwitchMentions;
}
void ChannelView::setChannel(ChannelPtr underlyingChannel)
{
2018-11-14 17:26:08 +01:00
/// Clear connections from the last channel
this->channelConnections_.clear();
2018-04-20 19:54:45 +02:00
this->clearMessages();
this->scrollBar_->clearHighlights();
/// make copy of channel and expose
this->channel_ = std::make_unique<Channel>(underlyingChannel->getName(),
underlyingChannel->getType());
//
// Proxy channel connections
// Use a proxy channel to keep filtered messages past the time they are removed from their origin channel
//
this->channelConnections_.managedConnect(
underlyingChannel->messageAppended,
[this](MessagePtr &message,
boost::optional<MessageFlags> overridingFlags) {
if (this->shouldIncludeMessage(message))
{
if (this->channel_->lastDate_ != QDate::currentDate())
{
this->channel_->lastDate_ = QDate::currentDate();
auto msg = makeSystemMessage(
QLocale().toString(QDate::currentDate(),
QLocale::LongFormat),
QTime(0, 0));
this->channel_->addMessage(msg);
}
// When the message was received in the underlyingChannel,
// logging will be handled. Prevent duplications.
if (overridingFlags)
{
overridingFlags.get().set(MessageFlag::DoNotLog);
}
else
{
overridingFlags = MessageFlags(message->flags);
overridingFlags.get().set(MessageFlag::DoNotLog);
}
this->channel_->addMessage(message, overridingFlags);
}
});
this->channelConnections_.managedConnect(
underlyingChannel->messagesAddedAtStart,
[this](std::vector<MessagePtr> &messages) {
std::vector<MessagePtr> filtered;
std::copy_if(messages.begin(), messages.end(),
std::back_inserter(filtered), [this](MessagePtr msg) {
return this->shouldIncludeMessage(msg);
});
if (!filtered.empty())
this->channel_->addMessagesAtStart(filtered);
});
this->channelConnections_.managedConnect(
underlyingChannel->messageReplaced,
[this](size_t index, MessagePtr replacement) {
if (this->shouldIncludeMessage(replacement))
this->channel_->replaceMessage(index, replacement);
});
this->channelConnections_.managedConnect(
underlyingChannel->filledInMessages, [this](const auto &messages) {
std::vector<MessagePtr> filtered;
filtered.reserve(messages.size());
std::copy_if(messages.begin(), messages.end(),
std::back_inserter(filtered), [this](MessagePtr msg) {
return this->shouldIncludeMessage(msg);
});
this->channel_->fillInMissingMessages(filtered);
});
//
// Standard channel connections
//
// on new message
this->channelConnections_.managedConnect(
this->channel_->messageAppended,
[this](MessagePtr &message,
boost::optional<MessageFlags> overridingFlags) {
this->messageAppended(message, std::move(overridingFlags));
});
this->channelConnections_.managedConnect(
this->channel_->messagesAddedAtStart,
[this](std::vector<MessagePtr> &messages) {
this->messageAddedAtStart(messages);
});
// on message removed
this->channelConnections_.managedConnect(
this->channel_->messageRemovedFromStart, [this](MessagePtr &message) {
this->messageRemoveFromStart(message);
});
2018-01-05 23:14:55 +01:00
// on message replaced
this->channelConnections_.managedConnect(
this->channel_->messageReplaced,
2018-08-06 21:17:03 +02:00
[this](size_t index, MessagePtr replacement) {
2018-11-03 21:26:57 +01:00
this->messageReplaced(index, replacement);
});
2018-01-05 23:14:55 +01:00
// on messages filled in
this->channelConnections_.managedConnect(this->channel_->filledInMessages,
[this](const auto &) {
this->messagesUpdated();
});
auto snapshot = underlyingChannel->getMessageSnapshot();
for (const auto &msg : snapshot)
2018-10-21 13:43:02 +02:00
{
auto messageLayout = std::make_shared<MessageLayout>(msg);
2018-10-21 13:43:02 +02:00
if (this->lastMessageHasAlternateBackground_)
{
messageLayout->flags.set(MessageLayoutFlag::AlternateBackground);
}
2018-08-06 21:17:03 +02:00
this->lastMessageHasAlternateBackground_ =
!this->lastMessageHasAlternateBackground_;
if (underlyingChannel->shouldIgnoreHighlights())
2019-09-21 22:19:03 +02:00
{
2019-09-22 10:42:22 +02:00
messageLayout->flags.set(MessageLayoutFlag::IgnoreHighlights);
2019-09-21 22:19:03 +02:00
}
this->messages_.pushBack(messageLayout);
if (this->showScrollbarHighlights())
{
this->scrollBar_->addHighlight(msg->getScrollBarHighlight());
}
}
this->underlyingChannel_ = underlyingChannel;
2018-11-03 21:26:57 +01:00
this->queueLayout();
2018-01-07 00:05:32 +01:00
this->queueUpdate();
// Notifications
if (auto tc = dynamic_cast<TwitchChannel *>(underlyingChannel.get()))
2018-10-21 13:43:02 +02:00
{
this->channelConnections_.managedConnect(
tc->liveStatusChanged, [this]() {
this->liveStatusChanged.invoke();
});
}
}
void ChannelView::setFilters(const QList<QUuid> &ids)
{
this->channelFilters_ = std::make_shared<FilterSet>(ids);
}
const QList<QUuid> ChannelView::getFilterIds() const
{
if (!this->channelFilters_)
{
return QList<QUuid>();
}
return this->channelFilters_->filterIds();
}
FilterSetPtr ChannelView::getFilterSet() const
{
return this->channelFilters_;
}
bool ChannelView::shouldIncludeMessage(const MessagePtr &m) const
{
if (this->channelFilters_)
{
if (getSettings()->excludeUserMessagesFromFilter &&
getApp()->accounts->twitch.getCurrent()->getUserName().compare(
m->loginName, Qt::CaseInsensitive) == 0)
return true;
return this->channelFilters_->filter(m, this->channel_);
}
return true;
}
ChannelPtr ChannelView::sourceChannel() const
{
return this->sourceChannel_;
}
void ChannelView::setSourceChannel(ChannelPtr sourceChannel)
{
this->sourceChannel_ = std::move(sourceChannel);
}
bool ChannelView::hasSourceChannel() const
{
return this->sourceChannel_ != nullptr;
}
2018-11-03 21:26:57 +01:00
void ChannelView::messageAppended(MessagePtr &message,
boost::optional<MessageFlags> overridingFlags)
{
2018-11-03 21:26:57 +01:00
auto *messageFlags = &message->flags;
if (overridingFlags)
{
messageFlags = overridingFlags.get_ptr();
}
auto messageRef = std::make_shared<MessageLayout>(message);
2018-11-03 21:26:57 +01:00
if (this->lastMessageHasAlternateBackground_)
{
messageRef->flags.set(MessageLayoutFlag::AlternateBackground);
}
if (this->channel_->shouldIgnoreHighlights())
{
messageRef->flags.set(MessageLayoutFlag::IgnoreHighlights);
}
this->lastMessageHasAlternateBackground_ =
!this->lastMessageHasAlternateBackground_;
if (!this->scrollBar_->isAtBottom() &&
this->scrollBar_->getCurrentValueAnimation().state() ==
QPropertyAnimation::Running)
{
QEventLoop loop;
connect(&this->scrollBar_->getCurrentValueAnimation(),
&QAbstractAnimation::stateChanged, &loop, &QEventLoop::quit);
loop.exec();
}
if (this->messages_.pushBack(messageRef))
2018-11-03 21:26:57 +01:00
{
if (this->paused())
2018-11-03 21:26:57 +01:00
{
if (!this->scrollBar_->isAtBottom())
this->pauseScrollOffset_--;
2018-11-03 21:26:57 +01:00
}
else
{
if (this->scrollBar_->isAtBottom())
this->scrollBar_->scrollToBottom();
else
this->scrollBar_->offset(-1);
2018-11-03 21:26:57 +01:00
}
}
if (!messageFlags->has(MessageFlag::DoNotTriggerNotification))
{
if (messageFlags->has(MessageFlag::Highlighted) &&
messageFlags->has(MessageFlag::ShowInMentions) &&
!messageFlags->has(MessageFlag::Subscription) &&
(getSettings()->highlightMentions ||
this->channel_->getType() != Channel::Type::TwitchMentions))
2018-11-03 21:26:57 +01:00
{
this->tabHighlightRequested.invoke(HighlightState::Highlighted);
}
else
{
this->tabHighlightRequested.invoke(HighlightState::NewMessage);
}
}
if (this->showScrollbarHighlights())
2018-11-03 21:26:57 +01:00
{
this->scrollBar_->addHighlight(message->getScrollBarHighlight());
}
this->messageWasAdded_ = true;
this->queueLayout();
}
2018-11-03 21:26:57 +01:00
void ChannelView::messageAddedAtStart(std::vector<MessagePtr> &messages)
{
2018-11-03 21:26:57 +01:00
std::vector<MessageLayoutPtr> messageRefs;
messageRefs.resize(messages.size());
2018-11-03 21:40:48 +01:00
/// Create message layouts
2018-11-03 21:26:57 +01:00
for (size_t i = 0; i < messages.size(); i++)
{
auto message = messages.at(i);
auto layout = std::make_shared<MessageLayout>(message);
2018-11-03 21:40:48 +01:00
// alternate color
if (!this->lastMessageHasAlternateBackgroundReverse_)
layout->flags.set(MessageLayoutFlag::AlternateBackground);
this->lastMessageHasAlternateBackgroundReverse_ =
!this->lastMessageHasAlternateBackgroundReverse_;
messageRefs.at(i) = std::move(layout);
2018-11-03 21:26:57 +01:00
}
2018-11-03 21:40:48 +01:00
/// Add the messages at the start
2018-11-14 17:26:08 +01:00
if (this->messages_.pushFront(messageRefs).size() > 0)
2018-10-21 13:43:02 +02:00
{
2018-11-03 21:40:48 +01:00
if (this->scrollBar_->isAtBottom())
this->scrollBar_->scrollToBottom();
else
this->scrollBar_->offset(qreal(messages.size()));
2018-11-03 21:26:57 +01:00
}
if (this->showScrollbarHighlights())
2018-11-03 21:26:57 +01:00
{
std::vector<ScrollbarHighlight> highlights;
highlights.reserve(messages.size());
for (const auto &message : messages)
{
highlights.push_back(message->getScrollBarHighlight());
}
2018-11-03 21:26:57 +01:00
this->scrollBar_->addHighlightsAtStart(highlights);
}
2018-11-03 21:26:57 +01:00
this->messageWasAdded_ = true;
this->queueLayout();
}
void ChannelView::messageRemoveFromStart(MessagePtr &message)
{
if (this->paused())
{
this->pauseSelectionOffset_ += 1;
}
else
{
this->selection_.shiftMessageIndex(1);
}
2018-06-13 13:27:10 +02:00
2018-11-03 21:26:57 +01:00
this->queueLayout();
}
void ChannelView::messageReplaced(size_t index, MessagePtr &replacement)
{
auto oMessage = this->messages_.get(index);
if (!oMessage)
2018-11-03 21:26:57 +01:00
{
return;
}
auto message = *oMessage;
auto newItem = std::make_shared<MessageLayout>(replacement);
2018-11-03 21:26:57 +01:00
if (message->flags.has(MessageLayoutFlag::AlternateBackground))
{
newItem->flags.set(MessageLayoutFlag::AlternateBackground);
}
this->scrollBar_->replaceHighlight(index,
replacement->getScrollBarHighlight());
2018-11-14 17:26:08 +01:00
this->messages_.replaceItem(message, newItem);
2018-11-03 21:26:57 +01:00
this->queueLayout();
}
void ChannelView::messagesUpdated()
{
auto snapshot = this->channel_->getMessageSnapshot();
this->messages_.clear();
this->scrollBar_->clearHighlights();
this->lastMessageHasAlternateBackground_ = false;
this->lastMessageHasAlternateBackgroundReverse_ = true;
for (const auto &msg : snapshot)
{
auto messageLayout = std::make_shared<MessageLayout>(msg);
if (this->lastMessageHasAlternateBackground_)
{
messageLayout->flags.set(MessageLayoutFlag::AlternateBackground);
}
this->lastMessageHasAlternateBackground_ =
!this->lastMessageHasAlternateBackground_;
if (this->channel_->shouldIgnoreHighlights())
{
messageLayout->flags.set(MessageLayoutFlag::IgnoreHighlights);
}
this->messages_.pushBack(messageLayout);
if (this->showScrollbarHighlights())
{
this->scrollBar_->addHighlight(msg->getScrollBarHighlight());
}
}
this->queueLayout();
}
2018-01-23 22:48:33 +01:00
void ChannelView::updateLastReadMessage()
{
if (auto lastMessage = this->messages_.last())
2018-10-21 13:43:02 +02:00
{
this->lastReadMessage_ = *lastMessage;
2018-01-23 22:48:33 +01:00
}
this->update();
}
void ChannelView::resizeEvent(QResizeEvent *)
2017-04-12 17:46:44 +02:00
{
this->scrollBar_->setGeometry(this->width() - this->scrollBar_->width(), 0,
this->scrollBar_->width(), this->height());
2018-12-02 18:26:21 +01:00
this->goToBottom_->setGeometry(0, this->height() - int(this->scale() * 26),
this->width(), int(this->scale() * 26));
this->scrollBar_->raise();
2018-11-03 21:26:57 +01:00
this->queueLayout();
2017-02-07 00:03:15 +01:00
2017-06-06 17:18:23 +02:00
this->update();
2017-01-03 21:19:33 +01:00
}
2017-01-05 16:07:20 +01:00
2018-08-06 21:17:03 +02:00
void ChannelView::setSelection(const SelectionItem &start,
const SelectionItem &end)
2017-08-18 15:12:07 +02:00
{
// selections
2018-10-21 13:43:02 +02:00
if (!this->selecting_ && start != end)
{
2018-11-03 21:26:57 +01:00
// this->messagesAddedSinceSelectionPause_ = 0;
2018-06-13 13:27:10 +02:00
this->selecting_ = true;
2018-11-03 21:26:57 +01:00
// this->pausedBySelection_ = true;
}
2018-06-13 13:27:10 +02:00
this->selection_ = Selection(start, end);
2017-08-18 15:12:07 +02:00
this->selectionChanged.invoke();
2017-08-18 15:12:07 +02:00
}
2018-08-07 07:55:31 +02:00
MessageElementFlags ChannelView::getFlags() const
2018-01-17 16:52:51 +01:00
{
auto app = getApp();
2018-10-21 13:43:02 +02:00
if (this->overrideFlags_)
{
2018-06-13 13:27:10 +02:00
return this->overrideFlags_.get();
2018-01-27 21:13:22 +01:00
}
2018-08-07 07:55:31 +02:00
MessageElementFlags flags = app->windows->getWordFlags();
2018-01-17 16:52:51 +01:00
Split *split = dynamic_cast<Split *>(this->parentWidget());
if (split == nullptr)
{
SearchPopup *searchPopup =
dynamic_cast<SearchPopup *>(this->parentWidget());
if (searchPopup != nullptr)
{
split = dynamic_cast<Split *>(searchPopup->parentWidget());
}
}
2018-10-21 13:43:02 +02:00
if (split != nullptr)
{
if (split->getModerationMode())
{
2018-08-07 07:55:31 +02:00
flags.set(MessageElementFlag::ModeratorTools);
2018-01-17 16:52:51 +01:00
}
if (this->underlyingChannel_ == app->twitch->mentionsChannel ||
this->underlyingChannel_ == app->twitch->liveChannel)
2018-10-21 13:43:02 +02:00
{
2018-08-07 07:55:31 +02:00
flags.set(MessageElementFlag::ChannelName);
flags.unset(MessageElementFlag::ChannelPointReward);
2018-01-23 23:28:06 +01:00
}
2018-01-17 16:52:51 +01:00
}
if (this->sourceChannel_ == app->twitch->mentionsChannel)
flags.set(MessageElementFlag::ChannelName);
if (this->context_ == Context::ReplyThread ||
getSettings()->hideReplyContext)
{
// Don't show inline replies within the ReplyThreadPopup
// or if they're hidden
flags.unset(MessageElementFlag::RepliedMessage);
}
if (!this->canReplyToMessages())
{
flags.unset(MessageElementFlag::ReplyButton);
}
2018-01-17 16:52:51 +01:00
return flags;
}
bool ChannelView::scrollToMessage(const MessagePtr &message)
{
if (!this->mayContainMessage(message))
{
return false;
}
auto &messagesSnapshot = this->getMessagesSnapshot();
if (messagesSnapshot.size() == 0)
{
return false;
}
// TODO: Figure out if we can somehow binary-search here.
// Currently, a message only sometimes stores a QDateTime,
// but always a QTime (inaccurate on midnight).
//
// We're searching from the bottom since it's more likely for a user
// wanting to go to a message that recently scrolled out of view.
size_t messageIdx = messagesSnapshot.size() - 1;
for (; messageIdx < SIZE_MAX; messageIdx--)
{
if (messagesSnapshot[messageIdx]->getMessagePtr() == message)
{
break;
}
}
if (messageIdx == SIZE_MAX)
{
return false;
}
this->scrollToMessageLayout(messagesSnapshot[messageIdx].get(), messageIdx);
getApp()->windows->select(this->split_);
return true;
}
bool ChannelView::scrollToMessageId(const QString &messageId)
{
auto &messagesSnapshot = this->getMessagesSnapshot();
if (messagesSnapshot.size() == 0)
{
return false;
}
// We're searching from the bottom since it's more likely for a user
// wanting to go to a message that recently scrolled out of view.
size_t messageIdx = messagesSnapshot.size() - 1;
for (; messageIdx < SIZE_MAX; messageIdx--)
{
if (messagesSnapshot[messageIdx]->getMessagePtr()->id == messageId)
{
break;
}
}
if (messageIdx == SIZE_MAX)
{
return false;
}
this->scrollToMessageLayout(messagesSnapshot[messageIdx].get(), messageIdx);
getApp()->windows->select(this->split_);
return true;
}
void ChannelView::scrollToMessageLayout(MessageLayout *layout,
size_t messageIdx)
{
this->highlightedMessage_ = layout;
this->highlightAnimation_.setCurrentTime(0);
this->highlightAnimation_.start(QAbstractAnimation::KeepWhenStopped);
if (this->showScrollBar_)
{
this->getScrollBar().setDesiredValue(messageIdx);
}
}
void ChannelView::paintEvent(QPaintEvent * /*event*/)
2017-01-05 16:07:20 +01:00
{
2018-08-09 18:39:46 +02:00
// BenchmarkGuard benchmark("paint");
2017-02-07 00:03:15 +01:00
QPainter painter(this);
2017-02-07 00:03:15 +01:00
2018-07-06 17:11:37 +02:00
painter.fillRect(rect(), this->theme->splits.background);
2017-08-18 15:12:07 +02:00
// draw messages
this->drawMessages(painter);
2018-11-03 21:26:57 +01:00
// draw paused sign
if (this->paused())
{
auto a = this->scale() * 20;
auto brush = QBrush(QColor(127, 127, 127, 255));
painter.fillRect(QRectF(5, a / 4, a / 4, a), brush);
painter.fillRect(QRectF(15, a / 4, a / 4, a), brush);
2018-11-03 21:26:57 +01:00
}
2017-08-18 15:12:07 +02:00
}
2018-08-06 21:17:03 +02:00
// if overlays is false then it draws the message, if true then it draws things
// such as the grey overlay when a message is disabled
void ChannelView::drawMessages(QPainter &painter)
2017-08-18 15:12:07 +02:00
{
auto &messagesSnapshot = this->getMessagesSnapshot();
2017-01-11 01:08:20 +01:00
size_t start = size_t(this->scrollBar_->getCurrentValue());
2017-01-11 01:08:20 +01:00
2018-11-03 21:26:57 +01:00
if (start >= messagesSnapshot.size())
2018-10-21 13:43:02 +02:00
{
2017-01-18 04:33:30 +01:00
return;
}
2018-05-25 12:45:18 +02:00
int y = int(-(messagesSnapshot[start].get()->getHeight() *
(fmod(this->scrollBar_->getCurrentValue(), 1))));
2017-01-18 04:33:30 +01:00
MessageLayout *end = nullptr;
2018-01-23 22:48:33 +01:00
bool windowFocused = this->window() == QApplication::activeWindow();
auto app = getApp();
bool isMentions = this->underlyingChannel_ == app->twitch->mentionsChannel;
2018-11-03 21:26:57 +01:00
for (size_t i = start; i < messagesSnapshot.size(); ++i)
2018-10-21 13:43:02 +02:00
{
MessageLayout *layout = messagesSnapshot[i].get();
2017-02-07 00:03:15 +01:00
2018-01-23 22:48:33 +01:00
bool isLastMessage = false;
2018-10-21 13:43:02 +02:00
if (getSettings()->showLastMessageIndicator)
{
2018-06-13 13:27:10 +02:00
isLastMessage = this->lastReadMessage_.get() == layout;
2018-01-23 22:48:33 +01:00
}
2018-08-06 21:17:03 +02:00
layout->paint(painter, DRAW_WIDTH, y, i, this->selection_,
isLastMessage, windowFocused, isMentions);
if (this->highlightedMessage_ == layout)
{
painter.fillRect(
0, y, layout->getWidth(), layout->getHeight(),
this->highlightAnimation_.currentValue().value<QColor>());
if (this->highlightAnimation_.state() == QVariantAnimation::Stopped)
{
this->highlightedMessage_ = nullptr;
}
}
y += layout->getHeight();
2017-01-20 06:10:28 +01:00
end = layout;
2018-10-21 13:43:02 +02:00
if (y > this->height())
{
2017-01-20 06:10:28 +01:00
break;
}
2017-01-11 01:08:20 +01:00
}
2018-10-21 13:43:02 +02:00
if (end == nullptr)
{
return;
}
// remove messages that are on screen
// the messages that are left at the end get their buffers reset
2018-11-03 21:26:57 +01:00
for (size_t i = start; i < messagesSnapshot.size(); ++i)
2018-10-21 13:43:02 +02:00
{
2018-06-13 13:27:10 +02:00
auto it = this->messagesOnScreen_.find(messagesSnapshot[i]);
2018-10-21 13:43:02 +02:00
if (it != this->messagesOnScreen_.end())
{
2018-06-13 13:27:10 +02:00
this->messagesOnScreen_.erase(it);
}
}
// delete the message buffers that aren't on screen
2018-10-21 13:43:02 +02:00
for (const std::shared_ptr<MessageLayout> &item : this->messagesOnScreen_)
{
item->deleteBuffer();
}
2018-06-13 13:27:10 +02:00
this->messagesOnScreen_.clear();
// add all messages on screen to the map
2018-11-03 21:26:57 +01:00
for (size_t i = start; i < messagesSnapshot.size(); ++i)
2018-10-21 13:43:02 +02:00
{
const std::shared_ptr<MessageLayout> &layout = messagesSnapshot[i];
2018-06-13 13:27:10 +02:00
this->messagesOnScreen_.insert(layout);
2018-10-21 13:43:02 +02:00
if (layout.get() == end)
{
break;
}
}
2017-08-18 15:12:07 +02:00
}
2017-02-07 00:03:15 +01:00
void ChannelView::wheelEvent(QWheelEvent *event)
2017-01-26 04:26:40 +01:00
{
if (!event->angleDelta().y())
{
2018-06-19 18:55:45 +02:00
return;
}
2018-10-21 13:43:02 +02:00
if (event->modifiers() & Qt::ControlModifier)
{
2018-06-11 15:04:54 +02:00
event->ignore();
return;
}
2018-10-21 13:43:02 +02:00
if (this->scrollBar_->isVisible())
{
float mouseMultiplier = getSettings()->mouseScrollMultiplier;
2017-04-14 17:47:28 +02:00
qreal desired = this->scrollBar_->getDesiredValue();
qreal delta = event->angleDelta().y() * qreal(1.5) * mouseMultiplier;
2018-01-05 02:55:24 +01:00
auto &snapshot = this->getMessagesSnapshot();
2018-11-03 21:26:57 +01:00
int snapshotLength = int(snapshot.size());
2018-07-07 11:41:01 +02:00
int i = std::min<int>(int(desired), snapshotLength);
2018-01-05 02:55:24 +01:00
2018-10-21 13:43:02 +02:00
if (delta > 0)
{
2018-06-13 13:27:10 +02:00
qreal scrollFactor = fmod(desired, 1);
qreal currentScrollLeft = std::max<qreal>(
0.01, int(scrollFactor * snapshot[i]->getHeight()));
2018-01-05 02:55:24 +01:00
2018-10-21 13:43:02 +02:00
for (; i >= 0; i--)
{
if (delta < currentScrollLeft)
{
2018-01-05 02:55:24 +01:00
desired -= scrollFactor * (delta / currentScrollLeft);
break;
2018-10-21 13:43:02 +02:00
}
else
{
2018-01-05 02:55:24 +01:00
delta -= currentScrollLeft;
desired -= scrollFactor;
}
2018-10-21 13:43:02 +02:00
if (i == 0)
{
2018-01-05 02:55:24 +01:00
desired = 0;
2018-10-21 13:43:02 +02:00
}
else
{
2018-08-06 21:17:03 +02:00
snapshot[i - 1]->layout(this->getLayoutWidth(),
2018-11-21 21:37:41 +01:00
this->scale(), this->getFlags());
2018-01-05 02:55:24 +01:00
scrollFactor = 1;
currentScrollLeft = snapshot[i - 1]->getHeight();
}
}
2018-10-21 13:43:02 +02:00
}
else
{
2018-01-05 02:55:24 +01:00
delta = -delta;
2018-06-13 13:27:10 +02:00
qreal scrollFactor = 1 - fmod(desired, 1);
qreal currentScrollLeft = std::max<qreal>(
0.01, int(scrollFactor * snapshot[i]->getHeight()));
2018-01-05 02:55:24 +01:00
2018-10-21 13:43:02 +02:00
for (; i < snapshotLength; i++)
{
if (delta < currentScrollLeft)
{
2018-08-06 21:17:03 +02:00
desired +=
scrollFactor * (qreal(delta) / currentScrollLeft);
2018-01-05 02:55:24 +01:00
break;
2018-10-21 13:43:02 +02:00
}
else
{
2018-01-05 02:55:24 +01:00
delta -= currentScrollLeft;
desired += scrollFactor;
}
2018-10-21 13:43:02 +02:00
if (i == snapshotLength - 1)
{
2018-11-03 21:26:57 +01:00
desired = snapshot.size();
2018-10-21 13:43:02 +02:00
}
else
{
2018-08-06 21:17:03 +02:00
snapshot[i + 1]->layout(this->getLayoutWidth(),
2018-11-21 21:37:41 +01:00
this->scale(), this->getFlags());
2018-01-05 02:55:24 +01:00
scrollFactor = 1;
currentScrollLeft = snapshot[i + 1]->getHeight();
}
}
}
this->scrollBar_->setDesiredValue(desired, true);
}
2017-01-26 04:26:40 +01:00
}
2017-02-17 23:51:35 +01:00
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void ChannelView::enterEvent(QEnterEvent * /*event*/)
#else
void ChannelView::enterEvent(QEvent * /*event*/)
#endif
{
}
void ChannelView::leaveEvent(QEvent *)
{
TooltipWidget::instance()->hide();
2018-11-03 22:02:52 +01:00
this->unpause(PauseReason::Mouse);
2018-11-03 21:26:57 +01:00
this->queueLayout();
}
void ChannelView::mouseMoveEvent(QMouseEvent *event)
2017-02-17 23:51:35 +01:00
{
2018-11-03 21:26:57 +01:00
/// Pause on hover
if (float pauseTime = getSettings()->pauseOnHoverDuration;
pauseTime > 0.001f)
2018-10-21 13:43:02 +02:00
{
this->pause(PauseReason::Mouse, uint(pauseTime * 1000.f));
}
else if (pauseTime < -0.5f)
{
this->pause(PauseReason::Mouse);
}
auto tooltipWidget = TooltipWidget::instance();
std::shared_ptr<MessageLayout> layout;
2017-02-17 23:51:35 +01:00
QPoint relativePos;
2017-08-18 15:12:07 +02:00
int messageIndex;
2017-02-17 23:51:35 +01:00
// no message under cursor
2018-10-21 13:43:02 +02:00
if (!tryGetMessageAt(event->pos(), layout, relativePos, messageIndex))
{
this->setCursor(Qt::ArrowCursor);
tooltipWidget->hide();
return;
}
if (this->isScrolling_)
{
this->currentMousePosition_ = event->screenPos();
}
// is selecting
if (this->isLeftMouseDown_)
2018-10-21 13:43:02 +02:00
{
2018-11-03 21:26:57 +01:00
// this->pause(PauseReason::Selecting, 300);
int index = layout->getSelectionIndex(relativePos);
2017-08-18 15:12:07 +02:00
2018-08-06 21:17:03 +02:00
this->setSelection(this->selection_.start,
SelectionItem(messageIndex, index));
2017-08-18 15:12:07 +02:00
this->queueUpdate();
}
// message under cursor is collapsed
2018-10-21 13:43:02 +02:00
if (layout->flags.has(MessageLayoutFlag::Collapsed))
{
this->setCursor(Qt::PointingHandCursor);
tooltipWidget->hide();
return;
2017-08-18 15:12:07 +02:00
}
2017-04-24 23:00:26 +02:00
// check if word underneath cursor
2018-08-06 21:17:03 +02:00
const MessageLayoutElement *hoverLayoutElement =
layout->getElementAt(relativePos);
2018-10-21 13:43:02 +02:00
if (hoverLayoutElement == nullptr)
{
this->setCursor(Qt::ArrowCursor);
tooltipWidget->hide();
2017-02-17 23:51:35 +01:00
return;
}
2018-10-06 13:43:21 +02:00
2018-10-21 13:43:02 +02:00
if (this->isDoubleClick_)
{
2018-10-06 13:43:21 +02:00
int wordStart;
int wordEnd;
this->getWordBounds(layout.get(), hoverLayoutElement, relativePos,
wordStart, wordEnd);
SelectionItem newStart(messageIndex, wordStart);
SelectionItem newEnd(messageIndex, wordEnd);
// Selection changed in same message
2018-11-14 17:26:08 +01:00
if (messageIndex == this->doubleClickSelection_.origMessageIndex)
2018-10-21 13:43:02 +02:00
{
2018-10-06 13:43:21 +02:00
// Selecting to the left
if (wordStart < this->selection_.start.charIndex &&
2018-11-14 17:26:08 +01:00
!this->doubleClickSelection_.selectingRight)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.selectingLeft = true;
// Ensure that the original word stays selected(Edge case)
2018-11-14 17:26:08 +01:00
if (wordStart > this->doubleClickSelection_.originalEnd)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
this->setSelection(
this->doubleClickSelection_.origStartItem, newEnd);
2018-10-21 13:43:02 +02:00
}
else
{
this->setSelection(newStart, this->selection_.end);
}
2018-10-06 13:43:21 +02:00
// Selecting to the right
2018-10-21 13:43:02 +02:00
}
else if (wordEnd > this->selection_.end.charIndex &&
2018-11-14 17:26:08 +01:00
!this->doubleClickSelection_.selectingLeft)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.selectingRight = true;
// Ensure that the original word stays selected(Edge case)
2018-11-14 17:26:08 +01:00
if (wordEnd < this->doubleClickSelection_.originalStart)
2018-10-21 13:43:02 +02:00
{
this->setSelection(newStart,
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.origEndItem);
2018-10-21 13:43:02 +02:00
}
else
{
this->setSelection(this->selection_.start, newEnd);
}
2018-10-06 13:43:21 +02:00
}
// Swapping from selecting left to selecting right
if (wordStart > this->selection_.start.charIndex &&
2018-11-14 17:26:08 +01:00
!this->doubleClickSelection_.selectingRight)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
if (wordStart > this->doubleClickSelection_.originalEnd)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.selectingLeft = false;
this->doubleClickSelection_.selectingRight = true;
this->setSelection(
this->doubleClickSelection_.origStartItem, newEnd);
2018-10-21 13:43:02 +02:00
}
else
{
2018-10-06 13:43:21 +02:00
this->setSelection(newStart, this->selection_.end);
}
// Swapping from selecting right to selecting left
2018-10-21 13:43:02 +02:00
}
else if (wordEnd < this->selection_.end.charIndex &&
2018-11-14 17:26:08 +01:00
!this->doubleClickSelection_.selectingLeft)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
if (wordEnd < this->doubleClickSelection_.originalStart)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.selectingLeft = true;
this->doubleClickSelection_.selectingRight = false;
2018-10-06 13:43:21 +02:00
this->setSelection(newStart,
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.origEndItem);
2018-10-21 13:43:02 +02:00
}
else
{
2018-10-06 13:43:21 +02:00
this->setSelection(this->selection_.start, newEnd);
}
}
// Selection changed in a different message
2018-10-21 13:43:02 +02:00
}
else
{
2018-10-06 13:43:21 +02:00
// Message over the original
2018-10-21 13:43:02 +02:00
if (messageIndex < this->selection_.start.messageIndex)
{
2018-10-06 13:43:21 +02:00
// Swapping from left to right selecting
2018-11-14 17:26:08 +01:00
if (!this->doubleClickSelection_.selectingLeft)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.selectingLeft = true;
this->doubleClickSelection_.selectingRight = false;
2018-10-06 13:43:21 +02:00
}
if (wordStart < this->selection_.start.charIndex &&
2018-11-14 17:26:08 +01:00
!this->doubleClickSelection_.selectingRight)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.selectingLeft = true;
2018-10-06 13:43:21 +02:00
}
2018-11-14 17:26:08 +01:00
this->setSelection(newStart,
this->doubleClickSelection_.origEndItem);
2018-10-06 13:43:21 +02:00
// Message under the original
2018-10-21 13:43:02 +02:00
}
else if (messageIndex > this->selection_.end.messageIndex)
{
2018-10-06 13:43:21 +02:00
// Swapping from right to left selecting
2018-11-14 17:26:08 +01:00
if (!this->doubleClickSelection_.selectingRight)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.selectingLeft = false;
this->doubleClickSelection_.selectingRight = true;
2018-10-06 13:43:21 +02:00
}
if (wordEnd > this->selection_.end.charIndex &&
2018-11-14 17:26:08 +01:00
!this->doubleClickSelection_.selectingLeft)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.selectingRight = true;
2018-10-06 13:43:21 +02:00
}
2018-11-14 17:26:08 +01:00
this->setSelection(this->doubleClickSelection_.origStartItem,
newEnd);
2018-10-06 13:43:21 +02:00
// Selection changed in non original message
2018-10-21 13:43:02 +02:00
}
else
{
2018-11-14 17:26:08 +01:00
if (this->doubleClickSelection_.selectingLeft)
2018-10-21 13:43:02 +02:00
{
2018-10-06 13:43:21 +02:00
this->setSelection(newStart, this->selection_.end);
2018-10-21 13:43:02 +02:00
}
else
{
2018-10-06 13:43:21 +02:00
this->setSelection(this->selection_.start, newEnd);
}
}
}
// Reset direction of selection
2018-11-14 17:26:08 +01:00
if (wordStart == this->doubleClickSelection_.originalStart &&
wordEnd == this->doubleClickSelection_.originalEnd)
2018-10-21 13:43:02 +02:00
{
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.selectingLeft =
this->doubleClickSelection_.selectingRight = false;
2018-10-06 13:43:21 +02:00
}
}
auto element = &hoverLayoutElement->getCreator();
2018-08-25 20:21:18 +02:00
bool isLinkValid = hoverLayoutElement->getLink().isValid();
auto emoteElement = dynamic_cast<const EmoteElement *>(element);
2017-12-19 03:36:05 +01:00
if (element->getTooltip().isEmpty() ||
(isLinkValid && emoteElement == nullptr &&
!getSettings()->linkInfoTooltip))
2018-10-21 13:43:02 +02:00
{
tooltipWidget->hide();
2018-10-21 13:43:02 +02:00
}
else
{
auto badgeElement = dynamic_cast<const BadgeElement *>(element);
2019-08-21 01:52:01 +02:00
if ((badgeElement || emoteElement) &&
getSettings()->emotesTooltipPreview.getValue())
{
if (event->modifiers() == Qt::ShiftModifier ||
getSettings()->emotesTooltipPreview.getValue() == 1)
{
2019-08-21 01:52:01 +02:00
if (emoteElement)
{
tooltipWidget->setImage(
2019-08-21 01:52:01 +02:00
emoteElement->getEmote()->images.getImage(3.0));
}
else if (badgeElement)
{
tooltipWidget->setImage(
2019-08-21 01:52:01 +02:00
badgeElement->getEmote()->images.getImage(3.0));
}
}
else
{
tooltipWidget->clearImage();
}
}
else
{
if (element->getTooltip() == "No link info loaded")
{
std::weak_ptr<MessageLayout> weakLayout = layout;
LinkResolver::getLinkInfo(
element->getLink().value, nullptr,
[weakLayout, element](QString tooltipText,
Link originalLink,
ImagePtr thumbnail) {
auto shared = weakLayout.lock();
if (!shared)
return;
element->setTooltip(tooltipText);
element->setThumbnail(thumbnail);
});
}
auto thumbnailSize = getSettings()->thumbnailSize;
if (!thumbnailSize)
{
tooltipWidget->clearImage();
}
else
{
const auto shouldHideThumbnail =
isInStreamerMode() &&
getSettings()->streamerModeHideLinkThumbnails &&
element->getThumbnail() != nullptr &&
!element->getThumbnail()->url().string.isEmpty();
auto thumb =
shouldHideThumbnail
? Image::fromResourcePixmap(getResources().streamerMode)
: element->getThumbnail();
tooltipWidget->setImage(std::move(thumb));
if (element->getThumbnailType() ==
MessageElement::ThumbnailType::Link_Thumbnail)
{
tooltipWidget->setImageScale(thumbnailSize, thumbnailSize);
}
}
}
tooltipWidget->moveTo(this, event->globalPos());
2018-08-25 20:21:18 +02:00
tooltipWidget->setWordWrap(isLinkValid);
tooltipWidget->setText(element->getTooltip());
2018-01-17 03:26:32 +01:00
tooltipWidget->show();
}
2017-12-19 03:36:05 +01:00
// check if word has a link
2018-10-21 13:43:02 +02:00
if (isLinkValid)
{
this->setCursor(Qt::PointingHandCursor);
2018-10-21 13:43:02 +02:00
}
else
{
this->setCursor(Qt::ArrowCursor);
2017-02-17 23:51:35 +01:00
}
}
void ChannelView::mousePressEvent(QMouseEvent *event)
2017-04-12 17:46:44 +02:00
{
2018-06-04 21:44:03 +02:00
this->mouseDown.invoke(event);
std::shared_ptr<MessageLayout> layout;
2017-08-18 15:12:07 +02:00
QPoint relativePos;
int messageIndex;
2018-10-21 13:43:02 +02:00
if (!tryGetMessageAt(event->pos(), layout, relativePos, messageIndex))
{
2017-08-18 15:12:07 +02:00
setCursor(Qt::ArrowCursor);
auto &messagesSnapshot = this->getMessagesSnapshot();
2018-11-03 21:26:57 +01:00
if (messagesSnapshot.size() == 0)
2018-10-21 13:43:02 +02:00
{
return;
}
// Start selection at the last message at its last index
2018-10-21 13:43:02 +02:00
if (event->button() == Qt::LeftButton)
{
2018-11-03 21:26:57 +01:00
auto lastMessageIndex = messagesSnapshot.size() - 1;
auto lastMessage = messagesSnapshot[lastMessageIndex];
auto lastCharacterIndex = lastMessage->getLastCharacterIndex();
SelectionItem selectionItem(lastMessageIndex, lastCharacterIndex);
this->setSelection(selectionItem, selectionItem);
}
return;
2017-08-18 15:12:07 +02:00
}
// check if message is collapsed
2018-10-21 13:43:02 +02:00
switch (event->button())
{
2019-09-26 00:51:05 +02:00
case Qt::LeftButton: {
if (this->isScrolling_)
this->disableScrolling();
this->lastLeftPressPosition_ = event->screenPos();
this->isLeftMouseDown_ = true;
2018-06-05 14:24:01 +02:00
2018-10-21 13:43:02 +02:00
if (layout->flags.has(MessageLayoutFlag::Collapsed))
2018-06-05 14:24:01 +02:00
return;
2018-10-21 13:43:02 +02:00
if (getSettings()->linksDoubleClickOnly.getValue())
{
2018-11-03 21:26:57 +01:00
this->pause(PauseReason::DoubleClick, 200);
}
int index = layout->getSelectionIndex(relativePos);
auto selectionItem = SelectionItem(messageIndex, index);
this->setSelection(selectionItem, selectionItem);
2018-10-21 13:43:02 +02:00
}
break;
2019-09-26 00:51:05 +02:00
case Qt::RightButton: {
if (this->isScrolling_)
this->disableScrolling();
2018-06-13 13:27:10 +02:00
this->lastRightPressPosition_ = event->screenPos();
this->isRightMouseDown_ = true;
2018-10-21 13:43:02 +02:00
}
break;
case Qt::MiddleButton: {
const MessageLayoutElement *hoverLayoutElement =
layout->getElementAt(relativePos);
if (hoverLayoutElement != nullptr &&
hoverLayoutElement->getLink().isUrl() &&
this->isScrolling_ == false)
{
break;
}
else
{
if (this->isScrolling_)
this->disableScrolling();
else if (hoverLayoutElement != nullptr &&
hoverLayoutElement->getFlags().has(
MessageElementFlag::Username))
break;
else if (this->scrollBar_->isVisible())
this->enableScrolling(event->screenPos());
}
}
break;
default:;
}
this->update();
}
2017-04-12 17:46:44 +02:00
void ChannelView::mouseReleaseEvent(QMouseEvent *event)
{
// find message
this->queueLayout();
std::shared_ptr<MessageLayout> layout;
QPoint relativePos;
int messageIndex;
bool foundElement =
tryGetMessageAt(event->pos(), layout, relativePos, messageIndex);
// check if mouse was pressed
2018-10-21 13:43:02 +02:00
if (event->button() == Qt::LeftButton)
{
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.selectingLeft =
this->doubleClickSelection_.selectingRight = false;
2018-10-21 13:43:02 +02:00
if (this->isDoubleClick_)
{
2018-10-06 16:19:58 +02:00
this->isDoubleClick_ = false;
// Was actually not a wanted triple-click
if (fabsf(distanceBetweenPoints(this->lastDClickPosition_,
2018-10-21 13:43:02 +02:00
event->screenPos())) > 10.f)
{
this->clickTimer_->stop();
return;
}
2018-10-21 13:43:02 +02:00
}
else if (this->isLeftMouseDown_)
2018-10-21 13:43:02 +02:00
{
this->isLeftMouseDown_ = false;
if (fabsf(distanceBetweenPoints(this->lastLeftPressPosition_,
2018-10-21 13:43:02 +02:00
event->screenPos())) > 15.f)
{
return;
}
2018-10-21 13:43:02 +02:00
}
else
{
return;
}
2018-10-21 13:43:02 +02:00
}
else if (event->button() == Qt::RightButton)
{
if (this->isRightMouseDown_)
{
2018-06-13 13:27:10 +02:00
this->isRightMouseDown_ = false;
2017-04-12 17:46:44 +02:00
2018-08-06 21:17:03 +02:00
if (fabsf(distanceBetweenPoints(this->lastRightPressPosition_,
2018-10-21 13:43:02 +02:00
event->screenPos())) > 15.f)
{
return;
}
2018-10-21 13:43:02 +02:00
}
else
{
return;
}
2018-10-21 13:43:02 +02:00
}
else if (event->button() == Qt::MiddleButton)
{
if (this->isScrolling_ && this->scrollBar_->isVisible())
{
if (event->screenPos() == this->lastMiddlePressPosition_)
this->enableScrolling(event->screenPos());
else
this->disableScrolling();
return;
}
else if (foundElement)
{
const MessageLayoutElement *hoverLayoutElement =
layout->getElementAt(relativePos);
if (hoverLayoutElement == nullptr)
{
return;
}
else if (hoverLayoutElement->getFlags().has(
MessageElementFlag::Username))
{
openTwitchUsercard(this->channel_->getName(),
hoverLayoutElement->getLink().value);
return;
}
else if (hoverLayoutElement->getLink().isUrl() == false)
{
return;
}
}
}
2018-10-21 13:43:02 +02:00
else
{
// not left or right button
2017-04-12 17:46:44 +02:00
return;
}
// no message found
if (!foundElement)
2018-10-21 13:43:02 +02:00
{
2017-04-12 17:46:44 +02:00
// No message at clicked position
return;
}
// message under cursor is collapsed
2018-10-21 13:43:02 +02:00
if (layout->flags.has(MessageLayoutFlag::Collapsed))
{
2018-08-07 07:55:31 +02:00
layout->flags.set(MessageLayoutFlag::Expanded);
layout->flags.set(MessageLayoutFlag::RequiresLayout);
2018-04-18 09:12:29 +02:00
2018-11-03 21:26:57 +01:00
this->queueLayout();
return;
}
2018-08-06 21:17:03 +02:00
const MessageLayoutElement *hoverLayoutElement =
layout->getElementAt(relativePos);
// Triple-clicking a message selects the whole message
2018-10-21 13:43:02 +02:00
if (this->clickTimer_->isActive() && this->selecting_)
{
if (fabsf(distanceBetweenPoints(this->lastDClickPosition_,
2018-10-21 13:43:02 +02:00
event->screenPos())) < 10.f)
{
this->selectWholeMessage(layout.get(), messageIndex);
}
}
// handle the click
this->handleMouseClick(event, hoverLayoutElement, layout);
2020-09-26 00:58:29 +02:00
this->update();
}
2018-08-06 21:17:03 +02:00
void ChannelView::handleMouseClick(QMouseEvent *event,
const MessageLayoutElement *hoveredElement,
MessageLayoutPtr layout)
{
2018-10-21 13:43:02 +02:00
switch (event->button())
{
2019-09-26 00:51:05 +02:00
case Qt::LeftButton: {
2018-10-21 13:43:02 +02:00
if (this->selecting_)
{
2018-06-13 13:27:10 +02:00
// this->pausedBySelection = false;
this->selecting_ = false;
// this->pauseTimeout.stop();
// this->pausedTemporarily = false;
2018-11-03 21:26:57 +01:00
this->queueLayout();
}
if (hoveredElement == nullptr)
{
return;
}
const auto &link = hoveredElement->getLink();
2018-10-21 13:43:02 +02:00
if (!getSettings()->linksDoubleClickOnly)
{
this->handleLinkClick(event, link, layout.get());
}
// Invoke to signal from EmotePopup.
2018-10-21 13:43:02 +02:00
if (link.type == Link::InsertText)
{
this->linkClicked.invoke(link);
}
2018-10-21 13:43:02 +02:00
}
break;
2019-09-26 00:51:05 +02:00
case Qt::RightButton: {
// insert user mention to input, only in default context
if ((this->context_ == Context::None) &&
(hoveredElement != nullptr))
2018-10-21 13:43:02 +02:00
{
auto split = dynamic_cast<Split *>(this->parentWidget());
auto insertText = [=](QString text) {
if (split)
{
split->insertTextToInput(text);
}
};
const auto &link = hoveredElement->getLink();
if (link.type == Link::UserInfo)
{
const bool commaMention =
getSettings()->mentionUsersWithComma;
const bool isFirstWord =
split && split->getInput().isEditFirstWord();
auto userMention = formatUserMention(
link.value, isFirstWord, commaMention);
insertText("@" + userMention + " ");
return;
}
if (link.type == Link::UserWhisper)
{
insertText("/w " + link.value + " ");
return;
}
}
this->addContextMenuItems(hoveredElement, layout, event);
2018-10-21 13:43:02 +02:00
}
break;
case Qt::MiddleButton: {
if (hoveredElement == nullptr)
{
return;
}
const auto &link = hoveredElement->getLink();
if (!getSettings()->linksDoubleClickOnly)
{
this->handleLinkClick(event, link, layout.get());
}
}
break;
default:;
}
}
2018-08-06 21:17:03 +02:00
void ChannelView::addContextMenuItems(
const MessageLayoutElement *hoveredElement, MessageLayoutPtr layout,
QMouseEvent *event)
{
static QMenu *previousMenu = nullptr;
if (previousMenu != nullptr)
{
previousMenu->deleteLater();
previousMenu = nullptr;
}
auto menu = new QMenu;
previousMenu = menu;
2018-05-23 21:16:34 +02:00
// Add image options if the element clicked contains an image (e.g. a badge or an emote)
this->addImageContextMenuItems(hoveredElement, layout, event, *menu);
// Add link options if the element clicked contains a link
this->addLinkContextMenuItems(hoveredElement, layout, event, *menu);
// Add message options
this->addMessageContextMenuItems(hoveredElement, layout, event, *menu);
// Add Twitch-specific link options if the element clicked contains a link detected as a Twitch username
this->addTwitchLinkContextMenuItems(hoveredElement, layout, event, *menu);
// Add hidden options (e.g. copy message ID) if the user held down Shift
this->addHiddenContextMenuItems(hoveredElement, layout, event, *menu);
// Add executable command options
this->addCommandExecutionContextMenuItems(hoveredElement, layout, event,
*menu);
menu->popup(QCursor::pos());
menu->raise();
}
void ChannelView::addImageContextMenuItems(
const MessageLayoutElement *hoveredElement, MessageLayoutPtr /*layout*/,
QMouseEvent * /*event*/, QMenu &menu)
{
if (hoveredElement == nullptr)
{
return;
}
const auto &creator = hoveredElement->getCreator();
auto creatorFlags = creator.getFlags();
// Badge actions
if (creatorFlags.hasAny({MessageElementFlag::Badges}))
{
if (auto badgeElement = dynamic_cast<const BadgeElement *>(&creator))
{
addEmoteContextMenuItems(*badgeElement->getEmote(), creatorFlags,
menu);
}
}
// Emote actions
2018-10-21 13:43:02 +02:00
if (creatorFlags.hasAny(
{MessageElementFlag::EmoteImages, MessageElementFlag::EmojiImage}))
{
if (auto emoteElement = dynamic_cast<const EmoteElement *>(&creator))
{
2018-08-06 21:17:03 +02:00
addEmoteContextMenuItems(*emoteElement->getEmote(), creatorFlags,
menu);
}
}
// add seperator
if (!menu.actions().empty())
2018-10-21 13:43:02 +02:00
{
menu.addSeparator();
}
}
void ChannelView::addLinkContextMenuItems(
const MessageLayoutElement *hoveredElement, MessageLayoutPtr /*layout*/,
QMouseEvent * /*event*/, QMenu &menu)
{
if (hoveredElement == nullptr)
2018-10-21 13:43:02 +02:00
{
return;
}
2018-05-23 21:16:34 +02:00
const auto &link = hoveredElement->getLink();
if (link.type != Link::Url)
{
return;
}
// Link copy
QString url = link.value;
// open link
menu.addAction("Open link", [url] {
QDesktopServices::openUrl(QUrl(url));
});
// open link default
if (supportsIncognitoLinks())
{
menu.addAction("Open link incognito", [url] {
openLinkIncognito(url);
});
}
menu.addAction("Copy link", [url] {
crossPlatformCopy(url);
});
menu.addSeparator();
}
void ChannelView::addMessageContextMenuItems(
const MessageLayoutElement * /*hoveredElement*/, MessageLayoutPtr layout,
QMouseEvent * /*event*/, QMenu &menu)
{
// Copy actions
2018-10-21 13:43:02 +02:00
if (!this->selection_.isEmpty())
{
menu.addAction("Copy selection", [this] {
crossPlatformCopy(this->getSelectedText());
});
}
menu.addAction("Copy message", [layout] {
2018-06-05 14:24:54 +02:00
QString copyString;
2018-08-16 00:16:33 +02:00
layout->addSelectionText(copyString, 0, INT_MAX,
CopyMode::OnlyTextAndEmotes);
2018-06-05 14:24:54 +02:00
crossPlatformCopy(copyString);
2018-06-05 14:24:54 +02:00
});
menu.addAction("Copy full message", [layout] {
2018-08-16 00:16:33 +02:00
QString copyString;
layout->addSelectionText(copyString, 0, INT_MAX,
CopyMode::EverythingButReplies);
crossPlatformCopy(copyString);
2018-08-16 00:16:33 +02:00
});
// Only display reply option where it makes sense
if (this->canReplyToMessages() && layout->isReplyable())
{
const auto &messagePtr = layout->getMessagePtr();
menu.addAction("Reply to message", [this, &messagePtr] {
this->setInputReply(messagePtr);
});
if (messagePtr->replyThread != nullptr)
{
menu.addAction("View thread", [this, &messagePtr] {
this->showReplyThreadPopup(messagePtr);
});
}
}
bool isSearch = this->context_ == Context::Search;
bool isReplyOrUserCard = (this->context_ == Context::ReplyThread ||
this->context_ == Context::UserCard) &&
this->split_;
bool isMentions =
this->channel()->getType() == Channel::Type::TwitchMentions;
if (isSearch || isMentions || isReplyOrUserCard)
{
const auto &messagePtr = layout->getMessagePtr();
menu.addAction("Go to message", [this, &messagePtr, isSearch,
isMentions, isReplyOrUserCard] {
if (isSearch)
{
if (const auto &search =
dynamic_cast<SearchPopup *>(this->parentWidget()))
{
search->goToMessage(messagePtr);
}
}
else if (isMentions)
{
getApp()->windows->scrollToMessage(messagePtr);
}
else if (isReplyOrUserCard)
{
// If the thread is in the mentions channel,
// we need to find the original split.
if (this->split_->getChannel()->getType() ==
Channel::Type::TwitchMentions)
{
getApp()->windows->scrollToMessage(messagePtr);
}
else
{
this->split_->getChannelView().scrollToMessage(messagePtr);
}
}
});
}
}
void ChannelView::addTwitchLinkContextMenuItems(
const MessageLayoutElement *hoveredElement, MessageLayoutPtr /*layout*/,
QMouseEvent * /*event*/, QMenu &menu)
{
if (hoveredElement == nullptr)
{
return;
}
const auto &link = hoveredElement->getLink();
if (link.type != Link::Url)
{
return;
}
static QRegularExpression twitchChannelRegex(
R"(^(?:https?:\/\/)?(?:www\.|go\.)?twitch\.tv\/(?:popout\/)?(?<username>[a-z0-9_]{3,}))",
QRegularExpression::CaseInsensitiveOption);
static QSet<QString> ignoredUsernames{
"directory", //
"downloads", //
"drops", //
"friends", //
"inventory", //
"jobs", //
"login", //
"messages", //
"payments", //
"profile", //
"security", //
"settings", //
"signup", //
"subscriptions", //
"turbo", //
"videos", //
"wallet", //
};
auto twitchMatch = twitchChannelRegex.match(link.value);
auto twitchUsername = twitchMatch.captured("username");
if (!twitchUsername.isEmpty() && !ignoredUsernames.contains(twitchUsername))
{
menu.addSeparator();
menu.addAction("Open in new split", [twitchUsername, this] {
this->openChannelIn.invoke(twitchUsername,
FromTwitchLinkOpenChannelIn::Split);
});
menu.addAction("Open in new tab", [twitchUsername, this] {
this->openChannelIn.invoke(twitchUsername,
FromTwitchLinkOpenChannelIn::Tab);
});
menu.addSeparator();
menu.addAction("Open player in browser", [twitchUsername, this] {
this->openChannelIn.invoke(
twitchUsername, FromTwitchLinkOpenChannelIn::BrowserPlayer);
});
menu.addAction("Open in streamlink", [twitchUsername, this] {
this->openChannelIn.invoke(twitchUsername,
FromTwitchLinkOpenChannelIn::Streamlink);
});
}
}
void ChannelView::addHiddenContextMenuItems(
const MessageLayoutElement * /*hoveredElement*/, MessageLayoutPtr layout,
QMouseEvent *event, QMenu &menu)
{
if (!layout)
{
return;
}
if (event->modifiers() != Qt::ShiftModifier)
{
// NOTE: We currently require the modifier to be ONLY shift - we might want to check if shift is among the modifiers instead
return;
}
2018-01-24 21:44:31 +01:00
if (!layout->getMessage()->id.isEmpty())
{
menu.addAction("Copy message ID",
[messageID = layout->getMessage()->id] {
crossPlatformCopy(messageID);
});
}
2018-01-24 21:44:31 +01:00
}
void ChannelView::addCommandExecutionContextMenuItems(
const MessageLayoutElement * /*hoveredElement*/, MessageLayoutPtr layout,
QMouseEvent * /*event*/, QMenu &menu)
{
/* Get commands to be displayed in context menu;
* only those that had the showInMsgContextMenu check box marked in the Commands page */
std::vector<Command> cmds;
for (auto &cmd : getApp()->commands->items)
{
if (cmd.showInMsgContextMenu)
{
cmds.push_back(cmd);
}
}
if (cmds.empty())
{
return;
}
menu.addSeparator();
auto executeAction = menu.addAction("Execute command");
auto cmdMenu = new QMenu;
executeAction->setMenu(cmdMenu);
for (auto &cmd : cmds)
{
QString inputText = this->selection_.isEmpty()
? layout->getMessage()->messageText
: this->getSelectedText();
inputText.push_front(cmd.name + " ");
cmdMenu->addAction(cmd.name, [this, layout, cmd, inputText] {
ChannelPtr channel;
/* Search popups and user message history's underlyingChannels aren't of type TwitchChannel, but
* we would still like to execute commands from them. Use their source channel instead if applicable. */
if (this->hasSourceChannel())
{
channel = this->sourceChannel();
}
else
{
channel = this->underlyingChannel_;
}
auto split = dynamic_cast<Split *>(this->parentWidget());
QString userText;
if (split)
{
userText = split->getInput().getInputText();
}
// Execute command through right-clicking a message -> Execute command
QString value = getApp()->commands->execCustomCommand(
inputText.split(' '), cmd, true, channel, layout->getMessage(),
{
{"input.text", userText},
});
value = getApp()->commands->execCommand(value, channel, false);
channel->sendMessage(value);
});
}
}
2018-01-24 21:44:31 +01:00
void ChannelView::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() != Qt::LeftButton)
{
return;
}
std::shared_ptr<MessageLayout> layout;
QPoint relativePos;
int messageIndex;
2018-01-24 21:44:31 +01:00
2018-10-21 13:43:02 +02:00
if (!tryGetMessageAt(event->pos(), layout, relativePos, messageIndex))
{
return;
}
2018-01-24 21:44:31 +01:00
// message under cursor is collapsed
2018-10-21 13:43:02 +02:00
if (layout->flags.has(MessageLayoutFlag::Collapsed))
{
return;
}
2017-04-12 17:46:44 +02:00
const MessageLayoutElement *hoverLayoutElement =
layout->getElementAt(relativePos);
this->lastDClickPosition_ = event->screenPos();
2018-01-24 21:44:31 +01:00
2018-10-21 13:43:02 +02:00
if (hoverLayoutElement == nullptr)
{
// Possibility for triple click which doesn't have to be over an
// existing layout element
this->clickTimer_->start();
return;
}
2018-11-14 17:26:08 +01:00
if (!this->isLeftMouseDown_)
2018-10-21 13:43:02 +02:00
{
2018-10-06 13:43:21 +02:00
this->isDoubleClick_ = true;
int wordStart;
int wordEnd;
this->getWordBounds(layout.get(), hoverLayoutElement, relativePos,
wordStart, wordEnd);
this->clickTimer_->start();
SelectionItem wordMin(messageIndex, wordStart);
SelectionItem wordMax(messageIndex, wordEnd);
2018-10-06 13:43:21 +02:00
2018-11-14 17:26:08 +01:00
this->doubleClickSelection_.originalStart = wordStart;
this->doubleClickSelection_.originalEnd = wordEnd;
this->doubleClickSelection_.origMessageIndex = messageIndex;
this->doubleClickSelection_.origStartItem = wordMin;
this->doubleClickSelection_.origEndItem = wordMax;
2018-10-06 13:43:21 +02:00
this->setSelection(wordMin, wordMax);
}
2017-04-12 17:46:44 +02:00
2018-10-21 13:43:02 +02:00
if (getSettings()->linksDoubleClickOnly)
{
2018-01-24 21:44:31 +01:00
auto &link = hoverLayoutElement->getLink();
this->handleLinkClick(event, link, layout.get());
}
}
2018-04-18 09:12:29 +02:00
void ChannelView::hideEvent(QHideEvent *)
{
2018-10-21 13:43:02 +02:00
for (auto &layout : this->messagesOnScreen_)
{
2018-04-18 09:12:29 +02:00
layout->deleteBuffer();
}
2018-06-13 13:27:10 +02:00
this->messagesOnScreen_.clear();
2018-04-18 09:12:29 +02:00
}
void ChannelView::showUserInfoPopup(const QString &userName,
QString alternativePopoutChannel)
{
auto *userCardParent =
static_cast<QWidget *>(&(getApp()->windows->getMainWindow()));
auto *userPopup = new UserInfoPopup(getSettings()->autoCloseUserPopup,
userCardParent, this->split_);
auto contextChannel =
getApp()->twitch->getChannelOrEmpty(alternativePopoutChannel);
auto openingChannel = this->hasSourceChannel() ? this->sourceChannel_
: this->underlyingChannel_;
userPopup->setData(userName, contextChannel, openingChannel);
2018-11-21 21:37:41 +01:00
QPoint offset(int(150 * this->scale()), int(70 * this->scale()));
userPopup->move(QCursor::pos() - offset);
userPopup->show();
}
bool ChannelView::mayContainMessage(const MessagePtr &message)
{
switch (this->channel()->getType())
{
case Channel::Type::Direct:
case Channel::Type::Twitch:
case Channel::Type::TwitchWatching:
case Channel::Type::Irc:
return this->channel()->getName() == message->channelName;
case Channel::Type::TwitchWhispers:
return message->flags.has(MessageFlag::Whisper);
case Channel::Type::TwitchMentions:
return message->flags.has(MessageFlag::Highlighted);
case Channel::Type::TwitchLive:
return message->flags.has(MessageFlag::System);
case Channel::Type::TwitchEnd: // TODO: not used?
case Channel::Type::None: // Unspecific
case Channel::Type::Misc: // Unspecific
return true;
default:
return true; // unreachable
}
}
2018-08-06 21:17:03 +02:00
void ChannelView::handleLinkClick(QMouseEvent *event, const Link &link,
MessageLayout *layout)
2018-01-24 21:44:31 +01:00
{
if (event->button() != Qt::LeftButton &&
event->button() != Qt::MiddleButton)
2018-10-21 13:43:02 +02:00
{
2018-05-23 21:16:34 +02:00
return;
}
2018-10-21 13:43:02 +02:00
switch (link.type)
{
case Link::UserWhisper:
2019-09-26 00:51:05 +02:00
case Link::UserInfo: {
2018-01-28 03:52:52 +01:00
auto user = link.value;
this->showUserInfoPopup(user, layout->getMessage()->channelName);
2018-10-21 13:43:02 +02:00
}
break;
2019-09-26 00:51:05 +02:00
case Link::Url: {
if (getSettings()->openLinksIncognito && supportsIncognitoLinks())
openLinkIncognito(link.value);
else
QDesktopServices::openUrl(QUrl(link.value));
2018-10-21 13:43:02 +02:00
}
break;
2019-09-26 00:51:05 +02:00
case Link::UserAction: {
2018-01-28 03:52:52 +01:00
QString value = link.value;
ChannelPtr channel = this->underlyingChannel_;
SearchPopup *searchPopup =
dynamic_cast<SearchPopup *>(this->parentWidget());
if (searchPopup != nullptr)
{
Split *split =
dynamic_cast<Split *>(searchPopup->parentWidget());
if (split != nullptr)
{
channel = split->getChannel();
}
}
// Execute command clicking a moderator button
value = getApp()->commands->execCustomCommand(
QStringList(), Command{"(modaction)", value}, true, channel,
layout->getMessage());
value = getApp()->commands->execCommand(value, channel, false);
channel->sendMessage(value);
2018-10-21 13:43:02 +02:00
}
break;
2019-09-26 00:51:05 +02:00
case Link::AutoModAllow: {
getApp()->accounts->twitch.getCurrent()->autoModAllow(
link.value, this->channel());
}
break;
2019-09-26 00:51:05 +02:00
case Link::AutoModDeny: {
getApp()->accounts->twitch.getCurrent()->autoModDeny(
link.value, this->channel());
}
break;
case Link::OpenAccountsPage: {
SettingsDialog::showDialog(this,
SettingsDialogPreference::Accounts);
}
break;
case Link::JumpToChannel: {
// Get all currently open pages
QList<SplitContainer *> openPages;
auto &nb = getApp()->windows->getMainWindow().getNotebook();
for (int i = 0; i < nb.getPageCount(); ++i)
{
openPages.push_back(
static_cast<SplitContainer *>(nb.getPageAt(i)));
}
for (auto *page : openPages)
{
auto splits = page->getSplits();
// Search for channel matching link in page/split container
// TODO(zneix): Consider opening a channel if it's closed (?)
auto it = std::find_if(
splits.begin(), splits.end(), [link](Split *split) {
return split->getChannel()->getName() == link.value;
});
if (it != splits.end())
{
// Select SplitContainer and Split itself where mention message was sent
// TODO(zneix): Try exploring ways of scrolling to a certain message as well
nb.select(page);
Split *split = *it;
page->setSelected(split);
break;
}
}
}
break;
case Link::CopyToClipboard: {
crossPlatformCopy(link.value);
}
break;
case Link::Reconnect: {
this->underlyingChannel_.get()->reconnect();
}
break;
case Link::ReplyToMessage: {
this->setInputReply(layout->getMessagePtr());
}
break;
case Link::ViewThread: {
this->showReplyThreadPopup(layout->getMessagePtr());
}
break;
case Link::JumpToMessage: {
if (this->context_ == Context::Search)
{
if (auto search =
dynamic_cast<SearchPopup *>(this->parentWidget()))
{
search->goToMessageId(link.value);
}
}
else
{
this->scrollToMessageId(link.value);
}
}
break;
default:;
2017-04-24 23:00:26 +02:00
}
2017-04-12 17:46:44 +02:00
}
2018-08-06 21:17:03 +02:00
bool ChannelView::tryGetMessageAt(QPoint p,
std::shared_ptr<MessageLayout> &_message,
QPoint &relativePos, int &index)
2017-02-17 23:51:35 +01:00
{
auto &messagesSnapshot = this->getMessagesSnapshot();
2017-02-17 23:51:35 +01:00
size_t start = this->scrollBar_->getCurrentValue();
2017-02-17 23:51:35 +01:00
2018-11-03 21:26:57 +01:00
if (start >= messagesSnapshot.size())
2018-10-21 13:43:02 +02:00
{
2017-02-17 23:51:35 +01:00
return false;
}
2018-08-06 21:17:03 +02:00
int y = -(messagesSnapshot[start]->getHeight() *
(fmod(this->scrollBar_->getCurrentValue(), 1)));
2017-02-17 23:51:35 +01:00
2018-11-03 21:26:57 +01:00
for (size_t i = start; i < messagesSnapshot.size(); ++i)
2018-10-21 13:43:02 +02:00
{
auto message = messagesSnapshot[i];
2017-02-17 23:51:35 +01:00
2018-10-21 13:43:02 +02:00
if (p.y() < y + message->getHeight())
{
2017-04-24 23:00:26 +02:00
relativePos = QPoint(p.x(), p.y() - y);
2017-02-17 23:51:35 +01:00
_message = message;
2017-08-18 15:12:07 +02:00
index = i;
2017-02-17 23:51:35 +01:00
return true;
}
2017-04-24 23:00:26 +02:00
y += message->getHeight();
2017-02-17 23:51:35 +01:00
}
return false;
}
2017-06-06 17:18:23 +02:00
2018-06-13 03:58:52 +02:00
int ChannelView::getLayoutWidth() const
{
if (this->scrollBar_->isVisible())
2018-12-02 17:49:15 +01:00
return int(this->width() - scrollbarPadding * this->scale());
2018-06-13 03:58:52 +02:00
return this->width();
}
void ChannelView::selectWholeMessage(MessageLayout *layout, int &messageIndex)
{
SelectionItem msgStart(messageIndex,
layout->getFirstMessageCharacterIndex());
SelectionItem msgEnd(messageIndex, layout->getLastCharacterIndex());
this->setSelection(msgStart, msgEnd);
}
2018-10-06 13:43:21 +02:00
void ChannelView::getWordBounds(MessageLayout *layout,
const MessageLayoutElement *element,
const QPoint &relativePos, int &wordStart,
int &wordEnd)
{
const int mouseInWordIndex = element->getMouseOverIndex(relativePos);
wordStart = layout->getSelectionIndex(relativePos) - mouseInWordIndex;
const int selectionLength = element->getSelectionIndexCount();
const int length =
element->hasTrailingSpace() ? selectionLength - 1 : selectionLength;
wordEnd = wordStart + length;
}
void ChannelView::enableScrolling(const QPointF &scrollStart)
{
this->isScrolling_ = true;
this->lastMiddlePressPosition_ = scrollStart;
// The line below prevents a sudden jerk at the beginning
this->currentMousePosition_ = scrollStart;
this->scrollTimer_.start();
if (!QGuiApplication::overrideCursor())
QGuiApplication::setOverrideCursor(this->cursors_.neutral);
}
void ChannelView::disableScrolling()
{
this->isScrolling_ = false;
this->scrollTimer_.stop();
QGuiApplication::restoreOverrideCursor();
}
void ChannelView::scrollUpdateRequested()
{
const qreal dpi = this->devicePixelRatioF();
const qreal delta = dpi * (this->currentMousePosition_.y() -
this->lastMiddlePressPosition_.y());
const int cursorHeight = this->cursors_.neutral.pixmap().height();
if (fabs(delta) <= cursorHeight * dpi)
{
/*
* If within an area close to the initial position, don't do any
* scrolling at all.
*/
QGuiApplication::changeOverrideCursor(this->cursors_.neutral);
return;
}
qreal offset;
if (delta > 0)
{
QGuiApplication::changeOverrideCursor(this->cursors_.down);
offset = delta - cursorHeight;
}
else
{
QGuiApplication::changeOverrideCursor(this->cursors_.up);
offset = delta + cursorHeight;
}
// "Good" feeling multiplier found by trial-and-error
const qreal multiplier = qreal(0.02);
this->scrollBar_->offset(multiplier * offset);
}
void ChannelView::setInputReply(const MessagePtr &message)
{
if (message == nullptr)
{
return;
}
std::shared_ptr<MessageThread> thread;
if (message->replyThread == nullptr)
{
auto getThread = [&](TwitchChannel *tc) {
auto threadIt = tc->threads().find(message->id);
if (threadIt != tc->threads().end() && !threadIt->second.expired())
{
return threadIt->second.lock();
}
else
{
auto thread = std::make_shared<MessageThread>(message);
tc->addReplyThread(thread);
return thread;
}
};
if (auto tc =
dynamic_cast<TwitchChannel *>(this->underlyingChannel_.get()))
{
thread = getThread(tc);
}
else if (auto tc = dynamic_cast<TwitchChannel *>(this->channel_.get()))
{
thread = getThread(tc);
}
else
{
qCWarning(chatterinoCommon) << "Failed to create new reply thread";
// Unable to create new reply thread.
// TODO(dnsge): Should probably notify user?
return;
}
}
else
{
thread = message->replyThread;
}
this->split_->setInputReply(thread);
}
void ChannelView::showReplyThreadPopup(const MessagePtr &message)
{
if (message == nullptr || message->replyThread == nullptr)
{
return;
}
auto popupParent =
static_cast<QWidget *>(&(getApp()->windows->getMainWindow()));
auto popup = new ReplyThreadPopup(getSettings()->autoCloseThreadPopup,
popupParent, this->split_);
popup->setThread(message->replyThread);
QPoint offset(int(150 * this->scale()), int(70 * this->scale()));
popup->move(QCursor::pos() - offset);
popup->show();
popup->giveFocus(Qt::MouseFocusReason);
}
ChannelView::Context ChannelView::getContext() const
{
return this->context_;
}
bool ChannelView::canReplyToMessages() const
{
if (this->context_ == ChannelView::Context::ReplyThread ||
this->context_ == ChannelView::Context::Search)
{
return false;
}
if (this->channel_ == nullptr)
{
return false;
}
if (!this->channel_->isTwitchChannel())
{
return false;
}
if (this->channel_->getType() == Channel::Type::TwitchWhispers ||
this->channel_->getType() == Channel::Type::TwitchLive)
{
return false;
}
return true;
}
2017-04-14 17:52:22 +02:00
} // namespace chatterino