Add setting to prevent or highlight message overflow (#3418)

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
Adam Davies
2022-11-13 05:47:46 -06:00
committed by GitHub
parent d409e3f17d
commit a9d3c00369
5 changed files with 90 additions and 1 deletions
+52 -1
View File
@@ -29,7 +29,6 @@
#include <QPainter>
namespace chatterino {
const int TWITCH_MESSAGE_LIMIT = 500;
SplitInput::SplitInput(Split *_chatWidget, bool enableInlineReplying)
: SplitInput(_chatWidget, _chatWidget, enableInlineReplying)
@@ -848,6 +847,13 @@ void SplitInput::editTextChanged()
// set textLengthLabel value
QString text = this->ui_.textEdit->toPlainText();
if (this->shouldPreventInput(text))
{
this->ui_.textEdit->setPlainText(text.left(TWITCH_MESSAGE_LIMIT));
this->ui_.textEdit->moveCursor(QTextCursor::EndOfBlock);
return;
}
if (text.startsWith("/r ", Qt::CaseInsensitive) &&
this->split_->getChannel()->isTwitchChannel())
{
@@ -867,6 +873,28 @@ void SplitInput::editTextChanged()
app->commands->execCommand(text, this->split_->getChannel(), true);
}
if (getSettings()->messageOverflow.getValue() == MessageOverflow::Highlight)
{
if (text.length() > TWITCH_MESSAGE_LIMIT &&
text.length() > this->lastOverflowLength)
{
QTextCharFormat format;
format.setForeground(Qt::red);
QTextCursor cursor = this->ui_.textEdit->textCursor();
cursor.setPosition(lastOverflowLength, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
this->lastOverflowLength = text.length();
cursor.setCharFormat(format);
}
else if (this->lastOverflowLength != TWITCH_MESSAGE_LIMIT)
{
this->lastOverflowLength = TWITCH_MESSAGE_LIMIT;
}
}
QString labelText;
if (text.length() > 0 && getSettings()->showMessageLength)
@@ -1012,4 +1040,27 @@ void SplitInput::clearInput()
}
}
bool SplitInput::shouldPreventInput(const QString &text) const
{
if (getSettings()->messageOverflow.getValue() != MessageOverflow::Prevent)
{
return false;
}
auto channel = this->split_->getChannel();
if (channel == nullptr)
{
return false;
}
if (!channel->isTwitchChannel())
{
// Don't respect this setting for IRC channels as the limits might be server-specific
return false;
}
return text.length() > TWITCH_MESSAGE_LIMIT;
}
} // namespace chatterino