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
qohosuiabilitydelegate.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 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
5
6#include <QtHarmonyExtras/private/qohosharmonyextrasenums_p.h>
7
8#include <QtCore/private/qcore_ohos_p.h>
9#include <QtCore/private/qnapi_p.h>
10#include <QtCore/private/qohoscommon_p.h>
11#include <QtCore/private/qohoslogger_p.h>
12
13#include <unordered_map>
14
16
17namespace QtHarmonyExtras::Private {
18
19namespace {
20
21using OnContinueResult = QtOhosHarmonyExtras::enums::ohos::app::ability::AbilityConstant::OnContinueResult;
22
23struct UiAbilityDelegateState
24{
25 std::shared_ptr<void> delegateHandle;
26 std::unordered_map<QHarmonyJsObjectRef, std::shared_ptr<std::function<napi_value(QHarmonyAppJsContext &, napi_value)>>> onContinueHandlers;
27};
28
29UiAbilityDelegateState &uiAbilityDelegateState()
30{
31 return QOhosJsThreadOps::instance().jsState().getAttachedObjectWithLazyCreate<UiAbilityDelegateState>();
32}
33
34Napi::Value callOnContinueHandler(const QOhosCallbackInfo &cbInfo)
35{
36 QNapi::Object ability;
37 QNapi::Object wantParams;
38 cbInfo.getLeadingArgs(Q_FUNC_INFO, ability, wantParams);
39
40 auto &appJsContext = QHarmonyAppJsContext::fromEnv(cbInfo.Env());
41 auto &state = uiAbilityDelegateState();
42
43 auto handlerIter = state.onContinueHandlers.find(QHarmonyJsObjectRef::fromValue(appJsContext, ability));
44 if (handlerIter == state.onContinueHandlers.end()) {
45 qOhosPrintfDebug("%s: no continuation handler for the ability, rejecting", Q_FUNC_INFO);
46 return cbInfo.jsState().mapOhosEnumToJs(OnContinueResult::REJECT);
47 }
48
49 auto handler = handlerIter->second;
50 return Napi::Value(cbInfo.Env(), (*handler)(appJsContext, wantParams));
51}
52
53}
54
56 QHarmonyAppJsContext &appJsContext, QHarmonyJsObjectRef abilityRef,
57 std::function<napi_value(QHarmonyAppJsContext &, napi_value)> handler)
58{
59 auto &state = uiAbilityDelegateState();
60 if (state.onContinueHandlers.count(abilityRef) != 0) {
61 qOhosPrintfError("%s: a continuation handler is already set for the ability, not setting another", Q_FUNC_INFO);
62 return nullptr;
63 }
64
65 if (state.onContinueHandlers.empty()) {
66 auto delegate = QNapi::Object::New(appJsContext.env());
67 delegate.set(
68 "onContinue",
69 [](const QOhosCallbackInfo &cbInfo) {
70 return callOnContinueHandler(cbInfo);
71 });
72 state.delegateHandle = appJsContext.addUiAbilityDelegate(delegate);
73 }
74
75 state.onContinueHandlers.emplace(abilityRef, QtOhos::moveToSharedPtr(std::move(handler)));
76 return QtOhos::makeDestroyNotifier(
77 [abilityRef]() {
78 auto &state = uiAbilityDelegateState();
79 state.onContinueHandlers.erase(abilityRef);
80 if (state.onContinueHandlers.empty())
81 state.delegateHandle.reset();
82 });
83}
84
85}
86
87QT_END_NAMESPACE
Combined button and popup list for selecting options.
std::shared_ptr< void > setUiAbilityOnContinueHandler(QHarmonyAppJsContext &appJsContext, QHarmonyJsObjectRef abilityRef, std::function< napi_value(QHarmonyAppJsContext &, napi_value)> handler)