feat: add DockClicker event filter

Add an event filter to `qApp` that can detect a dock icon click (in macos) when the main window is hidden or closed.

Co-authored-by: Boris Melnik <82511785+borismelnik@users.noreply.github.com>
This commit is contained in:
Eric Mastro 2021-04-29 19:52:53 +10:00 committed by Michał
parent 37ebdf58b9
commit 1471e1c1a4
2 changed files with 49 additions and 0 deletions

View File

@ -83,6 +83,8 @@ DOS_API void DOS_CALL dos_qguiapplication_delete(void);
DOS_API void dos_qapplication_clipboard_setText(const char* text); DOS_API void dos_qapplication_clipboard_setText(const char* text);
DOS_API void dos_qapplication_installEventFilter(DosQQmlApplicationEngine *vptr);
/// @} /// @}
/// \defgroup QApplication QApplication /// \defgroup QApplication QApplication

View File

@ -155,6 +155,46 @@ void dos_qguiapplication_quit()
qGuiApp->quit(); qGuiApp->quit();
} }
class DockClicker : public QObject
{
Q_OBJECT
private:
Qt::ApplicationState _prevAppState;
QQmlApplicationEngine* _engine;
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
public:
DockClicker(::DosQQmlApplicationEngine *vptr) {
auto engine = static_cast<QQmlApplicationEngine *>(vptr);
_engine = engine;
}
};
bool DockClicker::eventFilter(QObject *obj, QEvent *event)
{
#ifdef Q_OS_MACOS
if (obj == qApp) {
if (event->type() == QEvent::ApplicationStateChange) {
auto ev = static_cast<QApplicationStateChangeEvent*>(event);
if (_prevAppState == Qt::ApplicationActive && ev->applicationState() == Qt::ApplicationActive) {
QObject *topLevel = _engine->rootObjects().value(0);
QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
window->setVisible(true);
window->showNormal();
}
_prevAppState = ev->applicationState();
return true;
} else {
return false;
}
}
#endif // Q_OS_MACOS
return QObject::eventFilter(obj, event);
}
void dos_qapplication_create(char *appName) void dos_qapplication_create(char *appName)
{ {
static int argc = 1; static int argc = 1;
@ -185,6 +225,12 @@ void dos_qapplication_quit()
qApp->quit(); qApp->quit();
} }
void dos_qapplication_installEventFilter(::DosQQmlApplicationEngine *vptr)
{
DockClicker *dockClicker = new DockClicker(vptr);
qApp->installEventFilter(dockClicker);
}
::DosQQmlApplicationEngine *dos_qqmlapplicationengine_create() ::DosQQmlApplicationEngine *dos_qqmlapplicationengine_create()
{ {
return new QQmlApplicationEngine(); return new QQmlApplicationEngine();
@ -1273,3 +1319,4 @@ char *dos_qurl_replaceHostAndAddPath(char* url, char* newScheme, char* newHost,
return convert_to_cstring(newQurl.toString()); return convert_to_cstring(newQurl.toString());
} }
#include "DOtherSide.moc"