mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor(capslockindicator): use a single shared event handler
Optimize the implementation by using a single shared event handler, managing the caps lock state check and propagating it appropriately to all instances at once.
This commit is contained in:
parent
6a40ca0b55
commit
61892d0c18
|
@ -4,21 +4,40 @@
|
||||||
#endif
|
#endif
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
|
||||||
|
CapsLockIndicator::EventHandler* CapsLockIndicator::eventHandler{nullptr};
|
||||||
|
|
||||||
CapsLockIndicator::CapsLockIndicator(QObject* parent) :
|
CapsLockIndicator::CapsLockIndicator(QObject* parent) :
|
||||||
QAction(parent)
|
QAction(parent)
|
||||||
{
|
{
|
||||||
setIcon(QIcon(":img/caps_lock.svg"));
|
setIcon(QIcon(":img/caps_lock.svg"));
|
||||||
setToolTip(tr("CAPS-LOCK ENABLED"));
|
setToolTip(tr("CAPS-LOCK ENABLED"));
|
||||||
|
|
||||||
QCoreApplication::instance()->installEventFilter(this);
|
if (!eventHandler)
|
||||||
|
eventHandler = new EventHandler();
|
||||||
|
eventHandler->actions.append(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
CapsLockIndicator::~CapsLockIndicator()
|
CapsLockIndicator::~CapsLockIndicator()
|
||||||
|
{
|
||||||
|
eventHandler->actions.removeOne(this);
|
||||||
|
if (eventHandler->actions.isEmpty())
|
||||||
|
{
|
||||||
|
delete eventHandler;
|
||||||
|
eventHandler = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CapsLockIndicator::EventHandler::EventHandler()
|
||||||
|
{
|
||||||
|
QCoreApplication::instance()->installEventFilter(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
CapsLockIndicator::EventHandler::~EventHandler()
|
||||||
{
|
{
|
||||||
QCoreApplication::instance()->removeEventFilter(this);
|
QCoreApplication::instance()->removeEventFilter(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CapsLockIndicator::updateIndicator()
|
void CapsLockIndicator::EventHandler::updateActions(const QObject* object)
|
||||||
{
|
{
|
||||||
bool caps = false;
|
bool caps = false;
|
||||||
// It doesn't needed for OSX, because it shows indicator by default
|
// It doesn't needed for OSX, because it shows indicator by default
|
||||||
|
@ -26,24 +45,27 @@ void CapsLockIndicator::updateIndicator()
|
||||||
caps = Platform::capsLockEnabled();
|
caps = Platform::capsLockEnabled();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
setVisible(caps);
|
for (QAction* action : actions)
|
||||||
|
{
|
||||||
|
if (! object || object == action)
|
||||||
|
action->setVisible(caps);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CapsLockIndicator::eventFilter(QObject *obj, QEvent *event)
|
bool CapsLockIndicator::EventHandler::eventFilter(QObject *obj, QEvent *event)
|
||||||
{
|
{
|
||||||
switch (event->type())
|
switch (event->type())
|
||||||
{
|
{
|
||||||
case QEvent::Show:
|
case QEvent::Show:
|
||||||
if (obj == this)
|
updateActions(obj);
|
||||||
updateIndicator();
|
|
||||||
break;
|
break;
|
||||||
case QEvent::WindowActivate:
|
case QEvent::WindowActivate:
|
||||||
case QEvent::KeyRelease:
|
case QEvent::KeyRelease:
|
||||||
updateIndicator();
|
updateActions();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return QAction::eventFilter(obj, event);
|
return QObject::eventFilter(obj, event);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,19 @@ public:
|
||||||
CapsLockIndicator(QObject *parent);
|
CapsLockIndicator(QObject *parent);
|
||||||
~CapsLockIndicator();
|
~CapsLockIndicator();
|
||||||
|
|
||||||
protected:
|
private:
|
||||||
bool eventFilter(QObject *obj, QEvent *event);
|
class EventHandler : QObject
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QVector<QAction*> actions;
|
||||||
|
|
||||||
|
EventHandler();
|
||||||
|
~EventHandler();
|
||||||
|
void updateActions(const QObject* object = nullptr);
|
||||||
|
bool eventFilter(QObject *obj, QEvent *event);
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void updateIndicator();
|
static EventHandler* eventHandler;
|
||||||
};
|
};
|
||||||
#endif // CAPSLOCKINDICATOR_H
|
#endif // CAPSLOCKINDICATOR_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user