Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting to prevent or highlight message overflow #3418

Merged
merged 20 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- Minor: Show picked outcome in prediction badges. (#3357)
- Minor: Add support for Emoji in IRC (#3354)
- Minor: Moved `/live` logs to its own subdirectory. (Logs from before this change will still be available in `Channels -> live`). (#3393)
- Minor: Add setting to limit message input length (#3418)
acdvs marked this conversation as resolved.
Show resolved Hide resolved
- Bugfix: Fix Split Input hotkeys not being available when input is hidden (#3362)
- Bugfix: Fixed colored usernames sometimes not working. (#3170)
- Bugfix: Restored ability to send duplicate `/me` messages. (#3166)
Expand Down
2 changes: 2 additions & 0 deletions src/singletons/Settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class Settings : public ABSettings, public ConcurrentSettings
// {"/appearance/messages/collapseLongMessages", false};
IntSetting collpseMessagesMinLines = {
"/appearance/messages/collapseMessagesMinLines", 0};
BoolSetting enforceMaxMessageLength = {
"/appearance/messages/enforceMaxMessageLength", false};
BoolSetting alternateMessages = {
"/appearance/messages/alternateMessageBackground", false};
FloatSetting boldScale = {"/appearance/boldScale", 63};
Expand Down
36 changes: 36 additions & 0 deletions src/widgets/helper/ResizingTextEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "common/Common.hpp"
#include "common/CompletionModel.hpp"
#include "singletons/Settings.hpp"
#include "widgets/splits/SplitInput.hpp"

#include <QMimeData>
#include <QMimeDatabase>
Expand Down Expand Up @@ -118,6 +119,13 @@ void ResizingTextEdit::keyPressEvent(QKeyEvent *event)

this->keyPressed.invoke(event);

if (getSettings()->enforceMaxMessageLength.getValue() &&
this->toPlainText().size() >= SplitInput::TWITCH_MESSAGE_LIMIT &&
!keyEventIsNav(event))
{
return;
}

bool doComplete =
(event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab) &&
(event->modifiers() & Qt::ControlModifier) == Qt::NoModifier &&
Expand Down Expand Up @@ -191,6 +199,18 @@ void ResizingTextEdit::keyPressEvent(QKeyEvent *event)
}
}

bool ResizingTextEdit::keyEventIsNav(QKeyEvent *event)
{
const bool hasNavKey =
std::any_of(navKeys.cbegin(), navKeys.cend(), [event](int navKey) {
return event->key() == navKey;
});
const bool hasModifier = event->modifiers().testFlag(Qt::ControlModifier) ||
event->modifiers().testFlag(Qt::MetaModifier);

return hasModifier || hasNavKey;
}

void ResizingTextEdit::focusInEvent(QFocusEvent *event)
{
QTextEdit::focusInEvent(event);
Expand Down Expand Up @@ -305,6 +325,22 @@ void ResizingTextEdit::insertFromMimeData(const QMimeData *source)
}
}

if (getSettings()->enforceMaxMessageLength.getValue())
{
const int remainingSize =
SplitInput::TWITCH_MESSAGE_LIMIT - this->toPlainText().size();

if (remainingSize == 0)
{
return;
}
else if (source->text().size() > remainingSize)
{
insertPlainText(source->text().left(remainingSize));
return;
}
}

insertPlainText(source->text());
}

Expand Down
6 changes: 6 additions & 0 deletions src/widgets/helper/ResizingTextEdit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class ResizingTextEdit : public QTextEdit
bool completionInProgress_ = false;

bool eventFilter(QObject *widget, QEvent *event) override;

bool keyEventIsNav(QKeyEvent *event);
const std::vector<Qt::Key> navKeys{
Qt::Key_Backspace, Qt::Key_Delete, Qt::Key_Home, Qt::Key_End,
Qt::Key_PageUp, Qt::Key_PageDown, Qt::Key_Left, Qt::Key_Up,
Qt::Key_Right, Qt::Key_Down};
private slots:
void insertCompletion(const QString &completion);
};
Expand Down
2 changes: 2 additions & 0 deletions src/widgets/settingspages/GeneralPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ void GeneralPage::initLayout(GeneralPageView &layout)
[](auto args) {
return fuzzyToInt(args.value, 0);
});
layout.addCheckbox("Prevent typing past maximum message length",
s.enforceMaxMessageLength);
layout.addSeperator();
layout.addCheckbox("Draw a line below the most recent message before "
"switching applications.",
Expand Down
1 change: 0 additions & 1 deletion src/widgets/splits/SplitInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <QPainter>

namespace chatterino {
const int TWITCH_MESSAGE_LIMIT = 500;

SplitInput::SplitInput(Split *_chatWidget)
: BaseWidget(_chatWidget)
Expand Down
2 changes: 2 additions & 0 deletions src/widgets/splits/SplitInput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class SplitInput : public BaseWidget
QString getInputText() const;
void insertText(const QString &text);

static const int TWITCH_MESSAGE_LIMIT = 500;

pajlada::Signals::Signal<const QString &> textChanged;

protected:
Expand Down