on xenial help sign in
Refactor focus handling: remove forceActiveFocus() usage
commit533eadadb555d864ef5c8bbe2dc98696bbafedb7 authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateThu 3 Sep 2015, 11:02 (11 years ago) parente0112a4
11 files changed+47−58
plugins/Utils/CMakeLists.txt+1 tests/uqmlscene/ActiveFocusLogger.cpp → plugins/Utils/activefocuslogger.cpp+21−8 tests/uqmlscene/ActiveFocusLogger.h → plugins/Utils/activefocuslogger.h+8−8 plugins/Utils/plugin.cpp+2 qml/Stages/ApplicationWindow.qml−3 qml/Stages/DesktopStage.qml+10−22 tests/mocks/Utils/CMakeLists.txt+1 tests/mocks/Utils/plugin.cpp+2 tests/qmltests/Stages/tst_DesktopStage.qml+2−3 tests/uqmlscene/CMakeLists.txt−1 tests/uqmlscene/main.cpp−13
plugins/Utils/CMakeLists.txt+1
@@ -9,6 +9,7 @@ include_directories(
99)
1010
1111set(QMLPLUGIN_SRC
12+ activefocuslogger.cpp
1213 constants.cpp
1314 HomeKeyWatcher.cpp
1415 inputwatcher.cpp
tests/uqmlscene/ActiveFocusLogger.cpp → plugins/Utils/activefocuslogger.cpprenamed+21−8
@@ -14,21 +14,34 @@
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1515 */
1616
17−#include "ActiveFocusLogger.h"
17+#include "activefocuslogger.h"
1818
19−#include <QDebug>
20−#include <QQuickItem>
19+#include <QQuickWindow>
2120
22−void ActiveFocusLogger::setWindow(QQuickWindow *window)
21+ActiveFocusLogger::ActiveFocusLogger(QQuickItem *parent)
22+ : QQuickItem(parent)
2323{
24− m_window = window;
25− QObject::connect(window, &QQuickWindow::activeFocusItemChanged,
26− this, &ActiveFocusLogger::printActiveFocusInfo);
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) {
36+ m_window = window;
37+ QObject::connect(window, &QQuickWindow::activeFocusItemChanged,
38+ this, &ActiveFocusLogger::printActiveFocusInfo);
39+ }
2740}
2841
2942void ActiveFocusLogger::printActiveFocusInfo()
3043{
31− if (!m_window) {
44+ if (!m_window || !isEnabled()) {
3245 return;
3346 }
3447
tests/uqmlscene/ActiveFocusLogger.h → plugins/Utils/activefocuslogger.hrenamed+8−8
@@ -14,24 +14,24 @@
1414 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1515 */
1616
17−#ifndef ACTIVE_FOCUS_LOGGER_H
18−#define ACTIVE_FOCUS_LOGGER_H
17+#ifndef UNITY_ACTIVEFOCUSLOGGER_H
18+#define UNITY_ACTIVEFOCUSLOGGER_H
1919
20−#include <QObject>
21−#include <QQuickWindow>
20+#include <QQuickItem>
2221#include <QPointer>
2322
24−class ActiveFocusLogger : public QObject {
23+class ActiveFocusLogger : public QQuickItem
24+{
2525 Q_OBJECT
26−
2726public:
28− void setWindow(QQuickWindow *window);
27+ ActiveFocusLogger(QQuickItem *parent = 0);
2928
3029private Q_SLOTS:
30+ void setupFilterOnWindow(QQuickWindow *window);
3131 void printActiveFocusInfo();
3232
3333private:
3434 QPointer<QQuickWindow> m_window;
3535};
3636
37−#endif // ACTIVE_FOCUS_LOGGER_H
37+#endif // UNITY_ACTIVEFOCUSLOGGER_H
plugins/Utils/plugin.cpp+2
@@ -23,6 +23,7 @@
2323#include "plugin.h"
2424
2525// local
26+#include "activefocuslogger.h"
2627#include "easingcurve.h"
2728#include "HomeKeyWatcher.h"
2829#include "inputwatcher.h"
@@ -66,6 +67,7 @@ void UtilsPlugin::registerTypes(const char *uri)
6667 qmlRegisterSingletonType<WindowStateStorage>(uri, 0, 1, "WindowStateStorage", createWindowStateStorage);
6768 qmlRegisterType<InputWatcher>(uri, 0, 1, "InputWatcher");
6869 qmlRegisterSingletonType<Constants>(uri, 0, 1, "Constants", createConstants);
70+ qmlRegisterType<ActiveFocusLogger>(uri, 0, 1, "ActiveFocusLogger");
6971}
7072
7173void UtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
qml/Stages/ApplicationWindow.qml−3
@@ -137,9 +137,6 @@ FocusScope {
137137 onSurfaceChanged: {
138138 if (sessionContainer.surface) {
139139 surfaceInitTimer.start();
140− if (ApplicationManager.focusedApplicationId == application.appId) {
141− root.forceActiveFocus();
142− }
143140 } else {
144141 d.surfaceInitialized = false;
145142 }
qml/Stages/DesktopStage.qml+10−22
@@ -77,7 +77,6 @@ Rectangle {
7777 var appIndex = priv.indexOf(appId);
7878 var appDelegate = appRepeater.itemAt(appIndex);
7979 appDelegate.minimized = false;
80− appDelegate.focus = true;
8180 ApplicationManager.focusApplication(appId);
8281 }
8382 }
@@ -91,12 +90,6 @@ Rectangle {
9190 return index >= 0 && index < appRepeater.count ? appRepeater.itemAt(index) : null
9291 }
9392
94− onFocusedAppDelegateChanged: {
95− if (focusedAppDelegate) {
96− focusedAppDelegate.focus = true;
97− }
98− }
99−
10093 function indexOf(appId) {
10194 for (var i = 0; i < ApplicationManager.count; i++) {
10295 if (ApplicationManager.get(i).appId == appId) {
@@ -133,23 +126,28 @@ Rectangle {
133126 id: appContainer
134127 objectName: "appContainer"
135128 anchors.fill: parent
129+ focus: true
136130
137131 Keys.onPressed: {
138132 switch (event.key) {
139133 case Qt.Key_Left:
140134 case Qt.Key_Backtab:
141135 selectPrevious(event.isAutoRepeat)
136+ event.accepted = true;
142137 break;
143138 case Qt.Key_Right:
144139 case Qt.Key_Tab:
145140 selectNext(event.isAutoRepeat)
141+ event.accepted = true;
146142 break;
147143 case Qt.Key_Escape:
148144 appRepeater.highlightedIndex = -1
145+ event.accepted = true;
149146 case Qt.Key_Enter:
150147 case Qt.Key_Return:
151148 case Qt.Key_Space:
152149 root.state = ""
150+ event.accepted = true;
153151 }
154152 }
155153
@@ -180,7 +178,8 @@ Rectangle {
180178
181179 function focusSelected() {
182180 if (appRepeater.highlightedIndex != -1) {
183− appRepeater.itemAt(appRepeater.highlightedIndex).focus = true;
181+ var application = ApplicationManager.get(appRepeater.highlightedIndex);
182+ ApplicationManager.focusApplication(application.appId);
184183 }
185184 }
186185
@@ -205,17 +204,7 @@ Rectangle {
205204 property bool maximized: false
206205 property bool minimized: false
207206
208− onFocusChanged: {
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− }
207+ focus: priv.focusedAppId === model.appId && root.state !== "altTab"
219208
220209 Binding {
221210 target: ApplicationManager.get(index)
@@ -326,7 +315,7 @@ Rectangle {
326315 resizeHandleWidth: units.gu(2)
327316 windowId: model.appId // FIXME: Change this to point to windowId once we have such a thing
328317
329− onPressed: appDelegate.focus = true;
318+ onPressed: ApplicationManager.focusApplication(model.appId);
330319 }
331320
332321 DecoratedWindow {
@@ -338,7 +327,7 @@ Rectangle {
338327 windowHeight: appDelegate.height
339328 application: ApplicationManager.get(index)
340329 active: ApplicationManager.focusedApplicationId === model.appId
341− focus: false
330+ focus: true
342331
343332 onClose: ApplicationManager.stopApplication(model.appId)
344333 onMaximize: appDelegate.maximize()
@@ -582,7 +571,6 @@ Rectangle {
582571 PropertyChanges { target: spreadFlickable; enabled: true }
583572 PropertyChanges { target: currentSelectedLabel; visible: true }
584573 PropertyChanges { target: spreadBackground; visible: true }
585− PropertyChanges { target: appContainer; focus: true }
586574 }
587575 ]
588576 signal updateWorkspaces();
tests/mocks/Utils/CMakeLists.txt+1
@@ -10,6 +10,7 @@ include_directories(
1010)
1111
1212set(QMLPLUGIN_SRC
13+ ${CMAKE_SOURCE_DIR}/plugins/Utils/activefocuslogger.cpp
1314 ${CMAKE_SOURCE_DIR}/plugins/Utils/qlimitproxymodelqml.cpp
1415 ${CMAKE_SOURCE_DIR}/plugins/Utils/unitysortfilterproxymodelqml.cpp
1516 ${CMAKE_SOURCE_DIR}/plugins/Utils/relativetimeformatter.cpp
tests/mocks/Utils/plugin.cpp+2
@@ -35,6 +35,7 @@
3535#include "constants.h"
3636
3737// plugin
38+#include <activefocuslogger.h>
3839#include <inputwatcher.h>
3940#include <qlimitproxymodelqml.h>
4041#include <unitysortfilterproxymodelqml.h>
@@ -74,6 +75,7 @@ void FakeUtilsPlugin::registerTypes(const char *uri)
7475 qmlRegisterSingletonType<WindowStateStorage>(uri, 0, 1, "WindowStateStorage", createWindowStateStorage);
7576 qmlRegisterType<InputWatcher>(uri, 0, 1, "InputWatcher");
7677 qmlRegisterSingletonType<Constants>(uri, 0, 1, "Constants", createConstants);
78+ qmlRegisterType<ActiveFocusLogger>(uri, 0, 1, "ActiveFocusLogger");
7779}
7880
7981void FakeUtilsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
tests/qmltests/Stages/tst_DesktopStage.qml+2−3
@@ -61,7 +61,6 @@ Item {
6161 Component.onDestruction: {
6262 desktopStageLoader.itemDestroyed = true;
6363 }
64− focus: true
6564 }
6665 }
6766 }
@@ -142,8 +141,8 @@ Item {
142141
143142 function test_appFocusSwitch_data() {
144143 return [
145− {tag: "dash", 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 },
144+ {tag: "dash to dialer", apps: [ "unity8-dash", "dialer-app", "camera-app" ], focusfrom: 0, focusTo: 1 },
145+ {tag: "dialer to dash", apps: [ "unity8-dash", "dialer-app", "camera-app" ], focusfrom: 1, focusTo: 0 },
147146 ]
148147 }
149148
tests/uqmlscene/CMakeLists.txt−1
@@ -1,6 +1,5 @@
11add_executable(uqmlscene
22 ${shellapplication_MOC_SRCS}
3− ActiveFocusLogger.cpp
43 main.cpp
54 )
65
tests/uqmlscene/main.cpp−13
@@ -63,12 +63,6 @@
6363#include <QtCore/QTranslator>
6464#include <QtCore/QLibraryInfo>
6565
66−#define UQMLSCENE_DEBUG_ACTIVE_FOCUS 0
67−
68−#if UQMLSCENE_DEBUG_ACTIVE_FOCUS
69− #include "ActiveFocusLogger.h"
70−#endif
71−
7266#ifdef QML_RUNTIME_TESTING
7367class RenderStatistics
7468{
@@ -393,10 +387,6 @@ int main(int argc, char ** argv)
393387{
394388 Options options;
395389
396− #if UQMLSCENE_DEBUG_ACTIVE_FOCUS
397− ActiveFocusLogger activeFocusLogger;
398− #endif
399−
400390 QStringList imports;
401391 QList<QPair<QString, QString> > bundles;
402392 for (int i = 1; i < argc; ++i) {
@@ -515,9 +505,6 @@ int main(int argc, char ** argv)
515505 QQuickItem *contentItem = qobject_cast<QQuickItem *>(topLevel);
516506 if (contentItem) {
517507 qxView = new QQuickView(&engine, nullptr);
518− #if UQMLSCENE_DEBUG_ACTIVE_FOCUS
519− activeFocusLogger.setWindow(qxView);
520− #endif
521508 window = qxView;
522509 // Set window default properties; the qml can still override them
523510 QString oname = contentItem->objectName();