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