Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohosjsutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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 <QtCore/private/qohoscommon_p.h>
5#include <QtCore/private/qohoslogger_p.h>
6#include "qohosjsutils.h"
7
9
10namespace QtOhos {
11
12std::shared_ptr<void> registerOnOffMethodsBasedEventHandler(
13 QNapi::Object eventSourceObject, const std::string &eventTypeName,
14 QNapi::CallbackFuncWrapper eventHandler, OnOffMethodsBasedEventHandlerOptions options)
15{
16 struct Context
17 {
18 std::function<QNapi::Value(const QOhosCallbackInfo &)> eventHandler;
19 std::function<bool(QNapi::Object)> eventSourceAliveCheckFunc;
20 QNapi::Reference<QNapi::Value> optExtraOnArg;
21 QNapi::Reference<QNapi::Value> optExtraOffArg;
22 };
23
24 auto env = eventSourceObject.Env();
25
26 auto sharedContext = moveToSharedPtr(
27 Context{
28 .eventHandler = std::move(eventHandler.callbackFunc()),
29 .eventSourceAliveCheckFunc = options.optEventSourceAliveCheckFunc
30 ? std::move(options.optEventSourceAliveCheckFunc)
31 : [](QNapi::Object) {
32 return true;
33 },
34 .optExtraOnArg = options.extraOnArg.has_value()
35 ? QNapi::Reference<>::makePersistentFrom(
36 options.extraOnArg.value().mapToValue(env))
37 : QNapi::Reference<>::makeEmpty(),
38 .optExtraOffArg = options.extraOffArg.has_value()
39 ? QNapi::Reference<>::makePersistentFrom(
40 options.extraOffArg.value().mapToValue(env))
41 : QNapi::Reference<>::makeEmpty(),
42 });
43
44 auto jsEventHandlerRef = moveToSharedPtr(
45 QNapi::Reference<>::makePersistentFrom(
46 QNapi::Function::New(
47 eventSourceObject.Env(),
48 [eventTypeName, weakContext = makeWeakPtr(sharedContext)](const QOhosCallbackInfo &cbInfo) {
49 auto sharedContext = weakContext.lock();
50 if (sharedContext) {
51 return sharedContext->eventHandler(cbInfo);
52 } else {
53 qOhosPrintfWarning(
54 "%s: got unexpected '%s' event callback call for detached handler",
55 Q_FUNC_INFO, eventTypeName.c_str());
56 return cbInfo.Env().Undefined();
57 }
58 })));
59
60 std::vector<QNapi::ValueWrapper> onCallArgs;
61 onCallArgs.push_back(eventTypeName);
62 if (!sharedContext->optExtraOnArg.IsEmpty())
63 onCallArgs.push_back(sharedContext->optExtraOnArg.Value());
64 onCallArgs.push_back(jsEventHandlerRef->Value());
65 eventSourceObject.call("on", onCallArgs);
66
67 auto eventSourceWeakRef = moveToSharedPtr(Napi::Weak(eventSourceObject));
68
69 return makeProxyWithJsThreadDeleter(
70 QtOhos::makeDestroyNotifier(
71 [eventSourceWeakRef, eventTypeName, sharedContext, jsEventHandlerRef]() {
72 auto eventSourceValue = eventSourceWeakRef->Value();
73 if (eventSourceValue.IsObject()) {
74 auto eventSourceObject = QNapi::checkedCast<QNapi::Object>(eventSourceValue);
75 if (sharedContext->eventSourceAliveCheckFunc(eventSourceObject)) {
76 try {
77 std::vector<QNapi::ValueWrapper> offCallArgs;
78 offCallArgs.push_back(eventTypeName);
79 if (!sharedContext->optExtraOffArg.IsEmpty())
80 offCallArgs.push_back(sharedContext->optExtraOffArg.Value());
81
82 offCallArgs.push_back(jsEventHandlerRef->Value());
83 eventSourceObject.call("off", offCallArgs);
84 } catch (const Napi::Error &e) {
85 qOhosPrintfError(
86 "%s: got exception from off(%s, ...) call (ignoring): %s",
87 Q_FUNC_INFO, eventTypeName.c_str(), e.what());
88 }
89 } else {
90 qOhosPrintfDebug(
91 "%s: not calling off(%s, ...), event source 'considered' not alive",
92 Q_FUNC_INFO, eventTypeName.c_str());
93 }
94 } else {
95 qOhosPrintfDebug(
96 "%s: not calling off(%s, ...), event source not alive",
97 Q_FUNC_INFO, eventTypeName.c_str());
98 }
99 }));
100}
101
102}
103
104QT_END_NAMESPACE
Combined button and popup list for selecting options.