1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(capslockindicator): avoid overhead on OSX

On OSX the indicator is disabled, so avoid most of the overhead by not
listening to any events that wouldn't lead to any change anyway.
This commit is contained in:
Colomban Wendling 2016-06-16 18:57:35 +02:00
parent 61892d0c18
commit 3454f96d4c

View File

@ -4,29 +4,41 @@
#endif #endif
#include <QCoreApplication> #include <QCoreApplication>
// It isn't needed for OSX, because it shows indicator by default
#if defined(QTOX_PLATFORM_EXT) && !defined(Q_OS_OSX)
#define ENABLE_CAPSLOCK_INDICATOR
#endif
CapsLockIndicator::EventHandler* CapsLockIndicator::eventHandler{nullptr}; CapsLockIndicator::EventHandler* CapsLockIndicator::eventHandler{nullptr};
CapsLockIndicator::CapsLockIndicator(QObject* parent) : CapsLockIndicator::CapsLockIndicator(QObject* parent) :
QAction(parent) QAction(parent)
{ {
#ifndef ENABLE_CAPSLOCK_INDICATOR
setVisible(false);
#else
setIcon(QIcon(":img/caps_lock.svg")); setIcon(QIcon(":img/caps_lock.svg"));
setToolTip(tr("CAPS-LOCK ENABLED")); setToolTip(tr("CAPS-LOCK ENABLED"));
if (!eventHandler) if (!eventHandler)
eventHandler = new EventHandler(); eventHandler = new EventHandler();
eventHandler->actions.append(this); eventHandler->actions.append(this);
#endif
} }
CapsLockIndicator::~CapsLockIndicator() CapsLockIndicator::~CapsLockIndicator()
{ {
#ifdef ENABLE_CAPSLOCK_INDICATOR
eventHandler->actions.removeOne(this); eventHandler->actions.removeOne(this);
if (eventHandler->actions.isEmpty()) if (eventHandler->actions.isEmpty())
{ {
delete eventHandler; delete eventHandler;
eventHandler = nullptr; eventHandler = nullptr;
} }
#endif
} }
#ifdef ENABLE_CAPSLOCK_INDICATOR
CapsLockIndicator::EventHandler::EventHandler() CapsLockIndicator::EventHandler::EventHandler()
{ {
QCoreApplication::instance()->installEventFilter(this); QCoreApplication::instance()->installEventFilter(this);
@ -39,11 +51,7 @@ CapsLockIndicator::EventHandler::~EventHandler()
void CapsLockIndicator::EventHandler::updateActions(const QObject* object) void CapsLockIndicator::EventHandler::updateActions(const QObject* object)
{ {
bool caps = false; bool caps = Platform::capsLockEnabled();
// It doesn't needed for OSX, because it shows indicator by default
#if defined(QTOX_PLATFORM_EXT) && !defined(Q_OS_OSX)
caps = Platform::capsLockEnabled();
#endif
for (QAction* action : actions) for (QAction* action : actions)
{ {
@ -69,3 +77,4 @@ bool CapsLockIndicator::EventHandler::eventFilter(QObject *obj, QEvent *event)
return QObject::eventFilter(obj, event); return QObject::eventFilter(obj, event);
} }
#endif // ENABLE_CAPSLOCK_INDICATOR