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
qohosview.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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
4#include <render/qohosview.h>
5
6#include <QtCore/private/qnapi_p.h>
7#include <QtCore/private/qohoscommon_p.h>
8#include <QtCore/qscopeguard.h>
9#include <QtGui/private/qguiapplication_p.h>
10#include <QtGui/private/qhighdpiscaling_p.h>
11#include <QtGui/private/qwindow_p.h>
12#include <QtGui/qbitmap.h>
13#include <QtGui/qpalette.h>
14#include <ace/xcomponent/native_interface_xcomponent.h>
15#include <arkui/native_type.h>
16#include <functional>
17#include <memory>
18#include <qarkui/window.h>
19#include <qohosdeviceinfo_p.h>
20#include <qohosinputmethodeventhandler.h>
21#include <qohosjsmain.h>
22#include <qohosplatformbackingstore.h>
23#include <qohosplatformintegration.h>
24#include <qohosplatformscreen.h>
25#include <qohosplatformwindow.h>
26#include <qohosqabilityinstancesmanager.h>
27#include <qohosutils.h>
28#include <qpa/qplatformscreen.h>
29#include <qpa/qplatformtheme.h>
30#include <qpa/qwindowsysteminterface.h>
31#include <render/qnativenode.h>
32#include <render/qohoswindowproxy.h>
33#include <render/qwindowproxyregistry.h>
34#include <string>
35#include <tuple>
36#include <vector>
37
39
41
42namespace
43{
44
50
56
58 Qt::WindowModality windowModality)
59{
60 using ModalityType = QOhosWindowProxy::ModalityType;
61
62 switch (windowModality) {
63 case Qt::WindowModality::NonModal:
64 return std::nullopt;
65 case Qt::WindowModality::WindowModal:
66 return ModalityType::WINDOW_MODALITY;
67 case Qt::WindowModality::ApplicationModal:
68 return ModalityType::APPLICATION_MODALITY;
69 }
70
71 qOhosPrintfWarning(
72 "%s: got illegal Qt::WindowModality value (%d), using the default instead",
73 Q_FUNC_INFO, static_cast<int>(windowModality));
74
75 return std::nullopt;
76}
77
78template<typename ...SignalParams>
79std::function<void(SignalParams...)> makeViewConditionalSignalEmitter(
80 QPointer<QOhosView> viewPtr, std::function<bool(QOhosView &)> predicate, void (QOhosView::*signalFuncPtr)(SignalParams ...))
81{
82 return [signalFuncPtr, viewPtr, predicate = std::move(predicate)](SignalParams ...args) {
83 if (viewPtr && predicate(*viewPtr))
84 Q_EMIT (*viewPtr.*signalFuncPtr)(args...);
85 };
86}
87
89{
90public:
92 using AvoidAreaType = QOhosWindowProxy::AvoidAreaType;
93
94 explicit AvoidAreaCache(std::function<AvoidArea(AvoidAreaType)> avoidAreasProvider);
95
96 void put(
97 AvoidAreaType avoidAreaType,
98 const AvoidArea &avoidArea);
99
101
102private:
103 std::function<AvoidArea(AvoidAreaType)> m_avoidAreasProvider;
104 QMap<AvoidAreaType, AvoidArea> m_store;
105};
106
107AvoidAreaCache::AvoidAreaCache(std::function<AvoidArea(AvoidAreaType)> avoidAreasProvider)
109 , m_store()
110{
111}
112
114 QOhosWindowProxy::AvoidAreaType avoidAreaType, const QOhosWindowProxy::AvoidArea &avoidArea)
115{
116 m_store[avoidAreaType] = avoidArea;
117}
118
120 QOhosWindowProxy::AvoidAreaType avoidAreaType)
121{
122 if (m_store.contains(avoidAreaType))
123 return m_store.value(avoidAreaType);
124
125 auto result = m_avoidAreasProvider(avoidAreaType);
126 m_store[avoidAreaType] = result;
127 return result;
128}
129
130void tryUpdateMaximumMarginsFromAvoidArea(QMargins &marginsToUpdate, const QOhosWindowProxy::AvoidArea &avoidArea)
131{
132 if (!avoidArea.visible)
133 return;
134
135 marginsToUpdate.setTop(qMax(marginsToUpdate.top(), avoidArea.topRect.height()));
136 marginsToUpdate.setLeft(qMax(marginsToUpdate.left(), avoidArea.leftRect.width()));
137 marginsToUpdate.setRight(qMax(marginsToUpdate.right(), avoidArea.rightRect.width()));
138 marginsToUpdate.setBottom(qMax(marginsToUpdate.bottom(), avoidArea.bottomRect.height()));
139}
140
142 const QPoint &geometryOrigin, const QRect &frameGeometry)
143{
144 int topFrameMargin = qAbs(frameGeometry.top() - geometryOrigin.y());
145 int sideAndBottomFrameMargin = qAbs(frameGeometry.left() - geometryOrigin.x());
146
147 return {
148 frameGeometry.width() - 2 * sideAndBottomFrameMargin,
149 frameGeometry.height() - topFrameMargin - sideAndBottomFrameMargin
150 };
151}
152
154{
155 auto visibleWindows = QWindowProxyRegistry::instance().queryWindowsWithVisibleSystemWindow();
156 return !visibleWindows.empty()
157 ? visibleWindows.front()
158 : nullptr;
159}
160
162{
163 auto focusedWindows = QWindowProxyRegistry::instance().queryWindowsWithSystemWindowAndFocus();
164 return !focusedWindows.empty()
165 ? focusedWindows.front()
166 : nullptr;
167}
168
170{
171 auto *focusWindow = getFirstTopLevelWindowWithSystemFocusOrNull();
172 auto *firstTopLevelWindow = getFirstTopLevelWindowOrNull();
173
174 auto isValidSyntheticParent = [&](QWindow *w) {
175 return w != nullptr && w != qWindow && w->isVisible();
176 };
177
178 return isValidSyntheticParent(focusWindow)
179 ? focusWindow
180 : isValidSyntheticParent(firstTopLevelWindow)
181 ? firstTopLevelWindow
182 : nullptr;
183}
184
186{
187 using ViewType = QOhosView::ViewType;
188
189 auto *qWindow = platformWindow->window();
190 auto *parent = qWindow->parent();
191 auto *transientParent = qWindow->transientParent();
192
193 auto windowType = qWindow->type();
194
195 QWindow *subWindowTagValue = platformWindow->validSubWindowOfTagValueOrNull();
196
197 static QSet<Qt::WindowType> fallbackToSubWindowWindowTypes{
198 Qt::Popup,
199 Qt::ToolTip,
200 Qt::Dialog,
201 Qt::Tool,
202 };
203
204 bool taggedAsMainWindow = platformWindow->mainWindowTagValueOrFalse();
205 if (taggedAsMainWindow) {
206 return ViewTypeInfo {
207 .viewType = ViewType::MainWindow,
208 .optLogicalParent = nullptr,
209 };
210 }
211
212 bool taggedAsFloatWindow = platformWindow->floatWindowTagValueOrFalse();
213 if (taggedAsFloatWindow) {
214 return ViewTypeInfo {
215 .viewType = ViewType::FloatWindow,
216 .optLogicalParent = nullptr,
217 };
218 }
219
220 bool overrideAsSubWindow = subWindowTagValue != nullptr;
221 if (overrideAsSubWindow) {
222 return ViewTypeInfo {
223 .viewType = ViewType::SubWindow,
224 .optLogicalParent = subWindowTagValue,
225 };
226 }
227
228 if (parent != nullptr) {
229 return ViewTypeInfo {
230 .viewType = ViewType::EmbeddedWindow,
231 .optLogicalParent = parent,
232 };
233 }
234
235 if (transientParent != nullptr) {
236 return ViewTypeInfo {
237 .viewType = ViewType::SubWindow,
238 .optLogicalParent = transientParent,
239 };
240 }
241
242 auto *syntheticParent = syntheticParentForQWindowOrNull(platformWindow->window());
243
244 bool automaticOverrideActive =
245 fallbackToSubWindowWindowTypes.contains(windowType)
246 && syntheticParent != nullptr;
247 if (automaticOverrideActive) {
248 return ViewTypeInfo {
249 .viewType = ViewType::SubWindow,
250 .optLogicalParent = syntheticParent,
251 };
252 }
253
254 return ViewTypeInfo {
255 .viewType = ViewType::MainWindow,
256 .optLogicalParent = nullptr,
257 };
258}
259
260// Reads the QWindow flags, since the platform flags drop the hint on frameless windows.
261// QWidget gives popups and tool tips the hint by default, so they are excluded.
262bool hasMaximizeButtonHintForSubWindow(const QWindow *qWindow)
263{
264 const auto windowType = qWindow->type();
265 return qWindow->flags().testFlag(Qt::WindowMaximizeButtonHint)
266 && windowType != Qt::Popup && windowType != Qt::ToolTip;
267}
268
269QBitmap getCursorBitmap(const QCursor &cursor)
270{
271 return cursor.bitmap();
272}
273
274QBitmap getCursorMask(const QCursor &cursor)
275{
276 return cursor.mask();
277}
278
279QImage createImageFromBitmapAndMask(const QBitmap &bitmap, const QBitmap &mask)
280{
281 const auto maskTransparentColor = QColor(Qt::color0).rgba();
282 const auto transparentColor = QColor(Qt::transparent).rgba();
283
284 QImage image = bitmap.toImage().convertToFormat(QImage::Format_RGBA8888);
285 QImage maskImage = mask.toImage().convertToFormat(QImage::Format_RGB32);
286
287 for (int row = 0; row < image.height(); ++row) {
288 auto *imageData = reinterpret_cast<QRgb *>(image.scanLine(row));
289 auto *maskData = reinterpret_cast<QRgb *>(maskImage.scanLine(row));
290 for (int col = 0; col < image.width(); ++col) {
291 if (maskData[col] == maskTransparentColor)
292 imageData[col] = transparentColor;
293 }
294 }
295
296 return image;
297}
298
300{
301 QBitmap cursorPixels(cursorSize);
302 cursorPixels.fill(Qt::transparent);
303 QBitmap cursorMask(cursorSize);
304 cursorMask.fill(Qt::color0);
305 return QCursor(cursorPixels, cursorMask);
306}
307
308ViewGeometryPersistencePolicy determineViewGeometryPersistencePolicy()
309{
310 using WindowGeometryPersistencePolicy = QOhosPlatformIntegration::WindowGeometryPersistencePolicy;
311
312 auto viewGeometryPersistencePolicy = ViewGeometryPersistencePolicy::Ignore;
313
315 switch (policy) {
316 case WindowGeometryPersistencePolicy::Disabled:
317 viewGeometryPersistencePolicy = ViewGeometryPersistencePolicy::Disabled;
318 break;
319 case WindowGeometryPersistencePolicy::Enabled:
320 viewGeometryPersistencePolicy = ViewGeometryPersistencePolicy::Enabled;
321 break;
322 case WindowGeometryPersistencePolicy::FollowSystemSetting:
323 viewGeometryPersistencePolicy = ViewGeometryPersistencePolicy::FollowSystemSetting;
324 break;
325 }
326
327 return viewGeometryPersistencePolicy;
328}
329
331 QOhosWindowProxy *windowProxy, QtOhos::JsCallBatch &jsCallBatch)
332{
333 using WindowGeometryPersistencePolicy = QOhosPlatformIntegration::WindowGeometryPersistencePolicy;
334
335 bool geometryPersistenceEnabled = false;
337 switch (policy) {
338 case WindowGeometryPersistencePolicy::Disabled:
339 windowProxy->setWindowRectAutoSave(false, jsCallBatch);
340 break;
341 case WindowGeometryPersistencePolicy::Enabled:
342 windowProxy->setWindowRectAutoSave(true, jsCallBatch);
343 geometryPersistenceEnabled = windowProxy->isWindowRectAutoSave();
344 break;
345 case WindowGeometryPersistencePolicy::FollowSystemSetting:
346 geometryPersistenceEnabled = windowProxy->isWindowRectAutoSave();
347 break;
348 }
349
350 return geometryPersistenceEnabled
353}
354
356 QWindow *logicalParent, QOhosWindowProxy &windowProxy)
357{
358 auto *screen = logicalParent->screen();
359 auto *ohosPlatformScreen = screen != nullptr
360 ? static_cast<QOhosPlatformScreen *>(screen->handle())
361 : nullptr;
362
363 return ohosPlatformScreen != nullptr
364 ? std::optional(ohosPlatformScreen->displayInfo().id)
365 : windowProxy.tryGetMainWindowJsDisplayId();
366}
367
369{
370 if (window != nullptr) {
371 QWindowPrivate *windowPrivate = qt_window_private(window);
372 QPalette palette = windowPrivate->windowPalette();
373 QColor backgroundColor = palette.color(QPalette::Window);
374 return backgroundColor;
375 }
376 return std::nullopt;
377}
378
379QOhosView *viewOrNull(QWindow *window)
380{
381 return QOhosPlatformWindow::fromQWindow(window)->ownedViewOrNull();
382}
383
384QOhosView *hostViewOrNull(QWindow *window)
385{
386 for (; window != nullptr; window = window->parent()) {
387 auto *view = viewOrNull(window);
388 if (view && view->viewType() != QOhosView::ViewType::EmbeddedWindow)
389 return view;
390 }
391 return nullptr;
392}
393
394}
395
397{
398 m_surfaceHasContent.store(true, std::memory_order_relaxed);
399 auto optPostSurfaceDrawTask = std::exchange(m_optPostSurfaceDrawTask, {});
400 if (optPostSurfaceDrawTask)
401 optPostSurfaceDrawTask();
402}
403
404std::shared_ptr<QOhosWindowProxy> QOhosView::tryCreateWindowProxyIfNeeded(ViewType viewType, QWindow *optLogicalParent)
405{
406 QWindow *qWindow = m_ownerWindow;
407 QOhosPlatformWindow *window = QOhosPlatformWindow::fromQWindow(qWindow);
408
409 std::shared_ptr<QOhosWindowProxy> result;
410
411 switch (viewType) {
412 case ViewType::MainWindow:
413 {
414 // NOTE: treat windows automatically-created by the platform in a special way
416 if (useAutoStartedMainWindow) {
417 QOhosWindowProxy::ExistingMainWindowCreateInfo createInfo;
418 createInfo.qWindowRef = QtOhos::QObjectThreadSafeRef(qWindow);
419 createInfo.qAbilityInstanceId = QOhosJsThreadGateway::eval([](QOhosJsState &jsState) {
420 return QtOhos::QAbilityInstancesManager::instance(jsState).defaultQAbilityPeer()->instanceId();
421 },
422 Q_FUNC_INFO);
423 result = QOhosWindowProxy::createForExistingMainWindow(createInfo);
424 m_geometryPersistencePolicy = determineViewGeometryPersistencePolicy();
425 } else {
426 QOhosWindowProxy::MainWindowCreateInfo createInfo;
427 createInfo.qWindowRef = QtOhos::QObjectThreadSafeRef(qWindow);
428 createInfo.windowId = window->internalWindowId();
429 createInfo.windowTitle = qWindow->title().toStdString();
430 createInfo.frameGeometry = window->lastRequestedWindowFrameGeometry();
431 createInfo.fullscreen = qWindow->windowState() == Qt::WindowFullScreen;
432 result = QOhosWindowProxy::createMainWindow(createInfo);
433 }
434
435 break;
436 }
437 case ViewType::SubWindow:
438 {
439 auto *targetWindowToBeSubWindowOf = optLogicalParent;
440
441 if (Q_UNLIKELY(targetWindowToBeSubWindowOf == nullptr))
442 qOhosReportFatalErrorAndAbort("Failed to determine valid parent of a SubWindow");
443
444 if (!targetWindowToBeSubWindowOf->isVisible()) {
445 auto *targetParentPlatformWindow = QOhosPlatformWindow::fromQWindowOrNull(targetWindowToBeSubWindowOf);
446 if (targetParentPlatformWindow != nullptr)
447 targetParentPlatformWindow->setVisible(true);
448 else
449 targetWindowToBeSubWindowOf->show();
450 }
451
452 auto *targetWindowToBeSubWindowOfView =
453 QOhosPlatformWindow::fromQWindow(targetWindowToBeSubWindowOf)->ownedViewOrNull();
454
455 const QOhosView *firstViewWithWindow = nullptr;
456 switch (targetWindowToBeSubWindowOfView->viewType()) {
457 case ViewType::MainWindow:
458 case ViewType::SubWindow:
459 case ViewType::FloatWindow:
460 firstViewWithWindow = targetWindowToBeSubWindowOfView;
461 break;
462 case ViewType::EmbeddedWindow:
463 firstViewWithWindow = targetWindowToBeSubWindowOfView->ancestorViewWithWindowOrNull();
464 break;
465 }
466
467 if (firstViewWithWindow == nullptr)
468 qOhosReportFatalErrorAndAbort("Failed to determine valid parent for this window.");
469
470 auto parentWindowProxy = firstViewWithWindow->m_ohosWindowProxy;
471 if (Q_UNLIKELY(!parentWindowProxy)) {
472 qOhosReportFatalErrorAndAbort(
473 "parentWindowProxy is null but should not be. This is most likely a programming error");
474 }
475
476 QOhosWindowProxy::SubWindowCreateInfo createInfo;
477 createInfo.window = QtOhos::QObjectThreadSafeRef(qWindow);
478 createInfo.windowTitle = qWindow->title().toStdString();
479 createInfo.windowId = window->internalWindowId();
480 createInfo.qAbilityInstanceId = parentWindowProxy->qAbilityInstanceId();
481
483 createInfo.disableWindowFocusableBeforeLoadContentHack = window->windowFlags().testFlag(Qt::WindowDoesNotAcceptFocus);
484 createInfo.modal = qWindow->modality() != Qt::NonModal;
485 // Sub window maximize is supported since API 19, but its maximize button can be
486 // hidden only since API 26, which the fixed-size state needs.
487 constexpr int minApiVersionWithSupportedWindowModesPerWindow = 26;
488 createInfo.maximizeSupported =
489 QOhosDeviceInfo::sdkApiVersion() >= minApiVersionWithSupportedWindowModesPerWindow
490 && hasMaximizeButtonHintForSubWindow(qWindow);
491 createInfo.windowRect = window->lastRequestedWindowFrameGeometry();
492 result = parentWindowProxy->createSubWindow(createInfo);
493
494 break;
495 }
496 case ViewType::EmbeddedWindow:
497 {
498 // NOTE - NativeNode is now always created during view instantiation
499 break;
500 }
501 case ViewType::FloatWindow:
502 {
503 QOhosWindowProxy::FloatWindowCreateInfo createInfo = {
504 .qWindowRef = QtOhos::QObjectThreadSafeRef(qWindow),
505 .internalWindowId = window->internalWindowId(),
506 };
507 result = QOhosWindowProxy::createFloatWindow(createInfo);
508 break;
509 }
510 default:
511 qOhosReportFatalErrorAndAbort("Unsupported view type: %d", viewType);
512
513 }
514
515 if (result != nullptr) {
516 JsCallBatch jsCallBatch(Q_FUNC_INFO);
517
518 auto viewPtr = QPointer<QOhosView>(this);
519 std::weak_ptr<QOhosWindowProxy> weakWindowProxy = result;
520 auto shouldEmitSignalPredicate = [weakWindowProxy](QOhosView &view) {
521 auto windowProxy = weakWindowProxy.lock();
522 return windowProxy != nullptr && windowProxy.get() == view.m_ohosWindowProxy.get();
523 };
524
525 result->setWindowCallbackReceiver(
526 std::make_unique<QOhosWindowProxy::WindowCallbacks>(
527 QOhosWindowProxy::WindowCallbacks {
528 .onWindowEvent = makeViewConditionalSignalEmitter(viewPtr, shouldEmitSignalPredicate, &QOhosView::windowEvent),
529 .onWindowStatusChange = makeViewConditionalSignalEmitter(viewPtr, shouldEmitSignalPredicate, &QOhosView::windowStatusChange),
530 .onWindowVisibilityChange = makeViewConditionalSignalEmitter(viewPtr, shouldEmitSignalPredicate, &QOhosView::windowVisibilityChange),
531 .onTouchOutside = makeViewConditionalSignalEmitter(viewPtr, shouldEmitSignalPredicate, &QOhosView::windowTouchOutside),
532 .onAvoidAreaChange = makeViewConditionalSignalEmitter(viewPtr, shouldEmitSignalPredicate, &QOhosView::avoidAreaChanged),
533 .onWindowRectChange = makeViewConditionalSignalEmitter(viewPtr, shouldEmitSignalPredicate, &QOhosView::windowRectChanged),
534 .onWindowRectChangeInGlobalDisplay = makeViewConditionalSignalEmitter(
535 viewPtr, shouldEmitSignalPredicate, &QOhosView::windowRectChangedInGlobalDisplay),
536 .onWindowDisplayIdChange = makeViewConditionalSignalEmitter(viewPtr, shouldEmitSignalPredicate, &QOhosView::windowDisplayIdChanged),
537 .onWindowResizeRejected = makeViewConditionalSignalEmitter(viewPtr, shouldEmitSignalPredicate, &QOhosView::windowResizeRejected),
538 .onWindowMoveRejected = makeViewConditionalSignalEmitter(viewPtr, shouldEmitSignalPredicate, &QOhosView::windowMoveRejected),
539 }),
540 jsCallBatch);
541
542 result->setNonClientAreaMouseWindowCallbackReceiver(
543 this,
544 [viewPtr, shouldEmitSignalPredicate](std::vector<QOhosWindowProxy::NonClientAreaMouseEvent> &&batch) {
545 if (!viewPtr.isNull() && shouldEmitSignalPredicate(*viewPtr)) {
547 inputEventHandler->onNonClientAreaMouseEvents(viewPtr->m_ownerWindow, std::move(batch));
548 }
549 },
550 jsCallBatch);
551
552 result->setNonClientAreaTouchWindowCallbackReceiver(
553 this,
554 [viewPtr, shouldEmitSignalPredicate](std::vector<QOhosWindowProxy::NonClientAreaTouchEvent> &&batch) {
555 if (!viewPtr.isNull() && shouldEmitSignalPredicate(*viewPtr)) {
557 inputEventHandler->onNonClientAreaTouchEvents(viewPtr->m_ownerWindow, std::move(batch));
558 }
559 },
560 jsCallBatch);
561
562 if (result->qtIsMainWindow() && result->isFocused().value_or(false)) {
563 // HACK
564 // WINDOW_ACTIVE event should be handled by QOhosFloatingWindow after OHOS system event received.
565 // Until we cannot register this event properly, keep sending it manually here.
566 sendAsyncSyntheticWindowActiveEvent();
567 }
568 }
569
570 return result;
571}
572
573bool QOhosView::isWindowTransparencyRequested() const
574{
575 return m_ownerWindow->requestedFormat().hasAlpha();
576}
577
578QOhosView::QOhosView(QWindow *ownerWindow, QSharedPointer<QNativeNode> nativeNode, QOhosPropertiesProvider windowPropertyProvider, QWindow *optLogicalParent)
579 : m_windowPropertiesProvider(windowPropertyProvider)
583 , m_updateData()
584 , m_updatePending(false)
586 , m_requireHandheldDeviceSupport(isHandheldDeviceType())
590{
591 auto avoidAreasProvider = std::make_shared<AvoidAreaCache>([this](AvoidAreaCache::AvoidAreaType avoidAreaType) {
592 return m_ohosWindowProxy != nullptr
593 ? m_ohosWindowProxy->getWindowAvoidArea(avoidAreaType)
594 : AvoidAreaCache::AvoidArea{};
595 });
596
597 connect(
598 this, &QOhosView::avoidAreaChanged,
599 this,
600 [avoidAreasProvider](QOhosWindowProxy::AvoidAreaType avoidAreaType, const QOhosWindowProxy::AvoidArea &avoidArea) {
601 avoidAreasProvider->put(avoidAreaType, avoidArea);
602 });
603
604 m_avoidAreasProvider = [avoidAreasProvider](QOhosWindowProxy::AvoidAreaType type) {
605 return avoidAreasProvider->getStoredOrRetrieveFromWindowProxy(type);
606 };
607
608 connect(
609 m_nativeNode.get(), &QNativeNode::surfaceStatusChanged,
610 this, [this](const std::optional<QSize> &optSurfaceSize) {
611 m_surfaceHasContent.store(false, std::memory_order_relaxed);
612 Q_EMIT surfaceStatusChanged(optSurfaceSize);
613 });
614
615 connect(
618
619 connect(
620 this, &QOhosView::windowEvent,
621 this, [this](QOhosWindowProxy::WindowEvent event) {
622 if (event.type != QOhosWindowProxy::WindowEventType::WINDOW_ACTIVE
623 && event.type != QOhosWindowProxy::WindowEventType::WINDOW_INACTIVE) {
624 return;
625 }
626 auto embeddedView = std::exchange(m_embeddedViewToActivateOnActive, nullptr);
627 if (embeddedView != nullptr && event.type == QOhosWindowProxy::WindowEventType::WINDOW_ACTIVE)
628 embeddedView->sendAsyncSyntheticWindowActiveEvent();
629 });
630
631 {
632 JsCallBatch jsCallBatch(Q_FUNC_INFO);
633 m_nativeNode->setNodeAreaChangeHandler(
634 [this](auto nodeAreaChangeEvent) {
635 Q_EMIT nodeAreaChanged(nodeAreaChangeEvent);
636 },
637 jsCallBatch);
638 m_nativeNode->setNodeVisibilityChangeHandler(
639 [this](bool visible) {
640 if (m_ohosWindowProxy == nullptr)
641 Q_EMIT windowVisibilityChange(visible);
642 },
643 jsCallBatch);
644 }
645
646 std::vector<std::shared_ptr<void>> writeCallbacks = {
647 m_windowPropertiesProvider.addPropertyWriteCallback<double, &QOhosPlatformWindow::windowCornerRadiusProperty>(
648 [this](double windowCornerRadius) {
649 setWindowCornerRadius(windowCornerRadius);
650 }),
651 m_windowPropertiesProvider.addPropertyWriteCallback<bool, &QOhosPlatformWindow::windowPrivacyModeSettingProperty>(
652 [this](bool windowPrivacyModeSetting) {
653 setPrivacyMode(windowPrivacyModeSetting);
654 }),
655 m_windowPropertiesProvider.addPropertyWriteCallback<QColor, &QOhosPlatformWindow::surfaceBackgroundColorProperty>(
656 [this](QColor surfaceBackgroundColor) {
657 setBackgroundColor(surfaceBackgroundColor);
658 }),
659 m_windowPropertiesProvider.addPropertyWriteCallback<bool, &QOhosPlatformWindow::windowKeepScreenOnProperty>(
660 [this](bool keepScreenOn) {
661 setWindowKeepScreenOn(keepScreenOn);
662 }),
663 m_windowPropertiesProvider.addPropertyWriteCallback<bool, &QOhosPlatformWindow::windowFixedSizeStateProperty>(
664 [this](bool fixedSizeStateEnabled) {
665 setFixedSizeStateEnabled(fixedSizeStateEnabled);
666 }),
667 m_windowPropertiesProvider.addPropertyWriteCallback<int, &QOhosPlatformWindow::windowBrightnessProperty>(
668 [this](int brightness) {
669 setBrightness(brightness);
670 }),
671 m_windowPropertiesProvider.addPropertyWriteCallback<int, &QOhosPlatformWindow::windowContrastProperty>(
672 [this](int contrast) {
673 setContrast(contrast);
674 }),
675 m_windowPropertiesProvider.addPropertyWriteCallback<int, &QOhosPlatformWindow::windowSaturationProperty>(
676 [this](int saturation) {
677 setSaturation(saturation);
678 }),
679 m_windowPropertiesProvider.addPropertyWriteCallback<bool, &QOhosPlatformWindow::windowDragResizableProperty>(
680 [this](bool dragResizable) {
681 setWindowDragResizable(dragResizable);
682 }),
683 };
684
685 m_windowPropertiesProviderCallbacksHandle = QtOhos::moveToSharedPtr(std::move(writeCallbacks));
686}
687
689{
690 // This call is necessary in order to avoid receiving callbacks
691 // from the underlying XComponent after we terminate the window.
692 // Not having this resulted in exceptions thrown from the system
693 // because QOhosNativeXComponent reffered to the window object that is null.
694 m_nativeNode.reset();
695}
696
697void QOhosView::setPosition(const QPoint &position)
698{
699 setSystemUpdateProperty(&SystemUpdateData::position, {position, {}});
700}
701
702void QOhosView::setSize(const QSize &size)
703{
704 setSystemUpdateProperty(&SystemUpdateData::size, size);
705}
706
707void QOhosView::setSizeLimits(const QSize &minSize, const QSize &maxSize)
708{
709 setSystemUpdateProperty(&SystemUpdateData::sizeLimits, std::make_pair(minSize, maxSize));
710}
711
712void QOhosView::setTransparentBackground(bool transparent)
713{
714 setSystemUpdateProperty(&SystemUpdateData::backgroundTransparent, transparent);
715}
716
717void QOhosView::setFocusable(bool focusable)
718{
719 setSystemUpdateProperty(&SystemUpdateData::focusable, focusable);
720}
721
722void QOhosView::setBackgroundColor(const QColor &color)
723{
724 setSystemUpdateProperty(&SystemUpdateData::backgroundColor, color);
725}
726
727void QOhosView::setBrightness(int brightness)
728{
729 setSystemUpdateProperty(&SystemUpdateData::brightness, brightness);
730}
731
732void QOhosView::setContrast(int contrast)
733{
734 setSystemUpdateProperty(&SystemUpdateData::contrast, contrast);
735}
736
737void QOhosView::setSaturation(int saturation)
738{
739 setSystemUpdateProperty(&SystemUpdateData::saturation, saturation);
740}
741
742void QOhosView::setWindowKeepScreenOn(bool keepScreenOn, JsCallBatch &jsCallBatch)
743{
744 if (m_ohosWindowProxy != nullptr)
745 m_ohosWindowProxy->setWindowKeepScreenOn(keepScreenOn, jsCallBatch);
746}
747
748void QOhosView::setFixedSizeStateEnabled(bool enabled, JsCallBatch &jsCallBatch)
749{
750 if (m_ohosWindowProxy == nullptr)
751 return;
752
753 const bool systemMaximizable = viewType() == ViewType::MainWindow
754 || (viewType() == ViewType::SubWindow && m_ohosWindowProxy->isSubWindowMaximizeSupported());
755 if (!systemMaximizable)
756 return;
757
759 qOhosPrintfWarning(
760 "%s: fixed size state is not supported while in HandheldDeviceFullScreen mode",
761 Q_FUNC_INFO);
762 return;
763 }
764
765 using SupportWindowMode = QOhosWindowProxy::SupportWindowMode;
766 const auto supportedWindowModes = enabled
767 ? std::set<SupportWindowMode>({SupportWindowMode::FLOATING})
768 : viewType() == ViewType::MainWindow
769 ? std::set<SupportWindowMode>({SupportWindowMode::FULL_SCREEN, SupportWindowMode::FLOATING, SupportWindowMode::SPLIT})
770 : std::set<SupportWindowMode>({SupportWindowMode::FULL_SCREEN, SupportWindowMode::FLOATING});
771
772 m_ohosWindowProxy->setSupportedWindowModes(supportedWindowModes, jsCallBatch);
773}
774
775void QOhosView::showWindow(JsCallBatch &jsCallBatch)
776{
777 auto *ohosPlatformWindow = static_cast<QOhosPlatformWindow *>(m_ownerWindow->handle());
778 m_ohosWindowProxy->showWindow(
779 QOhosWindowProxy::ShowWindowOptions{
780 .focusOnShow = ohosPlatformWindow->shouldShowWindowWithoutActivating()
781 ? std::optional(false)
782 : std::nullopt,
783 },
784 jsCallBatch);
785}
786
787void QOhosView::sendAsyncSyntheticWindowActiveEvent()
788{
789 QMetaObject::invokeMethod(
790 this,
791 [this]() {
792 QOhosWindowProxy::WindowEvent syntheticWindowActiveEvent = {
793 .type = QOhosWindowProxy::WindowEventType::WINDOW_ACTIVE,
794 };
795 Q_EMIT windowEvent(syntheticWindowActiveEvent);
796 },
797 Qt::QueuedConnection);
798}
799
800void QOhosView::setCursor(const QCursor &cursor)
801{
802 setSystemUpdateProperty(&SystemUpdateData::cursor, cursor);
803}
804
805void QOhosView::setModality(Qt::WindowModality modality)
806{
807 setSystemUpdateProperty(&SystemUpdateData::modality, modality);
808}
809
810void QOhosView::setTitle(const QString &title)
811{
812 setSystemUpdateProperty(&SystemUpdateData::title, title);
813}
814
816{
817 if (m_ohosWindowProxy == nullptr) {
818 m_nativeNode->raise();
819 return;
820 }
821
822 switch (viewType()) {
823 case ViewType::MainWindow:
824 showWindow();
825 break;
826 case ViewType::SubWindow:
827 m_ohosWindowProxy->raiseToAppTop();
828 break;
829 case ViewType::FloatWindow:
830 case ViewType::EmbeddedWindow:
831 break;
832 }
833}
834
836{
837 if (m_ohosWindowProxy != nullptr && m_ownerWindow->isVisible())
838 showWindow();
839 else
840 m_nativeNode->lower();
841}
842
843void QOhosView::updateWindowSize(const QSize &size, JsCallBatch &jsCallBatch)
844{
845 if (m_ohosWindowProxy != nullptr)
846 m_ohosWindowProxy->setSize(size, jsCallBatch);
847 else
848 m_nativeNode->setSize(size, jsCallBatch);
849}
850
851void QOhosView::updateWindowPosition(
852 const std::pair<QPoint, std::optional<QOhosDisplayInfo::JsDisplayId>> &positionProp, JsCallBatch &jsCallBatch)
853{
854 QPoint position;
855 std::optional<QOhosDisplayInfo::JsDisplayId> displayId;
856 std::tie(position, displayId) = positionProp;
857
858 if (m_ohosWindowProxy != nullptr) {
859 m_ohosWindowProxy->moveWindowToGlobalOrGlobalDisplay(position, displayId, jsCallBatch);
860 } else {
861 m_nativeNode->setPosition(position, jsCallBatch);
862 }
863}
864
865void QOhosView::updateWindowBackgroundTransparency(bool transparent, JsCallBatch &jsCallBatch)
866{
867 if (m_ohosWindowProxy != nullptr) {
868 auto opaqueSystemBackgroundRgb = QGuiApplicationPrivate::platformTheme()
869 ->palette(QPlatformTheme::SystemPalette)
870 ->window()
871 .color()
872 .rgb();
873 m_ohosWindowProxy->setWindowBackgroundColor(
874 transparent
875 ? QColor(Qt::transparent)
876 : tryGetBackgroundColorFromWindow(m_ownerWindow).value_or(
877 QColor(opaqueSystemBackgroundRgb)),
878 jsCallBatch);
879 }
880}
881
882void QOhosView::updateWindowCursor(const QCursor &cursor, JsCallBatch &jsCallBatch)
883{
884 if (m_ohosWindowProxy == nullptr)
885 return;
886
887 if (cursor.shape() == Qt::BitmapCursor || cursor.shape() == Qt::BlankCursor) {
888 auto bitmapCursor =
889 cursor.shape() == Qt::BlankCursor
890 ? makeTransparentBitmapCursor({1, 1})
891 : cursor;
892
893 QImage bitmapCursorImage = bitmapCursor.pixmap().isNull()
894 ? createImageFromBitmapAndMask(getCursorBitmap(bitmapCursor), getCursorMask(bitmapCursor))
895 : bitmapCursor.pixmap().toImage();
896
897 m_ohosWindowProxy->setCustomCursor(bitmapCursorImage, bitmapCursor.hotSpot(), jsCallBatch);
898 } else {
899 m_ohosWindowProxy->setPointerStyleSync(cursor, jsCallBatch);
900 }
901}
902
903void QOhosView::updateWindowFocusable(bool focusable, JsCallBatch &jsCallBatch)
904{
905 if (m_ohosWindowProxy != nullptr)
906 m_ohosWindowProxy->setWindowFocusable(focusable, jsCallBatch);
907 m_nativeNode->setFocusable(focusable, jsCallBatch);
908}
909
910void QOhosView::updateWindowBackgroundColor(const QColor &color, JsCallBatch &jsCallBatch)
911{
912 m_nativeNode->setBackgroundColor(color, jsCallBatch);
913}
914
915void QOhosView::updateWindowBrightness(int brightness, JsCallBatch &jsCallBatch)
916{
917 m_nativeNode->setBrightness(brightness, jsCallBatch);
918}
919
920void QOhosView::updateWindowContrast(int contrast, JsCallBatch &jsCallBatch)
921{
922 m_nativeNode->setContrast(contrast, jsCallBatch);
923}
924
925void QOhosView::updateWindowSaturation(int saturation, JsCallBatch &jsCallBatch)
926{
927 m_nativeNode->setSaturation(saturation, jsCallBatch);
928}
929
930void QOhosView::updateWindowTransparentForInput(bool transparentForInput, JsCallBatch &jsCallBatch)
931{
932 if (m_ohosWindowProxy != nullptr)
933 m_ohosWindowProxy->setWindowTouchable(!transparentForInput, jsCallBatch);
934 m_nativeNode->setTransparentForInput(transparentForInput, jsCallBatch);
935}
936
937void QOhosView::updateWindowSizeLimits(const std::pair<QSize, QSize> &sizeLimits, JsCallBatch &jsCallBatch)
938{
939 if (m_ohosWindowProxy != nullptr) {
940 auto windowMargins = QOhosPlatformWindow::fromQWindow(m_ownerWindow)->frameMargins();
941 // HACK: the system reads a minimum size of 0 as "not set" and applies
942 // its default (320x72 vp), so send at least 1.
943 m_ohosWindowProxy->setWindowLimits(
944 sizeLimits.first.grownBy(windowMargins).expandedTo(QSize(1, 1)),
945 sizeLimits.second.grownBy(windowMargins).expandedTo(QSize(1, 1)),
946 jsCallBatch);
947 }
948}
949
950void QOhosView::updateWindowModality(Qt::WindowModality modality, JsCallBatch &jsCallBatch)
951{
952 if (m_ohosWindowProxy != nullptr) {
953 if (viewType() != ViewType::SubWindow) {
954 qCWarning(QtForOhos, "%s: Modality is supported only for sub-windows.", Q_FUNC_INFO);
955 return;
956 }
957 if (modality == Qt::WindowModality::ApplicationModal) {
958 qCWarning(
959 QtForOhos,
960 "%s: Qt::ApplicationModal policy is unsupported by the platform. The window will behave like Qt::WindowModal.",
961 Q_FUNC_INFO);
962 }
963
964 auto ohosModalityType = mapQtWindowModalityToOhosOrDefault(modality);
965 if (ohosModalityType.has_value())
966 m_ohosWindowProxy->setSubWindowModalEnabled(ohosModalityType.value(), jsCallBatch);
967 else
968 m_ohosWindowProxy->setSubWindowModalDisabled(jsCallBatch);
969 }
970}
971
972void QOhosView::updateWindowTitle(const QString &title, JsCallBatch &jsCallBatch)
973{
974 if (m_ohosWindowProxy != nullptr)
975 m_ohosWindowProxy->setTitle(title, jsCallBatch);
976}
977
978void QOhosView::scheduleSystemUpdateIfNeeded()
979{
980 if (m_updatePending)
981 return;
982 m_updatePending = QMetaObject::invokeMethod(
983 this,
984 [this]() {
985 JsCallBatch jsCallBatch(Q_FUNC_INFO);
986 flushSystemPropertyUpdatesImmediate(jsCallBatch);
987 },
988 Qt::QueuedConnection);
989}
990
992{
993 return m_ownerWindow;
994}
995
997{
998 auto *platformWindow = QOhosPlatformWindow::fromQWindow(m_ownerWindow);
999
1000 auto currentViewTypeInfo = determineViewTypeAndLogicalParent(platformWindow);
1001 const bool hadWindowProxy = m_ohosWindowProxy != nullptr;
1002 const bool needsWindowProxy = !hadWindowProxy
1003 && currentViewTypeInfo.viewType != ViewType::EmbeddedWindow;
1004 const bool windowProxyRoleChanged = hadWindowProxy
1005 && viewType() != currentViewTypeInfo.viewType;
1006 const bool windowProxySetupNeeded = needsWindowProxy || windowProxyRoleChanged;
1007
1008 const auto qOhosWindowProxy = windowProxySetupNeeded
1009 ? tryCreateWindowProxyIfNeeded(currentViewTypeInfo.viewType, currentViewTypeInfo.optLogicalParent)
1010 : nullptr;
1011
1012 if (windowProxySetupNeeded) {
1013 if (qOhosWindowProxy != nullptr
1014 && currentViewTypeInfo.viewType == ViewType::SubWindow) {
1015 constexpr bool preventSubWindowClose = true;
1016 qOhosWindowProxy->setSubWindowCloseHandler(
1017 [this]() {
1018 if (!m_ownerWindow.isNull()) {
1019 qOhosPrintfDebug("onSubWindowCloseHandler: calling close()");
1020 QOhosCloseEventContext::runWithCloseRootCauseSet(
1021 QOhosCloseEventContext::CloseRootCause::SubWindowClose,
1022 [&]() {
1023 m_ownerWindow->close();
1024 });
1025 }
1026 },
1027 preventSubWindowClose);
1028 }
1029
1030 m_viewType = currentViewTypeInfo.viewType;
1031 setOrResetWindowProxy(qOhosWindowProxy, currentViewTypeInfo.optLogicalParent);
1032 } else {
1033 syncWindowStateImmediate();
1034 }
1035
1036 {
1037 JsCallBatch jsCallBatch(Q_FUNC_INFO);
1038
1039 if (windowProxySetupNeeded) {
1040 if (qOhosWindowProxy != nullptr) {
1041 // Keep these two adjacent in one batch, or ArkUI can lay out the node between them.
1042 m_nativeNode->setParent(qOhosWindowProxy->nodeXComponent(), jsCallBatch);
1043 m_nativeNode->fillToParent(jsCallBatch);
1044 }
1045#if QT_CONFIG(draganddrop)
1046 m_nativeNode->enableDropDisallowedBadge(jsCallBatch);
1047#endif
1048 }
1049
1050 if (viewType() == ViewType::MainWindow) {
1051 const auto windowStates = m_ownerWindow->windowStates();
1052 if (windowStates.testFlag(Qt::WindowState::WindowFullScreen)) {
1053 // A phone window created fullscreen still shows the system bars, so
1054 // hide them explicitly. On 2-in-1/tablet the window is already
1055 // immersive at creation; re-applying would only make it twitch.
1056 if (hadWindowProxy || QOhosDeviceInfo::isPhone())
1057 updateWindowFullScreen(jsCallBatch);
1058 } else if (windowStates.testFlag(Qt::WindowState::WindowMaximized)) {
1059 updateWindowMaximized(jsCallBatch);
1060 } else if (QOhosDeviceInfo::isPhone()
1061 && m_ownerWindow->flags().testFlag(Qt::ExpandedClientAreaHint)) {
1062 applyPhoneWindowChrome(jsCallBatch);
1063 }
1064 } else if (viewType() == ViewType::SubWindow && m_ohosWindowProxy != nullptr) {
1065 m_ohosWindowProxy->setFollowParentMultiScreenPolicy(true, jsCallBatch);
1066 }
1067
1068 if (m_ohosWindowProxy != nullptr) {
1069 if (hadWindowProxy)
1070 updateMainWindowRestored(jsCallBatch);
1071 showWindow(jsCallBatch);
1072 }
1073
1074 // A sub window ignores maximize requests until it is shown.
1075 if (viewType() == ViewType::SubWindow && m_ohosWindowProxy != nullptr) {
1076 const auto windowStates = m_ownerWindow->windowStates();
1077 if (windowStates.testFlag(Qt::WindowState::WindowFullScreen))
1078 updateWindowFullScreen(jsCallBatch);
1079 else if (windowStates.testFlag(Qt::WindowState::WindowMaximized))
1080 updateWindowMaximized(jsCallBatch);
1081 }
1082 }
1083
1084 auto *surface = surfaceOrNull();
1085 const bool isVulkanWindow =
1086 m_ownerWindow && m_ownerWindow->surfaceType() == QSurface::VulkanSurface;
1087
1088 if (surface != nullptr && !isVulkanWindow
1089 && !m_surfaceHasContent.load(std::memory_order_relaxed))
1091
1092 m_nativeNode->setVisibility(true);
1093
1094 // Mirrors hide(): ArkUI does not report the node visibility change for an
1095 // embedded window, so the view has to.
1096 if (viewType() == ViewType::EmbeddedWindow)
1097 Q_EMIT windowVisibilityChange(true);
1098}
1099
1100void QOhosView::applyPhoneWindowChrome(JsCallBatch &jsCallBatch)
1101{
1102 if (m_ohosWindowProxy == nullptr)
1103 return;
1104
1105 // Lay the window out under the system bars when fullscreen or when the
1106 // client area is expanded; only fullscreen also hides the bars.
1107 const bool fullScreen = m_ownerWindow->windowStates().testFlag(Qt::WindowFullScreen);
1108 const bool expanded = m_ownerWindow->flags().testFlag(Qt::ExpandedClientAreaHint);
1109 m_ohosWindowProxy->setWindowLayoutFullScreen(fullScreen || expanded, jsCallBatch);
1110 m_ohosWindowProxy->setWindowSystemBarEnable(
1111 fullScreen ? QStringList{}
1112 : QStringList{ QStringLiteral("status"), QStringLiteral("navigation") },
1113 jsCallBatch);
1114}
1115
1117{
1118 JsCallBatch jsCallBatch(Q_FUNC_INFO);
1119 updateWindowFullScreen(jsCallBatch);
1120}
1121
1122void QOhosView::updateWindowFullScreen(JsCallBatch &jsCallBatch)
1123{
1124 auto *windowProxy = windowProxyAfterPropertyUpdatesOrNull(jsCallBatch);
1125 if (windowProxy == nullptr)
1126 return;
1127
1129 applyPhoneWindowChrome(jsCallBatch);
1130 return;
1131 }
1132
1133 if (!isMaximizeRequestSupportedOrLogWarning()) {
1134 QOhosPlatformWindow::fromQWindow(m_ownerWindow)->resetRefusedMaximizeState();
1135 return;
1136 }
1137
1138 if (!windowProxy->tryMaximize(QOhosWindowProxy::MaximizePresentation::ENTER_IMMERSIVE))
1139 QOhosPlatformWindow::fromQWindow(m_ownerWindow)->resetRefusedMaximizeState();
1140}
1141
1143{
1144 JsCallBatch jsCallBatch(Q_FUNC_INFO);
1145
1146 auto *windowProxy = windowProxyAfterPropertyUpdatesOrNull(jsCallBatch);
1147 if (windowProxy == nullptr)
1148 return;
1149
1151 windowProxy->recover(jsCallBatch);
1152 else
1153 applyPhoneWindowChrome(jsCallBatch);
1154}
1155
1157{
1158 JsCallBatch jsCallBatch(Q_FUNC_INFO);
1159 auto *windowProxy = windowProxyAfterPropertyUpdatesOrNull(jsCallBatch);
1160 if (windowProxy != nullptr)
1161 windowProxy->minimize(jsCallBatch);
1162}
1163
1164bool QOhosView::canShiftSystemFocus(const QOhosView *fromHostView, const QOhosView *toHostView)
1165{
1166 return fromHostView
1167 && toHostView
1168 && fromHostView != toHostView
1169 && fromHostView->m_ohosWindowProxy
1170 && toHostView->m_ohosWindowProxy;
1171}
1172
1174{
1175 if (m_ohosWindowProxy != nullptr) {
1176 if (QGuiApplicationPrivate::focus_window) {
1177 const auto *focusView = viewOrNull(QGuiApplicationPrivate::focus_window);
1178 if (focusView && focusView != this) {
1179 if (focusView->m_ohosWindowProxy) {
1180 focusView->m_ohosWindowProxy->tryShiftAppWindowFocus(*m_ohosWindowProxy);
1181 } else {
1182 const auto *focusHostView =
1183 hostViewOrNull(QGuiApplicationPrivate::focus_window);
1184 if (canShiftSystemFocus(focusHostView, this)) {
1185 focusHostView->m_ohosWindowProxy->tryShiftAppWindowFocus(*m_ohosWindowProxy);
1186 } else {
1187 // If the previously focused window is an EmbeddedWindow of this window.
1188 sendAsyncSyntheticWindowActiveEvent();
1189 }
1190 }
1191 }
1192 }
1193 } else {
1194 auto *hostView = hostViewOrNull(m_ownerWindow);
1195 const auto *focusHostView = hostViewOrNull(QGuiApplicationPrivate::focus_window);
1196 if (canShiftSystemFocus(focusHostView, hostView)) {
1197 hostView->m_embeddedViewToActivateOnActive = this;
1198 if (!focusHostView->m_ohosWindowProxy->tryShiftAppWindowFocus(*hostView->m_ohosWindowProxy))
1199 hostView->m_embeddedViewToActivateOnActive = nullptr;
1200 } else {
1201 // for EmbeddedWindow
1202 sendAsyncSyntheticWindowActiveEvent();
1203 }
1204 }
1205}
1206
1208{
1209 JsCallBatch jsCallBatch(Q_FUNC_INFO);
1210 updateWindowMaximized(jsCallBatch);
1211}
1212
1213void QOhosView::updateWindowMaximized(JsCallBatch &jsCallBatch)
1214{
1215 auto *windowProxy = windowProxyAfterPropertyUpdatesOrNull(jsCallBatch);
1216 if (windowProxy == nullptr)
1217 return;
1218
1219 if (!isMaximizeRequestSupportedOrLogWarning()) {
1220 QOhosPlatformWindow::fromQWindow(m_ownerWindow)->resetRefusedMaximizeState();
1221 return;
1222 }
1223
1224 if (!windowProxy->tryMaximize(QOhosWindowProxy::MaximizePresentation::EXIT_IMMERSIVE))
1225 QOhosPlatformWindow::fromQWindow(m_ownerWindow)->resetRefusedMaximizeState();
1226}
1227
1228std::unique_ptr<QOhosView> QOhosView::createForWindow(QOhosPlatformWindow *window, QOhosPropertiesProvider windowPropertiesProvider)
1229{
1230 auto *qWindow = window->window();
1231 QNativeNode::CreateInfo createInfo;
1232 createInfo.geometry = window->geometry();
1233 createInfo.window = qWindow;
1234 createInfo.backgroundColor = windowPropertiesProvider.tryGetProperty<QColor, &QOhosPlatformWindow::surfaceBackgroundColorProperty>();
1235 createInfo.renderFitPolicyHint = qTransform(
1236 windowPropertiesProvider.tryGetProperty<int, &QOhosPlatformWindow::nativeNodeRenderFitPolicyHintProperty>(),
1237 [](int renderFit) {
1238 return static_cast<::ArkUI_RenderFit>(renderFit);
1239 });
1240
1241 QPlatformWindow *parentPlatformWindow = window->parent();
1242
1243 if (parentPlatformWindow != nullptr) {
1244 auto *parentPlatformWindow = static_cast<QOhosPlatformWindow *>(window->parent());
1245 auto *parentView = parentPlatformWindow->ownedViewOrNull();
1246 createInfo.optParent = parentView != nullptr
1247 ? std::optional(parentView->m_nativeNode.get())
1248 : std::nullopt;
1249 }
1250
1251 auto nativeNode = QSharedPointer<QNativeNode>::create(createInfo);
1252 return std::make_unique<QOhosView>(qWindow, nativeNode, windowPropertiesProvider,
1253 createInfo.optParent ? qWindow->parent() : nullptr);
1254}
1255
1257{
1258 QOhosView::ViewGeometry result;
1259 if (m_ohosWindowProxy != nullptr) {
1260 auto windowProperties = m_ohosWindowProxy->getWindowProperties();
1261 result.frameGeometry = windowProperties.windowRect;
1262 // Translate relative coordinates of the window to absolute screen values
1263 result.geometry = windowProperties.drawableRect.translated(windowProperties.windowRect.topLeft());
1264 // HACK:
1265 // "result.geometry" might contain invalid size but (1) it will likely contain proper
1266 // top-left corner coords and (2) "result.frameGeometry" is also valid, so adjust the
1267 // invalidated size based on these two.
1268 result.geometry.setSize(
1269 evaluateGeometrySizeBasedOnFrameGeometry(result.geometry.topLeft(), result.frameGeometry));
1270 result.displayId = windowProperties.displayId;
1271 } else {
1272 result.frameGeometry = m_nativeNode->geometry().toRect();
1273 result.geometry = result.frameGeometry;
1274
1275 const auto *ancestorViewWithWindow = ancestorViewWithWindowOrNull();
1276 result.displayId = ancestorViewWithWindow != nullptr
1277 ? ancestorViewWithWindow->m_ohosWindowProxy->getWindowProperties().displayId
1278 : std::nullopt;
1279 }
1280
1281 return result;
1282}
1283
1284QMargins QOhosView::avoidAreaMargins(QOhosWindowProxy::AvoidAreaType type) const
1285{
1286 auto avoidArea = m_avoidAreasProvider(type);
1287 QMargins margins;
1288 tryUpdateMaximumMarginsFromAvoidArea(margins, avoidArea);
1289 return margins;
1290}
1291
1293{
1294 return m_ohosWindowProxy != nullptr ? m_ohosWindowProxy->getDisplayCutoutRects()
1295 : QList<QRect>();
1296}
1297
1299{
1300 return m_viewType.value_or(ViewType::EmbeddedWindow);
1301}
1302
1304{
1305 auto *platformWindow = QOhosPlatformWindow::fromQWindow(m_ownerWindow);
1306 auto targetWindowGeometry = platformWindow->geometry().marginsAdded(platformWindow->frameMargins());
1307
1308 m_updateData.position.optPendingUpdateRequest = {targetWindowGeometry.topLeft(), {}};
1309 m_updateData.size.optPendingUpdateRequest = targetWindowGeometry.size();
1310 scheduleSystemUpdateIfNeeded();
1311}
1312
1314{
1315 switch (viewType()) {
1316 case ViewType::MainWindow:
1317 {
1318 JsCallBatch jsCallBatch(Q_FUNC_INFO);
1319 hideMainWindow(jsCallBatch);
1320 m_nativeNode->setVisibility(false, jsCallBatch);
1321 break;
1322 }
1323 case ViewType::FloatWindow:
1324 case ViewType::SubWindow:
1325 {
1326 setOrResetWindowProxy(nullptr, nullptr);
1327 auto syntheticWindowHiddenEvent = QOhosWindowProxy::WindowEvent {
1328 .type = QOhosWindowProxy::WindowEventType::WINDOW_HIDDEN,
1329 };
1330 Q_EMIT windowEvent(syntheticWindowHiddenEvent);
1331 m_nativeNode->setVisibility(false);
1332 break;
1333 }
1334 case ViewType::EmbeddedWindow:
1335 Q_EMIT windowVisibilityChange(false);
1336 m_nativeNode->setVisibility(false);
1337 break;
1338 }
1339}
1340
1342{
1343 setTransparentBackground(isWindowTransparencyRequested());
1344}
1345
1346void QOhosView::setWindowMask(const QOhosWindowProxy::WindowMask &mask)
1347{
1348 if (m_ohosWindowProxy != nullptr)
1349 m_ohosWindowProxy->setWindowMask(mask);
1350}
1351
1352QOhosSurface *QOhosView::surfaceOrNull() const
1353{
1354 return m_nativeNode->surfaceOrNull();
1355}
1356
1358{
1359 return m_ohosWindowProxy ? m_ohosWindowProxy->abilityObjectRef() : QHarmonyJsObjectRef();
1360}
1361
1363{
1364 return m_ohosWindowProxy ? m_ohosWindowProxy->jsWindowObjectRef() : QHarmonyJsObjectRef();
1365}
1366
1368{
1369 return m_nativeNode->windowId();
1370}
1371
1373{
1374 qCDebug(QtForOhos) << "view:" << this << "setParentOrReparent parentView:" << &parentView;
1375 m_nativeNode->setParent(*parentView.m_nativeNode);
1376 m_viewType.reset();
1377 setOrResetWindowProxy(nullptr, parentView.ownerWindow());
1378}
1379
1381{
1382 if (viewType() != ViewType::EmbeddedWindow) {
1383 qCDebug(QtForOhos) << Q_FUNC_INFO << m_ownerWindow;
1384 return;
1385 }
1386
1387 m_nativeNode->detachFromParentIfPresent();
1388 m_optLogicalParent = nullptr;
1389
1390 if (m_ownerWindow->isVisible() && !m_ohosWindowProxy)
1392}
1393
1394void QOhosView::updateNativeNodeVisibility(bool visible, JsCallBatch &jsCallBatch)
1395{
1396 m_nativeNode->setVisibility(visible, jsCallBatch);
1397 m_lastMainWindowHideMethod = WindowHideMethod::NativeNodeVisibility;
1398}
1399
1400void QOhosView::hideMainWindow(JsCallBatch &jsCallBatch)
1401{
1402 if (m_ohosWindowProxy == nullptr) {
1403 updateNativeNodeVisibility(false, jsCallBatch);
1404 return;
1405 }
1406
1407 if (m_ohosWindowProxy->tryHideAbility()) {
1408 m_lastMainWindowHideMethod = WindowHideMethod::HideAbility;
1409 return;
1410 }
1411
1412 if (QOhosPlatformWindow::isWindowBeingClosedOrDestroyed(m_ownerWindow)) {
1413 updateNativeNodeVisibility(false, jsCallBatch);
1414 return;
1415 }
1416
1417 m_ohosWindowProxy->minimize(jsCallBatch);
1418 m_lastMainWindowHideMethod = WindowHideMethod::Minimize;
1419
1420 auto *platformWindow = QOhosPlatformWindow::fromQWindow(m_ownerWindow);
1421 if (!platformWindow->windowStates().testFlag(Qt::WindowMinimized))
1422 ++m_pendingSelfMinimizeEchoes;
1423}
1424
1425void QOhosView::syncWindowStateImmediate(WindowStateSyncReason reason)
1426{
1427 if (m_ohosWindowProxy == nullptr && m_optLogicalParent == nullptr)
1428 return;
1429
1430 std::vector<std::function<void()>> postSurfaceDrawTasks;
1431 auto updatePostSurfaceDrawTask = qScopeGuard([&]() {
1432 if (!postSurfaceDrawTasks.empty()) {
1433 m_optPostSurfaceDrawTask = [postSurfaceDrawTasks = std::move(postSurfaceDrawTasks)]() {
1434 for (const auto &task: postSurfaceDrawTasks)
1435 task();
1436 };
1437 }
1438 });
1439
1440 if (reason == WindowStateSyncReason::ViewTypeChanged && m_ohosWindowProxy
1441 && viewType() == ViewType::MainWindow) {
1442 postSurfaceDrawTasks.emplace_back(
1443 [weakWindowProxy = QtOhos::makeWeakPtr(m_ohosWindowProxy)]() {
1444 auto windowProxy = weakWindowProxy.lock();
1445 if (windowProxy)
1446 windowProxy->removeStartingWindow();
1447 });
1448 }
1449
1450 auto *platformWindow = QOhosPlatformWindow::fromQWindow(m_ownerWindow);
1451
1452 auto targetWindowLimitsToSet =
1453 viewType() == ViewType::EmbeddedWindow
1454 ? std::make_pair(m_ownerWindow->minimumSize(), m_ownerWindow->maximumSize())
1455 : std::make_pair(platformWindow->windowMinimumSize(), platformWindow->windowMaximumSize());
1456
1457 auto windowFlags = QOhosPlatformWindow::platformWindowFlagsForQWindow(m_ownerWindow);
1458 bool focusable = !windowFlags.testFlag(Qt::WindowDoesNotAcceptFocus);
1459
1460 auto submitSystemPropertyUpdate = [](auto &targetProperty, const auto &targetValue) {
1461 targetProperty.optPendingUpdateRequest = targetValue;
1462 };
1463
1464 auto submitOrResetSystemPropertyUpdate = [&submitSystemPropertyUpdate](
1465 auto &targetProperty,
1466 const auto &targetValue,
1467 WindowGeometryPersistenceState windowGeometryPersistenceState) {
1468 if (windowGeometryPersistenceState == WindowGeometryPersistenceState::Enabled)
1469 targetProperty.optPendingUpdateRequest.reset();
1470 else
1471 submitSystemPropertyUpdate(targetProperty, targetValue);
1472 };
1473
1474 submitSystemPropertyUpdate(m_updateData.title, m_ownerWindow->title());
1475
1476 std::optional<QOhosDisplayInfo::JsDisplayId> optDisplayIdToSynchronize;
1477 {
1478 JsCallBatch jsCallBatch(Q_FUNC_INFO);
1479
1480 auto windowGeometryPersistenceState =
1481 viewType() == ViewType::MainWindow
1482 && m_geometryPersistencePolicy != ViewGeometryPersistencePolicy::Ignore
1483 ? syncWindowGeometryPersistenceState(m_ohosWindowProxy.get(), jsCallBatch)
1484 : WindowGeometryPersistenceState::Disabled;
1485
1486 auto currentRuntimeDeviceTypeAndMode = queryQOhosRuntimeDeviceAndMode();
1487 bool windowGeometrySyncEnabled = false;
1488 switch (currentRuntimeDeviceTypeAndMode) {
1491 windowGeometrySyncEnabled = true;
1492 break;
1494 windowGeometrySyncEnabled = viewType() != ViewType::MainWindow;
1495 break;
1496 }
1497
1498 std::optional<QOhosDisplayInfo::JsDisplayId> targetDisplayId;
1499 switch (viewType()) {
1500 case ViewType::SubWindow:
1501 if (m_optLogicalParent == nullptr)
1502 qOhosReportFatalErrorAndAbort("%s: logical parent for a subwindow is null", Q_FUNC_INFO);
1503 targetDisplayId = tryGetSubWindowJsDisplayId(m_optLogicalParent, *m_ohosWindowProxy);
1504 break;
1505 case ViewType::MainWindow:
1506 {
1507 targetDisplayId = m_ohosWindowProxy->tryGetMainWindowJsDisplayId();
1508 break;
1509 }
1510 case ViewType::FloatWindow:
1511 case ViewType::EmbeddedWindow:
1512 break;
1513 }
1514
1515 auto targetGeometryToSet = platformWindow->lastRequestedWindowFrameGeometry();
1516
1517 if (windowGeometrySyncEnabled) {
1518 submitSystemPropertyUpdate(m_updateData.sizeLimits, targetWindowLimitsToSet);
1519 submitOrResetSystemPropertyUpdate(
1520 m_updateData.size, targetGeometryToSet.size(), windowGeometryPersistenceState);
1521
1522 if (!qt_window_private(m_ownerWindow)->positionAutomatic) {
1523 submitOrResetSystemPropertyUpdate(
1524 m_updateData.position,
1525 std::make_pair(targetGeometryToSet.topLeft(), targetDisplayId),
1526 windowGeometryPersistenceState);
1527 }
1528 }
1529
1530 submitSystemPropertyUpdate(m_updateData.backgroundTransparent, isWindowTransparencyRequested());
1531
1532 if (currentRuntimeDeviceTypeAndMode == QOhosRuntimeDeviceTypeAndMode::_2in1)
1533 submitSystemPropertyUpdate(m_updateData.focusable, focusable);
1534
1535 if (viewType() == ViewType::SubWindow)
1536 submitSystemPropertyUpdate(m_updateData.modality, m_ownerWindow->modality());
1537
1538 submitSystemPropertyUpdate(
1539 m_updateData.windowMinMaxCloseButtonsState,
1541 .maxButtonShown = windowFlags.testFlag(Qt::WindowMaximizeButtonHint),
1542 .minButtonShown = windowFlags.testFlag(Qt::WindowMinimizeButtonHint),
1543 .closeButtonShown = windowFlags.testFlag(Qt::WindowCloseButtonHint),
1544 });
1545
1546 submitSystemPropertyUpdate(
1547 m_updateData.windowStaysOnTop, windowFlags.testFlag(Qt::WindowStaysOnTopHint));
1548
1549 submitSystemPropertyUpdate(
1550 m_updateData.frameless,
1551 platformWindow->decorationPreset() == QOhosPlatformWindow::DecorationPreset::Frameless);
1552
1553 submitSystemPropertyUpdate(
1554 m_updateData.windowTransparentForInput, windowFlags.testFlag(Qt::WindowTransparentForInput));
1555
1556 flushSystemPropertyUpdatesImmediate(jsCallBatch);
1557
1558 bool disableWindowShadow = windowFlags.testFlag(Qt::WindowType::NoDropShadowWindowHint);
1559 if (disableWindowShadow)
1560 updateWindowShadowDisabled(jsCallBatch);
1561
1562 if (!m_ownerWindow->mask().isNull() && m_ohosWindowProxy != nullptr) {
1563 m_ohosWindowProxy->setWindowMask(
1564 QOhosWindowProxy::WindowMask {
1565 .windowMaskRegion = m_ownerWindow->mask(),
1566 },
1567 targetGeometryToSet.size(),
1568 jsCallBatch);
1569 }
1570 const auto privacyModeSetting = m_windowPropertiesProvider
1571 .tryGetProperty<bool, &QOhosPlatformWindow::windowPrivacyModeSettingProperty>();
1572 if (privacyModeSetting.has_value())
1573 setPrivacyMode(privacyModeSetting.value(), jsCallBatch);
1574
1575 const auto windowCornerRadius = m_windowPropertiesProvider
1576 .tryGetProperty<double, &QOhosPlatformWindow::windowCornerRadiusProperty>();
1577 if (windowCornerRadius.has_value())
1578 setWindowCornerRadius(windowCornerRadius.value(), jsCallBatch);
1579
1580 const auto keepScreenOn = m_windowPropertiesProvider
1581 .tryGetProperty<bool, &QOhosPlatformWindow::windowKeepScreenOnProperty>();
1582 if (keepScreenOn.has_value())
1583 setWindowKeepScreenOn(keepScreenOn.value(), jsCallBatch);
1584
1585 auto fixedSizeStateEnabled = m_windowPropertiesProvider
1586 .tryGetProperty<bool, &QOhosPlatformWindow::windowFixedSizeStateProperty>();
1587 if (fixedSizeStateEnabled.has_value())
1588 setFixedSizeStateEnabled(fixedSizeStateEnabled.value(), jsCallBatch);
1589
1590 auto windowDragResizable = m_windowPropertiesProvider
1591 .tryGetProperty<bool, &QOhosPlatformWindow::windowDragResizableProperty>();
1592 if (windowDragResizable.has_value())
1593 setWindowDragResizable(windowDragResizable.value(), jsCallBatch);
1594
1595 // NOTE - when position automatic is set we do not control window position
1596 // therefore no window callback about window position change will be sent
1597 // and the screen that given window belongs to needs to be synchronized
1598 bool shouldSynchronizeTargetDisplayIdWithQpa =
1599 qt_window_private(m_ownerWindow)->positionAutomatic
1600 && reason == WindowStateSyncReason::ViewTypeChanged
1601 && m_ohosWindowProxy != nullptr;
1602
1603 if (shouldSynchronizeTargetDisplayIdWithQpa)
1604 optDisplayIdToSynchronize = m_ohosWindowProxy->getWindowProperties().displayId;
1605 }
1606
1607 if (optDisplayIdToSynchronize) {
1608 QOhosDisplayInfo::JsDisplayId syntheticDisplayIdChangeEvent = optDisplayIdToSynchronize.value();
1609 Q_EMIT windowDisplayIdChanged(syntheticDisplayIdChangeEvent);
1610 }
1611}
1612
1613void QOhosView::flushSystemPropertyUpdatesImmediate(JsCallBatch &jsCallBatch)
1614{
1615 static const auto systemDataPropertyUpdateFuncPairsTuple = makeSystemUpdateDataPropertyUpdateFuncPairsTuple();
1616
1617 QtOhos::tupleForEach(
1618 systemDataPropertyUpdateFuncPairsTuple,
1619 [&](const auto &updateDataMemberPtrUpdateFuncPair) {
1620 auto memberPtr = updateDataMemberPtrUpdateFuncPair.first;
1621 auto updateFuncPtr = updateDataMemberPtrUpdateFuncPair.second;
1622 auto &property = m_updateData.*memberPtr;
1623 auto optUpdateRequest = std::exchange(property.optPendingUpdateRequest, {});
1624 if (optUpdateRequest.has_value())
1625 (this->*updateFuncPtr)(optUpdateRequest.value(), jsCallBatch);
1626 });
1627 m_updatePending = false;
1628}
1629
1630QOhosWindowProxy *QOhosView::windowProxyAfterPropertyUpdatesOrNull(JsCallBatch &jsCallBatch)
1631{
1632 if (m_ohosWindowProxy == nullptr)
1633 return nullptr;
1634
1635 flushSystemPropertyUpdatesImmediate(jsCallBatch);
1636 return m_ohosWindowProxy.get();
1637}
1638
1639void QOhosView::addForeignWindowChild(QOhosForeignWindow *foreignWindow)
1640{
1641 m_nativeNode->addForeignWindowChild(foreignWindow);
1642}
1643
1645{
1646 auto *surface = m_nativeNode->surfaceOrNull();
1647 return surface != nullptr
1648 ? surface->surfaceResolution()
1649 : std::nullopt;
1650}
1651
1653{
1654 return m_ohosWindowProxy != nullptr
1655 ? m_ohosWindowProxy->getImmersiveModeEnabledState()
1656 : false;
1657}
1658
1660{
1661 if (viewType() == ViewType::SubWindow) {
1662 auto *screen = m_ownerWindow->screen();
1663 return screen != nullptr && m_ownerWindow->geometry().size() == screen->geometry().size();
1664 }
1665
1666 return false;
1667}
1668
1669void QOhosView::updateWindowMinMaxCloseButtonsState(
1670 const WindowMinMaxCloseButtonsState &state, JsCallBatch &jsCallBatch)
1671{
1672 if (m_ohosWindowProxy != nullptr) {
1673 m_ohosWindowProxy->setWindowTitleButtonVisible(
1674 state.maxButtonShown, state.minButtonShown, state.closeButtonShown, jsCallBatch);
1675 }
1676}
1677
1678void QOhosView::updateWindowStaysOnTop(bool staysOnTop, JsCallBatch &jsCallBatch)
1679{
1680 if (m_ohosWindowProxy != nullptr)
1681 m_ohosWindowProxy->setWindowTopmost(staysOnTop, jsCallBatch);
1682}
1683
1684void QOhosView::updateWindowFrameless(bool frameless, JsCallBatch &jsCallBatch)
1685{
1686 bool supportsFramelessWindow = viewType() == ViewType::MainWindow || viewType() == ViewType::SubWindow;
1687 if (m_ohosWindowProxy != nullptr && supportsFramelessWindow) {
1688 m_ohosWindowProxy->setWindowDecorVisible(!frameless, jsCallBatch);
1689 m_ohosWindowProxy->setWindowTitleMoveEnabled(!frameless, jsCallBatch);
1690 }
1691}
1692
1693void QOhosView::setWindowDragResizable(bool dragResizable, JsCallBatch &jsCallBatch)
1694{
1695 if (m_ohosWindowProxy != nullptr)
1696 m_ohosWindowProxy->enableDrag(dragResizable, jsCallBatch);
1697}
1698
1700{
1701 setSystemUpdateProperty(&SystemUpdateData::windowMinMaxCloseButtonsState, state);
1702}
1703
1704void QOhosView::setWindowStaysOnTop(bool staysOnTop)
1705{
1706 setSystemUpdateProperty(&SystemUpdateData::windowStaysOnTop, staysOnTop);
1707}
1708
1709void QOhosView::setFramelessWindow(bool frameless)
1710{
1711 setSystemUpdateProperty(&SystemUpdateData::frameless, frameless);
1712}
1713
1714void QOhosView::updateWindowShadowDisabled(JsCallBatch &jsCallBatch)
1715{
1716 if (m_ohosWindowProxy != nullptr) {
1717 constexpr double windowShadowDisabledRadius = 0.0;
1718 m_ohosWindowProxy->setWindowShadowRadius(windowShadowDisabledRadius, jsCallBatch);
1719 }
1720}
1721
1722void QOhosView::setWindowCornerRadius(double radius, JsCallBatch &jsCallBatch)
1723{
1724 if (m_ohosWindowProxy != nullptr)
1725 m_ohosWindowProxy->setWindowCornerRadius(radius, jsCallBatch);
1726}
1727
1728void QOhosView::setWindowTransparentForInput(bool transparentForInput)
1729{
1730 setSystemUpdateProperty(&SystemUpdateData::windowTransparentForInput, transparentForInput);
1731}
1732
1734{
1735 if (m_ohosWindowProxy != nullptr)
1736 return m_ohosWindowProxy->startMoving();
1737 return false;
1738}
1739
1740void QOhosView::setPrivacyMode(bool privacyModeEnabled, JsCallBatch &jsCallBatch)
1741{
1742 if (m_ohosWindowProxy != nullptr)
1743 m_ohosWindowProxy->setWindowPrivacyMode(privacyModeEnabled, jsCallBatch);
1744}
1745
1746void QOhosView::startDrag(
1747 const std::vector<QImage> &images, const QPointF &hotspot,
1748 const QMimeData &mimeData, QOhosConsumer<Qt::DropAction> dropActionConsumer)
1749{
1750 m_nativeNode->startDrag(images, hotspot, mimeData, std::move(dropActionConsumer));
1751}
1752
1754{
1755 updateMainWindowRestored();
1756}
1757
1758void QOhosView::updateMainWindowRestored(JsCallBatch &jsCallBatch)
1759{
1760 const auto lastMainWindowHideMethod = std::exchange(m_lastMainWindowHideMethod, {});
1761 if (m_ohosWindowProxy == nullptr || !m_ohosWindowProxy->qtIsMainWindow())
1762 return;
1763
1764 if (!lastMainWindowHideMethod.has_value()) {
1765 m_ohosWindowProxy->restore(jsCallBatch);
1766 return;
1767 }
1768
1769 switch (lastMainWindowHideMethod.value()) {
1770 case WindowHideMethod::NativeNodeVisibility:
1771 case WindowHideMethod::Minimize:
1772 m_ohosWindowProxy->restore(jsCallBatch);
1773 break;
1774 case WindowHideMethod::HideAbility:
1775 m_ohosWindowProxy->showAbility(jsCallBatch);
1776 break;
1777 }
1778}
1779
1781 Qt::WindowStates previousWindowState, Qt::WindowStates currentWindowState)
1782{
1783 auto stateChange = previousWindowState ^ currentWindowState;
1784 if (stateChange.testFlag(Qt::WindowState::WindowMinimized) && !currentWindowState.testFlag(Qt::WindowState::WindowMinimized)) {
1785 if (viewType() == ViewType::MainWindow)
1787 else if (m_ohosWindowProxy)
1788 showWindow();
1789 }
1790
1791 if (stateChange.testFlag(Qt::WindowState::WindowMaximized) && currentWindowState.testFlag(Qt::WindowState::WindowMaximized)) {
1792 maximize();
1793 return;
1794 }
1795
1796 if (stateChange.testFlag(Qt::WindowState::WindowFullScreen) && currentWindowState.testFlag(Qt::WindowState::WindowFullScreen)) {
1798 return;
1799 }
1800
1801 if (stateChange.testFlag(Qt::WindowState::WindowMinimized) && currentWindowState.testFlag(Qt::WindowState::WindowMinimized)) {
1802 minimize();
1803 return;
1804 }
1805
1806 if (!currentWindowState) {
1807 recover();
1808 return;
1809 }
1810}
1811
1813 Qt::WindowFlags previousWindowFlags, Qt::WindowFlags currentWindowFlags)
1814{
1815 const auto flagsChange = previousWindowFlags ^ currentWindowFlags;
1816
1817 bool minMaxCloseButtonStateChanged = (flagsChange & (Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint)) != 0;
1818 bool windowTypeChanged =
1819 (previousWindowFlags & Qt::WindowType_Mask) != (currentWindowFlags & Qt::WindowType_Mask);
1820 bool framelessChanged = (flagsChange & Qt::FramelessWindowHint) != 0 || windowTypeChanged;
1821
1822 if (minMaxCloseButtonStateChanged || framelessChanged) {
1824 .maxButtonShown = currentWindowFlags.testFlag(Qt::WindowMaximizeButtonHint),
1825 .minButtonShown = currentWindowFlags.testFlag(Qt::WindowMinimizeButtonHint),
1826 .closeButtonShown = currentWindowFlags.testFlag(Qt::WindowCloseButtonHint),
1827 });
1828 }
1829
1830 bool windowStaysOnTopChanged = (flagsChange & Qt::WindowStaysOnTopHint) != 0;
1831 if (windowStaysOnTopChanged)
1832 setWindowStaysOnTop(currentWindowFlags.testFlag(Qt::WindowStaysOnTopHint));
1833
1834 bool transparentForInputChanged = (flagsChange & Qt::WindowTransparentForInput) != 0;
1835 if (transparentForInputChanged)
1836 setWindowTransparentForInput(currentWindowFlags.testFlag(Qt::WindowTransparentForInput));
1837
1838 if (framelessChanged) {
1839 auto *platformWindow = QOhosPlatformWindow::fromQWindow(m_ownerWindow);
1841 platformWindow->decorationPreset() == QOhosPlatformWindow::DecorationPreset::Frameless);
1842 }
1843
1844 bool focusableChanged = (flagsChange & Qt::WindowDoesNotAcceptFocus) != 0;
1845 if (focusableChanged) {
1846 bool windowAcceptsFocus = !currentWindowFlags.testFlag(Qt::WindowDoesNotAcceptFocus);
1847 setFocusable(windowAcceptsFocus);
1848 }
1849
1850 {
1851 JsCallBatch jsCallBatch(Q_FUNC_INFO);
1852
1853 bool disableWindowShadowChanged = (flagsChange & Qt::NoDropShadowWindowHint) != 0;
1854 if (disableWindowShadowChanged && currentWindowFlags.testFlag(Qt::NoDropShadowWindowHint))
1855 updateWindowShadowDisabled(jsCallBatch);
1856
1857 bool expandedClientAreaChanged = (flagsChange & Qt::ExpandedClientAreaHint) != 0;
1858 if (expandedClientAreaChanged && QOhosDeviceInfo::isPhone())
1859 applyPhoneWindowChrome(jsCallBatch);
1860 }
1861}
1862
1863QArkUi::QQtEmbeddedWindowNode::NodeAreaInfo QOhosView::nodeAreaInfo() const
1864{
1865 return m_nativeNode->nodeAreaInfo();
1866}
1867
1874
1876{
1877 return !(*this == other);
1878}
1879
1880void QOhosView::setOrResetWindowProxy(std::shared_ptr<QOhosWindowProxy> windowProxy, QWindow *optLogicalParent)
1881{
1882 m_ohosWindowProxy.reset();
1883 m_ohosWindowProxy = windowProxy
1884 ? QtOhos::makeSharedPtrWithAttachedExtraData<QOhosWindowProxy>(
1885 windowProxy,
1886 QWindowProxyRegistry::instance().registerQWindowWithWindowProxy(ownerWindow(), *windowProxy))
1887 : nullptr;
1888 m_optLogicalParent = optLogicalParent;
1889 syncWindowStateImmediate(WindowStateSyncReason::ViewTypeChanged);
1890}
1891
1893{
1894 auto *platformWindow = m_optLogicalParent != nullptr
1895 ? QOhosPlatformWindow::fromQWindowOrNull(m_optLogicalParent)
1896 : nullptr;
1897
1898 return platformWindow != nullptr
1899 ? platformWindow->ownedViewOrNull()
1900 : nullptr;
1901}
1902
1903const QOhosView *QOhosView::ancestorViewWithWindowOrNull() const
1904{
1905 const QOhosView *parent = viewParentOrNull();
1906 while (parent != nullptr && !parent->m_ohosWindowProxy)
1907 parent = parent->viewParentOrNull();
1908
1909 return parent != nullptr && parent->m_ohosWindowProxy
1910 ? parent
1911 : nullptr;
1912}
1913
1915{
1916 return m_ohosWindowProxy ? m_ohosWindowProxy->snapshot() : QPixmap();
1917}
1918
1920{
1921 return m_nativeNode->nodeScreenGeometryPixels();
1922}
1923
1925{
1926 return m_nativeNode->nodeParentRelativeGeometryPixels();
1927}
1928
1930{
1931 if (m_pendingSelfMinimizeEchoes <= 0)
1932 return false;
1933
1934 --m_pendingSelfMinimizeEchoes;
1935 return true;
1936}
1937
1938bool QOhosView::isFixedSizeStateEnabled() const
1939{
1940 return m_windowPropertiesProvider
1941 .tryGetProperty<bool, &QOhosPlatformWindow::windowFixedSizeStateProperty>()
1942 .value_or(false);
1943}
1944
1945bool QOhosView::isMaximizeRequestSupportedOrLogWarning() const
1946{
1947 if (viewType() != ViewType::SubWindow)
1948 return true;
1949
1950 if (!m_ohosWindowProxy->isSubWindowMaximizeSupported()) {
1951 qCWarning(QtForOhos) << Q_FUNC_INFO << m_ownerWindow
1952 << "maximize request ignored: the sub window was created without maximize support"
1953 " (requires API 26, Qt::WindowMaximizeButtonHint and free window mode at creation)";
1954 return false;
1955 }
1956
1957 if (isFixedSizeStateEnabled()) {
1958 qCWarning(QtForOhos) << Q_FUNC_INFO << m_ownerWindow
1959 << "maximize request ignored: the sub window has a fixed size";
1960 return false;
1961 }
1962
1963 return true;
1964}
1965
1966QT_END_NAMESPACE
void externalContentClickDetected()
static QOhosPlatformIntegration * instance()
static WindowGeometryPersistencePolicy getMainWindowGeometryPersistencePolicy()
QOhosInputMethodEventHandler * inputMethodEventHandler() const
bool mainWindowTagValueOrFalse() const
bool floatWindowTagValueOrFalse() const
DecorationPreset decorationPreset() const
void clearNativeWindowSurface()
void windowStatusChange(QOhosWindowProxy::WindowStatus windowStatus)
void setParentOrReparent(QOhosView &parentView)
void requestActivate()
bool consumePendingSelfMinimizeEcho()
void setCursor(const QCursor &cursor)
void handleWindowFlagsChange(Qt::WindowFlags previousWindowFlags, Qt::WindowFlags currentWindowFlags)
void addForeignWindowChild(QOhosForeignWindow *foreignWindow)
void handleWindowStateChange(Qt::WindowStates previousWindowState, Qt::WindowStates currentWindowState)
void restoreMainWindow()
void setFocusable(bool focusable)
QArkUi::QQtEmbeddedWindowNode::NodeAreaInfo nodeAreaInfo() const
QOhosSurface * surfaceOrNull() const
void windowVisibilityChange(bool visibility)
QList< QRect > displayCutoutRects() const
void setTransparentBackground(bool transparent)
void setModality(Qt::WindowModality modality)
WId viewWindowId() const
bool isFullscreenImmersiveModeEnabled()
void setFramelessWindow(bool frameless)
QPixmap makeSnapshot() const
void setWindowTransparentForInput(bool transparentForInput)
void tryDetachFromEmbeddedParent()
ViewType viewType() const
QMargins avoidAreaMargins(QOhosWindowProxy::AvoidAreaType type) const
QWindow * ownerWindow() const
void handleSurfaceContentsUpdated()
void hide()
WindowStateSyncReason
Definition qohosview.h:93
void forceGeometryUpdate()
void externalContentInteractionDetected()
void setWindowMinMaxCloseButtonState(const WindowMinMaxCloseButtonsState &state)
void setWindowMask(const QOhosWindowProxy::WindowMask &windowMask)
void setSizeLimits(const QSize &minSize, const QSize &maxSize)
std::optional< QSize > surfaceResolution() const
QHarmonyJsObjectRef abilityObjectRef() const
QOhosView(QWindow *ownerWindow, QSharedPointer< QNativeNode > nativeNode, QOhosPropertiesProvider propertiesProvider, QWindow *optLogicalParent)
void windowTouchOutside()
void handlePaletteChange()
void lower()
void windowDisplayIdChanged(QOhosDisplayInfo::JsDisplayId)
void minimize()
QHarmonyJsObjectRef jsWindowObjectRef() const
void setWindowStaysOnTop(bool staysOnTop)
void setFullScreen()
void maximize()
ViewGeometry viewGeometry() const
void setSize(const QSize &size)
void recover()
bool startMoving()
void setPosition(const QPoint &position)
bool isSubWindowCoveringFullScreen() const
void showImmediate()
QRect nodeParentRelativeGeometryPixels() const
void setTitle(const QString &title)
const QOhosView * viewParentOrNull() const
QRect nodeScreenGeometryPixels() const
void raise()
QtOhosQpa::enums::ohos::window::MaximizePresentation MaximizePresentation
static std::shared_ptr< QOhosWindowProxy > createFloatWindow(const FloatWindowCreateInfo &createInfo)
void setWindowRectAutoSave(bool enabed, JsCallBatch &jsCallBatch=JsCallBatch::immediate())
QtOhosQpa::enums::ohos::window::WindowEventType WindowEventType
std::optional< QOhosDisplayInfo::JsDisplayId > tryGetMainWindowJsDisplayId() const
QtOhosQpa::enums::ohos::bundle::bundleManager::SupportWindowMode SupportWindowMode
QOhosWindowProxyExistingMainWindowCreateInfo ExistingMainWindowCreateInfo
bool tryMaximize(MaximizePresentation maximizePresentation)
static std::shared_ptr< QOhosWindowProxy > createForExistingMainWindow(const ExistingMainWindowCreateInfo &createInfo)
QtOhosQpa::enums::ohos::window::ModalityType ModalityType
bool isWindowRectAutoSave() const
QOhosWindowProxySubWindowCreateInfo SubWindowCreateInfo
QtOhosQpa::enums::ohos::window::AvoidAreaType AvoidAreaType
QOhosWindowProxyMainWindowCreateInfo MainWindowCreateInfo
QOhosWindowProxyFloatWindowCreateInfo FloatWindowCreateInfo
static std::shared_ptr< QOhosWindowProxy > createMainWindow(const MainWindowCreateInfo &createInfo)
QOhosWindowProxy::AvoidAreaType AvoidAreaType
Definition qohosview.cpp:92
AvoidAreaCache(std::function< AvoidArea(AvoidAreaType)> avoidAreasProvider)
AvoidArea getStoredOrRetrieveFromWindowProxy(AvoidAreaType avoidAreaType)
void put(AvoidAreaType avoidAreaType, const AvoidArea &avoidArea)
static QWindowProxyRegistry & instance()
Combined button and popup list for selecting options.
QSize evaluateGeometrySizeBasedOnFrameGeometry(const QPoint &geometryOrigin, const QRect &frameGeometry)
std::function< void(SignalParams...)> makeViewConditionalSignalEmitter(QPointer< QOhosView > viewPtr, std::function< bool(QOhosView &)> predicate, void(QOhosView::*signalFuncPtr)(SignalParams ...))
Definition qohosview.cpp:79
std::optional< QColor > tryGetBackgroundColorFromWindow(QWindow *window)
QCursor makeTransparentBitmapCursor(QSize cursorSize)
bool hasMaximizeButtonHintForSubWindow(const QWindow *qWindow)
QWindow * syntheticParentForQWindowOrNull(QWindow *qWindow)
std::optional< QOhosWindowProxy::ModalityType > mapQtWindowModalityToOhosOrDefault(Qt::WindowModality windowModality)
Definition qohosview.cpp:57
QBitmap getCursorMask(const QCursor &cursor)
QOhosView * viewOrNull(QWindow *window)
QOhosView * hostViewOrNull(QWindow *window)
void tryUpdateMaximumMarginsFromAvoidArea(QMargins &marginsToUpdate, const QOhosWindowProxy::AvoidArea &avoidArea)
QWindow * getFirstTopLevelWindowWithSystemFocusOrNull()
QWindow * getFirstTopLevelWindowOrNull()
QBitmap getCursorBitmap(const QCursor &cursor)
ViewGeometryPersistencePolicy determineViewGeometryPersistencePolicy()
std::optional< QOhosDisplayInfo::JsDisplayId > tryGetSubWindowJsDisplayId(QWindow *logicalParent, QOhosWindowProxy &windowProxy)
WindowGeometryPersistenceState syncWindowGeometryPersistenceState(QOhosWindowProxy *windowProxy, QtOhos::JsCallBatch &jsCallBatch)
ViewTypeInfo determineViewTypeAndLogicalParent(const QOhosPlatformWindow *platformWindow)
QImage createImageFromBitmapAndMask(const QBitmap &bitmap, const QBitmap &mask)
bool acquireAndCleanPendingAutoStartedInstanceWindowFlag()
bool isHandheldDeviceType()
QOhosRuntimeDeviceTypeAndMode queryQOhosRuntimeDeviceAndMode()
bool operator==(const WindowMinMaxCloseButtonsState &other) const
bool operator!=(const WindowMinMaxCloseButtonsState &other) const
QOhosView::ViewType viewType
Definition qohosview.cpp:47