108 QNapi::Object windowStageOrWindowObject,
111 auto subWindowOptionsObject =
113 windowStageOrWindowObject.Env(),
115 {
"title", subWindowOptions.windowTitle},
116 {
"decorEnabled", subWindowOptions.decorEnabled},
117 {
"isModal", subWindowOptions.isModal},
119 "windowRect", makeRectObject(windowStageOrWindowObject.Env(), subWindowOptions.windowRect)
123 subWindowOptionsObject.set(
"maximizeSupported",
true);
125 return windowStageOrWindowObject.evalToPromiseOrRejectOnThrow(
126 "createSubWindowWithOptions(*)",
127 {windowName, subWindowOptionsObject});
172 auto sharedContext = QtOhos::moveToSharedPtr(std::move(subWindowOnAppearCbCtx));
177 auto xComponent = takeNodeXComponentFromRegistryOrFail(sharedContext->xComponentId);
179 sharedContext->resultConsumer(
181 QOhosWindowProxyData {
182 .qAbilityPeer = sharedContext->qAbilityPeer,
183 .jsWindow = std::move(sharedContext->windowObject),
184 .windowProxyType = sharedContext->windowProxyType,
185 .subWindowMaximizeSupported = sharedContext->subWindowMaximizeSupported,
186 .nodeXComponent = std::make_shared<QXComponentNode>(xComponent),
187 .jsKeepAliveData = QtOhos::moveToSharedPtr(std::move(sharedContext->localStorageObj)),
188 .owningQWindowRef = sharedContext->owningQWindowRef,
191 sharedContext.reset();
203 auto *env = jsState.env();
205 auto localStorage = makeLocalStorage(jsState);
206 auto subWindowNativeNodeCreateInfo = QNapi::makeObject(
209 {
"xComponentId", createInfo.xComponentId.toNapiValue(env)},
212 [xComponentId = createInfo.xComponentId.stringId()]() {
214 "WindowNativeNodeCreateInfo.onDisAppear() called from JS for xComponentId='%s'",
215 xComponentId.c_str());
220 makeSubWindowOnAppearCallbackHandler(SubWindowOnAppearContext {
221 .xComponentId = createInfo.xComponentId,
222 .localStorageObj = Napi::Persistent(localStorage),
223 .windowObject = Napi::Persistent(createInfo.windowObject),
224 .resultConsumer = std::move(createInfo.resultConsumer),
225 .qAbilityPeer = std::move(createInfo.qAbilityPeer),
226 .windowProxyType = createInfo.windowProxyType,
227 .subWindowMaximizeSupported = createInfo.subWindowMaximizeSupported,
228 .owningQWindowRef = createInfo.owningQWindowRef,
233 localStorage.eval(
"setOrCreate(*)", {
"createInfo", subWindowNativeNodeCreateInfo});
241 struct LoadWindowContentsArgs
243 QNapi::Reference<QNapi::Object> window;
244 QNapi::Reference<QNapi::Object> localStorage;
245 std::string contentPagePath;
248 if (!context.disableWindowFocusableBeforeLoadContentHack)
249 return loadWindowContents(windowObject, context.localStorage, context.contentPagePath);
251 auto loadWindowContentsArgs = LoadWindowContentsArgs {
252 .window = Napi::Persistent(windowObject),
253 .localStorage = Napi::Persistent(context.localStorage),
254 .contentPagePath = context.contentPagePath,
257 return windowObject.evalToPromiseOrRejectOnThrow(
"setWindowFocusable(*)", {
false})
258 .onThen([loadWindowContentsArgs = std::move(loadWindowContentsArgs)]() {
259 return loadWindowContents(
260 loadWindowContentsArgs.window.Value(),
261 loadWindowContentsArgs.localStorage.Value(),
262 loadWindowContentsArgs.contentPagePath);
271 QOhosConsumer<QtOhos::JsState &, QOhosWindowProxyData> resultConsumer)
273 auto &qAbilityInstancesManager = QtOhos::QAbilityInstancesManager::instance(jsState);
274 auto optDefaultQAbility = jsState.defaultQAbility();
275 auto optForegroundQAbility = qAbilityInstancesManager.tryGetForegroundQAbility();
276 auto optBaseQAbility =
277 ((optDefaultQAbility && qAbilityInstancesManager.isQAbilityInForeground(*optDefaultQAbility)) || !optForegroundQAbility)
279 : optForegroundQAbility;
280 if (!optBaseQAbility)
281 qOhosReportFatalErrorAndAbort(
"%s: can't create window without an Ability instance to start it from", Q_FUNC_INFO);
283 auto abilityStartupOptions =
287 {
"windowLeft", createInfo.frameGeometry.x()},
288 {
"windowTop", createInfo.frameGeometry.y()},
289 {
"windowWidth", createInfo.frameGeometry.width()},
290 {
"windowHeight", createInfo.frameGeometry.height()},
294 auto fullscreenWindowMode = jsState.eval<QNapi::Number>(
295 "@ohos.app.ability.AbilityConstant.WindowMode.WINDOW_MODE_FULLSCREEN");
296 abilityStartupOptions.set(
"windowMode", fullscreenWindowMode);
299 qAbilityInstancesManager.startNewInstance(
300 optBaseQAbility.value(), createInfo.qWindowRef,
301 abilityStartupOptions,
302 [qWindowRef = createInfo.qWindowRef,
303 windowId = createInfo.windowId,
305 auto createInfo = QOhosWindowProxyExistingMainWindowCreateInfo {
306 .qWindowRef = qWindowRef,
307 .qAbilityInstanceId = qAbilityPeer->instanceId(),
308 .windowId = windowId,
311 makeWindowProxyDataForExistingMainWindowInJsThread(
312 jsState, createInfo,
std::move(resultConsumer));
319 QOhosConsumer<QtOhos::JsState &, QOhosWindowProxyData> resultConsumer)
322 getQAbilityPeerByInstanceIdOrFail(jsState, createInfo.qAbilityInstanceId));
323 if (!optQUiAbilityPeer) {
324 qOhosReportFatalErrorAndAbort(
325 "%s Attempting to make window proxy for main window for ability without windowStage. This is most likely a programming error. Aborting...",
328 optQUiAbilityPeer->setQWindow(jsState.env(), createInfo.qWindowRef);
330 auto window = optQUiAbilityPeer->window();
332 auto nodeXComponent = takeNodeXComponentFromRegistryOrFail(nativeNodeXComponentId);
336 QOhosWindowProxyData {
337 .qAbilityPeer = optQUiAbilityPeer,
338 .jsWindow = Napi::Persistent(window),
339 .windowProxyType = WindowProxyType::MainWindow,
340 .nodeXComponent = std::make_shared<QXComponentNode>(nodeXComponent),
341 .jsKeepAliveData =
nullptr,
342 .owningQWindowRef = createInfo.qWindowRef,
364 QNapi::Object windowStageOrWindowObject,
366 QOhosConsumer<QtOhos::JsState &, QOhosWindowProxyData> resultConsumer)
368 auto xComponentId = QXComponentId::createForNativeNodeSubWindow(createInfo.windowId);
371 bool disableWindowFocusableBeforeLoadContentHack;
374 QtOhos::QObjectThreadSafeRef owningQWindowRef;
375 bool createdWithMaximizeSupport;
378 auto qAbilityPeer = getQAbilityPeerByInstanceIdOrFail(jsState, createInfo.qAbilityInstanceId);
382 auto context = QtOhos::moveToSharedPtr(Context {
383 .disableWindowFocusableBeforeLoadContentHack = createInfo.disableWindowFocusableBeforeLoadContentHack,
384 .xComponentId = xComponentId,
385 .qAbilityPeer = qAbilityPeer,
386 .owningQWindowRef = createInfo.window,
387 .createdWithMaximizeSupport = createInfo.maximizeSupported,
394 auto subWindowCreationPromise = [&]() -> QNapi::Promise {
395 if (!isWindowSessionManagerAvailable(jsState)) {
396 context->createdWithMaximizeSupport =
false;
397 return createSubWindowViaWindowStage(qAbilityPeer, windowName);
400 return createSubWindowWithOptions(
401 windowStageOrWindowObject,
404 .windowTitle = createInfo.windowTitle,
405 .decorEnabled = createInfo.decorEnabled,
406 .isModal = createInfo.modal,
407 .maximizeSupported = createInfo.maximizeSupported,
408 .windowRect = createInfo.windowRect,
410 .onCatch([context, qAbilityPeer, windowName](
const QtOhos::CallbackInfo &) -> QNapi::Promise {
413 "createSubWindowWithOptions() is unavailable in this context; "
414 "falling back to createSubWindow()");
415 context->createdWithMaximizeSupport =
false;
416 return createSubWindowViaWindowStage(qAbilityPeer, windowName);
420 subWindowCreationPromise
421 .onThen([resultConsumer = std::move(resultConsumer), context](
const QtOhos::
CallbackInfo &cbInfo)
mutable {
422 auto windowObject = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
424 auto localStorage = makeLocalStorageForWindow(
426 LocalStorageForWindowCreateInfo {
427 .xComponentId = context->xComponentId,
428 .windowObject = windowObject,
429 .resultConsumer = std::move(resultConsumer),
430 .qAbilityPeer = context->qAbilityPeer,
431 .windowProxyType = WindowProxyType::SubWindow,
432 .subWindowMaximizeSupported = context->createdWithMaximizeSupport,
433 .owningQWindowRef = context->owningQWindowRef,
436 return onWindowCreatedLoadWindowContents(
437 cbInfo.jsState(), windowObject,
438 OnWindowCreatedLoadWindowContentsContext {
439 .disableWindowFocusableBeforeLoadContentHack = context->disableWindowFocusableBeforeLoadContentHack,
440 .contentPagePath =
"pages/SubWindowNativeNode",
441 .localStorage = localStorage,
445 QtOhos::logJsCallbackError(cbInfo,
"sub window creation failed");
446 qOhosReportFatalErrorAndAbort(
447 "Failed to create subwindow for windowId='%s'",
448 windowId.toStdString().c_str());
454 QOhosConsumer<QtOhos::JsState &, QOhosWindowProxyData> resultConsumer)
456 auto xComponentId = QXComponentId::createForNativeNodeFloatWindow(createInfo.internalWindowId);
457 auto qAbilityPeer = QtOhos::QAbilityInstancesManager::instance(jsState).defaultQAbilityPeer();
459 if (qAbilityPeer->qAbility().IsEmpty())
460 qOhosReportFatalErrorAndAbort(
"%s: can't create window without the default Ability instance", Q_FUNC_INFO);
462 auto configurationObject = QNapi::makeObject(
466 {
"name", makeOhosUniqueSystemWindowName(createInfo.internalWindowId)},
467 {
"windowType", jsState.eval<QNapi::Number>(
"@ohos.window.WindowType.TYPE_FLOAT")},
468 {
"ctx", qAbilityPeer->qAbility().get<QNapi::Object>(
"context")},
475 QtOhos::QObjectThreadSafeRef owningQWindowRef;
478 auto context = Context {
479 .xComponentId = xComponentId,
480 .qAbilityPeer = qAbilityPeer,
481 .owningQWindowRef = createInfo.qWindowRef,
485 .evalToPromiseOrRejectOnThrow(
"@ohos.window.createWindow(*)", {configurationObject})
486 .onThen([resultConsumer = std::move(resultConsumer), context = std::move(context)](
const QtOhos::
CallbackInfo &cbInfo)
mutable {
487 auto windowObject = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
489 auto localStorage = makeLocalStorageForWindow(
491 LocalStorageForWindowCreateInfo {
492 .xComponentId = context.xComponentId,
493 .windowObject = windowObject,
494 .resultConsumer = std::move(resultConsumer),
495 .qAbilityPeer = context.qAbilityPeer,
496 .windowProxyType = WindowProxyType::FloatWindow,
497 .owningQWindowRef = context.owningQWindowRef,
500 return onWindowCreatedLoadWindowContents(
501 cbInfo.jsState(), windowObject,
502 OnWindowCreatedLoadWindowContentsContext {
503 .disableWindowFocusableBeforeLoadContentHack =
false,
504 .contentPagePath =
"pages/FloatWindowNativeNode",
505 .localStorage = localStorage,
508 .onCatch([internalWindowId = createInfo.internalWindowId](
const QtOhos::
CallbackInfo &cbInfo) {
509 QtOhos::logJsCallbackError(cbInfo,
"Failed to create TYPE_FLOAT window");
510 qOhosReportFatalErrorAndAbort(
511 "Failed to create TYPE_FLOAT window for windowId='%s'",
512 internalWindowId.toStdString().c_str());
QOhosConsumer< QtOhos::JsState &, QOhosWindowProxyData > resultConsumer