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
|
||||
#include <QCoreApplication>
|
||||
|
||||
CapsLockIndicator::EventHandler* CapsLockIndicator::eventHandler{nullptr};
|
||||
|
||||
CapsLockIndicator::CapsLockIndicator(QObject* parent) :
|
||||
QAction(parent)
|
||||
{
|
||||
setIcon(QIcon(":img/caps_lock.svg"));
|
||||
setToolTip(tr("CAPS-LOCK ENABLED"));
|
||||
|
||||
QCoreApplication::instance()->installEventFilter(this);
|
||||
if (!eventHandler)
|
||||
eventHandler = new EventHandler();
|
||||
eventHandler->actions.append(this);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void CapsLockIndicator::updateIndicator()
|
||||
void CapsLockIndicator::EventHandler::updateActions(const QObject* object)
|
||||
{
|
||||
bool caps = false;
|
||||
// It doesn't needed for OSX, because it shows indicator by default
|
||||
|
@ -26,24 +45,27 @@ void CapsLockIndicator::updateIndicator()
|
|||
caps = Platform::capsLockEnabled();
|
||||
#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())
|
||||
{
|
||||
case QEvent::Show:
|
||||
if (obj == this)
|
||||
updateIndicator();
|
||||
updateActions(obj);
|
||||
break;
|
||||
case QEvent::WindowActivate:
|
||||
case QEvent::KeyRelease:
|
||||
updateIndicator();
|
||||
updateActions();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return QAction::eventFilter(obj, event);
|
||||
return QObject::eventFilter(obj, event);
|
||||
}
|
||||
|
|
|
@ -10,10 +10,19 @@ public:
|
|||
CapsLockIndicator(QObject *parent);
|
||||
~CapsLockIndicator();
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *obj, QEvent *event);
|
||||
private:
|
||||
class EventHandler : QObject
|
||||
{
|
||||
public:
|
||||
QVector<QAction*> actions;
|
||||
|
||||
EventHandler();
|
||||
~EventHandler();
|
||||
void updateActions(const QObject* object = nullptr);
|
||||
bool eventFilter(QObject *obj, QEvent *event);
|
||||
};
|
||||
|
||||
private:
|
||||
void updateIndicator();
|
||||
static EventHandler* eventHandler;
|
||||
};
|
||||
#endif // CAPSLOCKINDICATOR_H
|
||||
|
|
Loading…
Reference in New Issue
Block a user