on xenial help sign in
DesktopStage: Refactor focus handling

- Remove forceActiveFocus() usage - Reduce places where window focus is set, consolidating it in a single place. Removed redundancy - Turn ActiveFocusLogger into a QML item so it can be easily added and removed Approved by: Michael Zanetti

commite18d93d42110767bb0fb34f5e6fea4544c0e6348 authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateTue 15 Sep 2015, 12:05 (11 years ago) parent62fc5e80237ea8
13 files changed+77−55
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+14−15 tests/mocks/Unity/Application/MirSurfaceItem.cpp+15 tests/mocks/Unity/Application/MirSurfaceItem.h+3 tests/mocks/Utils/CMakeLists.txt+1 tests/mocks/Utils/plugin.cpp+2 tests/qmltests/Stages/tst_DesktopStage.qml+10−7 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+14−15
@@ -78,7 +78,6 @@ Rectangle {
7878 var appDelegate = appRepeater.itemAt(appIndex);
7979 appDelegate.minimized = false;
8080 appDelegate.focus = true;
81− ApplicationManager.focusApplication(appId);
8281 }
8382 }
8483
@@ -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.requestFocusApplication(application.appId)
184183 }
185184 }
186185
@@ -222,13 +221,14 @@ Rectangle {
222221
223222 onFocusChanged: {
224223 if (focus && ApplicationManager.focusedApplicationId !== model.appId) {
225− ApplicationManager.requestFocusApplication(model.appId);
226− decoratedWindow.forceActiveFocus();
224+ ApplicationManager.focusApplication(model.appId);
227225 }
228226 }
227+
229228 Component.onCompleted: {
230− if (ApplicationManager.focusedApplicationId == model.appId) {
231− decoratedWindow.forceActiveFocus();
229+ // Focus the top-most or AppMan-focused application on start up.
230+ if (ApplicationManager.focusedApplicationId === model.appId && !focus) {
231+ focus = true;
232232 }
233233 }
234234
@@ -356,7 +356,7 @@ Rectangle {
356356 resizeHandleWidth: units.gu(2)
357357 windowId: model.appId // FIXME: Change this to point to windowId once we have such a thing
358358
359− onPressed: appDelegate.focus = true;
359+ onPressed: { appDelegate.focus = true; }
360360 }
361361
362362 DecoratedWindow {
@@ -368,7 +368,7 @@ Rectangle {
368368 windowHeight: appDelegate.windowHeight
369369 application: ApplicationManager.get(index)
370370 active: ApplicationManager.focusedApplicationId === model.appId
371− focus: false
371+ focus: true
372372
373373 onClose: ApplicationManager.stopApplication(model.appId)
374374 onMaximize: appDelegate.maximize()
@@ -642,7 +642,6 @@ Rectangle {
642642 PropertyChanges { target: spreadFlickable; enabled: spreadFlickable.contentWidth > spreadFlickable.minContentWidth }
643643 PropertyChanges { target: currentSelectedLabel; visible: true }
644644 PropertyChanges { target: spreadBackground; visible: true }
645− PropertyChanges { target: appContainer; focus: true }
646645 PropertyChanges { target: hoverMouseArea; enabled: true }
647646 }
648647 ]
tests/mocks/Unity/Application/MirSurfaceItem.cpp+15
@@ -161,6 +161,21 @@ void MirSurfaceItem::touchEvent(QTouchEvent * event)
161161 }
162162}
163163
164+void MirSurfaceItem::mousePressEvent(QMouseEvent * event)
165+{
166+ event->accept();
167+}
168+
169+void MirSurfaceItem::mouseMoveEvent(QMouseEvent * event)
170+{
171+ event->accept();
172+}
173+
174+void MirSurfaceItem::mouseReleaseEvent(QMouseEvent * event)
175+{
176+ event->accept();
177+}
178+
164179void MirSurfaceItem::setSurface(MirSurfaceInterface* surface)
165180{
166181 qDebug().nospace() << "MirSurfaceItem::setSurface() this=" << (void*)(this)
tests/mocks/Unity/Application/MirSurfaceItem.h+3
@@ -79,6 +79,9 @@ Q_SIGNALS:
7979
8080protected:
8181 void touchEvent(QTouchEvent * event) override;
82+ void mousePressEvent(QMouseEvent * event) override;
83+ void mouseMoveEvent(QMouseEvent * event) override;
84+ void mouseReleaseEvent(QMouseEvent * event) override;
8285 void itemChange(ItemChange change, const ItemChangeData & value) override;
8386
8487private Q_SLOTS:
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+10−7
@@ -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
@@ -162,8 +161,8 @@ Item {
162161
163162 function test_tappingOnWindowChangesFocusedApp_data() {
164163 return [
165− {tag: "dash", apps: [ "unity8-dash", "dialer-app", "camera-app" ], focusfrom: 0, focusTo: 1 },
166− {tag: "dash", apps: [ "unity8-dash", "dialer-app", "camera-app" ], focusfrom: 1, focusTo: 0 },
164+ {tag: "dash to dialer", apps: [ "unity8-dash", "dialer-app", "camera-app" ], focusfrom: 0, focusTo: 1 },
165+ {tag: "dialer to dash", apps: [ "unity8-dash", "dialer-app", "camera-app" ], focusfrom: 1, focusTo: 0 },
167166 ]
168167 }
169168
@@ -172,16 +171,20 @@ Item {
172171 for (i = 0; i < data.apps.length; i++) {
173172 startApplication(data.apps[i]);
174173 }
174+ var fromAppId = data.apps[data.focusfrom];
175+ var toAppId = data.apps[data.focusTo]
175176
176− var fromAppWindow = findChild(desktopStage, "appWindow_" + data.apps[data.focusfrom]);
177+ var fromAppWindow = findChild(desktopStage, "appWindow_" + fromAppId);
177178 verify(fromAppWindow);
178179 tap(fromAppWindow);
179180 compare(fromAppWindow.application.session.surface.activeFocus, true);
181+ compare(ApplicationManager.focusedApplicationId, fromAppId);
180182
181− var toAppWindow = findChild(desktopStage, "appWindow_" + data.apps[data.focusTo]);
183+ var toAppWindow = findChild(desktopStage, "appWindow_" + toAppId);
182184 verify(toAppWindow);
183185 tap(toAppWindow);
184186 compare(toAppWindow.application.session.surface.activeFocus, true);
187+ compare(ApplicationManager.focusedApplicationId, toAppId);
185188 }
186189
187190 function test_tappingOnDecorationFocusesApplication_data() {
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();