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
qohosfloatingwindow.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 <qohosfloatingwindow.h>
5
6#include <QtCore/private/qohoslogger_p.h>
7#include "QtGui/private/qguiapplication_p.h"
8#include <QtGui/private/qwindow_p.h>
9#include <qohosdeviceinfo_p.h>
10#include <qohosinputcontext.h>
11#include <qohosinputmethodeventhandler.h>
12#include <qohosjsmain.h>
13#include <qohosplatformbackingstore.h>
14#include <qohosplatformwindow.h>
15#include <qohosruntimedevicetypeandmode.h>
16#include <qohossettings.h>
17#include <render/qohossurface.h>
18#include <render/qohoswindowproxy.h>
19#include <render/qwindowproxyregistry.h>
20
22
23namespace {
24
26 QWindow *window, QOhosWindowProxy::RectChangeOptions rectChangeOptions)
27{
29 && rectChangeOptions.rect.isValid()
30 && window->geometry() != rectChangeOptions.rect
31 && rectChangeOptions.reason == QOhosWindowProxy::RectChangeReason::UNDEFINED;
32}
33
34bool shouldClearQtFocusOnSystemFocusLoss(QWindow *windowLosingSystemFocus)
35{
36 return QGuiApplicationPrivate::focus_window == windowLosingSystemFocus
37 || (QGuiApplicationPrivate::focus_window != nullptr
38 && QOhosPlatformWindow::platformWindowFlagsForQWindow(
39 QGuiApplicationPrivate::focus_window).testFlag(Qt::WindowDoesNotAcceptFocus));
40}
41
43{
44 return !QWindowProxyRegistry::instance().queryWindowsWithSystemWindowAndFocus().empty();
45}
46
47}
48
50: QOhosPlatformWindow(window)
51{
52}
53
55
56void QOhosFloatingWindow::setGeometry(const QRect &rect)
57{
58 auto *view = ownedViewOrNull();
59 bool geometryControlledBySystem =
61 && view != nullptr
62 && view->viewType() == QOhosView::ViewType::MainWindow;
63
64 if (geometryControlledBySystem) {
65 const auto actualGeometry = QPlatformWindow::geometry();
66 QPlatformWindow::setGeometry(rect);
67 setWindowGeometryFromOhos(actualGeometry);
68 return;
69 }
70
71 auto *targetScreen = geometryConversionScreen(rect);
72 const bool onlyFramePositionChanged =
73 qt_window_private(window())->positionPolicy == QWindowPrivate::WindowFrameInclusive
74 && rect.size() == geometry().size();
75 QOhosPlatformWindow::setGeometry(rect);
76 m_lastRequestRecordedAsNormalGeometry =
77 !windowStates().testAnyFlags(Qt::WindowMaximized | Qt::WindowFullScreen);
78 if (m_lastRequestRecordedAsNormalGeometry)
79 m_optNormalGeometry = geometry();
80 const auto frameGeometry = lastRequestedWindowFrameGeometry();
81
82 if (view != nullptr) {
83 if (!qt_window_private(window())->positionAutomatic)
84 view->setPosition(frameGeometry.topLeft());
85 if (!onlyFramePositionChanged)
86 view->setSize(frameGeometry.size());
87
88 const QSize sizeBeforeRequest = lastReportedWindowGeometry().size();
89 if (view->viewType() == QOhosView::ViewType::EmbeddedWindow)
90 setWindowGeometryFromOhos(rect);
91
92 if (rect.size() != sizeBeforeRequest)
93 m_optSizeBeforeResizeRequest = sizeBeforeRequest;
94 else
95 m_optSizeBeforeResizeRequest.reset();
96 }
97
98 if (targetScreen != nullptr && targetScreen != QPlatformWindow::screen()) {
99 QWindowSystemInterface::handleWindowScreenChanged<QWindowSystemInterface::SynchronousDelivery>(
100 window(), targetScreen->screen());
101 }
102}
103
105{
106 return m_optNormalGeometry.value_or(QRect());
107}
108
110{
111 if (!visible)
112 m_view->hide();
113 else
114 m_view->showImmediate();
115 startAsyncWaitForNodeResizeIfNeeded();
116}
117
119{
120 auto windowObjectName = window()->objectName();
121
122 qOhosPrintfDebug(
123 "%s: winId called for window named: \"%s\"",
124 Q_FUNC_INFO,
125 windowObjectName.toStdString().c_str());
126
127 return reinterpret_cast<WId>(m_view->viewWindowId());
128}
129
131{
132 auto *view = ownedViewOrNull();
133 if (view != nullptr) {
134 view->raise();
135 window()->requestUpdate();
136 }
137}
138
140{
141 auto *view = ownedViewOrNull();
142 if (view != nullptr) {
143 view->lower();
144 window()->requestUpdate();
145 }
146}
147
149{
150 auto *view = ownedViewOrNull();
151 return view != nullptr ? view->surfaceOrNull() : nullptr;
152}
153
155{
156 return m_view.get();
157}
158
160{
162 m_view = QOhosView::createForWindow(this, propertiesProvider());
163
164 auto *qWindow = window();
165
166 // HACK
167 // There is no functionality to fetch the initial window status from ohos
168 // We can only listen for the changes - we need to assume sane default for tablets
169 // so assign the default most-likely status of the tablet here.
170 if (m_view->viewType() == QOhosView::ViewType::MainWindow
171 && !QOhosSettings::instance().isWindowPcModeEnabled()
172 && !m_lastWindowStatusType.has_value()) {
173 m_lastWindowStatusType = QOhosWindowProxy::WindowStatusType::FULL_SCREEN;
174 }
175
176 if (isFloatingAsRequestedAndReported()) {
177 m_optNormalGeometry = geometry();
178 m_optConfirmedNormalGeometry = m_optNormalGeometry;
179 }
180
181 QObject::connect(
182 m_view.get(), &QOhosView::externalContentInteractionDetected,
183 qWindow, &QOhosPlatformWindow::closeAllActivePopups);
184
185 QObject::connect(
186 m_view.get(), &QOhosView::nodeAreaChanged, m_view.get(),
187 [this](QArkUi::QQtEmbeddedWindowNode::NodeAreaInfo event) {
188 handleNodeResizeEvent(event);
189 });
190
191 QObject::connect(
192 m_view.get(), &QOhosView::windowEvent,
193 qWindow,
194 [this](QOhosWindowProxy::WindowEvent evt) { handleWindowEvent(evt); });
195
196 QObject::connect(
197 m_view.get(), &QOhosView::windowStatusChange,
198 qWindow,
199 [this](QOhosWindowProxy::WindowStatus evt) { handleWindowStatusChange(evt); });
200
201 QObject::connect(
202 m_view.get(), &QOhosView::windowVisibilityChange,
203 qWindow,
204 [this](bool visible) { handleWindowVisibilityChange(visible); });
205
206 QObject::connect(
207 m_view.get(), &QOhosView::windowResizeRejected,
208 qWindow,
209 [this](QSize rejectedSize) { handleWindowResizeRejected(rejectedSize); });
210
211 QObject::connect(
212 m_view.get(), &QOhosView::windowMoveRejected,
213 qWindow,
214 [this](QPoint rejectedPosition) { handleWindowMoveRejected(rejectedPosition); });
215
216 bool monitorAvoidAreaChange =
217 !(isHandheldDeviceType() && m_view->viewType() == QOhosView::ViewType::SubWindow);
218
219 if (monitorAvoidAreaChange) {
220 QObject::connect(
221 m_view.get(), &QOhosView::avoidAreaChanged,
222 qWindow,
223 [this](QOhosWindowProxy::AvoidAreaType avoidAreaType,
224 const QOhosWindowProxy::AvoidArea &systemAvoidArea) {
225 handleAvoidAreaChanged(avoidAreaType, systemAvoidArea);
226 });
227 }
228
229 QObject::connect(
230 m_view.get(), &QOhosView::windowRectChangedInGlobalDisplay,
231 qWindow,
232 [this](const QOhosWindowProxy::RectChangeOptions &rectChangeOptions) {
233 handleWindowRectChanged(rectChangeOptions);
234 });
235
236 QObject::connect(qWindow, &QWindow::modalityChanged, m_view.get(), &QOhosView::setModality);
237
238 QObject::connect(qWindow, &QWindow::screenChanged, m_view.get(), [this]() { propagateSizeHints(); });
239
240 QObject::connect(
241 m_view.get(), &QOhosView::surfaceStatusChanged,
242 qWindow,
243 [this](const std::optional<QSize> &optSurfaceSize) { handleSurfaceStatusChanged(optSurfaceSize); });
244
245 QObject::connect(qWindow, &QWindow::windowTitleChanged, m_view.get(), &QOhosView::setTitle);
246
247 QObject::connect(
248 m_view.get(), &QOhosView::windowDisplayIdChanged,
249 qWindow,
250 [this](QOhosDisplayInfo::JsDisplayId displayId) { handleWindowDisplayIdChanged(displayId); });
251}
252
253void QOhosFloatingWindow::restoreWindowCurrentCursorIfNeeded()
254{
255 auto *view = ownedViewOrNull();
256 if (view != nullptr && m_cursor.has_value())
257 view->setCursor(m_cursor.value());
258}
259
261 Qt::WindowFlags previousWindowFlags, Qt::WindowFlags currentWindowFlags)
262{
263 auto *view = ownedViewOrNull();
264 if (view == nullptr)
265 return;
266
267 view->handleWindowFlagsChange(previousWindowFlags, currentWindowFlags);
268}
269
271 Qt::WindowStates oldWindowState, Qt::WindowStates currentWindowState)
272{
273 auto *view = ownedViewOrNull();
274 if (view == nullptr)
275 return;
276
277 view->handleWindowStateChange(oldWindowState, currentWindowState);
278}
279
280void QOhosFloatingWindow::internalHijackSystemFocusAsPopup()
281{
282 QWindowSystemInterface::handleFocusWindowChanged(window(), Qt::PopupFocusReason);
283}
284
285void QOhosFloatingWindow::focusHijackingPopupHidden()
286{
287 auto systemFocusedWindows = QWindowProxyRegistry::instance().queryWindowsWithSystemWindowAndFocus();
288 QWindowSystemInterface::handleFocusWindowChanged(
289 !systemFocusedWindows.empty() ? systemFocusedWindows.front() : nullptr,
290 Qt::ActiveWindowFocusReason);
291}
292
293void QOhosFloatingWindow::setMask(const QRegion &region)
294{
295 m_windowMask = region;
296
297 auto *view = ownedViewOrNull();
298 if (view == nullptr)
299 return;
300
301 view->setWindowMask(QOhosWindowProxy::WindowMask{region});
302}
303
305{
306 auto *view = ownedViewOrNull();
307 if (view != nullptr)
308 return view->startMoving();
309 return false;
310}
311
313{
314 if (windowFlags().testFlag(Qt::WindowDoesNotAcceptFocus) && window()->type() == Qt::Popup) {
315 internalHijackSystemFocusAsPopup();
316 return;
317 }
318
320}
321
322void QOhosFloatingWindow::handleWindowEvent(QOhosWindowProxy::WindowEvent evt)
323{
324 auto *qWindow = window();
325 bool windowAcceptsFocusAndInput = checkWindowAcceptsFocus() && checkWindowAcceptsInput();
326 auto previousWindowEventType = std::exchange(m_lastWindowEventType, evt.type);
327
328 if (evt.type != QOhosWindowProxy::WindowEventType::WINDOW_SHOWN)
329 m_activationEventDeferredUntilWindowUnblocked.reset();
330 if (evt.type == QOhosWindowProxy::WindowEventType::WINDOW_INACTIVE
331 || evt.type == QOhosWindowProxy::WindowEventType::WINDOW_HIDDEN) {
332 m_activationEventDeferredUntilExposed.reset();
333 }
334
335 switch (evt.type) {
336 case QOhosWindowProxy::WindowEventType::WINDOW_ACTIVE:
337 {
338 if (m_view->viewType() != QOhosView::ViewType::EmbeddedWindow && !isExposed()) {
339 m_activationEventDeferredUntilExposed = evt;
340 return;
341 }
342
343 QWindow *modalWindow = nullptr;
344 if (QGuiApplicationPrivate::instance()->isWindowBlocked(qWindow, &modalWindow)
345 && qWindow != modalWindow) {
346 if (qWindow->isTopLevel())
347 m_activationEventDeferredUntilWindowUnblocked = evt;
348 modalWindow->requestActivate();
349 return;
350 }
351
352 // FIXME - restore cursor set by the User every time window is activated.
353 // Perform it because Ohos overrides it with default "standard arrow" cursor (when
354 // window is deactivated) and does not bring selected cursor back (when window is
355 // activated again). This bahaviour is Ohos platform issue.
356 restoreWindowCurrentCursorIfNeeded();
357 if (windowAcceptsFocusAndInput) {
358 QWindowSystemInterface::handleFocusWindowChanged(qWindow, Qt::ActiveWindowFocusReason);
360 }
361 break;
362 }
363 case QOhosWindowProxy::WindowEventType::WINDOW_INACTIVE:
364 if (!windowAcceptsFocusAndInput)
365 break;
366 if (shouldClearQtFocusOnSystemFocusLoss(qWindow) && !anyQtWindowHoldsSystemFocus())
367 QWindowSystemInterface::handleFocusWindowChanged(nullptr, Qt::ActiveWindowFocusReason);
369 break;
370 case QOhosWindowProxy::WindowEventType::WINDOW_HIDDEN:
371 if (!checkWindowAcceptsFocus() && QGuiApplicationPrivate::focus_window == qWindow)
372 focusHijackingPopupHidden();
373 updateMinimizedFromPlatformVisibility(false);
374 setShownFromOhos(false);
375 break;
376 case QOhosWindowProxy::WindowEventType::WINDOW_SHOWN:
377 // HACK
378 // This is a fix for the windows hidden using `QWidget::hide()`.
379 // When a window is hidden and then bring back from the system Dock
380 // Qt side keeps wrong (hidden) state. This causes different issues due to
381 // improper state. To fix it - change visibility state based on the system
382 // window state.
383 if (previousWindowEventType == QOhosWindowProxy::WindowEventType::WINDOW_HIDDEN && !qWindow->isVisible())
384 qWindow->setVisible(true);
385 updateMinimizedFromPlatformVisibility(true);
386 setShownFromOhos(true);
387 startAsyncWaitForNodeResizeIfNeeded();
388 break;
389 case QOhosWindowProxy::WindowEventType::WINDOW_DESTROYED:
391 return;
392 }
393}
394
395void QOhosFloatingWindow::updateMinimizedFromPlatformVisibility(bool platformShown)
396{
397 if (m_view->viewType() != QOhosView::ViewType::SubWindow)
398 return;
399
400 const bool platformHidden = !platformShown;
401 if (platformHidden && !window()->isVisible())
402 return;
403
404 auto states = windowStates();
405 if (states.testFlag(Qt::WindowMinimized) == platformHidden)
406 return;
407
408 states.setFlag(Qt::WindowMinimized, platformHidden);
409 setWindowStateFromOhos(states);
410}
411
412void QOhosFloatingWindow::handleWindowStatusChange(QOhosWindowProxy::WindowStatus evt)
413{
414 auto *qWindow = window();
415 qCDebug(
416 QtForOhos,
417 "Window status changed, window: %p(%s) status: %d",
418 qWindow,
419 qPrintable(qWindow->objectName()),
420 evt.type);
421
422 // Drop duplicate FULL_SCREEN notifications; MainWindow views in non-PC mode
423 // are exempt because initialize() pre-seeds their status to FULL_SCREEN.
424 if (m_lastWindowStatusType == evt.type
425 && m_lastWindowStatusType == QOhosWindowProxy::WindowStatusType::FULL_SCREEN
426 && !(m_view->viewType() == QOhosView::ViewType::MainWindow
427 && !QOhosSettings::instance().isWindowPcModeEnabled())) {
428 qCDebug(QtForOhos, "Window status is the same as the previous one");
429 return;
430 }
431
432 Qt::WindowStates windowStatesToSet = windowStates();
433 m_lastWindowStatusType = evt.type;
434
435 switch (evt.type) {
436 // NOTE - Qt::WindowFullScreen and Qt::WindowMaximized represent the exact same
437 // OHOS Window state.
438 case QOhosWindowProxy::WindowStatusType::FULL_SCREEN:
439 case QOhosWindowProxy::WindowStatusType::MAXIMIZE:
440 windowStatesToSet = m_view->isFullscreenImmersiveModeEnabled()
441 ? Qt::WindowState::WindowFullScreen
442 : Qt::WindowState::WindowMaximized;
443 break;
444 case QOhosWindowProxy::WindowStatusType::MINIMIZE:
445 if (m_view->consumePendingSelfMinimizeEcho()) {
446 // The window is hidden in Qt's view, so ignore this echo entirely.
447 // Deliberately skip setWindowStateFromOhos() and the node-resize
448 // reconciliation below: reporting WindowMinimized would cause a
449 // spurious restore -> QShowEvent, and the reconciliation would apply
450 // the minimized node's geometry to a window Qt still considers to be
451 // at its normal (pre-hide) geometry. showImmediate() reconciles it
452 // when the window is shown again.
453 return;
454 }
455 windowStatesToSet.setFlag(Qt::WindowState::WindowMinimized);
456 break;
457 case QOhosWindowProxy::WindowStatusType::UNDEFINED:
458 case QOhosWindowProxy::WindowStatusType::FLOATING:
459 case QOhosWindowProxy::WindowStatusType::SPLIT_SCREEN:
460 windowStatesToSet = Qt::WindowState::WindowNoState;
461 break;
462 }
463
464 setWindowStateFromOhos(windowStatesToSet);
465
466 // HACK
467 // There is no functionality in OHOS to fetch the window status that would allow checking
468 // the value while processing the WINDOW_SHOWN window event type. Therefore, the window
469 // geometry view needs to be recalculated with any change.
470 startAsyncWaitForNodeResizeIfNeeded();
471}
472
473void QOhosFloatingWindow::handleWindowVisibilityChange(bool visible)
474{
475 auto *qWindow = window();
476 qCDebug(
477 QtForOhos,
478 "Window %p(%s) visibility changed: %s",
479 qWindow,
480 qPrintable(qWindow->objectName()),
481 visible ? "true" : "false");
482 if (visible && m_windowMask.has_value())
483 m_view->setWindowMask(QOhosWindowProxy::WindowMask{m_windowMask.value()});
484
485 setShownFromOhos(visible);
486}
487
488void QOhosFloatingWindow::setShownFromOhos(bool shown)
489{
490 m_shownFromOhos = shown;
491 updateExposedFromOhos();
492}
493
494void QOhosFloatingWindow::updateExposedFromOhos()
495{
496 const bool exposed = m_shownFromOhos && m_view->surfaceOrNull();
497 if (exposed == isExposed())
498 return;
499
500 setExposedFromOhos(exposed);
501 activateIfDeferredUntilExposed();
502}
503
504void QOhosFloatingWindow::activateIfDeferredUntilExposed()
505{
506 if (!isExposed())
507 return;
508
509 if (auto deferredActivationEvent = std::exchange(m_activationEventDeferredUntilExposed, std::nullopt))
510 handleWindowEvent(*deferredActivationEvent);
511}
512
513void QOhosFloatingWindow::handleAvoidAreaChanged(
514 QOhosWindowProxy::AvoidAreaType avoidAreaType,
515 const QOhosWindowProxy::AvoidArea &systemAvoidArea)
516{
517 const auto &cached = m_avoidAreaCache[avoidAreaType];
518
519 bool actuallyChanged =
520 cached.visible != systemAvoidArea.visible
521 || cached.leftRect != systemAvoidArea.leftRect
522 || cached.rightRect != systemAvoidArea.rightRect
523 || cached.bottomRect != systemAvoidArea.bottomRect
524 || cached.topRect != systemAvoidArea.topRect;
525
526 if (actuallyChanged) {
527 m_avoidAreaCache[avoidAreaType] = systemAvoidArea;
528 qCDebug(QtForOhos) << "Avoid area changed:"
529 << static_cast<int>(avoidAreaType)
530 << "visible:" << systemAvoidArea.visible
531 << "top:" << systemAvoidArea.topRect
532 << "left:" << systemAvoidArea.leftRect
533 << "right:" << systemAvoidArea.rightRect
534 << "bottom:" << systemAvoidArea.bottomRect;
535 startAsyncWaitForNodeResizeIfNeeded();
536 }
537}
538
539void QOhosFloatingWindow::handleSurfaceStatusChanged(const std::optional<QSize> &optSurfaceSize)
540{
541 m_optLastSurfaceSize = optSurfaceSize;
542 bool hasSurface = m_view->surfaceOrNull() != nullptr;
543 if (m_view->viewType() == QOhosView::ViewType::EmbeddedWindow) {
544 setExposedFromOhos(hasSurface);
545 } else {
546 updateExposedFromOhos();
547 }
548
549 if (hasSurface)
550 startAsyncWaitForNodeResizeIfNeeded();
551}
552
553void QOhosFloatingWindow::handleWindowDisplayIdChanged(QOhosDisplayInfo::JsDisplayId displayId)
554{
555 setDisplayIdFromOhos(displayId);
556 startAsyncWaitForNodeResizeIfNeeded();
557}
558
559void QOhosFloatingWindow::handleWindowRectChanged(
560 const QOhosWindowProxy::RectChangeOptions &rectChangeOptions)
561{
562 bool needsCloseAllActivePopups = rectChangeOptions.reason == QOhosWindowProxy::RectChangeReason::DRAG_START;
563
564 qCDebug(QtForOhos)
565 << "windowRectChanged window:" << window()
566 << "rect:" << rectChangeOptions.rect
567 << "reason:" << static_cast<int>(rectChangeOptions.reason);
568
569 if (rectChangeOptions.rect.size() != lastRequestedWindowFrameGeometry().size())
570 m_optSizeBeforeResizeRequest.reset();
571
572 startAsyncWaitForNodeResizeIfNeeded();
573
574 if (isWindowRotatedByTabletScreenRotation(window(), rectChangeOptions))
575 needsCloseAllActivePopups = true;
576
577 if (needsCloseAllActivePopups)
579}
580
581void QOhosFloatingWindow::handleWindowResizeRejected(const QSize &rejectedSize)
582{
583 qCDebug(QtForOhos) << "window resize rejected, window:" << window() << "size:" << rejectedSize;
584
585 if (rejectedSize != lastRequestedWindowFrameGeometry().size())
586 return;
587
588 m_optSizeBeforeResizeRequest.reset();
589 if (m_lastRequestRecordedAsNormalGeometry)
590 m_optNormalGeometry = m_optConfirmedNormalGeometry;
591 startAsyncWaitForNodeResizeIfNeeded();
592 reportWindowStatesAfterRejectedGeometryUpdateIfNeeded();
593}
594
595void QOhosFloatingWindow::handleWindowMoveRejected(const QPoint &rejectedPosition)
596{
597 qCDebug(QtForOhos) << "window move rejected, window:" << window() << "position:" << rejectedPosition;
598
599 if (rejectedPosition != lastRequestedWindowFrameGeometry().topLeft())
600 return;
601
602 if (m_lastRequestRecordedAsNormalGeometry)
603 m_optNormalGeometry = m_optConfirmedNormalGeometry;
604 startAsyncWaitForNodeResizeIfNeeded();
605 reportWindowStatesAfterRejectedGeometryUpdateIfNeeded();
606
607 auto *reportedScreen = QOhosPlatformWindow::screen();
608 if (reportedScreen != nullptr && reportedScreen != QPlatformWindow::screen()) {
609 QWindowSystemInterface::handleWindowScreenChanged<QWindowSystemInterface::SynchronousDelivery>(
610 window(), reportedScreen->screen());
611 }
612}
613
614void QOhosFloatingWindow::reportWindowStatesAfterRejectedGeometryUpdateIfNeeded()
615{
616 // HACK
617 // QWidget clears Maximized and FullScreen on a move or resize and restores them only from a
618 // reported window state.
619 if (windowStates().testAnyFlags(Qt::WindowMaximized | Qt::WindowFullScreen))
620 QWindowSystemInterface::handleWindowStateChanged(window(), windowStates());
621}
622
624{
625 if (event->type() == QEvent::WindowUnblocked) {
626 auto deferredActivationEvent =
627 std::exchange(m_activationEventDeferredUntilWindowUnblocked, std::nullopt);
628 const QWindow *windowHoldingFocus = QGuiApplicationPrivate::focus_window;
629 bool noVisibleWindowHoldsFocus =
630 windowHoldingFocus == nullptr || !windowHoldingFocus->isVisible();
631 if (deferredActivationEvent && noVisibleWindowHoldsFocus)
632 handleWindowEvent(*deferredActivationEvent);
633 }
634
635 if (event->type() == QEvent::Timer) {
636 auto *timerEvent = static_cast<QTimerEvent *>(event);
637 if (m_view && timerEvent->timerId() == m_geometryChangeTimer.timerId()) {
638 auto syntheticEvent = m_view->nodeAreaInfo();
639 handleNodeResizeEvent(syntheticEvent);
640 }
641 }
642
643 return QOhosPlatformWindow::windowEvent(event);
644}
645
646bool QOhosFloatingWindow::isFloatingAsRequestedAndReported() const
647{
648 constexpr auto nonFloatingStates =
649 Qt::WindowMinimized | Qt::WindowMaximized | Qt::WindowFullScreen;
650 const bool floatingRequested = !windowStates().testAnyFlags(nonFloatingStates);
651 const bool floatingReported = !m_lastWindowStatusType
652 || *m_lastWindowStatusType == QOhosWindowProxy::WindowStatusType::FLOATING;
653 return floatingRequested && floatingReported;
654}
655
656void QOhosFloatingWindow::startAsyncWaitForNodeResizeIfNeeded()
657{
658 constexpr auto geometryChangeEventTimeoutMs = std::chrono::milliseconds(16);
659
660 if (!m_geometryChangeTimer.isActive()) {
661 m_geometryChangeTimer.start(
662 geometryChangeEventTimeoutMs.count(),
663 Qt::PreciseTimer,
664 window());
665 }
666}
667
668void QOhosFloatingWindow::handleNodeResizeEvent(const QArkUi::QQtEmbeddedWindowNode::NodeAreaInfo &areaChangeEvent)
669{
670 m_geometryChangeTimer.stop();
671
672 if (Q_UNLIKELY(!m_view))
673 return;
674
675 if (m_view->viewType() != QOhosView::ViewType::EmbeddedWindow) {
676 const QSize size = areaChangeEvent.screenGeometryPixels.size();
677 if (size.isEmpty())
678 return;
679
680 if (size != m_optSizeBeforeResizeRequest) {
681 m_optSizeBeforeResizeRequest.reset();
682 const QRect nodeGeometry(areaChangeEvent.globalRelativeOffsetPixels, size);
683 const bool isAlreadyReported =
684 nodeGeometry == lastReportedWindowGeometry() && nodeGeometry == geometry();
685 if (isFloatingAsRequestedAndReported()) {
686 m_optNormalGeometry = nodeGeometry;
687 m_optConfirmedNormalGeometry = nodeGeometry;
688 }
689 if (!isAlreadyReported)
690 setWindowGeometryFromOhos(nodeGeometry);
691 }
692 }
693
694 updateSafeAreaMargins();
695}
696
697// Map display-space cutout rects to window-relative margins, each attributed to
698// the nearest window edge.
699static QMargins cutoutMarginsForWindow(const QList<QRect> &cutoutRects, const QRect &windowRect)
700{
701 QMargins margins;
702 for (const QRect &cutout : cutoutRects) {
703 if (cutout.isEmpty() || !cutout.intersects(windowRect))
704 continue;
705 const int distTop = cutout.top() - windowRect.top();
706 const int distBottom = windowRect.bottom() - cutout.bottom();
707 const int distLeft = cutout.left() - windowRect.left();
708 const int distRight = windowRect.right() - cutout.right();
709 int nearest = qMin(qMin(distTop, distBottom), qMin(distLeft, distRight));
710 if (nearest == distTop)
711 margins.setTop(qMax(margins.top(), cutout.bottom() + 1 - windowRect.top()));
712 else if (nearest == distBottom)
713 margins.setBottom(qMax(margins.bottom(), windowRect.bottom() + 1 - cutout.top()));
714 else if (nearest == distLeft)
715 margins.setLeft(qMax(margins.left(), cutout.right() + 1 - windowRect.left()));
716 else
717 margins.setRight(qMax(margins.right(), windowRect.right() + 1 - cutout.left()));
718 }
719 return margins;
720}
721
722void QOhosFloatingWindow::updateSafeAreaMargins()
723{
724 // The system bar and navigation indicator avoid areas are window-relative
725 // and visibility-aware: they report zero when hidden (e.g. in fullscreen).
726 static constexpr QOhosWindowProxy::AvoidAreaType kSafeAreaContributors[] = {
727 QOhosWindowProxy::AvoidAreaType::TYPE_SYSTEM,
729 };
730
731 // Avoid-area sizes are relative to the full window; subtract the inset the
732 // compositor already applies (frame vs. true on-screen content) so a normal
733 // window reports no safe area, matching Android's max(0, inset - offset).
734 // nodeScreenGeometryPixels() is the real content rect; drawableRect lies.
735 const QRect frame = m_view->viewGeometry().frameGeometry;
736 const QRect content = m_view->nodeScreenGeometryPixels();
737 if (!frame.contains(content))
738 return;
739 const int alreadyTop = content.top() - frame.top();
740 const int alreadyLeft = content.left() - frame.left();
741 const int alreadyRight = frame.right() - content.right();
742 const int alreadyBottom = frame.bottom() - content.bottom();
743
744 QMargins combined;
745 for (auto type : kSafeAreaContributors) {
746 const QMargins m = m_view->avoidAreaMargins(type);
747 combined.setTop(qMax(combined.top(), qMax(0, m.top() - alreadyTop)));
748 combined.setLeft(qMax(combined.left(), qMax(0, m.left() - alreadyLeft)));
749 combined.setRight(qMax(combined.right(), qMax(0, m.right() - alreadyRight)));
750 combined.setBottom(qMax(combined.bottom(), qMax(0, m.bottom() - alreadyBottom)));
751 }
752
753 // The display cutout is not in the avoid areas; fold it in from
754 // getCutoutInfo() (fetched fresh as it rotates with the display) and report
755 // it even in fullscreen, mapped against the content area.
756 const QMargins cutout = cutoutMarginsForWindow(m_view->displayCutoutRects(), content);
757 combined.setTop(qMax(combined.top(), cutout.top()));
758 combined.setLeft(qMax(combined.left(), cutout.left()));
759 combined.setRight(qMax(combined.right(), cutout.right()));
760 combined.setBottom(qMax(combined.bottom(), cutout.bottom()));
761
762 setSafeAreaMarginsFromOhos(combined);
763}
764
765QT_END_NAMESPACE
QOhosFloatingWindow(QWindow *window)
~QOhosFloatingWindow() override
void requestActivateWindow() override
Reimplement to let Qt be able to request activation/focus for a window.
void setVisible(bool visible) override
Reimplemented in subclasses to show the surface if visible is true, and hide it if visible is false.
bool startSystemMove() override
Reimplement this method to start a system move operation if the system supports it and return true to...
QOhosView * ownedViewOrNull() const override
void onWindowStateChanged(Qt::WindowStates oldWindowState, Qt::WindowStates currentWindowState) override
QRect normalGeometry() const override
Returns the geometry of a window in 'normal' state (neither maximized, fullscreen nor minimized) for ...
WId winId() const override
Reimplement in subclasses to return a handle to the native window.
void initialize() override
Called as part of QWindow::create(), after constructing the window.
void onWindowFlagsChanged(Qt::WindowFlags previousWindowFlags, Qt::WindowFlags currentWindowFlags) override
bool windowEvent(QEvent *event) override
Reimplement this method to be able to do any platform specific event handling.
void setGeometry(const QRect &rect) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
QOhosSurface * ownedSurfaceOrNull() const override
void setMask(const QRegion &region) override
Reimplement to be able to let Qt set the mask of a window.
void lower() override
Reimplement to be able to let Qt lower windows to the bottom of the desktop.
void raise() override
Reimplement to be able to let Qt raise windows to the top of the desktop.
void setDisplayIdFromOhos(std::optional< QOhosDisplayInfo::JsDisplayId > displayId)
void setExposedFromOhos(bool exposed)
void initialize() override
Called as part of QWindow::create(), after constructing the window.
void notifyInputSystemsWindowActiveStatusChanged(bool active)
bool isExposed() const final
Returns if this window is exposed in the windowing system.
void requestActivateWindow() override
Reimplement to let Qt be able to request activation/focus for a window.
bool isWindowPcModeEnabled() const
static QOhosSettings & instance()
QOhosSurface * surfaceOrNull() const
void setWindowMask(const QOhosWindowProxy::WindowMask &windowMask)
void lower()
bool startMoving()
void raise()
QtOhosQpa::enums::ohos::window::WindowEventType WindowEventType
QtOhosQpa::enums::ohos::window::RectChangeReason RectChangeReason
QtOhosQpa::enums::ohos::window::AvoidAreaType AvoidAreaType
QtOhosQpa::enums::ohos::window::WindowStatusType WindowStatusType
static QWindowProxyRegistry & instance()
Combined button and popup list for selecting options.
bool shouldClearQtFocusOnSystemFocusLoss(QWindow *windowLosingSystemFocus)
bool isWindowRotatedByTabletScreenRotation(QWindow *window, QOhosWindowProxy::RectChangeOptions rectChangeOptions)
static QMargins cutoutMarginsForWindow(const QList< QRect > &cutoutRects, const QRect &windowRect)