Krita Source Code Documentation
Loading...
Searching...
No Matches
KisGrabKeyboardFocusRecoveryWorkaround.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 Deif Lou <ginoba@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include <QApplication>
8#include <QWidget>
9#include <QDebug>
10
12
14{
15public:
16
17 QScopedPointer<QWidget> dummyFocusRecoveryWidget {nullptr};
18
20 {
21#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
22 dummyFocusRecoveryWidget.reset(new QWidget);
23 dummyFocusRecoveryWidget->setObjectName("dummyFocusRecoveryWidget");
24#endif
25 }
26};
27
31
33{
34 static KisGrabKeyboardFocusRecoveryWorkaround *instance_ {nullptr};
35 if (!instance_) {
37 }
38 return instance_;
39}
40
42{
43#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
44 // Get the top-most window
45 QWidget *activeWindow = qApp->activeWindow();
46 QWidget *mainWindow = activeWindow;
47 // It may be that the active window is some dialog (e.g. the color
48 // selector). If the dummy widget is set as a child of that one, it may
49 // interfere somehow with the task bar (a new entry may appear on some
50 // window managers). So we get the parent window and use that as parent of
51 // the dummy widget.
52 while (mainWindow->parentWidget()) {
53 mainWindow = mainWindow->parentWidget();
54 }
55 // The window flags have to be set as Qt::Dialog so that the workaround
56 // works as expected
57 m_d->dummyFocusRecoveryWidget->setParent(mainWindow, Qt::Dialog);
58 // Perform the following a couple of times to ensure good results. Doing it
59 // only once may fail sometimes for some unknown reason
60 for (int i = 0; i < 2; ++i) {
61 m_d->dummyFocusRecoveryWidget->show();
62 m_d->dummyFocusRecoveryWidget->close();
63 // Ensure that the previously activated window is again active. If the
64 // active window was a dialog (e.g. the color selector), then the parent
65 // window of the dummy widget will be the main window, so the main
66 // window may be active at this point
67 activeWindow->activateWindow();
68 }
69#endif
70}
Background: For some reason, when using a combination of 'grabKeyboard' and 'releaseKeyboard' (e....
static KisGrabKeyboardFocusRecoveryWorkaround * instance()