fix(macOS): command + delete (#6676)
Reviewed-by: pajlada <rasmus.karlsson@pajlada.com> Reviewed-by: Nerixyz <nerixdev@outlook.de>
This commit is contained in:
committed by
GitHub
parent
e2ba7dff03
commit
399bacc4eb
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user