Skip to content

Commit 6d5b36b

Browse files
committed
Activate window for the primary screen
Request activation for the view on the primary screen otherwise text fields won't get focus. Closes #501 [ChangeLog][Greeter] Fix text field focus (issue #501)
1 parent ef7b317 commit 6d5b36b

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/greeter/GreeterApp.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***************************************************************************
2-
* Copyright (c) 2015 Pier Luigi Fiorini <[email protected]>
2+
* Copyright (c) 2015-2016 Pier Luigi Fiorini <[email protected]>
33
* Copyright (c) 2013 Abdurrahman AVCI <[email protected]>
44
*
55
* This program is free software; you can redistribute it and/or modify
@@ -36,6 +36,7 @@
3636
#include <QQmlContext>
3737
#include <QQmlEngine>
3838
#include <QDebug>
39+
#include <QTimer>
3940
#include <QTranslator>
4041

4142
#include <iostream>
@@ -135,6 +136,11 @@ namespace SDDM {
135136

136137
// handle screens
137138
connect(this, &GreeterApp::screenAdded, this, &GreeterApp::addViewForScreen);
139+
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
140+
connect(this, &GreeterApp::primaryScreenChanged, this, [this](QScreen *) {
141+
activatePrimary();
142+
});
143+
#endif
138144
}
139145

140146
void GreeterApp::addViewForScreen(QScreen *screen) {
@@ -144,6 +150,7 @@ namespace SDDM {
144150
view->setResizeMode(QQuickView::SizeRootObjectToView);
145151
//view->setGeometry(QRect(QPoint(0, 0), screen->geometry().size()));
146152
view->setGeometry(screen->geometry());
153+
m_views.append(view);
147154

148155
// remove the view when the screen is removed, but we
149156
// need to be careful here since Qt will move the view to
@@ -201,11 +208,33 @@ namespace SDDM {
201208
// show
202209
qDebug() << "Adding view for" << screen->name() << screen->geometry();
203210
view->show();
211+
212+
// activate windows for the primary screen to give focus to text fields
213+
if (QGuiApplication::primaryScreen() == screen)
214+
view->requestActivate();
204215
}
205216

206217
void GreeterApp::removeViewForScreen(QQuickView *view) {
218+
// screen is gone, remove the window
207219
m_views.removeOne(view);
208220
view->deleteLater();
221+
222+
#if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
223+
// starting from Qt 5.6 we are notified when the primary screen is changed
224+
// and we request activation for the view when we get the signal, with
225+
// older version we iterate the views and request activation
226+
activatePrimary();
227+
#endif
228+
}
229+
230+
void GreeterApp::activatePrimary() {
231+
// activate and give focus to the window assigned to the primary screen
232+
Q_FOREACH (QQuickView *view, m_views) {
233+
if (view->screen() == QGuiApplication::primaryScreen()) {
234+
view->requestActivate();
235+
break;
236+
}
237+
}
209238
}
210239
}
211240

src/greeter/GreeterApp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***************************************************************************
2-
* Copyright (c) 2015 Pier Luigi Fiorini <[email protected]>
2+
* Copyright (c) 2015-2016 Pier Luigi Fiorini <[email protected]>
33
* Copyright (c) 2013 Nikita Mikhaylov <[email protected]>
44
*
55
* This program is free software; you can redistribute it and/or modify
@@ -65,6 +65,8 @@ namespace SDDM {
6565
UserModel *m_userModel { nullptr };
6666
GreeterProxy *m_proxy { nullptr };
6767
KeyboardModel *m_keyboard { nullptr };
68+
69+
void activatePrimary();
6870
};
6971
}
7072

0 commit comments

Comments
 (0)