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
qohoswindowutils.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtGui/qcolor.h>
7#include <QtGui/qpa/qplatformintegration.h>
8#include <QtGui/qscreen.h>
9#include <QtGui/qwindow.h>
10#include <QtGui/private/qguiapplication_p.h>
11#include <QtGui/private/qohoswindowhints_p.h>
12#include <QtGui/qpa/qplatformwindow_p.h>
13#include <QtWidgets/qwidget.h>
14
15#include <QtCore/qcoreapplication_platform.h>
16#include <QtCore/qharmonyjscontext.h>
17#include <QtCore/qharmonyjsobjectref.h>
18#include <QtCore/qvariant.h>
19#include <QtCore/private/qnapi_p.h>
20
21#include <optional>
22
24
25/*!
26 \namespace QtHarmonyExtras::Window
27 \inmodule QtHarmonyExtras
28 \brief Applies OpenHarmony-specific hints to a QWindow or QWidget.
29
30 Each hint is recorded on the passed QWindow or QWidget and consumed by the
31 platform plugin. A hint may be set on a QWidget before its window exists; it
32 is carried over to the window once the widget is shown.
33*/
34
35namespace QtHarmonyExtras {
36
37namespace Window {
38
39/*!
40 Sets or unsets a given \a window as a floating window according to
41 \a showAsFloatWindow. The OpenHarmony window type is fixed at creation, so
42 this has to be called before the window is shown.
43
44 \code
45 auto window = std::make_unique<MainWindow>();
46 QtHarmonyExtras::Window::setShowWindowAsFloatWindowHint(window.get(), true);
47 window->show();
48 \endcode
49*/
50void setShowWindowAsFloatWindowHint(QWindow *window, bool showAsFloatWindow)
51{
52 window->setProperty(QOhosWindowHints::floatWindowKey, QVariant::fromValue(showAsFloatWindow));
53}
54
55/*! \overload */
56void setShowWindowAsFloatWindowHint(QWidget *widget, bool showAsFloatWindow)
57{
58 widget->setProperty(QOhosWindowHints::floatWindowKey, QVariant::fromValue(showAsFloatWindow));
59}
60
61/*!
62 Changes the privacy mode of \a window according to \a privacyModeEnabled. In
63 privacy mode the window content cannot be captured or recorded. This requires
64 the ohos.permission.PRIVACY_WINDOW permission to be declared in the
65 application's module.json5.
66*/
67void setWindowPrivacyMode(QWindow *window, bool privacyModeEnabled)
68{
69 window->setProperty(QOhosWindowHints::privacyModeKey, QVariant::fromValue(privacyModeEnabled));
70}
71
72/*!
73 Sets a given \a color as the background color of the \a window inner native
74 layer.
75*/
76void setSurfaceBackgroundColor(QWindow *window, const QColor &color)
77{
78 window->setProperty(QOhosWindowHints::surfaceBackgroundColorKey, QVariant::fromValue(color));
79}
80
81/*!
82 \overload
83
84 Sets a given \a color as the background color of the \a widget inner native
85 layer.
86*/
87void setSurfaceBackgroundColor(QWidget *widget, const QColor &color)
88{
89 widget->setProperty(QOhosWindowHints::surfaceBackgroundColorKey, QVariant::fromValue(color));
90}
91
92/*!
93 Sets the corner \a radius of a \a window. The window has to be a sub or
94 floating window (for example a window with the Qt::Window, Qt::Popup or
95 Qt::Dialog flags, or one marked with setShowWindowAsFloatWindowHint()). The
96 \a radius must not be negative; there is no upper limit, though OHOS visually
97 clamps it to the maximum achievable value.
98*/
99void setWindowCornerRadius(QWindow *window, double radius)
100{
101 if (radius < 0.0) {
102 qWarning("QtHarmonyExtras::Window::setWindowCornerRadius: radius must not be negative");
103 return;
104 }
105 window->setProperty(QOhosWindowHints::cornerRadiusKey, QVariant::fromValue(radius));
106}
107
108/*!
109 \overload
110
111 Sets the corner \a radius of a \a widget. The widget has to be a window on its
112 own (not embedded in a parent widget). The \a radius must not be negative.
113*/
114void setWindowCornerRadius(QWidget *widget, double radius)
115{
116 if (radius < 0.0) {
117 qWarning("QtHarmonyExtras::Window::setWindowCornerRadius: radius must not be negative");
118 return;
119 }
120 widget->setProperty(QOhosWindowHints::cornerRadiusKey, QVariant::fromValue(radius));
121}
122
123/*!
124 Sets or unsets a given \a window to keep the screen on according to
125 \a keepScreenOn.
126*/
127void setWindowKeepScreenOn(QWindow *window, bool keepScreenOn)
128{
129 window->setProperty(QOhosWindowHints::keepScreenOnKey, QVariant::fromValue(keepScreenOn));
130}
131
132/*!
133 \overload
134
135 Sets or unsets a given \a widget to keep the screen on according to
136 \a keepScreenOn.
137*/
138void setWindowKeepScreenOn(QWidget *widget, bool keepScreenOn)
139{
140 widget->setProperty(QOhosWindowHints::keepScreenOnKey, QVariant::fromValue(keepScreenOn));
141}
142
143/*!
144 Enables or disables resizing of \a window by dragging its edges according to
145 \a dragResizable. Takes effect on sub windows and floating windows, but not on
146 the application's main window.
147*/
148void setWindowDragResizable(QWindow *window, bool dragResizable)
149{
150 window->setProperty(QOhosWindowHints::dragResizableKey, QVariant::fromValue(dragResizable));
151}
152
153/*!
154 \overload
155
156 Enables or disables resizing of \a widget by dragging its edges according to
157 \a dragResizable. The widget has to be a window on its own (not embedded in a
158 parent widget). Takes effect on sub windows and floating windows, but not on
159 the application's main window.
160*/
161void setWindowDragResizable(QWidget *widget, bool dragResizable)
162{
163 widget->setProperty(QOhosWindowHints::dragResizableKey, QVariant::fromValue(dragResizable));
164}
165
166/*!
167 Sets whether the application's main window restores its last geometry on
168 startup according to \a hint. WindowGeometryPersistenceHint::FollowSystemSetting
169 defers to the system default. This has to be called before the main window is
170 shown.
171*/
173{
174 std::optional<bool> enabled;
175 switch (hint) {
177 enabled = false;
178 break;
180 enabled = true;
181 break;
183 break;
184 }
185
186 using QOhosIntegration = QNativeInterface::Private::QOhosIntegration;
187 auto *integration = QGuiApplicationPrivate::platformIntegration();
188 integration->call<&QOhosIntegration::setMainWindowGeometryPersistenceEnabled>(enabled);
189}
190
191}
192
194{
195 auto *harmonyWindow = window->nativeInterface<QNativeInterface::QHarmonyWindow>();
196 const auto jsWindowRef = harmonyWindow ? harmonyWindow->jsWindow() : QHarmonyJsObjectRef();
197 if (jsWindowRef.isNull())
198 return {};
199
200 const auto optWindowId = QNativeInterface::QHarmonyApplication::runOnJsThreadAndWait(
201 [&](QHarmonyJsContext &jsContext) -> std::optional<double> {
202 auto jsWindow = QNapi::Object(jsContext.env(), jsWindowRef.value(jsContext));
203 return jsWindow.eval<QNapi::Number>("getWindowProperties().id");
204 });
205 if (!optWindowId)
206 return {};
207
208 qint64 windowId = 0;
209 if (!convertDoubleTo(*optWindowId, &windowId)) {
210 qWarning("tryGetNativeWindowId: window id %g out of qint64 range", *optWindowId);
211 return {};
212 }
213
214 return windowId;
215}
216
218{
219 auto *harmonyScreen = screen->nativeInterface<QNativeInterface::QHarmonyScreen>();
220 if (!harmonyScreen)
221 return {};
222
223 return harmonyScreen->displayId();
224}
225
226}
227
228QT_END_NAMESPACE
Combined button and popup list for selecting options.
void setWindowKeepScreenOn(QWindow *window, bool keepScreenOn)
Sets or unsets a given window to keep the screen on according to keepScreenOn.
void setWindowCornerRadius(QWindow *window, double radius)
Sets the corner radius of a window.
void setShowWindowAsFloatWindowHint(QWindow *window, bool showAsFloatWindow)
Sets or unsets a given window as a floating window according to showAsFloatWindow.
void setSurfaceBackgroundColor(QWindow *window, const QColor &color)
Sets a given color as the background color of the window inner native layer.
void setWindowDragResizable(QWindow *window, bool dragResizable)
Enables or disables resizing of window by dragging its edges according to dragResizable.
void setWindowPrivacyMode(QWindow *window, bool privacyModeEnabled)
Changes the privacy mode of window according to privacyModeEnabled.
void setMainWindowGeometryPersistenceHint(WindowGeometryPersistenceHint hint)
Sets whether the application's main window restores its last geometry on startup according to hint.
std::optional< qint64 > tryGetNativeWindowId(QWindow *window)
std::optional< qint64 > tryGetScreenDisplayId(QScreen *screen)