Skip to content

Commit 888c247

Browse files
committed
core/window: add fullscreen and maximize
1 parent 95820e9 commit 888c247

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed

src/window/proxywindow.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ void ProxyWindowBase::onVisibleChanged() {
248248
}
249249

250250
emit this->visibleChanged();
251+
emit this->windowVisibilityChanged();
251252
}
252253

253254
bool ProxyWindowBase::deleteOnInvisible() const { return false; }
@@ -545,6 +546,12 @@ ProxyWindowBase::mapFromItem(QQuickItem* item, qreal x, qreal y, qreal width, qr
545546
}
546547

547548
ProxyWindowAttached::ProxyWindowAttached(QQuickItem* parent): QsWindowAttached(parent) {
549+
this->bMaximized.setBinding([this]() { return this->bVisibility.value() == QWindow::Maximized; });
550+
551+
this->bFullscreen.setBinding([this]() {
552+
return this->bVisibility.value() == QWindow::FullScreen;
553+
});
554+
548555
this->updateWindow();
549556
}
550557

@@ -567,12 +574,30 @@ void ProxyWindowAttached::updateWindow() {
567574

568575
void ProxyWindowAttached::setWindow(ProxyWindowBase* window) {
569576
if (window == this->mWindow) return;
577+
578+
if (this->mWindow) {
579+
QObject::disconnect(this->mWindow, nullptr, this, nullptr);
580+
}
581+
570582
this->mWindow = window;
583+
584+
if (this->mWindow) {
585+
// clang-format off
586+
QObject::connect(this->mWindow, &ProxyWindowBase::windowVisibilityChanged, this, &ProxyWindowAttached::onVisibilityChanged);
587+
// clang-format on
588+
this->onVisibilityChanged();
589+
}
590+
571591
auto* parentInterface = window ? qobject_cast<WindowInterface*>(window->parent()) : nullptr;
572592
this->mWindowInterface = parentInterface ? static_cast<QObject*>(parentInterface) : window;
573593
emit this->windowChanged();
574594
}
575595

596+
void ProxyWindowAttached::onVisibilityChanged() {
597+
auto* qw = this->mWindow ? this->mWindow->backingWindow() : nullptr;
598+
this->bVisibility = qw ? qw->visibility() : QWindow::Hidden;
599+
}
600+
576601
bool ProxiedWindow::event(QEvent* event) {
577602
if (event->type() == QEvent::DevicePixelRatioChange) {
578603
emit this->devicePixelRatioChanged();

src/window/proxywindow.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class ProxyWindowBase: public Reloadable {
147147
void windowDestroyed();
148148
void visibleChanged();
149149
void backerVisibilityChanged();
150+
void windowVisibilityChanged();
150151
void xChanged();
151152
void yChanged();
152153
void implicitWidthChanged();
@@ -221,13 +222,27 @@ class ProxyWindowAttached: public QsWindowAttached {
221222
[[nodiscard]] ProxyWindowBase* proxyWindow() const override;
222223
[[nodiscard]] QQuickItem* contentItem() const override;
223224

225+
[[nodiscard]] bool maximized() const override { return this->bMaximized; }
226+
[[nodiscard]] bool fullscreen() const override { return this->bFullscreen; }
227+
[[nodiscard]] QBindable<bool> bindableMaximized() override { return &this->bMaximized; }
228+
[[nodiscard]] QBindable<bool> bindableFullscreen() override { return &this->bFullscreen; }
229+
224230
protected:
225231
void updateWindow() override;
226232

233+
private slots:
234+
void onVisibilityChanged();
235+
227236
private:
228237
ProxyWindowBase* mWindow = nullptr;
229238
QObject* mWindowInterface = nullptr;
230239

240+
// clang-format off
241+
Q_OBJECT_BINDABLE_PROPERTY(ProxyWindowAttached, QWindow::Visibility, bVisibility);
242+
Q_OBJECT_BINDABLE_PROPERTY(ProxyWindowAttached, bool, bMaximized, &ProxyWindowAttached::maximizedChanged);
243+
Q_OBJECT_BINDABLE_PROPERTY(ProxyWindowAttached, bool, bFullscreen, &ProxyWindowAttached::fullscreenChanged);
244+
// clang-format on
245+
231246
void setWindow(ProxyWindowBase* window);
232247
};
233248

src/window/windowinterface.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <qquickitem.h>
1010
#include <qrect.h>
1111
#include <qtypes.h>
12+
#include <qwindow.h>
1213

1314
#include "../core/qmlscreen.hpp"
1415
#include "../core/region.hpp"
@@ -122,6 +123,46 @@ bool QsWindowAttached::startSystemResize(Qt::Edges edges) const {
122123
return qw->startSystemResize(edges);
123124
}
124125

126+
void QsWindowAttached::showNormal() const {
127+
auto* pw = this->proxyWindow();
128+
if (!pw) {
129+
qCritical() << "Cannot call showNormal before item is a member of a window.";
130+
return;
131+
}
132+
133+
if (auto* qw = pw->backingWindow()) qw->showNormal();
134+
}
135+
136+
void QsWindowAttached::showMaximized() const {
137+
auto* pw = this->proxyWindow();
138+
if (!pw) {
139+
qCritical() << "Cannot call showMaximized before item is a member of a window.";
140+
return;
141+
}
142+
143+
if (auto* qw = pw->backingWindow()) qw->showMaximized();
144+
}
145+
146+
void QsWindowAttached::showMinimized() const {
147+
auto* pw = this->proxyWindow();
148+
if (!pw) {
149+
qCritical() << "Cannot call showMinimized before item is a member of a window.";
150+
return;
151+
}
152+
153+
if (auto* qw = pw->backingWindow()) qw->showMinimized();
154+
}
155+
156+
void QsWindowAttached::showFullScreen() const {
157+
auto* pw = this->proxyWindow();
158+
if (!pw) {
159+
qCritical() << "Cannot call showFullScreen before item is a member of a window.";
160+
return;
161+
}
162+
163+
if (auto* qw = pw->backingWindow()) qw->showFullScreen();
164+
}
165+
125166
// clang-format off
126167
QQuickItem* WindowInterface::contentItem() const { return this->proxyWindow()->contentItem(); }
127168

src/window/windowinterface.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <qcolor.h>
44
#include <qobject.h>
5+
#include <qproperty.h>
56
#include <qqmlintegration.h>
67
#include <qqmllist.h>
78
#include <qquickitem.h>
@@ -267,13 +268,22 @@ class QsWindowAttached: public QObject {
267268
Q_OBJECT;
268269
Q_PROPERTY(QObject* window READ window NOTIFY windowChanged);
269270
Q_PROPERTY(QQuickItem* contentItem READ contentItem NOTIFY windowChanged);
271+
/// Whether the window is currently maximized.
272+
Q_PROPERTY(bool maximized READ maximized NOTIFY maximizedChanged BINDABLE bindableMaximized);
273+
/// Whether the window is currently fullscreen.
274+
Q_PROPERTY(bool fullscreen READ fullscreen NOTIFY fullscreenChanged BINDABLE bindableFullscreen);
270275
QML_ANONYMOUS;
271276

272277
public:
273278
[[nodiscard]] virtual QObject* window() const = 0;
274279
[[nodiscard]] virtual ProxyWindowBase* proxyWindow() const = 0;
275280
[[nodiscard]] virtual QQuickItem* contentItem() const = 0;
276281

282+
[[nodiscard]] virtual bool maximized() const = 0;
283+
[[nodiscard]] virtual bool fullscreen() const = 0;
284+
[[nodiscard]] virtual QBindable<bool> bindableMaximized() = 0;
285+
[[nodiscard]] virtual QBindable<bool> bindableFullscreen() = 0;
286+
277287
Q_INVOKABLE [[nodiscard]] QPointF itemPosition(QQuickItem* item) const;
278288
Q_INVOKABLE [[nodiscard]] QRectF itemRect(QQuickItem* item) const;
279289
Q_INVOKABLE [[nodiscard]] QPointF mapFromItem(QQuickItem* item, QPointF point) const;
@@ -288,8 +298,19 @@ class QsWindowAttached: public QObject {
288298
/// Start a system resize operation. Must be called during a pointer press/drag.
289299
Q_INVOKABLE [[nodiscard]] bool startSystemResize(Qt::Edges edges) const;
290300

301+
/// Show the window in normal (restored) state.
302+
Q_INVOKABLE void showNormal() const;
303+
/// Show the window maximized.
304+
Q_INVOKABLE void showMaximized() const;
305+
/// Show the window minimized.
306+
Q_INVOKABLE void showMinimized() const;
307+
/// Show the window fullscreen.
308+
Q_INVOKABLE void showFullScreen() const;
309+
291310
signals:
292311
void windowChanged();
312+
void maximizedChanged();
313+
void fullscreenChanged();
293314

294315
protected slots:
295316
virtual void updateWindow() = 0;

0 commit comments

Comments
 (0)