1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtQml/qqmlextensionplugin.h>
5#include <QtQml/private/qqmlglobal_p.h>
6#include <QtQuickTemplates2/private/qtquicktemplates2global_p.h>
7
8#if QT_CONFIG(shortcut)
9#include <QtQuickTemplates2/private/qquickshortcutcontext_p_p.h>
10
11// qtdeclarative/src/quick/util/qquickshortcut.cpp
12typedef bool (*ShortcutContextMatcher)(QObject *, Qt::ShortcutContext);
13extern ShortcutContextMatcher qt_quick_shortcut_context_matcher();
14extern void qt_quick_set_shortcut_context_matcher(ShortcutContextMatcher matcher);
15#endif
16
17QT_BEGIN_NAMESPACE
18
19Q_GHS_KEEP_REFERENCE(qml_register_types_QtQuick_Templates);
20Q_GHS_KEEP_REFERENCE(QQuickTemplates_initializeModule);
21
22class QtQuickTemplates2Plugin : public QQmlExtensionPlugin
23{
24 Q_OBJECT
25 Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
26
27public:
28 QtQuickTemplates2Plugin(QObject *parent = nullptr);
29 ~QtQuickTemplates2Plugin();
30
31 void registerTypes(const char *uri) override;
32 void unregisterTypes() override;
33
34private:
35#if QT_CONFIG(shortcut)
36 ShortcutContextMatcher originalContextMatcher;
37#endif
38};
39
40QtQuickTemplates2Plugin::QtQuickTemplates2Plugin(QObject *parent)
41 : QQmlExtensionPlugin(parent)
42{
43 volatile auto registration = &qml_register_types_QtQuick_Templates;
44 volatile auto initialization = &QQuickTemplates_initializeModule;
45
46 Q_UNUSED(registration)
47 Q_UNUSED(initialization)
48}
49
50QtQuickTemplates2Plugin::~QtQuickTemplates2Plugin()
51{
52 // Intentionally empty: we use register/unregisterTypes() to do
53 // initialization and cleanup, as plugins are not unloaded on macOS.
54}
55
56void QtQuickTemplates2Plugin::registerTypes(const char * /*uri*/)
57{
58#if QT_CONFIG(shortcut)
59 originalContextMatcher = qt_quick_shortcut_context_matcher();
60 qt_quick_set_shortcut_context_matcher(matcher: QQuickShortcutContext::matcher);
61#endif
62}
63
64void QtQuickTemplates2Plugin::unregisterTypes()
65{
66#if QT_CONFIG(shortcut)
67 qt_quick_set_shortcut_context_matcher(matcher: originalContextMatcher);
68#endif
69}
70
71QT_END_NAMESPACE
72
73#include "qtquicktemplates2plugin.moc"
74

source code of qtdeclarative/src/quicktemplates/qtquicktemplates2plugin.cpp