Skip to content

Commit

Permalink
Implement Secure Input Field mode on macOS
Browse files Browse the repository at this point in the history
* Fixes #4738
* Also fixes flaky handling of caps lock detection events
  • Loading branch information
droidmonkey committed Jan 4, 2025
1 parent edab0fa commit 965cf58
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
28 changes: 21 additions & 7 deletions src/gui/PasswordWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@
#include <QTimer>
#include <QToolTip>

#ifdef Q_OS_MACOS
#include <Carbon/Carbon.h>
#endif

PasswordWidget::PasswordWidget(QWidget* parent)
: QWidget(parent)
, m_ui(new Ui::PasswordWidget())
{
m_ui->setupUi(this);
setFocusProxy(m_ui->passwordEdit);
m_ui->passwordEdit->installEventFilter(this);

const QIcon errorIcon = icons()->icon("dialog-error");
m_errorAction = m_ui->passwordEdit->addAction(errorIcon, QLineEdit::TrailingPosition);
Expand Down Expand Up @@ -223,14 +228,23 @@ void PasswordWidget::updateRepeatStatus()
}
}

bool PasswordWidget::event(QEvent* event)
bool PasswordWidget::eventFilter(QObject* watched, QEvent* event)
{
if (isVisible()
&& (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease
|| event->type() == QEvent::FocusIn)) {
checkCapslockState();
if (watched == m_ui->passwordEdit) {
auto type = event->type();
if (isVisible() && (type == QEvent::KeyPress || type == QEvent::KeyRelease || type == QEvent::FocusIn)) {
checkCapslockState();
}
#ifdef Q_OS_MACOS
if (type == QEvent::FocusIn) {
EnableSecureEventInput();
} else if (type == QEvent::FocusOut) {
DisableSecureEventInput();
}
#endif
}
return QWidget::event(event);
// Continue with normal operations
return false;
}

void PasswordWidget::checkCapslockState()
Expand Down Expand Up @@ -306,4 +320,4 @@ void PasswordWidget::updatePasswordStrength(const QString& password)

break;
}
}
}
5 changes: 2 additions & 3 deletions src/gui/PasswordWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class PasswordWidget : public QWidget
bool isPasswordVisible() const;
QString text();

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

signals:
void textChanged(QString text);

Expand All @@ -57,9 +59,6 @@ public slots:
void setEchoMode(QLineEdit::EchoMode mode);
void setClearButtonEnabled(bool enabled);

protected:
bool event(QEvent* event) override;

private slots:
void popupPasswordGenerator();
void updateRepeatStatus();
Expand Down

0 comments on commit 965cf58

Please sign in to comment.