fix(macOS): command + delete (#6676)

Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
Reviewed-by: Nerixyz <nerixdev@outlook.de>
This commit is contained in:
Jacob Alexander Thompson
2025-12-28 04:42:14 -08:00
committed by GitHub
parent e2ba7dff03
commit 399bacc4eb
7 changed files with 72 additions and 16 deletions
+46
View File
@@ -0,0 +1,46 @@
#include "widgets/helper/CmdDeleteKeyFilter.hpp"
#include <QKeyEvent>
#include <QTextCursor>
#include <QTextEdit>
CmdDeleteKeyFilter::CmdDeleteKeyFilter(QObject *parent)
: QObject(parent)
{
}
bool CmdDeleteKeyFilter::eventFilter(QObject *obj, QEvent *event)
{
#ifdef Q_OS_MACOS
if (event->type() != QEvent::KeyPress)
{
return false;
}
auto *textEdit = qobject_cast<QTextEdit *>(obj);
const auto *keyEvent = dynamic_cast<QKeyEvent *>(event);
if (!textEdit || !keyEvent)
{
return false;
}
if (keyEvent->modifiers() == Qt::ControlModifier &&
keyEvent->key() == Qt::Key_Backspace)
{
QTextCursor cursor = textEdit->textCursor();
if (!cursor.hasSelection())
{
cursor.movePosition(QTextCursor::StartOfLine,
QTextCursor::KeepAnchor);
}
cursor.removeSelectedText();
textEdit->setTextCursor(cursor);
return true;
}
#endif
return false;
}
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include <QObject>
/// CmdDeleteKeyFilter is an event filter for QTextEdit objects
/// to handle the behavior of the Command + Delete shortcut on macOS
class CmdDeleteKeyFilter : public QObject
{
Q_OBJECT
public:
explicit CmdDeleteKeyFilter(QObject *parent = nullptr);
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
};
-16
View File
@@ -131,22 +131,6 @@ bool ResizingTextEdit::eventFilter(QObject *obj, QEvent *event)
}
void ResizingTextEdit::keyPressEvent(QKeyEvent *event)
{
#ifdef Q_OS_MACOS
if ((event->modifiers() == Qt::ControlModifier) &&
(event->key() == Qt::Key_Backspace))
{
QTextCursor cursor = this->textCursor();
if (!cursor.hasSelection())
{
cursor.movePosition(QTextCursor::StartOfLine,
QTextCursor::KeepAnchor);
}
cursor.removeSelectedText();
this->setTextCursor(cursor);
return;
}
#endif
event->ignore();
this->keyPressed.invoke(event);