Handle incomplete commands better

This commit is contained in:
Loren Burkholder 2023-03-07 19:10:42 -05:00
parent 2dc7492456
commit b266185ce8
4 changed files with 34 additions and 15 deletions

View file

@ -11,6 +11,7 @@ Rectangle {
id: warningRoot id: warningRoot
required property string text required property string text
property color bubbleColor: Nheko.theme.error
implicitHeight: visible ? warningDisplay.implicitHeight + 4 * Nheko.paddingSmall : 0 implicitHeight: visible ? warningDisplay.implicitHeight + 4 * Nheko.paddingSmall : 0
height: implicitHeight height: implicitHeight
@ -22,9 +23,9 @@ Rectangle {
visible: warningRoot.visible visible: warningRoot.visible
// TODO: Qt.alpha() would make more sense but it wasn't working... // TODO: Qt.alpha() would make more sense but it wasn't working...
color: Qt.rgba(Nheko.theme.error.r, Nheko.theme.error.g, Nheko.theme.error.b, 0.3) color: Qt.rgba(bubbleColor.r, bubbleColor.g, bubbleColor.b, 0.3)
border.width: 1 border.width: 1
border.color: Nheko.theme.error border.color: bubbleColor
radius: 3 radius: 3
anchors.fill: parent anchors.fill: parent
anchors.margins: visible ? Nheko.paddingSmall : 0 anchors.margins: visible ? Nheko.paddingSmall : 0

View file

@ -160,7 +160,13 @@ Item {
MessageInputWarning { MessageInputWarning {
text: qsTr("The command /%1 is not recognized and will be sent as part of your message").arg(room ? room.input.currentCommand : "") text: qsTr("The command /%1 is not recognized and will be sent as part of your message").arg(room ? room.input.currentCommand : "")
visible: room ? room.input.containsInvalidCommand : false visible: room ? room.input.containsInvalidCommand && !room.input.containsIncompleteCommand : false
}
MessageInputWarning {
text: qsTr("/%1 looks like an incomplete command. To send it anyway, add a space to the end of your message.").arg(room ? room.input.currentCommand : "")
visible: room ? room.input.containsIncompleteCommand : false
bubbleColor: Nheko.theme.orange
} }
ReplyPopup { ReplyPopup {

View file

@ -252,7 +252,7 @@ InputBar::updateTextContentProperties(const QString &t)
} }
// check for invalid commands // check for invalid commands
auto commandName = getCommandAndArgs().first; auto commandName = getCommandAndArgs(t).first;
static const QSet<QString> validCommands{QStringLiteral("me"), static const QSet<QString> validCommands{QStringLiteral("me"),
QStringLiteral("react"), QStringLiteral("react"),
QStringLiteral("join"), QStringLiteral("join"),
@ -284,14 +284,18 @@ InputBar::updateTextContentProperties(const QString &t)
QStringLiteral("goto"), QStringLiteral("goto"),
QStringLiteral("converttodm"), QStringLiteral("converttodm"),
QStringLiteral("converttoroom")}; QStringLiteral("converttoroom")};
bool hasInvalidCommand = bool hasInvalidCommand = !commandName.isNull() && !validCommands.contains(commandName);
!commandName.isNull() && '/' + commandName != text() && !validCommands.contains(commandName); bool hasIncompleteCommand = hasInvalidCommand && '/' + commandName == t;
bool signalsChanged{false}; bool signalsChanged{false};
if (containsInvalidCommand_ != hasInvalidCommand) { if (containsInvalidCommand_ != hasInvalidCommand) {
containsInvalidCommand_ = hasInvalidCommand; containsInvalidCommand_ = hasInvalidCommand;
signalsChanged = true; signalsChanged = true;
} }
if (containsIncompleteCommand_ != hasIncompleteCommand) {
containsIncompleteCommand_ = hasIncompleteCommand;
signalsChanged = true;
}
if (currentCommand_ != commandName) { if (currentCommand_ != commandName) {
currentCommand_ = commandName; currentCommand_ = commandName;
signalsChanged = true; signalsChanged = true;
@ -299,6 +303,7 @@ InputBar::updateTextContentProperties(const QString &t)
if (signalsChanged) { if (signalsChanged) {
emit currentCommandChanged(); emit currentCommandChanged();
emit containsInvalidCommandChanged(); emit containsInvalidCommandChanged();
emit containsIncompleteCommandChanged();
} }
} }
@ -392,9 +397,11 @@ InputBar::send()
auto wasEdit = !room->edit().isEmpty(); auto wasEdit = !room->edit().isEmpty();
if (auto [commandName, args] = getCommandAndArgs(); commandName.isEmpty()) auto [commandName, args] = getCommandAndArgs();
message(text()); updateTextContentProperties(text());
else if (!command(commandName, args)) if (containsIncompleteCommand_)
return;
if (commandName.isEmpty() || !command(commandName, args))
message(text()); message(text());
if (!wasEdit) { if (!wasEdit) {
@ -758,9 +765,8 @@ InputBar::video(const QString &filename,
} }
QPair<QString, QString> QPair<QString, QString>
InputBar::getCommandAndArgs() const InputBar::getCommandAndArgs(const QString &currentText) const
{ {
const auto currentText = text();
if (!currentText.startsWith('/')) if (!currentText.startsWith('/'))
return {{}, currentText}; return {{}, currentText};

View file

@ -175,6 +175,8 @@ class InputBar final : public QObject
Q_PROPERTY(bool containsAtRoom READ containsAtRoom NOTIFY containsAtRoomChanged) Q_PROPERTY(bool containsAtRoom READ containsAtRoom NOTIFY containsAtRoomChanged)
Q_PROPERTY( Q_PROPERTY(
bool containsInvalidCommand READ containsInvalidCommand NOTIFY containsInvalidCommandChanged) bool containsInvalidCommand READ containsInvalidCommand NOTIFY containsInvalidCommandChanged)
Q_PROPERTY(bool containsIncompleteCommand READ containsIncompleteCommand NOTIFY
containsIncompleteCommandChanged)
Q_PROPERTY(QString currentCommand READ currentCommand NOTIFY currentCommandChanged) Q_PROPERTY(QString currentCommand READ currentCommand NOTIFY currentCommandChanged)
Q_PROPERTY(QString text READ text NOTIFY textChanged) Q_PROPERTY(QString text READ text NOTIFY textChanged)
Q_PROPERTY(QVariantList uploads READ uploads NOTIFY uploadsChanged) Q_PROPERTY(QVariantList uploads READ uploads NOTIFY uploadsChanged)
@ -202,6 +204,7 @@ public slots:
[[nodiscard]] bool containsAtRoom() const { return containsAtRoom_; } [[nodiscard]] bool containsAtRoom() const { return containsAtRoom_; }
bool containsInvalidCommand() const { return containsInvalidCommand_; } bool containsInvalidCommand() const { return containsInvalidCommand_; }
bool containsIncompleteCommand() const { return containsIncompleteCommand_; }
QString currentCommand() const { return currentCommand_; } QString currentCommand() const { return currentCommand_; }
void send(); void send();
@ -231,6 +234,7 @@ signals:
void uploadingChanged(bool value); void uploadingChanged(bool value);
void containsAtRoomChanged(); void containsAtRoomChanged();
void containsInvalidCommandChanged(); void containsInvalidCommandChanged();
void containsIncompleteCommandChanged();
void currentCommandChanged(); void currentCommandChanged();
void uploadsChanged(); void uploadsChanged();
@ -274,7 +278,8 @@ private:
const QSize &thumbnailDimensions, const QSize &thumbnailDimensions,
const QString &blurhash); const QString &blurhash);
QPair<QString, QString> getCommandAndArgs() const; QPair<QString, QString> getCommandAndArgs() const { return getCommandAndArgs(text()); }
QPair<QString, QString> getCommandAndArgs(const QString &currentText) const;
mtx::common::Relations generateRelations() const; mtx::common::Relations generateRelations() const;
void startUploadFromPath(const QString &path); void startUploadFromPath(const QString &path);
@ -299,6 +304,7 @@ private:
bool uploading_ = false; bool uploading_ = false;
bool containsAtRoom_ = false; bool containsAtRoom_ = false;
bool containsInvalidCommand_ = false; bool containsInvalidCommand_ = false;
bool containsIncompleteCommand_ = false;
QString currentCommand_; QString currentCommand_;
using UploadHandle = std::unique_ptr<MediaUpload, DeleteLaterDeleter>; using UploadHandle = std::unique_ptr<MediaUpload, DeleteLaterDeleter>;