17 #include "HomeKeyWatcher.h"
19 #include <QQuickWindow>
23 HomeKeyWatcher::HomeKeyWatcher(QQuickItem *parent)
32 , m_windowBeingTouched(false)
33 , m_homeKeyPressed(false)
34 , m_windowLastTouchedTimer(elapsedTimer)
35 , m_activationTimer(timer)
37 m_windowLastTouchedTimer->start();
39 connect(
this, &QQuickItem::windowChanged,
40 this, &HomeKeyWatcher::setupFilterOnWindow);
42 connect(m_activationTimer, &UnityUtil::AbstractTimer::timeout,
43 this, &HomeKeyWatcher::emitActivatedIfNoTouchesAround);
44 m_activationTimer->setInterval(msecsWithoutTouches);
47 HomeKeyWatcher::~HomeKeyWatcher()
49 delete m_windowLastTouchedTimer;
50 delete m_activationTimer;
53 bool HomeKeyWatcher::eventFilter(QObject *watched, QEvent *event)
55 Q_ASSERT(!m_filteredWindow.isNull());
56 Q_ASSERT(watched == static_cast<QObject*>(m_filteredWindow.data()));
65 void HomeKeyWatcher::update(QEvent *event)
67 if (event->type() == QEvent::KeyPress) {
68 QKeyEvent *keyEvent =
static_cast<QKeyEvent*
>(event);
70 m_homeKeyPressed =
true;
72 if (keyEvent->key() == Qt::Key_Super_L && !keyEvent->isAutoRepeat()
73 && !m_windowBeingTouched
74 && m_windowLastTouchedTimer->elapsed() >= msecsWithoutTouches) {
75 m_activationTimer->start();
78 }
else if (event->type() == QEvent::KeyRelease) {
79 QKeyEvent *keyEvent =
static_cast<QKeyEvent*
>(event);
81 if (keyEvent->key() == Qt::Key_Super_L) {
82 m_homeKeyPressed =
false;
85 }
else if (event->type() == QEvent::TouchBegin) {
87 m_activationTimer->stop();
88 m_windowBeingTouched =
true;
90 }
else if (event->type() == QEvent::TouchEnd) {
92 m_windowBeingTouched =
false;
93 m_windowLastTouchedTimer->start();
97 void HomeKeyWatcher::setupFilterOnWindow(QQuickWindow *window)
99 if (!m_filteredWindow.isNull()) {
100 m_filteredWindow->removeEventFilter(
this);
101 m_filteredWindow.clear();
105 window->installEventFilter(
this);
106 m_filteredWindow = window;
110 void HomeKeyWatcher::emitActivatedIfNoTouchesAround()
112 if (!m_homeKeyPressed && !m_windowBeingTouched &&
113 (m_windowLastTouchedTimer->elapsed() > msecsWithoutTouches)) {