4#include <render/qohoswindowproxy.h>
6#include <QtCore/qcoreapplication.h>
7#include <QtCore/qset.h>
8#include <QtCore/private/qnapi_p.h>
9#include <QtCore/qscopeguard.h>
10#include <QtGui/private/qohosimageconversions_p.h>
11#include <QtGui/qguiapplication.h>
12#include <QtGui/qimage.h>
16#include <multimedia/image_framework/image/pixelmap_native.h>
17#include <qarkui/input.h>
18#include <qarkui/qarkuiutils.h>
19#include <qarkui/qxcomponentregistry.h>
20#include <qarkui/window.h>
21#include <qarkui/window_manager.h>
22#include <qohosdeviceinfo_p.h>
23#include <qohosdisplayinfo.h>
24#include <qohosjsutils.h>
25#include <qohospixelmapconversions.h>
26#include <qohosplugincore.h>
27#include <qohosqpaenums.h>
28#include <qohossettings.h>
29#include <qohosutils.h>
30#include <render/qohosbatchingrequestshandler.h>
31#include <render/qohosjswindowregistry.h>
32#include <render/qohoswindowproxydatafactory.h>
33#include <render/qxcomponent.h>
46 ohosWindowRect.get<QNapi::Number>(
"left"),
47 ohosWindowRect.get<QNapi::Number>(
"top"),
48 ohosWindowRect.get<QNapi::Number>(
"width"),
49 ohosWindowRect.get<QNapi::Number>(
"height"),
56 .visible = avoidAreaObject.get<QNapi::Boolean>(
"visible"),
57 .leftRect = ohosWindowRectToQRect(avoidAreaObject.get<QNapi::Object>(
"leftRect")),
58 .topRect = ohosWindowRectToQRect(avoidAreaObject.get<QNapi::Object>(
"topRect")),
59 .rightRect = ohosWindowRectToQRect(avoidAreaObject.get<QNapi::Object>(
"rightRect")),
60 .bottomRect = ohosWindowRectToQRect(avoidAreaObject.get<QNapi::Object>(
"bottomRect")),
69 case Qt::UpArrowCursor:
77 case Qt::SizeVerCursor:
79 case Qt::SizeHorCursor:
81 case Qt::SizeBDiagCursor:
83 case Qt::SizeFDiagCursor:
85 case Qt::SizeAllCursor:
90 case Qt::SplitVCursor:
92 case Qt::SplitHCursor:
94 case Qt::PointingHandCursor:
96 case Qt::ForbiddenCursor:
98 case Qt::WhatsThisCursor:
102 case Qt::OpenHandCursor:
104 case Qt::ClosedHandCursor:
106 case Qt::DragCopyCursor:
108 case Qt::DragMoveCursor:
110 case Qt::DragLinkCursor:
113 case Qt::BitmapCursor:
116 case Qt::CustomCursor:
126 auto propOrEmpty = QNapi::getOptionalPropOrEmpty<QNapi::Number>(object, propertyName);
127 return !propOrEmpty.IsEmpty()
128 ?
std::optional<
double>(propOrEmpty)
134 auto windowPropsObj = jsWindow.eval<QNapi::Object>(
"getWindowProperties()");
135 auto displayIdOrEmpty = QNapi::getOptionalPropOrEmpty<QNapi::Number>(windowPropsObj,
"displayId");
136 return QArkUi::WindowProperties {
137 .windowRect = ohosWindowRectToQRect(windowPropsObj.get<QNapi::Object>(
"windowRect")),
138 .drawableRect = ohosWindowRectToQRect(windowPropsObj.get<QNapi::Object>(
"drawableRect")),
139 .id = QArkUi::JsWindowId(windowPropsObj.get<QNapi::Number>(
"id")),
140 .displayId = !displayIdOrEmpty.IsEmpty()
141 ? std::optional(QOhosDisplayInfo::JsDisplayId{displayIdOrEmpty.DoubleValue()})
148 auto moveConfigurationObject = QNapi::makeObject(env);
149 if (moveConfiguration.displayId.has_value()) {
150 moveConfigurationObject.set(
151 "displayId", moveConfiguration.displayId.value().value());
153 return moveConfigurationObject;
156template<
typename ...Args>
158 std::function<
void(Args...)> QOhosWindowProxy::WindowCallbacks::*memberPtr,
161 auto weakQtWindowCallbacks = QtOhos::makeWeakPtr(qtWindowCallbacks);
163 return [memberPtr, weakQtWindowCallbacks](Args ...args) {
164 QtOhos::invokeInQtThread([memberPtr, weakQtWindowCallbacks, args...]() {
165 auto qtWindowCallbacks = weakQtWindowCallbacks.lock();
166 if (qtWindowCallbacks)
167 (*qtWindowCallbacks.*memberPtr)(args...);
174 std::function<
void(T)> QOhosWindowProxy::WindowCallbacks::*memberPtr,
177 auto weakQtWindowCallbacks = QtOhos::makeWeakPtr(qtWindowCallbacks);
179 return QtOhos::makeCompressingAsyncConsumer<T>(
180 [memberPtr, weakQtWindowCallbacks](T value) {
181 auto qtWindowCallbacks = weakQtWindowCallbacks.lock();
182 if (qtWindowCallbacks)
183 (*qtWindowCallbacks.*memberPtr)(value);
185 QtOhos::invokeInQtThread);
190 constexpr bool containsPolicyExcludeEdgeValue =
true;
192 auto drawableRectInScreenSpace =
193 windowProperties.drawableRect.translated(windowProperties.windowRect.topLeft());
194 return !drawableRectInScreenSpace.contains(point, containsPolicyExcludeEdgeValue);
200 case ::MOUSE_ACTION_MOVE:
201 return QEvent::NonClientAreaMouseMove;
202 case ::MOUSE_ACTION_BUTTON_DOWN:
203 return QEvent::NonClientAreaMouseButtonPress;
204 case ::MOUSE_ACTION_BUTTON_UP:
205 return QEvent::NonClientAreaMouseButtonRelease;
206 case ::MOUSE_ACTION_CANCEL:
207 case ::MOUSE_ACTION_AXIS_BEGIN:
208 case ::MOUSE_ACTION_AXIS_UPDATE:
209 case ::MOUSE_ACTION_AXIS_END:
218 case ::MOUSE_BUTTON_LEFT:
219 return Qt::LeftButton;
220 case ::MOUSE_BUTTON_MIDDLE:
221 return Qt::MiddleButton;
222 case ::MOUSE_BUTTON_RIGHT:
223 return Qt::RightButton;
224 case ::MOUSE_BUTTON_FORWARD:
225 return Qt::ForwardButton;
226 case ::MOUSE_BUTTON_BACK:
227 return Qt::BackButton;
228 case ::MOUSE_BUTTON_NONE:
237 case ::TOUCH_ACTION_MOVE:
238 return QEvent::NonClientAreaMouseMove;
239 case ::TOUCH_ACTION_DOWN:
240 return QEvent::NonClientAreaMouseButtonPress;
241 case ::TOUCH_ACTION_UP:
242 return QEvent::NonClientAreaMouseButtonRelease;
243 case ::TOUCH_ACTION_CANCEL:
249template<
typename EnumsContainer>
253 for (
auto enumValue : enums) {
256 output +=
std::to_string(
static_cast<std::underlying_type_t<
decltype(enumValue)>>(enumValue));
265 return QtOhos::JsWindowsTracker::isWindowClosing(jsWindow) || (boundToAbility && abilityPeer->isTerminating());
270const QOhosWindowProxy::EventHandlerDescriptor QOhosWindowProxy::eventHandlerDescriptors[] = {
272 .eventName =
"avoidAreaChange",
273 .eventHandler = &QOhosWindowProxy::JsScopeData::handleAvoidAreaChangeCallback,
274 .eventHandlerFlags = {},
277 .eventName =
"touchOutside",
278 .eventHandler = &QOhosWindowProxy::JsScopeData::handleWindowTouchOutsideCallback,
279 .eventHandlerFlags = {},
282 .eventName =
"windowEvent",
283 .eventHandler = &QOhosWindowProxy::JsScopeData::handleWindowEventCallback,
284 .eventHandlerFlags = EventHandlerFlagBits::allowCallWhenAbilityIsTerminating,
287 .eventName =
"windowRectChange",
288 .eventHandler = &QOhosWindowProxy::JsScopeData::handleWindowRectChangeCallback,
289 .eventHandlerFlags = {},
292 .eventName =
"rectChangeInGlobalDisplay",
293 .eventHandler = &QOhosWindowProxy::JsScopeData::handleWindowRectChangeInGlobalDisplayCallback,
294 .eventHandlerFlags = {},
297 .eventName =
"windowStatusChange",
298 .eventHandler = &QOhosWindowProxy::JsScopeData::handleWindowStatusCallback,
299 .eventHandlerFlags = {},
302 .eventName =
"windowVisibilityChange",
303 .eventHandler = &QOhosWindowProxy::JsScopeData::handleWindowVisibilityCallback,
304 .eventHandlerFlags = {},
307 .eventName =
"displayIdChange",
308 .eventHandler = &QOhosWindowProxy::JsScopeData::handleWindowDisplayIdChangeCallback,
309 .eventHandlerFlags = EventHandlerFlagBits::allowEventHandlerRegistrationFailure,
313QOhosWindowProxy::QOhosWindowProxy(
316 QtOhos::makeProxyWithJsThreadDeleter(
317 std::make_shared<JsScopeData>(
318 windowProxyData.windowProxyType,
319 std::move(windowProxyData.jsWindow),
320 std::move(windowProxyData.jsKeepAliveData),
321 std::move(windowProxyData.qAbilityPeer),
322 windowProxyData.owningQWindowRef)))
324 , m_nodeXComponent(windowProxyData.nodeXComponent)
325 , m_qAbilityInstanceId(m_jsScopeData->jsWindowRef->owningQAbilityInstanceId())
327 std::vector<std::shared_ptr<
void>> eventListenersHandles;
328 for (
const auto &eventHandlerDescriptor : eventHandlerDescriptors) {
329 eventListenersHandles.push_back(
330 m_jsScopeData->registerEventListener(
331 eventHandlerDescriptor.eventName, eventHandlerDescriptor.eventHandler,
332 eventHandlerDescriptor.eventHandlerFlags));
334 m_jsScopeData->m_eventListenersHandle = QtOhos::moveToSharedPtr(std::move(eventListenersHandles));
344 m_subWindowCloseRegistrationHandle.reset();
345 m_jsScopeData.reset();
352 if (m_jsScopeData->isWindowClosing()) {
356 auto optQUiAbilityPeer
358 if (!optQUiAbilityPeer) {
362 auto promise = optQUiAbilityPeer->windowStage().evalToPromiseOrRejectOnThrow(
363 "removeStartingWindow()");
364 promise.onFinally(
std::move(taskPromise).makeChained(Q_FUNC_INFO));
372 auto displayIdValue = optMoveToTargetDisplay.value_or(QOhosDisplayInfo::JsDisplayId(-1)).value();
373 qOhosPrintfDebug(
"%s: %d,%d,%f", Q_FUNC_INFO, point.x(), point.y(), displayIdValue);
376 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<> taskPromise) {
377 if (m_jsScopeData->isWindowClosing()) {
382 const auto primaryJsDisplayId = QOhosDisplayInfo::JsDisplayId(0);
384 auto targetDisplayId = optMoveToTargetDisplay.has_value()
385 ? optMoveToTargetDisplay.value()
388 auto optDisplayInfo = qTransform(
389 QOhosDisplayInfo::tryGetDisplayById(jsState, targetDisplayId),
390 [&](
auto displayObject) {
391 return QOhosDisplayInfo::makeFromOhosDisplayObject(jsState, displayObject);
394 auto optIsDisplayMainOrExtended = qTransform(
400 bool isDisplayMainOrExtended;
401 if (optIsDisplayMainOrExtended.has_value()) {
402 isDisplayMainOrExtended = optIsDisplayMainOrExtended.value();
405 "%s: no display source mode detected. Assumming main/extended screen", Q_FUNC_INFO);
406 isDisplayMainOrExtended =
true;
409 QNapi::Promise promise;
410 if (!isDisplayMainOrExtended) {
411 auto optDisplayOffset = qAndThen(
414 return displayInfo.topLeftOffsetPixels;
417 const QPoint defaultDisplayOffset(0, 0);
418 auto targetCoordinates = point - optDisplayOffset.value_or(defaultDisplayOffset);
420 if (!optMoveToTargetDisplay.has_value())
421 qOhosPrintfWarning(
"%s: trying to move window to not valid target display", Q_FUNC_INFO);
423 auto moveConfigurationObject = toNapiObject(
426 .displayId = optMoveToTargetDisplay,
429 promise = m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
430 "moveWindowToGlobal(*)", {targetCoordinates.x(), targetCoordinates.y(), moveConfigurationObject});
432 promise = m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
433 "moveWindowToGlobalDisplay(*)", {point.x(), point.y()});
436 promise.onFinally(
std::move(taskPromise));
440void QOhosWindowProxy::
setSize(
const QSize &size)
442 qOhosPrintfDebug(
"%s: %d,%d", Q_FUNC_INFO, size.width(), size.height());
446 if (m_jsScopeData->isWindowClosing()) {
451 const auto currentSize = getWindowPropertiesFromJsWindow(
452 m_jsScopeData->jsWindowRef->jsObject()).windowRect.size();
453 if (currentSize == size) {
458 auto promise = m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
459 "resizeAsync(*)", {size.width(), size.height()});
460 promise.onFinally(
std::move(taskPromise).makeChained(Q_FUNC_INFO));
467 qCDebug(QtForOhos) << Q_FUNC_INFO << color;
470 if (m_jsScopeData->isWindowClosing())
472 m_jsScopeData->jsWindowRef->eval(
"setWindowBackgroundColor(*)", {color.name(QColor::HexArgb).toStdString()});
477void QOhosWindowProxy::
setCustomCursor(
const QImage &customCursorImage,
const QPoint &hotSpot)
479 const auto maxOhosCustomCursorSize = QSize(256, 256);
480 const auto customCursorSize = customCursorImage.size();
481 if (customCursorSize.width() > maxOhosCustomCursorSize.width()
482 || customCursorSize.height() > maxOhosCustomCursorSize.height()) {
484 "%s: can't set %dx%d custom cursor, OHOS max custom cursor size is %dx%d",
485 Q_FUNC_INFO, customCursorSize.width(), customCursorSize.height(),
486 maxOhosCustomCursorSize.width(), maxOhosCustomCursorSize.height());
490 auto convertedImage = customCursorImage.convertToFormat(QImage::Format_RGBA8888);
492 if (m_jsScopeData->isWindowClosing()) {
496 auto windowId = m_jsScopeData->jsWindowRef->jsObject().get<QNapi::Number>(
"getWindowProperties().id");
498 auto jsCursor = QNapi::makeObject(
501 {
"pixelMap", makeOhosNapiPixelMapFromQImage(jsState, convertedImage)},
502 {
"focusX", hotSpot.x()},
503 {
"focusY", hotSpot.y()},
506 auto jsCursorConfig = QNapi::makeObject(
509 {
"followSystem",
false},
512 jsState.evalToPromiseOrRejectOnThrow(
513 "@ohos.multimodalInput.pointer.setCustomCursor(*)", {windowId, jsCursor, jsCursorConfig})
514 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setCustomCursor()"))
515 .onFinally(
std::move(taskPromise).makeChained(Q_FUNC_INFO));
523 if (m_jsScopeData->isWindowClosing())
525 auto windowId = m_jsScopeData->jsWindowRef->jsObject().get<QNapi::Number>(
"getWindowProperties().id");
528 "@ohos.multimodalInput.pointer.setPointerStyleSync(*)",
529 {windowId, jsState.mapOhosEnumToJs(convertToOhosCursor(cursor.shape()))});
536 return QtOhos::evalInJsThread(
538 if (m_jsScopeData->isWindowClosing())
539 return QArkUi::WindowProperties {};
540 return getWindowPropertiesFromJsWindow(m_jsScopeData->jsWindowRef->jsObject());
550 m_jsScopeData->windowCallbackReceiver = QtOhos::moveToSharedPtr(WindowCallbacks {
551 .onWindowEvent = makeQtThreadWindowCallbackDelegate(&WindowCallbacks::onWindowEvent, sharedWindowCallbackReceiver),
552 .onWindowStatusChange = makeQtThreadWindowCallbackDelegate(&WindowCallbacks::onWindowStatusChange, sharedWindowCallbackReceiver),
553 .onWindowVisibilityChange = makeQtThreadWindowCallbackDelegate(&WindowCallbacks::onWindowVisibilityChange, sharedWindowCallbackReceiver),
554 .onTouchOutside = makeQtThreadWindowCallbackDelegate(&WindowCallbacks::onTouchOutside, sharedWindowCallbackReceiver),
555 .onAvoidAreaChange = makeQtThreadWindowCallbackDelegate(&WindowCallbacks::onAvoidAreaChange, sharedWindowCallbackReceiver),
556 .onWindowRectChange = makeCompressingQtThreadWindowCallbackDelegate(&WindowCallbacks::onWindowRectChange, sharedWindowCallbackReceiver),
557 .onWindowRectChangeInGlobalDisplay = makeCompressingQtThreadWindowCallbackDelegate(&WindowCallbacks::onWindowRectChangeInGlobalDisplay, sharedWindowCallbackReceiver),
558 .onWindowDisplayIdChange = makeQtThreadWindowCallbackDelegate(&WindowCallbacks::onWindowDisplayIdChange, sharedWindowCallbackReceiver),
563 m_qtWindowCallbacksReceiverHandle = sharedWindowCallbackReceiver;
567 QObject *contextObject, QOhosConsumer<std::vector<NonClientAreaMouseEvent>> mouseEventBatchConsumer)
569 auto qtConsumer = QtOhos::moveToSharedPtr(std::move(mouseEventBatchConsumer));
570 auto weakQtConsumer = QtOhos::makeWeakPtr(qtConsumer);
573 QtOhos::QObjectThreadSafeRef(contextObject),
574 [weakQtConsumer](std::vector<NonClientAreaMouseEvent> &&batch) {
575 auto qtConsumer = weakQtConsumer.lock();
577 (*qtConsumer)(
std::move(batch));
581 m_jsScopeData->nonClientAreaMouseEventConsumer = std::move(jsConsumer);
585 m_qtNonClientAreaMouseWindowCallbackReceiverHandle = qtConsumer;
589 QObject *contextObject, QOhosConsumer<std::vector<NonClientAreaTouchEvent>> touchEventBatchConsumer)
591 auto qtConsumer = QtOhos::moveToSharedPtr(std::move(touchEventBatchConsumer));
592 auto weakQtConsumer = QtOhos::makeWeakPtr(qtConsumer);
595 QtOhos::QObjectThreadSafeRef(contextObject),
596 [weakQtConsumer](std::vector<NonClientAreaTouchEvent> &&batch) {
597 auto qtConsumer = weakQtConsumer.lock();
599 (*qtConsumer)(
std::move(batch));
603 m_jsScopeData->nonClientAreaTouchEventConsumer = std::move(jsConsumer);
607 m_qtNonClientAreaTouchWindowCallbackReceiverHandle = qtConsumer;
617 qCDebug(QtForOhos,
"%s", Q_FUNC_INFO);
619 if (m_jsScopeData->isWindowClosing()) {
623 auto promise = m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"raiseToAppTop()");
624 promise.onFinally(
std::move(taskPromise).makeChained(Q_FUNC_INFO));
631 qCDebug(QtForOhos,
"%s", Q_FUNC_INFO);
633 if (m_jsScopeData->isWindowClosing()) {
638 std::vector<std::pair<std::string, QNapi::ValueWrapper>> jsOptionsProps;
640 if (options.focusOnShow.has_value())
641 jsOptionsProps.emplace_back(
"focusOnShow", options.focusOnShow.value());
643 std::vector<QNapi::ValueWrapper> showWindowArgs;
644 constexpr bool brokenHandlingOfEmptyOptionsParamInOhos =
true;
645 if (!(jsOptionsProps.empty() && brokenHandlingOfEmptyOptionsParamInOhos))
646 showWindowArgs.push_back(QNapi::makeObject(jsState.env(), jsOptionsProps));
648 auto promise = m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"showWindow(*)", showWindowArgs);
649 promise.onFinally(
std::move(taskPromise).makeChained(Q_FUNC_INFO));
656 qCDebug(QtForOhos,
"%s", Q_FUNC_INFO);
658 if (m_jsScopeData->isWindowClosing()) {
663 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"recover()")
664 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"recover()"))
665 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
672 qCDebug(QtForOhos,
"%s", Q_FUNC_INFO);
674 if (m_jsScopeData->isWindowClosing()) {
678 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"restore()").onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
685 qCDebug(QtForOhos,
"%s", Q_FUNC_INFO);
687 if (m_jsScopeData->isWindowClosing()) {
691 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"minimize()").onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
698 qCDebug(QtForOhos,
"%s", Q_FUNC_INFO);
701 qCWarning(QtForOhos(),
"%s: Maximize is currently supported only on main windows", Q_FUNC_INFO);
706 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<> taskPromise) {
707 if (m_jsScopeData->isWindowClosing()) {
712 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
713 "maximize(*)", {jsState.mapOhosEnumToJs(maximizePresentation)})
714 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"maximize()"))
715 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
722 qCDebug(QtForOhos,
"%s: %s", Q_FUNC_INFO, isLayoutFullScreen ?
"true" :
"false");
725 if (m_jsScopeData->isWindowClosing()) {
729 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
730 "setWindowLayoutFullScreen(*)", {isLayoutFullScreen})
731 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setWindowLayoutFullScreen()"))
732 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
739 qCDebug(QtForOhos) << Q_FUNC_INFO << names;
741 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<> taskPromise) {
742 if (m_jsScopeData->isWindowClosing()) {
746 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
747 "setWindowSystemBarEnable(*)",
748 {QNapi::makeArray(jsState.env(), names, std::mem_fn(&QString::toStdString))})
749 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setWindowSystemBarEnable()"))
750 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
757 qCDebug(QtForOhos,
"%s", Q_FUNC_INFO);
760 if (m_jsScopeData->isWindowClosing()) {
765 m_jsScopeData->qAbilityPeer->qAbility().evalToPromiseOrRejectOnThrow(
"context.showAbility()")
766 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"showAbility()"))
767 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
774 qCDebug(QtForOhos,
"%s", Q_FUNC_INFO);
776 return QtOhos::evalInJsThreadWithPromise<
bool>([&](
QtOhos::
JsState &, QOhosTaskPromise<
bool> evalPromise) {
777 if (m_jsScopeData->isWindowClosing()) {
782 auto thenCatchPromises =
std::move(evalPromise).makeThenCatchBranches(Q_FUNC_INFO);
783 m_jsScopeData->qAbilityPeer->qAbility().evalToPromiseOrRejectOnThrow(
"context.hideAbility()")
785 [thenPromise = std::move(thenCatchPromises.first)](
const QtOhos::CallbackInfo &) {
789 [catchPromise = std::move(thenCatchPromises.second)](
const QtOhos::CallbackInfo &cbInfo) {
790 QtOhos::logJsCallbackError(cbInfo,
"got error from hideAbility()");
799 return QtOhos::evalInJsThread(
801 if (m_jsScopeData->isWindowClosing())
803 return m_jsScopeData->jsWindowRef->eval<QNapi::Boolean>(
"getImmersiveModeEnabledState()").Value();
810 qCDebug(QtForOhos,
"%s: %s", Q_FUNC_INFO, privacyMode ?
"true" :
"false");
813 if (m_jsScopeData->isWindowClosing()) {
818 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"setWindowPrivacyMode(*)", {privacyMode})
819 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setPrivacyMode()"))
820 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
827 qCDebug(QtForOhos,
"%s: %s", Q_FUNC_INFO, focusable ?
"true" :
"false");
829 if (m_jsScopeData->isWindowClosing()) {
834 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"setWindowFocusable(*)", {focusable})
835 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setWindowFocusable()"))
836 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
843 qCDebug(QtForOhos,
"%s: %s", Q_FUNC_INFO, touchable ?
"true" :
"false");
845 if (m_jsScopeData->isWindowClosing()) {
850 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"setWindowTouchable(*)", {touchable})
851 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setWindowTouchable()"))
852 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
860 QtForOhos,
"%s: (%d x %d)-(%d x %d)", Q_FUNC_INFO, minSize.width(), minSize.height(),
861 maxSize.width(), maxSize.height());
863 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<> taskPromise) {
864 if (m_jsScopeData->isWindowClosing()) {
868 auto windowLimits = QNapi::makeObject(
871 {
"minWidth", minSize.width()},
872 {
"minHeight", minSize.height()},
873 {
"maxWidth", maxSize.width()},
874 {
"maxHeight", maxSize.height()},
877 std::vector<QNapi::ValueWrapper> setWindowLimitsArgs = {windowLimits};
879 constexpr bool isForcible =
true;
880 setWindowLimitsArgs.push_back(isForcible);
882 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
883 "setWindowLimits(*)", setWindowLimitsArgs)
884 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setWindowLimits()"))
885 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
892 qCDebug(QtForOhos,
"%s", Q_FUNC_INFO);
893 return QtOhos::evalInJsThread(
895 if (m_jsScopeData->isWindowClosing())
896 return WindowLimits {};
897 auto windowLimitsObject = m_jsScopeData->jsWindowRef->eval<QNapi::Object>(
"getWindowLimits()");
898 return WindowLimits {
899 .minWidth = getOptionalNumberPropAsOptionalDouble(windowLimitsObject,
"minWidth"),
900 .minHeight = getOptionalNumberPropAsOptionalDouble(windowLimitsObject,
"minHeight"),
901 .maxWidth = getOptionalNumberPropAsOptionalDouble(windowLimitsObject,
"maxWidth"),
902 .maxHeight = getOptionalNumberPropAsOptionalDouble(windowLimitsObject,
"maxHeight"),
910 qCDebug(QtForOhos,
"%s: %d", Q_FUNC_INFO, avoidAreaType);
911 return QtOhos::evalInJsThread(
913 if (m_jsScopeData->isWindowClosing())
915 auto avoidAreaObject = m_jsScopeData->jsWindowRef->eval<QNapi::Object>(
916 "getWindowAvoidArea(*)", {jsState.mapOhosEnumToJs(avoidAreaType)});
917 return mapAvoidAreaFromJs(avoidAreaObject);
923 const WindowMask &windowMask,
const std::optional<QSize> &ohosMaskSizeOverride)
928 auto ohosMaskSize = ohosMaskSizeOverride.has_value()
929 ? ohosMaskSizeOverride.value()
932 if (ohosMaskSize.isEmpty()) {
933 const auto *maskSrcSizeMsg = ohosMaskSizeOverride.has_value()
934 ?
"overridden mask source"
936 if (ohosMaskSizeOverride.has_value()) {
938 "%s failed - %s size is 0x0", maskSrcSizeMsg,
939 "QOhosWindowProxy::setWindowMask");
945 if (m_jsScopeData->isWindowClosing()) {
949 auto *env = jsState.env();
951 QNapi::Array maskRowsArray = QNapi::Array::New(env, ohosMaskSize.height());
952 const int defaultValue = windowMask.windowMaskRegion.isEmpty() ? 1 : 0;
954 for (
int rowIndex = 0; rowIndex < ohosMaskSize.height(); ++rowIndex) {
955 auto arr = QNapi::Array::New(env, ohosMaskSize.width());
956 arr.fill(defaultValue);
957 maskRowsArray[rowIndex] = arr;
960 auto valueToSet = QNapi::Number::New(env, 1);
961 for (
const auto &rect: windowMask.windowMaskRegion) {
962 auto top = qBound(0, rect.top(), ohosMaskSize.height() - 1);
963 auto bottom = qBound(0, rect.bottom(), ohosMaskSize.height() - 1);
964 auto left = qBound(0, rect.left(), ohosMaskSize.width() - 1);
965 auto right = qBound(0, rect.right(), ohosMaskSize.width() - 1);
967 for (
auto rowIndex = top; rowIndex <= bottom; ++rowIndex) {
968 auto row = maskRowsArray.Get(rowIndex).As<QNapi::Array>();
969 for (
auto columnIndex = left; columnIndex <= right; ++columnIndex)
970 row[columnIndex] = valueToSet;
974 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"setWindowMask(*)", {maskRowsArray})
975 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setWindowMask()"))
976 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
988 if (m_jsScopeData->isWindowClosing()) {
992 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"setSubWindowModal(*)", {
false})
993 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setSubWindowModal()"))
994 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
1006 "%s: APPLICATION_MODALITY option can be used only on devices in the freeform window state - skipping",
1012 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<> taskPromise) {
1013 if (m_jsScopeData->isWindowClosing()) {
1017 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
1018 "setSubWindowModal(*)", {
true, jsState.mapOhosEnumToJs(modalityType)})
1019 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setSubWindowModal()"))
1020 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
1029 if (m_jsScopeData->isWindowClosing()) {
1034 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"setWindowTitle(*)", {title.toStdString()})
1035 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
1043 "%s: isMaximizeVisible:%s, isMinimizeVisible:%s, isCloseVisible:%s",
1044 Q_FUNC_INFO, maximizeVisible ?
"true" :
"false", minimizeVisible ?
"true":
"false",
1045 closeVisible ?
"true" :
"false");
1052 if (m_jsScopeData->isWindowClosing())
1055 constexpr auto capabilityNotSupportedErrorCode = 801;
1057 jsState, capabilityNotSupportedErrorCode,
"setWindowTitleButtonVisible()",
1059 m_jsScopeData->jsWindowRef->eval(
1060 "setWindowTitleButtonVisible(*)",
1061 {maximizeVisible, minimizeVisible, closeVisible});
1073 qOhosPrintfWarning(
"%s: can be used only on 2-in-1 devices or tablets in PC mode - skipping", Q_FUNC_INFO);
1077 qOhosPrintfDebug(
"%s: topMost: %s", Q_FUNC_INFO, topmost ?
"true" :
"false");
1081 if (m_jsScopeData->isWindowClosing()) {
1086 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"setWindowTopmost(*)", {topmost})
1087 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
1095 if (m_jsScopeData->isWindowClosing())
1098 m_jsScopeData->jsWindowRef->eval(
"setWindowDecorVisible(*)", {visible});
1106 qOhosPrintfWarning(
"%s: can be used only on 2-in-1 devices or tablets in PC mode - skipping", Q_FUNC_INFO);
1111 if (m_jsScopeData->isWindowClosing())
1114 m_jsScopeData->jsWindowRef->eval(
"setWindowTitleMoveEnabled(*)", {enabled});
1122 qOhosPrintfWarning(
"%s: can be used only on 2-in-1 devices or tablets - skipping", Q_FUNC_INFO);
1130 m_jsScopeData->jsWindowRef->eval(
"setWindowShadowRadius(*)", {radius});
1142 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"setWindowCornerRadius(*)", {radius})
1143 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
1150 return QtOhos::evalInJsThreadWithPromise<
bool>(
1151 [&](
QtOhos::
JsState &, QOhosTaskPromise<
bool> evalPromise) {
1152 if (m_jsScopeData->isWindowClosing()) {
1157 if (!optQUiAbilityPeer) {
1162 auto thenCatchPromises =
std::move(evalPromise).makeThenCatchBranches(Q_FUNC_INFO);
1163 optQUiAbilityPeer->windowStage()
1164 .evalToPromiseOrRejectOnThrow(
"isWindowRectAutoSave()")
1166 [thenPromise = std::move(thenCatchPromises.first)](
const QtOhos::
CallbackInfo &cbInfo) {
1167 bool windowRectAutoSaveEnabled = cbInfo.getFirstArg<QNapi::Boolean>(Q_FUNC_INFO);
1168 thenPromise(windowRectAutoSaveEnabled);
1171 [catchPromise = std::move(thenCatchPromises.second)](
const QtOhos::
CallbackInfo &cbInfo) {
1172 QtOhos::logJsCallbackError(cbInfo,
"isWindowRectAutoSave()");
1173 catchPromise(
false);
1186 if (m_jsScopeData->isWindowClosing()) {
1190 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"setFollowParentMultiScreenPolicy(*)", {enabled})
1191 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setFollowParentMultiScreenPolicy()"))
1192 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
1203 if (m_jsScopeData->isWindowClosing()) {
1208 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"setWindowKeepScreenOn(*)", {keepScreenOn})
1209 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setWindowKeepScreenOn()"))
1210 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
1217 qCDebug(QtForOhos,
"%s: %s", Q_FUNC_INFO, mapEnumsToLogString(supportedWindowModes).c_str());
1220 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<> taskPromise) {
1221 if (m_jsScopeData->isWindowClosing()) {
1226 if (!qUiAbilityPeer) {
1231 auto jsSupportedWindowModes = QNapi::makeArray(
1232 jsState.env(), supportedWindowModes,
1234 return jsState.mapOhosEnumToJs(mode);
1237 qUiAbilityPeer->windowStage().evalToPromiseOrRejectOnThrow(
"setSupportedWindowModes(*)", {jsSupportedWindowModes})
1238 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setSupportedWindowModes()"))
1239 .onFinally(
std::move(taskPromise).makeChained(Q_FUNC_INFO));
1246 constexpr bool isSaveBySpecifiedFlag =
true;
1250 if (m_jsScopeData->isWindowClosing()) {
1255 if (!optQUiAbilityPeer) {
1260 optQUiAbilityPeer->windowStage()
1261 .evalToPromiseOrRejectOnThrow(
"setWindowRectAutoSave(*)", {enabled, isSaveBySpecifiedFlag})
1262 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"setWindowRectAutoSave()"))
1263 .onFinally(
std::move(taskPromise).makeChained(Q_FUNC_INFO));
1269 std::function<
void()> handler,
bool handlerReturnValue)
1271 m_subWindowCloseRegistrationHandle =
1272 registerSubWindowCloseHandler(std::move(handler), handlerReturnValue);
1277 m_subWindowCloseRegistrationHandle.reset();
1280std::shared_ptr<
void> QOhosWindowProxy::registerSubWindowCloseHandler(
1281 std::function<
void()> handler,
bool handlerReturnValue)
1283 auto sharedHandler = QtOhos::moveToSharedPtr(std::move(handler));
1284 auto jsWindowRegistrationHandle =
QtOhos::evalInJsThread(
1286 return QtOhos::makeProxyWithJsThreadDeleter(
1287 m_jsScopeData->registerSubWindowCloseHandler(
1289 [weakHandler = QtOhos::makeWeakPtr(sharedHandler), handlerReturnValue]() {
1290 QtOhos::invokeInQtThread(
1292 auto sharedHandler = weakHandler.lock();
1296 return handlerReturnValue;
1301 return QtOhos::moveToSharedPtr(
1302 std::make_tuple(sharedHandler, jsWindowRegistrationHandle));
1305std::shared_ptr<QOhosWindowProxy>
1308 return QtOhos::evalInJsThreadWithPromise<
std::shared_ptr<QOhosWindowProxy>>(
1309 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<std::shared_ptr<QOhosWindowProxy>> evalPromise) {
1310 auto sharedEvalPromise = QtOhos::moveToSharedPtr(std::move(evalPromise).makeChained(Q_FUNC_INFO));
1311 makeWindowProxyDataForExistingMainWindowInJsThread(
1315 (*sharedEvalPromise)(QOhosWindowProxy::create(jsState,
std::move(windowProxyData)));
1321std::shared_ptr<QOhosWindowProxy>
1324 return QtOhos::evalInJsThreadWithPromise<
std::shared_ptr<QOhosWindowProxy>>(
1325 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<std::shared_ptr<QOhosWindowProxy>> evalPromise) {
1326 auto sharedEvalPromise = QtOhos::moveToSharedPtr(std::move(evalPromise).makeChained(Q_FUNC_INFO));
1327 makeWindowProxyDataForFloatWindowInJsThread(
1328 jsState, createInfo,
1330 (*sharedEvalPromise)(QOhosWindowProxy::create(jsState,
std::move(windowProxyData)));
1336std::shared_ptr<QOhosWindowProxy>
1339 return QtOhos::evalInJsThreadWithPromise<
std::shared_ptr<QOhosWindowProxy>>(
1340 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<std::shared_ptr<QOhosWindowProxy>> evalPromise) {
1341 auto sharedEvalPromise = QtOhos::moveToSharedPtr(std::move(evalPromise).makeChained(Q_FUNC_INFO));
1342 makeWindowProxyDataForMainWindowInJsThread(
1346 (*sharedEvalPromise)(QOhosWindowProxy::create(jsState,
std::move(windowProxyData)));
1354 return m_nodeXComponent;
1359 return m_qAbilityInstanceId;
1362std::shared_ptr<QOhosWindowProxy>
1365 return QtOhos::evalInJsThreadWithPromise<
std::shared_ptr<QOhosWindowProxy>>(
1366 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<std::shared_ptr<QOhosWindowProxy>> evalPromise) {
1367 auto sharedEvalPromise = QtOhos::moveToSharedPtr(std::move(evalPromise).makeChained(Q_FUNC_INFO));
1368 auto proxyDataConsumer =
1370 (*sharedEvalPromise)(QOhosWindowProxy::create(jsState,
std::move(windowProxyData)));
1375 if (m_jsScopeData->isWindowClosing()) {
1376 makeWindowProxyDataForSubWindowInJsThread(
1377 jsState, createInfo,
std::move(proxyDataConsumer));
1379 makeWindowProxyDataForSubWindowInJsThread(
1380 jsState, m_jsScopeData->jsWindowRef->jsObject(), createInfo,
1381 std::move(proxyDataConsumer));
1387QOhosWindowProxy::JsScopeData::JsScopeData(
1388 WindowProxyType windowProxyType, QNapi::Reference<QNapi::Object> jsWindow,
1389 std::shared_ptr<
void> optKeepAliveData,
1391 QtOhos::QObjectThreadSafeRef owningQWindowRef)
1392 : windowProxyType(windowProxyType)
1393 , windowCallbackReceiver(
nullptr)
1394 , windowDestroyedFromSystem(
false)
1395 , optKeepAliveData(optKeepAliveData)
1396 , qAbilityPeer(qAbilityPeer)
1397 , m_windowFrameMouseFilterHandle(
1398 QArkUi::registerMouseEventsConsumer(
1399 getWindowPropertiesFromJsWindow(jsWindow.Value()).id,
1400 [
this](
const QArkUi::MouseEvent &event) {
1401 onMouseEventFromArkUi(event);
1403 , m_windowFrameTouchFilterHandle(
1404 QArkUi::registerTouchEventsConsumer(
1405 getWindowPropertiesFromJsWindow(jsWindow.Value()).id,
1406 [
this](
const QArkUi::TouchEvent &event) {
1407 onTouchEventFromArkUi(event);
1410 std::make_shared<QArkUi::JsWindowRef>(
1411 qAbilityPeer->instanceId(),
1412 getWindowPropertiesFromJsWindow(jsWindow.Value()).id,
1418QOhosWindowProxy::JsScopeData::~JsScopeData()
1420 if (isWindowClosingFromSystem(jsWindowRef->jsObject(), windowProxyType, qAbilityPeer)) {
1421 windowDestroyedFromSystem =
true;
1425 QtOhos::JsWindowsTracker::tagWindowAsClosing(jsWindowRef->jsObject(),
"QOhosWindowProxy::JsScopeData destructor");
1431 windowDestroyedFromSystem =
true;
1433 "Attempting to terminate qAbility with instance id: %s",
1434 jsWindowRef->owningQAbilityInstanceId().c_str());
1435 qAbilityPeer->qAbility().eval(
"context.terminateSelf()");
1436 }
else if (!windowDestroyedFromSystem) {
1439 jsWindowRef->eval(
"destroyWindow()");
1443std::shared_ptr<
void> QOhosWindowProxy::JsScopeData::registerEventListener(
1444 const std::string &eventName,
1446 QFlags<EventHandlerFlagBits> eventHandlerFlags)
1449 std::weak_ptr<JsScopeData> weakSelf = shared_from_this();
1450 bool ignoreWhenAbilityIsTerminating = !eventHandlerFlags.testFlag(EventHandlerFlagBits::allowCallWhenAbilityIsTerminating);
1452 return registerQOhosOnOffMethodsBasedEventHandler(
1453 jsWindowRef->jsObject(), eventName,
1454 [weakSelf, handleFunction, eventName, ignoreWhenAbilityIsTerminating](
const QtOhos::
CallbackInfo &cbInfo) {
1455 auto self = weakSelf.lock();
1456 if (Q_UNLIKELY(!self)) {
1458 "callback '%s' called for destroyed QOhosWindowProxy::JsScopeData, ignoring",
1463 if (self->isWindowClosing() && ignoreWhenAbilityIsTerminating) {
1465 "QOhosWindowProxy: Received callback for event '%s' during termination of the related QAbility.",
1470 if (Q_UNLIKELY(self->windowDestroyedFromSystem)) {
1472 "QOhosWindowProxy: Received callback for event '%s' after WINDOW_DESTROYED",
1477 ((*self).*handleFunction)(cbInfo);
1480 .optEventSourceAliveCheckFunc = {},
1483 .optOnCallExceptionHandler = [&](
const Napi::Error &error) {
1484 constexpr std::uint32_t capabilityNotSupportedErrorCode = 801;
1485 constexpr std::uint32_t windowStateIsAbnormalErrorCode = 1300002;
1487 const QSet<std::uint32_t> ignorableErrorCodes = {
1488 capabilityNotSupportedErrorCode,
1489 windowStateIsAbnormalErrorCode,
1492 auto errorCode = QtOhos::tryGetCodeFromJsBusinessError(error);
1494 auto ignorableError =
1495 eventHandlerFlags.testFlag(EventHandlerFlagBits::allowEventHandlerRegistrationFailure)
1496 && errorCode.has_value()
1497 && ignorableErrorCodes.contains(errorCode.value());
1499 if (!ignorableError)
1503 "%s: Ignored error %u while registering for window event '%s'",
1504 Q_FUNC_INFO, errorCode.value(), eventName.c_str());
1509std::shared_ptr<
void> QOhosWindowProxy::JsScopeData::registerSubWindowCloseHandler(
1512 auto weakSelf = QtOhos::makeWeakPtr(shared_from_this());
1513 return registerQOhosOnOffMethodsBasedEventHandler(
1514 jsWindowRef->jsObject(),
"subWindowClose",
1516 bool deferClose = handler();
1517 auto self = weakSelf.lock();
1518 if (self && !deferClose)
1519 QtOhos::JsWindowsTracker::tagWindowAsClosing(self->jsWindowRef->jsObject(),
"subWindowClose => false");
1520 return QNapi::Boolean::New(cbInfo.Env(), deferClose);
1524void QOhosWindowProxy::JsScopeData::handleWindowEventCallback(
const QtOhos::
CallbackInfo &cbInfo)
1526 auto eventType = cbInfo.getFirstArg<QNapi::Number>(Q_FUNC_INFO);
1531 event
.type = cbInfo
.jsState().mapOhosEnumFromJs<WindowEventType>(eventType);
1532 }
catch (
const Napi::Error &err) {
1534 "Error converting WindowEventType to known value: %s. Event will be ignored.", err.what());
1540 "Received WindowEvent for window when it's closing. WindowEventType: %d",
1548void QOhosWindowProxy::JsScopeData::handleWindowStatusCallback(
const QtOhos::
CallbackInfo &cbInfo)
1550 auto windowStatusType = cbInfo.getFirstArg<QNapi::Number>(Q_FUNC_INFO);
1551 if (windowCallbackReceiver !=
nullptr) {
1552 windowCallbackReceiver->onWindowStatusChange(
1554 .type = cbInfo.jsState().mapOhosEnumFromJs<WindowStatusType>(windowStatusType),
1559void QOhosWindowProxy::JsScopeData::handleWindowVisibilityCallback(
const QtOhos::
CallbackInfo &cbInfo)
1561 auto windowVisibility = cbInfo.getFirstArg<QNapi::Boolean>(Q_FUNC_INFO);
1562 if (windowCallbackReceiver !=
nullptr)
1563 windowCallbackReceiver->onWindowVisibilityChange(windowVisibility);
1566void QOhosWindowProxy::JsScopeData::handleWindowTouchOutsideCallback(
const QtOhos::
CallbackInfo &)
1568 if (windowCallbackReceiver !=
nullptr)
1569 windowCallbackReceiver->onTouchOutside();
1572void QOhosWindowProxy::JsScopeData::handleAvoidAreaChangeCallback(
const QtOhos::
CallbackInfo &cbInfo)
1574 if (windowCallbackReceiver !=
nullptr) {
1575 auto callbackArg = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
1576 windowCallbackReceiver->onAvoidAreaChange(
1577 cbInfo.jsState().mapOhosEnumFromJs<AvoidAreaType>(callbackArg.get<QNapi::Number>(
"type")),
1578 mapAvoidAreaFromJs(callbackArg.get<QNapi::Object>(
"area")));
1582void QOhosWindowProxy::JsScopeData::handleWindowRectChangeCallback(
const QtOhos::
CallbackInfo &cbInfo)
1584 auto rectChangeOptionsObjectArg = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
1585 auto rectChangeOptions = RectChangeOptions {
1586 .rect = ohosWindowRectToQRect(rectChangeOptionsObjectArg.get<QNapi::Object>(
"rect")),
1587 .reason = cbInfo.jsState().mapOhosEnumFromJs<RectChangeReason>(rectChangeOptionsObjectArg.get<QNapi::Number>(
"reason")),
1590 if (windowCallbackReceiver !=
nullptr)
1591 windowCallbackReceiver->onWindowRectChange(rectChangeOptions);
1594void QOhosWindowProxy::JsScopeData::handleWindowRectChangeInGlobalDisplayCallback(
const QtOhos::
CallbackInfo &cbInfo)
1596 auto rectChangeOptionsObjectArg = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
1597 auto rectChangeOptions = RectChangeOptions {
1598 .rect = ohosWindowRectToQRect(rectChangeOptionsObjectArg.get<QNapi::Object>(
"rect")),
1599 .reason = cbInfo.jsState().mapOhosEnumFromJs<RectChangeReason>(rectChangeOptionsObjectArg.get<QNapi::Number>(
"reason")),
1602 if (windowCallbackReceiver !=
nullptr)
1603 windowCallbackReceiver->onWindowRectChangeInGlobalDisplay(rectChangeOptions);
1606void QOhosWindowProxy::JsScopeData::handleWindowDisplayIdChangeCallback(
const QtOhos::
CallbackInfo &cbInfo)
1608 auto displayIdNumber = cbInfo.getFirstArg<QNapi::Number>(Q_FUNC_INFO);
1611 if (windowCallbackReceiver !=
nullptr)
1612 windowCallbackReceiver->onWindowDisplayIdChange(displayId);
1618 QtOhos::JsWindowsTracker::tagWindowAsClosing(jsWindowRef->jsObject(),
"WINDOW_DESTROYED");
1619 windowDestroyedFromSystem =
true;
1622 if (windowCallbackReceiver !=
nullptr)
1623 windowCallbackReceiver->onWindowEvent(windowEvent);
1626bool QOhosWindowProxy::JsScopeData::isWindowClosing()
const
1628 return isWindowClosingFromSystem(jsWindowRef->jsObject(), windowProxyType, qAbilityPeer);
1631void QOhosWindowProxy::JsScopeData::onMouseEventFromArkUi(
const QArkUi::
MouseEvent &event)
1633 if (nonClientAreaMouseEventConsumer ==
nullptr)
1636 auto optAction = tryMapMouseEventActionToNonClientAreaEventType(event.action);
1637 if (!optAction.has_value())
1641 if (!optWindowProperties.has_value()) {
1643 "%s: Failed to retrieve window properties for js window: %f. Ignoring event.",
1644 Q_FUNC_INFO, event.jsWindowId.value());
1648 const auto &windowProperties = optWindowProperties.value();
1649 if (!isPointInNonClientArea(event.displayPosition, windowProperties))
1652 auto windowOrigin = windowProperties.windowRect.topLeft() + windowProperties.drawableRect.topLeft();
1654 .timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(event.actionTime),
1655 .action = optAction.value(),
1656 .button = tryMapMouseEventButtonToQt(event.button).value_or(Qt::NoButton),
1657 .displayPosition = event.displayPosition,
1658 .localPosition = event.displayPosition - windowOrigin,
1659 .globalPosition = event.globalPosition,
1662 nonClientAreaMouseEventConsumer(nonClientAreaMouseEvent);
1665void QOhosWindowProxy::JsScopeData::onTouchEventFromArkUi(
const QArkUi::
TouchEvent &event)
1667 if (nonClientAreaTouchEventConsumer ==
nullptr)
1670 auto optAction = tryMapTouchEventActionToNonClientAreaEventType(event.action);
1671 if (!optAction.has_value())
1675 if (!optWindowProperties.has_value()) {
1677 "%s: Failed to retrieve window properties for js window: %f. Ignoring event.",
1678 Q_FUNC_INFO, event.jsWindowId.value());
1682 const auto &windowProperties = optWindowProperties.value();
1683 if (!isPointInNonClientArea(event.displayPosition, windowProperties))
1686 auto windowOrigin = windowProperties.windowRect.topLeft() + windowProperties.drawableRect.topLeft();
1689 .timestamp =
std::chrono::duration_cast<
std::chrono::milliseconds>(event.actionTime),
1690 .action = optAction.value(),
1691 .displayPosition = event.displayPosition,
1692 .localPosition = event.displayPosition - windowOrigin,
1693 .globalPosition = event.globalPosition,
1696 nonClientAreaTouchEventConsumer(nonClientAreaTouchEvent);
1701 return QtOhos::evalInJsThreadWithPromise<QPixmap>(
1702 [&](QtOhos::JsState &, QOhosTaskPromise<QPixmap> evalPromise) {
1703 auto thenCatchPromises = std::move(evalPromise).makeThenCatchBranches(Q_FUNC_INFO);
1704 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"snapshot()")
1706 [thenPromise = std::move(thenCatchPromises.first)](
const QtOhos::CallbackInfo &cbInfo) {
1707 auto napiPixmap = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
1709 ::OH_PixelmapNative *pixelMapNativePtr;
1710 QArkUi::callArkUiOrFailOnErrorResult(
1711 Q_OHOS_NAMED_FUNC(::OH_PixelmapNative_ConvertPixelmapNativeFromNapi),
1712 cbInfo.Env(), napiPixmap, &pixelMapNativePtr);
1713 auto pixelMap = wrapOhosNativePixelMapPtr(pixelMapNativePtr);
1716 QPixmap::fromImage(createQImageFromNativePixelMap(pixelMap.get())));
1719 [catchPromise = std::move(thenCatchPromises.second)](
const QtOhos::CallbackInfo &cbInfo) {
1720 QtOhos::logJsCallbackError(cbInfo,
"Got error from snapshot()");
1721 catchPromise(QPixmap());
1731 return QtOhos::evalInJsThreadWithPromise<QList<QRect>>(
1732 [&](QtOhos::JsState &jsState, QOhosTaskPromise<QList<QRect>> evalPromise) {
1733 if (m_jsScopeData->isWindowClosing()) {
1734 evalPromise(QList<QRect>());
1738 auto optWindowDisplay = qAndThen(
1739 getWindowPropertiesFromJsWindow(
1740 m_jsScopeData->jsWindowRef->jsObject()).displayId,
1741 [&](QOhosDisplayInfo::JsDisplayId id) {
1742 return QOhosDisplayInfo::tryGetDisplayById(jsState, id);
1744 auto display = optWindowDisplay.has_value()
1745 ? optWindowDisplay.value()
1746 : jsState.eval<QNapi::Object>(
"@ohos.display.getDefaultDisplaySync()");
1748 auto branches = std::move(evalPromise).makeThenCatchBranches(Q_FUNC_INFO);
1750 .evalToPromiseOrRejectOnThrow(
"getCutoutInfo()")
1752 [thenPromise = std::move(branches.first)](
const QtOhos::CallbackInfo &cbInfo) {
1753 auto cutoutInfo = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
1754 auto cutoutRects = QNapi::getArrayElements<QList<QRect>, QNapi::Object>(
1755 cutoutInfo.get<QNapi::Array>(
"boundingRects"), [](QNapi::Object r) {
1756 return QRect(r.get<QNapi::Number>(
"left"),
1757 r.get<QNapi::Number>(
"top"),
1758 r.get<QNapi::Number>(
"width"),
1759 r.get<QNapi::Number>(
"height"));
1761 thenPromise(cutoutRects);
1764 [catchPromise = std::move(branches.second)](
const QtOhos::CallbackInfo &cbInfo) {
1765 QtOhos::logJsCallbackError(cbInfo,
"getCutoutInfo() failed");
1766 catchPromise(QList<QRect>());
1776 if (m_jsScopeData->isWindowClosing()) {
1781 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"startMoving()")
1782 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"startMoving()"))
1783 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
1795 qCWarning(QtForOhos(),
"%s: enableDrag is not supported on main windows", Q_FUNC_INFO);
1801 if (m_jsScopeData->isWindowClosing()) {
1805 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
"enableDrag(*)", {enable})
1806 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"enableDrag()"))
1807 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
1814 return QtOhos::evalInJsThread(
1816 if (m_jsScopeData->isWindowClosing())
1817 return std::nullopt;
1819 return m_jsScopeData->jsWindowRef->eval<QNapi::Boolean>(
"isFocused()");
1824std::vector<QArkUi::JsWindowId> QOhosWindowProxy::queryWindowIdsByCoordinate(
1825 QOhosDisplayInfo::JsDisplayId displayId,
const QPoint &queryLocation,
std::uint32_t queryLimit)
1827 return QtOhos::evalInJsThreadWithPromise<std::vector<QArkUi::JsWindowId>>(
1828 [&](QtOhos::JsState &jsState,
auto evalPromise) {
1829 auto thenCatchPromises = std::move(evalPromise).makeThenCatchBranches(Q_FUNC_INFO);
1830 jsState.evalToPromiseOrRejectOnThrow(
"@ohos.window.getWindowsByCoordinate(*)", {
1836 .onThen([thenPromise = std::move(thenCatchPromises.first)](
const QtOhos::CallbackInfo &cbInfo) {
1837 auto windowsArray = cbInfo.getFirstArg<QNapi::Array>(Q_FUNC_INFO);
1839 QNapi::getArrayElements<std::vector<QArkUi::JsWindowId>, QNapi::Object>(
1841 [&](QNapi::Object jsWindow) {
1842 return getWindowPropertiesFromJsWindow(jsWindow).id;
1845 .onCatch([catchPromise = std::move(thenCatchPromises.second)](
const QtOhos::CallbackInfo &cbInfo) {
1846 QtOhos::logJsCallbackError(
1847 cbInfo,
"got error from @ohos.window.getWindowsByCoordinate()");
1854std::vector<QArkUi::JsWindowId> QOhosWindowProxy::queryQtManagedWindowIdsByPredicate(
1855 const std::function<
bool(QOhosJsState &,
const QArkUi::JsWindowRef &)> &predicate)
1859 return jsWindowRegistry.queryByPredicate(jsState, predicate);
1868 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<> taskPromise) {
1869 if (m_jsScopeData->isWindowClosing()) {
1874 auto moveConfigurationObject = toNapiObject(jsState.env(), moveConfiguration);
1875 auto moveConfigurationObjectStr = QNapi::toJsonString(moveConfigurationObject);
1878 Q_FUNC_INFO, position.x(), position.y(),
1879 moveConfigurationObjectStr.c_str());
1881 m_jsScopeData->jsWindowRef->evalToPromiseOrRejectOnThrow(
1882 "moveWindowToGlobal(*)", {position.x(), position.y(), moveConfigurationObject})
1883 .onFinally(std::move(taskPromise).makeChained(Q_FUNC_INFO));
1892 :
QtOhos::evalInJsThread(
1896 return qUiAbilityPeer
1897 ? getWindowPropertiesFromJsWindow(qUiAbilityPeer->window()).displayId
1906 [&](
QtOhos::
JsState &jsState, QOhosTaskPromise<> taskPromise) {
1907 if (m_jsScopeData->isWindowClosing() || targetProxy.m_jsScopeData->isWindowClosing()) {
1912 auto srcWindowId = getWindowPropertiesFromJsWindow(m_jsScopeData->jsWindowRef->jsObject()).id;
1913 auto targetWindowId = getWindowPropertiesFromJsWindow(targetProxy.m_jsScopeData->jsWindowRef->jsObject()).id;
1914 jsState.evalToPromiseOrRejectOnThrow(
1915 "@ohos.window.shiftAppWindowFocus(*)", {srcWindowId.value(), targetWindowId.value()})
1916 .onCatch(QtOhos::makeErrorLoggingJsCallback(
"@ohos.window.shiftAppWindowFocus()"))
1917 .onFinally(
std::move(taskPromise).makeChained(Q_FUNC_INFO));
1924 auto window =
std::shared_ptr<QOhosWindowProxy>(
new QOhosWindowProxy(
std::move(data)));
1926 return QtOhos::makeSharedPtrWithAttachedExtraData(
1928 jsWindowRegistry.registerJsWindow(window->m_jsScopeData->jsWindowRef));
bool isWindowPcModeEnabled() const
static QOhosSettings & instance()
void setWindowRectAutoSave(bool enabed)
void setSubWindowModalEnabled(ModalityType ModalityType)
QtOhosQpa::enums::ohos::window::MaximizePresentation MaximizePresentation
void setWindowKeepScreenOn(bool keepScreenOn)
void moveWindowToGlobal(const QPoint &position, const MoveConfiguration &moveConfiguration)
static std::shared_ptr< QOhosWindowProxy > createFloatWindow(const FloatWindowCreateInfo &createInfo)
void setWindowMask(const WindowMask &windowMask, const std::optional< QSize > &ohosMaskSizeOverride={})
QArkUi::WindowProperties getWindowProperties() const
void moveWindowToGlobalOrGlobalDisplay(const QPoint &position, std::optional< QOhosDisplayInfo::JsDisplayId > optDisplayId)
void setSubWindowModalDisabled()
void setWindowPrivacyMode(bool privacyMode)
void resetSubWindowCloseHandler()
QtOhosQpa::enums::ohos::window::WindowEventType WindowEventType
AvoidArea getWindowAvoidArea(AvoidAreaType type) const
void setWindowLayoutFullScreen(bool isLayoutFullScreen)
std::optional< QOhosDisplayInfo::JsDisplayId > tryGetMainWindowJsDisplayId() const
void enableDrag(bool enable)
void setWindowCornerRadius(double radius)
QOhosWindowProxyExistingMainWindowCreateInfo ExistingMainWindowCreateInfo
void setSupportedWindowModes(const std::set< SupportWindowMode > &supportedWindowModes)
void removeStartingWindow()
void setWindowSystemBarEnable(const QStringList &names)
std::shared_ptr< QOhosWindowProxy > createSubWindow(const SubWindowCreateInfo &createInfo)
static std::shared_ptr< QOhosWindowProxy > createForExistingMainWindow(const ExistingMainWindowCreateInfo &createInfo)
void setWindowTouchable(bool touchable)
void setTitle(const QString &title)
void setFollowParentMultiScreenPolicy(bool enabled)
void setWindowTopmost(bool topmost)
std::shared_ptr< QXComponentNode > nodeXComponent() const
void setWindowCallbackReceiver(std::unique_ptr< WindowCallbacks > receiver)
void setSubWindowCloseHandler(std::function< void()> handler, bool handlerReturnValue)
std::optional< bool > isFocused() const
QtOhosQpa::enums::ohos::window::ModalityType ModalityType
void setWindowLimits(const QSize &minSize, const QSize &maxSize)
bool isWindowRectAutoSave() const
void setWindowDecorVisible(bool visible)
void showWindow(const ShowWindowOptions &options=ShowWindowOptions())
void shiftAppWindowFocus(QOhosWindowProxy &targetProxy)
QOhosWindowProxySubWindowCreateInfo SubWindowCreateInfo
void setNonClientAreaMouseWindowCallbackReceiver(QObject *contextObject, QOhosConsumer< std::vector< NonClientAreaMouseEvent > > mouseEventBatchConsumer)
QtOhosQpa::enums::ohos::window::AvoidAreaType AvoidAreaType
void setCustomCursor(const QImage &customCursorImage, const QPoint &hotSpot)
void setWindowTitleMoveEnabled(bool enabled)
void setWindowFocusable(bool focusable)
void setWindowBackgroundColor(const QColor &color)
void setSize(const QSize &size)
QOhosWindowProxyMainWindowCreateInfo MainWindowCreateInfo
void setWindowShadowRadius(double radius)
bool qtIsMainWindow() const
QOhosWindowProxyFloatWindowCreateInfo FloatWindowCreateInfo
void setWindowTitleButtonVisible(bool maximizeVisible, bool minimizeVisible, bool closeVisible)
bool getImmersiveModeEnabledState()
void setPointerStyleSync(const QCursor &cursor)
QList< QRect > getDisplayCutoutRects() const
WindowLimits getWindowLimits() const
std::string qAbilityInstanceId() const
void maximize(MaximizePresentation maximizePresentation)
void setNonClientAreaTouchWindowCallbackReceiver(QObject *contextObject, QOhosConsumer< std::vector< NonClientAreaTouchEvent > > touchEventBatchConsumer)
static std::shared_ptr< QOhosWindowProxy > createMainWindow(const MainWindowCreateInfo &createInfo)
JsState & jsState() const
static std::shared_ptr< QUiAbilityPeer > tryCastFromQAbilityPeerOrNull(std::shared_ptr< QAbilityPeer > qAbilityPeer)
std::optional< WindowProperties > tryGetWindowProperties(JsWindowId jsWindowId)
Combined button and popup list for selecting options.
QRect ohosWindowRectToQRect(const QNapi::Object &ohosWindowRect)
bool isPointInNonClientArea(const QPoint &point, const QArkUi::WindowProperties &windowProperties)
std::optional< QEvent::Type > tryMapMouseEventActionToNonClientAreaEventType(::Input_MouseEventAction action)
std::optional< double > getOptionalNumberPropAsOptionalDouble(const QNapi::Object &object, const std::string &propertyName)
QOhosPointerStyle convertToOhosCursor(Qt::CursorShape shape)
QOhosWindowProxy::AvoidArea mapAvoidAreaFromJs(const QNapi::Object &avoidAreaObject)
std::string mapEnumsToLogString(const EnumsContainer &enums)
std::optional< Qt::MouseButton > tryMapMouseEventButtonToQt(::Input_MouseEventButton button)
bool isWindowClosingFromSystem(QNapi::Object jsWindow, WindowProxyType windowType, std::shared_ptr< QtOhos::QAbilityPeer > abilityPeer)
QtOhosQpa::enums::ohos::multimodalInput::pointer::PointerStyle QOhosPointerStyle
QArkUi::WindowProperties getWindowPropertiesFromJsWindow(QNapi::Object jsWindow)
std::function< void(Args...)> makeQtThreadWindowCallbackDelegate(std::function< void(Args...)> QOhosWindowProxy::WindowCallbacks::*memberPtr, std::shared_ptr< QOhosWindowProxy::WindowCallbacks > qtWindowCallbacks)
std::function< void(T)> makeCompressingQtThreadWindowCallbackDelegate(std::function< void(T)> QOhosWindowProxy::WindowCallbacks::*memberPtr, std::shared_ptr< QOhosWindowProxy::WindowCallbacks > qtWindowCallbacks)
QNapi::Object toNapiObject(napi_env env, const QOhosWindowProxy::MoveConfiguration &moveConfiguration)
std::optional< QEvent::Type > tryMapTouchEventActionToNonClientAreaEventType(::Input_TouchEventAction action)
std::string const char * mapBoolToTrueFalseStr(bool value)
bool runIgnoringJsBusinessError(QOhosJsState &, std::uint32_t suppressedErrorCode, const char *callerContextName, const std::function< void()> &action)
void runInJsThreadAndWait(const std::function< void(JsState &)> &task, std::string callerContextName={})
void invokeInJsThreadAndWaitForContinue(std::function< void(JsState &, QOhosTaskPromise<>)> &&task, std::string callerContextName={})
QXComponent< QXComponentType::Node > QXComponentNode
bool isDisplayMainOrExtended() const
WindowProxyType windowProxyType