on xenial help sign in
focus window gets global shortcuts
commit15874ddc594110e3bd51adb75f0232c49a939850 authorNick Dedekind <nick.dedekind@canonical.com> dateWed 22 Mar 2017, 11:56 (10 years ago) parent64a046c
4 files changed+9−37
plugins/GlobalShortcut/globalshortcut.cpp−15 plugins/GlobalShortcut/globalshortcut.h−4 plugins/GlobalShortcut/globalshortcutregistry.cpp+7−16 plugins/GlobalShortcut/globalshortcutregistry.h+2−2
plugins/GlobalShortcut/globalshortcut.cpp−15
@@ -56,11 +56,6 @@ void GlobalShortcut::setActive(bool active)
5656 Q_EMIT activeChanged(active);
5757}
5858
59−void GlobalShortcut::componentComplete()
60−{
61− connect(this, &QQuickItem::windowChanged, this, &GlobalShortcut::setupFilterOnWindow);
62−}
63−
6459void GlobalShortcut::keyPressEvent(QKeyEvent * event)
6560{
6661 if (!m_active) return;
@@ -76,13 +71,3 @@ void GlobalShortcut::keyReleaseEvent(QKeyEvent * event)
7671 event->accept();
7772 Q_EMIT released(m_shortcut.toString());
7873}
79−
80−void GlobalShortcut::setupFilterOnWindow(QQuickWindow *window)
81−{
82− if (!window) {
83−// qWarning() << Q_FUNC_INFO << "Failed to setup filter on window";
84− return;
85− }
86−
87− registry->setupFilterOnWindow((qulonglong) window->winId());
88−}
plugins/GlobalShortcut/globalshortcut.h−4
@@ -53,7 +53,6 @@ public:
5353 void setActive(bool active);
5454
5555protected:
56− void componentComplete() override;
5756 void keyPressEvent(QKeyEvent * event) override;
5857 void keyReleaseEvent(QKeyEvent * event) override;
5958
@@ -69,9 +68,6 @@ Q_SIGNALS:
6968 void released(const QString &shortcut);
7069 void activeChanged(bool active);
7170
72−private Q_SLOTS:
73− void setupFilterOnWindow(QQuickWindow* window);
74−
7571private:
7672 QVariant m_shortcut;
7773 bool m_active = true;
plugins/GlobalShortcut/globalshortcutregistry.cpp+7−16
@@ -21,11 +21,11 @@
2121
2222#include "globalshortcutregistry.h"
2323
24−static qulonglong s_windowId = 0;
25−
2624GlobalShortcutRegistry::GlobalShortcutRegistry(QObject *parent)
2725 : QObject(parent)
2826{
27+ connect(qGuiApp, &QGuiApplication::focusWindowChanged, this, &GlobalShortcutRegistry::setupFilterOnWindow);
28+ setupFilterOnWindow(qGuiApp->focusWindow());
2929}
3030
3131GlobalShortcutList GlobalShortcutRegistry::shortcuts() const
@@ -90,7 +90,7 @@ bool GlobalShortcutRegistry::eventFilter(QObject *obj, QEvent *event)
9090 if (m_shortcuts.contains(seq)) {
9191 const auto shortcuts = m_shortcuts.value(seq);
9292 Q_FOREACH(const auto &shortcut, shortcuts) {
93− if (shortcut) {
93+ if (shortcut && shortcut->window() == obj) {
9494 qApp->sendEvent(shortcut, &eCopy);
9595 }
9696 }
@@ -102,24 +102,15 @@ bool GlobalShortcutRegistry::eventFilter(QObject *obj, QEvent *event)
102102 return QObject::eventFilter(obj, event);
103103}
104104
105−void GlobalShortcutRegistry::setupFilterOnWindow(qulonglong wid)
105+void GlobalShortcutRegistry::setupFilterOnWindow(QWindow* window)
106106{
107− if (wid == s_windowId) {
108− return;
109− }
110−
111107 if (m_filteredWindow) {
112108 m_filteredWindow->removeEventFilter(this);
113109 m_filteredWindow.clear();
114− s_windowId = 0;
115110 }
116111
117− Q_FOREACH(QWindow *window, qApp->allWindows()) {
118− if (window && window->winId() == wid) {
119− m_filteredWindow = window;
120− window->installEventFilter(this);
121− s_windowId = wid;
122− break;
123− }
112+ if (window) {
113+ m_filteredWindow = window;
114+ window->installEventFilter(this);
124115 }
125116}
plugins/GlobalShortcut/globalshortcutregistry.h+2−2
@@ -52,9 +52,9 @@ public:
5252 void addShortcut(const QVariant &seq, GlobalShortcut * sc);
5353
5454 /**
55− * Sets up key events filtering on window @p wid
55+ * Sets up key events filtering on window @p window
5656 */
57− void setupFilterOnWindow(qulonglong wid);
57+ void setupFilterOnWindow(QWindow* window);
5858
5959protected:
6060 bool eventFilter(QObject *obj, QEvent *event) override;