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
qohosstartoptions.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 <QtCore/qpointer.h>
7#include <QtCore/private/qcore_ohos_p.h>
8#include <QtCore/private/qohoscommon_p.h>
9
11
12namespace QtHarmonyExtras {
13
14using namespace Private;
15
16/*!
17 \class QtHarmonyExtras::StartOptions
18 \inmodule QtHarmonyExtras
19 \since 5.12.12
20
21 \brief The StartOptions class is to provide new options for new started ability or process.
22
23 \sa createStartOptions()
24 \sa startAbility()
25 \sa startAppProcess()
26*/
27
28/*!
29 \enum QtHarmonyExtras::StartOptions::ProcessMode
30 \since 5.12.12
31
32 Enumerates the process modes. See
33 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V13/js-apis-app-ability-contextconstant-V13#processmode12}
34 {Process Mode}.
35
36 \value NewProcessAttachToParent A new process is created, the ability is started on the
37 process, and the process exits along with the parent process.
38 \value NewProcessAttachToStatusBarItem A new process is created, the ability is started
39 on the process, and the process is bound to the status bar icon.
40*/
41
42/*!
43 \enum QtHarmonyExtras::StartOptions::StartupVisibility
44 \since 5.12.12
45
46 Enumerates the visibility statuses of an ability after it is started. See
47 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V13/js-apis-app-ability-contextconstant-V13#startupvisibility12}
48 {Startup Visibility}.
49
50 \value Hide The target ability is hidden after it is started in the new process.
51 \value Show The target ability is displayed normally after it is started in the new process.
52*/
53
54/*!
55 \enum QtHarmonyExtras::StartOptions::WindowMode
56 \since 5.12.12
57
58 Enumerates the window mode when the ability is started. See
59 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V13/js-apis-app-ability-abilityconstant-V13#windowmode12}
60 {Window Mode}.
61
62 \value SplitPrimary Primary screen (left screen in the case of horizontal
63 orientation) in split-screen mode.
64 \value SplitSecondary Secondary screen (right screen in the case of horizontal
65 orientation) in split-screen mode.
66 \value Fullscreen Full screen mode.
67*/
68
69/*!
70 \enum QtHarmonyExtras::StartOptions::SupportWindowMode
71 \since 5.12.12
72
73 Enumerates the supported window modes when the ability is started. See
74 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-bundlemanager#supportwindowmode}
75 {Support Window Mode}.
76
77 \value FullScreen Indicates support for full screen mode.
78 \value Split Indicates support for split mode.
79 \value Floating Indicates support for floating mode.
80*/
81
82/*!
83 \class QtHarmonyExtras::WindowCreateParams
84 \inmodule QtHarmonyExtras
85 \since 5.12.12
86
87 \brief The WindowCreateParams class provides window creation parameters
88 for a started ability window.
89
90 See \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/arkts-apis-window-i#windowcreateparams20}
91 {Window Create Params}.
92
93 \sa createWindowCreateParams()
94*/
95
96/*!
97 \enum QtHarmonyExtras::WindowCreateParams::AnimationType
98 \since 5.12.12
99
100 Enumerates the animation types for window creation. See
101 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/arkts-apis-window-e#animationtype20}
102 {Animation Type}.
103
104 \value FadeInOut Fade-in/fade-out animation when the window is created and
105 destroyed.
106*/
107
108namespace {
109
110ElementName convertElementNameFromJsonObject(const QJsonObject &object)
111{
112 return ElementName{
113 .deviceId = object.value(QLatin1String("deviceId")).toString(),
114 .bundleName = object.value(QLatin1String("bundleName")).toString(),
115 .abilityName = object.value(QLatin1String("abilityName")).toString(),
116 .uri = object.value(QLatin1String("uri")).toString(),
117 .shortName = object.value(QLatin1String("shortName")).toString(),
118 .moduleName = object.value(QLatin1String("moduleName")).toString(),
119 };
120}
121
122std::optional<QOhosStartOptionsData::SupportWindowMode> tryMapSupportWindowModeToQpaFunctions(
123 StartOptions::SupportWindowMode supportWindowMode)
124{
125 switch (supportWindowMode) {
126 case StartOptions::SupportWindowMode::FullScreen:
127 return std::make_optional(QOhosStartOptionsData::SupportWindowMode::FULL_SCREEN);
128 case StartOptions::SupportWindowMode::Split:
129 return std::make_optional(QOhosStartOptionsData::SupportWindowMode::SPLIT);
130 case StartOptions::SupportWindowMode::Floating:
131 return std::make_optional(QOhosStartOptionsData::SupportWindowMode::FLOATING);
132 }
133 return {};
134}
135
136QList<QOhosStartOptionsData::SupportWindowMode> mapSupportWindowModesToQpaFunctions(
137 const QList<StartOptions::SupportWindowMode> &supportWindowModes)
138{
139 QList<QOhosStartOptionsData::SupportWindowMode> qpaFuncsSupportWindowModes;
140 for (auto supportWindowMode : supportWindowModes) {
141 auto optQpaFuncsSupportWindowMode = tryMapSupportWindowModeToQpaFunctions(supportWindowMode);
142 if (optQpaFuncsSupportWindowMode.has_value()) {
143 qpaFuncsSupportWindowModes.append(optQpaFuncsSupportWindowMode.value());
144 } else {
145 qCWarning(
146 QtForOhos, "%s: got unsupported supportWindowMode (%d), ignoring",
147 Q_FUNC_INFO, static_cast<int>(supportWindowMode));
148 }
149 }
150
151 return qpaFuncsSupportWindowModes;
152}
153
154class QOhosWindowCreateParamsImpl : public WindowCreateParams
155{
156public:
157 QOhosWindowCreateParamsImpl();
158
159 void setAnimationType(AnimationType animationType) override;
160
161 QOhosStartOptionsData::WindowCreateParamsPriv qpaWindowCreateParams() const;
162
163private:
164 QOhosStartOptionsData::WindowCreateParamsPriv m_qpaWindowCreateParams;
165};
166
167QOhosWindowCreateParamsImpl::QOhosWindowCreateParamsImpl() = default;
168
169void QOhosWindowCreateParamsImpl::setAnimationType(AnimationType animationType)
170{
171 bool supportedAnimationType = false;
172 switch (animationType) {
173 case AnimationType::FadeInOut:
174 m_qpaWindowCreateParams.setWindowFadeInOutAnimation = true;
175 supportedAnimationType = true;
176 break;
177 }
178
179 if (!supportedAnimationType) {
180 qCWarning(
181 QtForOhos, "%s: got unsupported AnimationType: %d",
182 Q_FUNC_INFO, static_cast<int>(animationType));
183 }
184}
185
186QOhosStartOptionsData::WindowCreateParamsPriv QOhosWindowCreateParamsImpl::qpaWindowCreateParams() const
187{
188 return m_qpaWindowCreateParams;
189}
190
191class QOhosStartOptionsImpl : public StartOptions
192{
193public:
194 QOhosStartOptionsImpl()
195 : StartOptions()
196 {}
197
198 /*!
199 \fn QtHarmonyExtras::StartOptions::setWindowMode(WindowMode windowMode)
200
201 Sets \a windowMode when the ability is started. See
202 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-startoptions-V5#properties}
203 {Window Mode}.
204 */
205 void setWindowMode(StartOptions::WindowMode windowMode) override
206 {
207 std::optional<QOhosStartOptionsData::WindowMode> internalWindowMode;
208 switch (windowMode) {
209 case StartOptions::WindowMode::SplitPrimary:
210 internalWindowMode = QOhosStartOptionsData::WindowMode::WINDOW_MODE_SPLIT_PRIMARY;
211 break;
212 case StartOptions::WindowMode::SplitSecondary:
213 internalWindowMode = QOhosStartOptionsData::WindowMode::WINDOW_MODE_SPLIT_SECONDARY;
214 break;
215 case StartOptions::WindowMode::Fullscreen:
216 internalWindowMode = QOhosStartOptionsData::WindowMode::WINDOW_MODE_FULLSCREEN;
217 break;
218 }
219
220 if (internalWindowMode.has_value())
221 m_startOptions.windowMode = internalWindowMode.value();
222 else
223 qCWarning(QtForOhos, "%s: unsupported windowMode: %d", Q_FUNC_INFO, static_cast<int>(windowMode));
224 }
225
226 /*!
227 \fn QtHarmonyExtras::StartOptions::setDisplayId(int displayId)
228
229 Sets \a displayId. The default value is 0, indicating the current display. See
230 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-startoptions-V5#properties}
231 {Display Id}.
232 */
233 void setDisplayId(int displayId) override
234 {
235 m_startOptions.displayId = displayId;
236 }
237
238 /*!
239 \fn QtHarmonyExtras::StartOptions::setWithAnimation(bool withAnimation)
240
241 Sets \a withAnimation whether the ability has the animation effect. See
242 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-startoptions-V5}
243 {With Animation}.
244 */
245 void setWithAnimation(bool withAnimation) override
246 {
247 m_startOptions.withAnimation = withAnimation;
248 }
249
250 /*!
251 \fn QtHarmonyExtras::StartOptions::setWindowLeft(int windowLeft)
252
253 Sets \a windowLeft left position of the window. See
254 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-startoptions-V5}
255 {Window Left}.
256 */
257 void setWindowLeft(int windowLeft) override
258 {
259 m_startOptions.windowLeft = windowLeft;
260 }
261
262 /*!
263 \fn QtHarmonyExtras::StartOptions::setWindowTop(int windowTop)
264
265 Sets \a windowTop top position of the window. See
266 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-startoptions-V5}
267 {Window Top}.
268 */
269 void setWindowTop(int windowTop) override
270 {
271 m_startOptions.windowTop = windowTop;
272 }
273
274 /*!
275 \fn QtHarmonyExtras::StartOptions::setWindowWidth(int windowWidth)
276
277 Sets \a windowWidth width of of the window. See
278 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-startoptions-V5}
279 {Window Width}.
280 */
281 void setWindowWidth(int windowWidth) override
282 {
283 m_startOptions.windowWidth = windowWidth;
284 }
285
286 /*!
287 \fn QtHarmonyExtras::StartOptions::setWindowHeight(int windowHeight)
288
289 Sets \a windowHeight height of of the window. See
290 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-startoptions-V5}
291 {Window Height}.
292 */
293 void setWindowHeight(int windowHeight) override
294 {
295 m_startOptions.windowHeight = windowHeight;
296 }
297
298 /*!
299 \fn QtHarmonyExtras::StartOptions::setProcessMode(ProcessMode processMode)
300
301 Sets \a processMode. See
302 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-startoptions-V5}
303 {Process Mode}.
304 */
305 void setProcessMode(StartOptions::ProcessMode processMode) override
306 {
307 std::optional<QOhosStartOptionsData::ProcessMode> internalProcessMode;
308 switch (processMode) {
309 case StartOptions::ProcessMode::NewProcessAttachToParent:
310 internalProcessMode = QOhosStartOptionsData::ProcessMode::NEW_PROCESS_ATTACH_TO_PARENT;
311 break;
312 case StartOptions::ProcessMode::NewProcessAttachToStatusBarItem:
313 internalProcessMode = QOhosStartOptionsData::ProcessMode::NEW_PROCESS_ATTACH_TO_STATUS_BAR_ITEM;
314 break;
315 }
316
317 if (internalProcessMode.has_value())
318 m_startOptions.processMode = internalProcessMode.value();
319 else
320 qCWarning(QtForOhos, "%s: unsupported processMode: %d", Q_FUNC_INFO, static_cast<int>(processMode));
321 }
322
323 /*!
324 \fn QtHarmonyExtras::StartOptions::setStartupVisibility(StartupVisibility startupVisibility)
325
326 Sets \a startupVisibility of the ability after it is started. See
327 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/js-apis-app-ability-startoptions-V5}
328 {Startup Visibility}.
329 */
330 void setStartupVisibility(StartOptions::StartupVisibility startupVisibility) override
331 {
332 std::optional<QOhosStartOptionsData::StartupVisibility> internalStartupVisibility;
333 switch (startupVisibility) {
334 case StartOptions::StartupVisibility::Hide:
335 internalStartupVisibility = QOhosStartOptionsData::StartupVisibility::STARTUP_HIDE;
336 break;
337 case StartOptions::StartupVisibility::Show:
338 internalStartupVisibility = QOhosStartOptionsData::StartupVisibility::STARTUP_SHOW;
339 break;
340 }
341
342 if (internalStartupVisibility.has_value())
343 m_startOptions.startupVisibility = internalStartupVisibility.value();
344 else
345 qCWarning(QtForOhos, "%s: unsupported startupVisibility: %d", Q_FUNC_INFO, static_cast<int>(startupVisibility));
346 }
347
348 /*!
349 \fn QtHarmonyExtras::StartOptions::setStartWindowIcon(const QImage &startWindowIcon)
350
351 Sets \a startWindowIcon for the start window.
352
353 See \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-app-ability-startoptions}
354 {Start Window Icon}.
355 */
356 void setStartWindowIcon(const QImage &startWindowIcon) override
357 {
358 m_startOptions.windowIcon = startWindowIcon;
359 }
360
361 /*!
362 \fn QtHarmonyExtras::StartOptions::setStartWindowBackgroundColor(const QColor &startWindowBackgroundColor)
363
364 Sets \a startWindowBackgroundColor for the start window.
365
366 See \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-app-ability-startoptions}
367 {Start Window Background Color}.
368 */
369 void setStartWindowBackgroundColor(const QColor &startWindowBackgroundColor) override
370 {
371 if (startWindowBackgroundColor.isValid())
372 m_startOptions.windowBackgroundColorHex = startWindowBackgroundColor.name(QColor::HexArgb);
373 }
374
375 /*!
376 \fn QtHarmonyExtras::StartOptions::setSupportWindowModes(const QList<SupportWindowMode> &supportWindowModes)
377
378 Sets \a supportWindowModes when the ability is started. See
379 \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-app-ability-startoptions}
380 {Support Window Modes}.
381 */
382 void setSupportWindowModes(const QList<SupportWindowMode> &supportWindowModes) override
383 {
384 auto qpaFuncsSupportWindowModes = mapSupportWindowModesToQpaFunctions(supportWindowModes);
385 if (!qpaFuncsSupportWindowModes.isEmpty())
386 m_startOptions.supportWindowModes = qpaFuncsSupportWindowModes;
387 else
388 qCWarning(QtForOhos, "%s: empty supportWindowModes is unsupported, skipping", Q_FUNC_INFO);
389 }
390
391 /*!
392 \fn QtHarmonyExtras::StartOptions::setMinWindowWidth(int minWindowWidth)
393
394 Sets \a minWindowWidth as the minimum width, in px.
395
396 See \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-app-ability-startoptions}
397 {Minimum Window Width}.
398 */
399 void setMinWindowWidth(int minWindowWidth) override
400 {
401 m_startOptions.minWindowWidth = minWindowWidth;
402 }
403
404 /*!
405 \fn QtHarmonyExtras::StartOptions::setMinWindowHeight(int minWindowHeight)
406
407 Sets \a minWindowHeight as the minimum height, in px.
408
409 See \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-app-ability-startoptions}
410 {Minimum Window Height}.
411 */
412 void setMinWindowHeight(int minWindowHeight) override
413 {
414 m_startOptions.minWindowHeight = minWindowHeight;
415 }
416
417 /*!
418 \fn QtHarmonyExtras::StartOptions::setMaxWindowWidth(int maxWindowWidth)
419
420 Sets \a maxWindowWidth as the maximum width, in px.
421
422 See \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-app-ability-startoptions}
423 {Maximum Window Width}.
424 */
425 void setMaxWindowWidth(int maxWindowWidth) override
426 {
427 m_startOptions.maxWindowWidth = maxWindowWidth;
428 }
429
430 /*!
431 \fn QtHarmonyExtras::StartOptions::setMaxWindowHeight(int maxWindowHeight)
432
433 Sets \a maxWindowHeight as the maximum height, in px.
434
435 See \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-app-ability-startoptions}
436 {Maximum Window Height}.
437 */
438 void setMaxWindowHeight(int maxWindowHeight) override
439 {
440 m_startOptions.maxWindowHeight = maxWindowHeight;
441 }
442
443 /*!
444 \fn QtHarmonyExtras::StartOptions::setHideStartWindow(bool hideStartWindow)
445
446 Controls whether to hide the start window when launching the current application's UIAbility.
447
448 See \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-app-ability-startoptions}
449 {Hide Start Window}.
450 */
451 void setHideStartWindow(bool hideStartWindow) override
452 {
453 m_startOptions.hideStartWindow = hideStartWindow;
454 }
455
456 /*!
457 \fn QtHarmonyExtras::StartOptions::setWindowCreateParams(const WindowCreateParams &windowCreateParams)
458
459 Sets \a windowCreateParams used when creating the window.
460
461 See \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/arkts-apis-window-i#windowcreateparams20}
462 {Window Create Params}.
463 */
464 void setWindowCreateParams(const WindowCreateParams &windowCreateParams) override
465 {
466 const auto *windowCreateParamsImpl = dynamic_cast<const QOhosWindowCreateParamsImpl *>(&windowCreateParams);
467 if (windowCreateParamsImpl != nullptr)
468 m_startOptions.windowCreateParams = windowCreateParamsImpl->qpaWindowCreateParams();
469 }
470
471 /*!
472 \fn QtHarmonyExtras::StartOptions::setCompletionHandler(QObject *context, std::function<void(bool, ElementName, QString)> callback)
473
474 Sets the completion \a callback invoked when the corresponding start request completes. It is
475 invoked on the thread of \a context; if \a context is destroyed before completion, \a callback
476 is not invoked. Its arguments report whether the ability was started, the launched element
477 name, and a message with the outcome details.
478
479 See \l {https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-app-ability-startoptions}
480 {Completion Handler}.
481 */
482 void setCompletionHandler(
483 QObject *context,
484 std::function<void(bool, ElementName, QString)> callback) override
485 {
486 m_startOptions.optCompletionHandler =
487 std::make_shared<QOhosConsumer<bool, QJsonObject, QString>>(
488 [contextPtr = QPointer<QObject>(context), callback = std::move(callback)](
489 bool succeeded, const QJsonObject &elementName, const QString &message) {
490 if (!contextPtr.isNull() && callback)
491 callback(succeeded, convertElementNameFromJsonObject(elementName), message);
492 });
493 }
494
495 QOhosStartOptionsData getStartOptions() const
496 {
497 return m_startOptions;
498 }
499
500private:
501 QOhosStartOptionsData m_startOptions;
502};
503
504}
505
507
509
510StartOptions::StartOptions() = default;
511StartOptions::~StartOptions() = default;
512
513/*!
514 \fn std::shared_ptr<QtHarmonyExtras::WindowCreateParams> QtHarmonyExtras::createWindowCreateParams()
515
516 Creates WindowCreateParams instance.
517*/
519{
520 return std::make_shared<QOhosWindowCreateParamsImpl>();
521}
522
523/*!
524 \fn std::shared_ptr<QtHarmonyExtras::StartOptions> QtHarmonyExtras::createStartOptions()
525
526 Creates StartOptions instance.
527*/
529{
530 return std::make_shared<QOhosStartOptionsImpl>();
531}
532
534 const StartOptions &options)
535{
536 const auto *startOptionsImpl = dynamic_cast<const QOhosStartOptionsImpl *>(&options);
537 return startOptionsImpl != nullptr
538 ? std::make_optional(startOptionsImpl->getStartOptions())
539 : std::nullopt;
540}
541
542}
543
544
545QT_END_NAMESPACE
Combined button and popup list for selecting options.
std::optional< QOhosStartOptionsData > tryConvertStartOptionsToQpaFunctionsStruct(const StartOptions &options)
std::shared_ptr< WindowCreateParams > createWindowCreateParams()
Creates WindowCreateParams instance.
std::shared_ptr< StartOptions > createStartOptions()
Creates StartOptions instance.