on xenial help sign in
Refactor focus handling: remove forceActiveFocus() usage
533eadaDaniel d'Andrada
plugins/Utils/CMakeLists.txt+1
@@ -9,6 +9,7 @@ include_directories(
9)
9)
10
10
11set(QMLPLUGIN_SRC
11set(QMLPLUGIN_SRC
12+ activefocuslogger.cpp
12 constants.cpp
13 constants.cpp
13 HomeKeyWatcher.cpp
14 HomeKeyWatcher.cpp
14 inputwatcher.cpp
15 inputwatcher.cpp
tests/uqmlscene/ActiveFocusLogger.cpp → plugins/Utils/activefocuslogger.cpprenamed+21−8
@@ -14,21 +14,34 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
15 */
16
16
17−#include "ActiveFocusLogger.h"
17+#include "activefocuslogger.h"
18
18
19−#include <QDebug>
19+#include <QQuickWindow>
20−#include <QQuickItem>
21
20
22−void ActiveFocusLogger::setWindow(QQuickWindow *window)
21+ActiveFocusLogger::ActiveFocusLogger(QQuickItem *parent)
22+ : QQuickItem(parent)
23{
23{
24+ connect(this, &QQuickItem::windowChanged,
25+ this, &ActiveFocusLogger::setupFilterOnWindow);
26+}
27+
28+void ActiveFocusLogger::setupFilterOnWindow(QQuickWindow *window)
29+{
30+ if (!m_window.isNull()) {
31+ disconnect(m_window.data(), nullptr, this, nullptr);
32+ m_window.clear();
33+ }
34+
35+ if (window) {
24− m_window = window;
36+ m_window = window;
25− QObject::connect(window, &QQuickWindow::activeFocusItemChanged,
37+ QObject::connect(window, &QQuickWindow::activeFocusItemChanged,
26− this, &ActiveFocusLogger::printActiveFocusInfo);
38+ this, &ActiveFocusLogger::printActiveFocusInfo);
39+ }
27}
40}
28
41
29void ActiveFocusLogger::printActiveFocusInfo()
42void ActiveFocusLogger::printActiveFocusInfo()
30{
43{
31− if (!m_window) {
44+ if (!m_window || !isEnabled()) {
32 return;
45 return;
33 }
46 }
34
47
tests/uqmlscene/ActiveFocusLogger.h → plugins/Utils/activefocuslogger.hrenamed+8−8
@@ -14,24 +14,24 @@
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
15 */
16
16
17−#ifndef ACTIVE_FOCUS_LOGGER_H
17+#ifndef UNITY_ACTIVEFOCUSLOGGER_H
18−#define ACTIVE_FOCUS_LOGGER_H
18+#define UNITY_ACTIVEFOCUSLOGGER_H
19
19
20−#include <QObject>
20+#include <QQuickItem>
21−#include <QQuickWindow>
22#include <QPointer>
21#include <QPointer>
23
22
24−class ActiveFocusLogger : public QObject {
23+class ActiveFocusLogger : public QQuickItem
24+{
25 Q_OBJECT
25 Q_OBJECT
26−
27public:
26public:
28− void setWindow(QQuickWindow *window);
27+ ActiveFocusLogger(QQuickItem *parent = 0);
29
28
30private Q_SLOTS:
29private Q_SLOTS:
30+ void setupFilterOnWindow(QQuickWindow *window);
31 void printActiveFocusInfo();
31 void printActiveFocusInfo();
32
32
33private:
33private:
34 QPointer<QQuickWindow> m_window;
34 QPointer<QQuickWindow> m_window;
35};
35};
36
36
37−#endif // ACTIVE_FOCUS_LOGGER_H
37+#endif // UNITY_ACTIVEFOCUSLOGGER_H
plugins/Utils/plugin.cpp+2
@@ -23,6 +23,7 @@
23#include "plugin.h"
23#include "plugin.h"
24
24
25// local
25// local
26+#include "activefocuslogger.h"
26#include "easingcurve.h"
27#include "easingcurve.h"
27#include "HomeKeyWatcher.h"
28#include "HomeKeyWatcher.h"
28#include "inputwatcher.h"
29#include "inputwatcher.h"
@@ -66,6 +67,7 @@ void UtilsPlugin::registerTypes(const char *uri)
66 qmlRegisterSingletonType<WindowStateStorage>(uri, 0, 1, "WindowStateStorage", createWindowStateStorage);
67 qmlRegisterSingletonType<WindowStateStorage>(uri, 0, 1, "WindowStateStorage", createWindowStateStorage);
67 qmlRegisterType<InputWatcher>(uri, 0, 1, "InputWatcher");
68 qmlRegisterType<InputWatcher>(uri, 0, 1, "InputWatcher");
68 qmlRegisterSingletonType<Constants>(uri, 0, 1, "Constants", createConstants);
69 qmlRegisterSingletonType<Constants>(uri, 0, 1, "Constants", createConstants);
70+ qmlRegisterType<ActiveFocusLogger>(uri, 0, 1, "ActiveFocusLogger");
69}
71}
70
72
71void UtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
73void UtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
qml/Stages/ApplicationWindow.qml−3
@@ -137,9 +137,6 @@ FocusScope {
137 onSurfaceChanged: {
137 onSurfaceChanged: {
138 if (sessionContainer.surface) {
138 if (sessionContainer.surface) {
139 surfaceInitTimer.start();
139 surfaceInitTimer.start();
140− if (ApplicationManager.focusedApplicationId == application.appId) {
141− root.forceActiveFocus();
142− }
143 } else {
140 } else {
144 d.surfaceInitialized = false;
141 d.surfaceInitialized = false;
145 }
142 }
qml/Stages/DesktopStage.qml+10−22
@@ -77,7 +77,6 @@ Rectangle {
77 var appIndex = priv.indexOf(appId);
77 var appIndex = priv.indexOf(appId);
78 var appDelegate = appRepeater.itemAt(appIndex);
78 var appDelegate = appRepeater.itemAt(appIndex);
79 appDelegate.minimized = false;
79 appDelegate.minimized = false;
80− appDelegate.focus = true;
81 ApplicationManager.focusApplication(appId);
80 ApplicationManager.focusApplication(appId);
82 }
81 }
83 }
82 }
@@ -91,12 +90,6 @@ Rectangle {
91 return index >= 0 && index < appRepeater.count ? appRepeater.itemAt(index) : null
90 return index >= 0 && index < appRepeater.count ? appRepeater.itemAt(index) : null
92 }
91 }
93
92
94− onFocusedAppDelegateChanged: {
95− if (focusedAppDelegate) {
96− focusedAppDelegate.focus = true;
97− }
98− }
99−
100 function indexOf(appId) {
93 function indexOf(appId) {
101 for (var i = 0; i < ApplicationManager.count; i++) {
94 for (var i = 0; i < ApplicationManager.count; i++) {
102 if (ApplicationManager.get(i).appId == appId) {
95 if (ApplicationManager.get(i).appId == appId) {
@@ -133,23 +126,28 @@ Rectangle {
133 id: appContainer
126 id: appContainer
134 objectName: "appContainer"
127 objectName: "appContainer"
135 anchors.fill: parent
128 anchors.fill: parent
129+ focus: true
136
130
137 Keys.onPressed: {
131 Keys.onPressed: {
138 switch (event.key) {
132 switch (event.key) {
139 case Qt.Key_Left:
133 case Qt.Key_Left:
140 case Qt.Key_Backtab:
134 case Qt.Key_Backtab:
141 selectPrevious(event.isAutoRepeat)
135 selectPrevious(event.isAutoRepeat)
136+ event.accepted = true;
142 break;
137 break;
143 case Qt.Key_Right:
138 case Qt.Key_Right:
144 case Qt.Key_Tab:
139 case Qt.Key_Tab:
145 selectNext(event.isAutoRepeat)
140 selectNext(event.isAutoRepeat)
141+ event.accepted = true;
146 break;
142 break;
147 case Qt.Key_Escape:
143 case Qt.Key_Escape:
148 appRepeater.highlightedIndex = -1
144 appRepeater.highlightedIndex = -1
145+ event.accepted = true;
149 case Qt.Key_Enter:
146 case Qt.Key_Enter:
150 case Qt.Key_Return:
147 case Qt.Key_Return:
151 case Qt.Key_Space:
148 case Qt.Key_Space:
152 root.state = ""
149 root.state = ""
150+ event.accepted = true;
153 }
151 }
154 }
152 }
155
153
@@ -180,7 +178,8 @@ Rectangle {
180
178
181 function focusSelected() {
179 function focusSelected() {
182 if (appRepeater.highlightedIndex != -1) {
180 if (appRepeater.highlightedIndex != -1) {
183− appRepeater.itemAt(appRepeater.highlightedIndex).focus = true;
181+ var application = ApplicationManager.get(appRepeater.highlightedIndex);
182+ ApplicationManager.focusApplication(application.appId);
184 }
183 }
185 }
184 }
186
185
@@ -205,17 +204,7 @@ Rectangle {
205 property bool maximized: false
204 property bool maximized: false
206 property bool minimized: false
205 property bool minimized: false
207
206
208− onFocusChanged: {
207+ focus: priv.focusedAppId === model.appId && root.state !== "altTab"
209− if (focus && ApplicationManager.focusedApplicationId !== model.appId) {
210− ApplicationManager.requestFocusApplication(model.appId);
211− decoratedWindow.forceActiveFocus();
212− }
213− }
214− Component.onCompleted: {
215− if (ApplicationManager.focusedApplicationId == model.appId) {
216− decoratedWindow.forceActiveFocus();
217− }
218− }
219
208
220 Binding {
209 Binding {
221 target: ApplicationManager.get(index)
210 target: ApplicationManager.get(index)
@@ -326,7 +315,7 @@ Rectangle {
326 resizeHandleWidth: units.gu(2)
315 resizeHandleWidth: units.gu(2)
327 windowId: model.appId // FIXME: Change this to point to windowId once we have such a thing
316 windowId: model.appId // FIXME: Change this to point to windowId once we have such a thing
328
317
329− onPressed: appDelegate.focus = true;
318+ onPressed: ApplicationManager.focusApplication(model.appId);
330 }
319 }
331
320
332 DecoratedWindow {
321 DecoratedWindow {
@@ -338,7 +327,7 @@ Rectangle {
338 windowHeight: appDelegate.height
327 windowHeight: appDelegate.height
339 application: ApplicationManager.get(index)
328 application: ApplicationManager.get(index)
340 active: ApplicationManager.focusedApplicationId === model.appId
329 active: ApplicationManager.focusedApplicationId === model.appId
341− focus: false
330+ focus: true
342
331
343 onClose: ApplicationManager.stopApplication(model.appId)
332 onClose: ApplicationManager.stopApplication(model.appId)
344 onMaximize: appDelegate.maximize()
333 onMaximize: appDelegate.maximize()
@@ -582,7 +571,6 @@ Rectangle {
582 PropertyChanges { target: spreadFlickable; enabled: true }
571 PropertyChanges { target: spreadFlickable; enabled: true }
583 PropertyChanges { target: currentSelectedLabel; visible: true }
572 PropertyChanges { target: currentSelectedLabel; visible: true }
584 PropertyChanges { target: spreadBackground; visible: true }
573 PropertyChanges { target: spreadBackground; visible: true }
585− PropertyChanges { target: appContainer; focus: true }
586 }
574 }
587 ]
575 ]
588 signal updateWorkspaces();
576 signal updateWorkspaces();
tests/mocks/Utils/CMakeLists.txt+1
@@ -10,6 +10,7 @@ include_directories(
10)
10)
11
11
12set(QMLPLUGIN_SRC
12set(QMLPLUGIN_SRC
13+ ${CMAKE_SOURCE_DIR}/plugins/Utils/activefocuslogger.cpp
13 ${CMAKE_SOURCE_DIR}/plugins/Utils/qlimitproxymodelqml.cpp
14 ${CMAKE_SOURCE_DIR}/plugins/Utils/qlimitproxymodelqml.cpp
14 ${CMAKE_SOURCE_DIR}/plugins/Utils/unitysortfilterproxymodelqml.cpp
15 ${CMAKE_SOURCE_DIR}/plugins/Utils/unitysortfilterproxymodelqml.cpp
15 ${CMAKE_SOURCE_DIR}/plugins/Utils/relativetimeformatter.cpp
16 ${CMAKE_SOURCE_DIR}/plugins/Utils/relativetimeformatter.cpp
tests/mocks/Utils/plugin.cpp+2
@@ -35,6 +35,7 @@
35#include "constants.h"
35#include "constants.h"
36
36
37// plugin
37// plugin
38+#include <activefocuslogger.h>
38#include <inputwatcher.h>
39#include <inputwatcher.h>
39#include <qlimitproxymodelqml.h>
40#include <qlimitproxymodelqml.h>
40#include <unitysortfilterproxymodelqml.h>
41#include <unitysortfilterproxymodelqml.h>
@@ -74,6 +75,7 @@ void FakeUtilsPlugin::registerTypes(const char *uri)
74 qmlRegisterSingletonType<WindowStateStorage>(uri, 0, 1, "WindowStateStorage", createWindowStateStorage);
75 qmlRegisterSingletonType<WindowStateStorage>(uri, 0, 1, "WindowStateStorage", createWindowStateStorage);
75 qmlRegisterType<InputWatcher>(uri, 0, 1, "InputWatcher");
76 qmlRegisterType<InputWatcher>(uri, 0, 1, "InputWatcher");
76 qmlRegisterSingletonType<Constants>(uri, 0, 1, "Constants", createConstants);
77 qmlRegisterSingletonType<Constants>(uri, 0, 1, "Constants", createConstants);
78+ qmlRegisterType<ActiveFocusLogger>(uri, 0, 1, "ActiveFocusLogger");
77}
79}
78
80
79void FakeUtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
81void FakeUtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
tests/qmltests/Stages/tst_DesktopStage.qml+2−3
@@ -61,7 +61,6 @@ Item {
61 Component.onDestruction: {
61 Component.onDestruction: {
62 desktopStageLoader.itemDestroyed = true;
62 desktopStageLoader.itemDestroyed = true;
63 }
63 }
64− focus: true
65 }
64 }
66 }
65 }
67 }
66 }
@@ -142,8 +141,8 @@ Item {
142
141
143 function test_appFocusSwitch_data() {
142 function test_appFocusSwitch_data() {
144 return [
143 return [
145− {tag: "dash", apps: [ "unity8-dash", "dialer-app", "camera-app" ], focusfrom: 0, focusTo: 1 },
144+ {tag: "dash to dialer", apps: [ "unity8-dash", "dialer-app", "camera-app" ], focusfrom: 0, focusTo: 1 },
146− {tag: "dash", apps: [ "unity8-dash", "dialer-app", "camera-app" ], focusfrom: 1, focusTo: 0 },
145+ {tag: "dialer to dash", apps: [ "unity8-dash", "dialer-app", "camera-app" ], focusfrom: 1, focusTo: 0 },
147 ]
146 ]
148 }
147 }
149
148
tests/uqmlscene/CMakeLists.txt−1
@@ -1,6 +1,5 @@
1add_executable(uqmlscene
1add_executable(uqmlscene
2 ${shellapplication_MOC_SRCS}
2 ${shellapplication_MOC_SRCS}
3− ActiveFocusLogger.cpp
4 main.cpp
3 main.cpp
5 )
4 )
6
5
tests/uqmlscene/main.cpp−13
@@ -63,12 +63,6 @@
63#include <QtCore/QTranslator>
63#include <QtCore/QTranslator>
64#include <QtCore/QLibraryInfo>
64#include <QtCore/QLibraryInfo>
65
65
66−#define UQMLSCENE_DEBUG_ACTIVE_FOCUS 0
67−
68−#if UQMLSCENE_DEBUG_ACTIVE_FOCUS
69− #include "ActiveFocusLogger.h"
70−#endif
71−
72#ifdef QML_RUNTIME_TESTING
66#ifdef QML_RUNTIME_TESTING
73class RenderStatistics
67class RenderStatistics
74{
68{
@@ -393,10 +387,6 @@ int main(int argc, char ** argv)
393{
387{
394 Options options;
388 Options options;
395
389
396− #if UQMLSCENE_DEBUG_ACTIVE_FOCUS
397− ActiveFocusLogger activeFocusLogger;
398− #endif
399−
400 QStringList imports;
390 QStringList imports;
401 QList<QPair<QString, QString> > bundles;
391 QList<QPair<QString, QString> > bundles;
402 for (int i = 1; i < argc; ++i) {
392 for (int i = 1; i < argc; ++i) {
@@ -515,9 +505,6 @@ int main(int argc, char ** argv)
515 QQuickItem *contentItem = qobject_cast<QQuickItem *>(topLevel);
505 QQuickItem *contentItem = qobject_cast<QQuickItem *>(topLevel);
516 if (contentItem) {
506 if (contentItem) {
517 qxView = new QQuickView(&engine, nullptr);
507 qxView = new QQuickView(&engine, nullptr);
518− #if UQMLSCENE_DEBUG_ACTIVE_FOCUS
519− activeFocusLogger.setWindow(qxView);
520− #endif
521 window = qxView;
508 window = qxView;
522 // Set window default properties; the qml can still override them
509 // Set window default properties; the qml can still override them
523 QString oname = contentItem->objectName();
510 QString oname = contentItem->objectName();