Skip to content

Commit 7d989bd

Browse files
committed
core/window: add parentWindow property to FloatingWindow
1 parent e9bad67 commit 7d989bd

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/window/floatingwindow.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <qobject.h>
44
#include <qqmlengine.h>
5-
#include <qqmllist.h>
5+
#include <qqmlinfo.h>
66
#include <qtmetamacros.h>
77
#include <qtypes.h>
88

@@ -17,6 +17,13 @@ void ProxyFloatingWindow::connectWindow() {
1717
this->window->setMaximumSize(this->bMaximumSize);
1818
}
1919

20+
void ProxyFloatingWindow::postCompleteWindow() {
21+
auto* parentBacking =
22+
this->mParentProxyWindow ? this->mParentProxyWindow->backingWindow() : nullptr;
23+
24+
this->window->setTransientParent(parentBacking);
25+
}
26+
2027
void ProxyFloatingWindow::trySetWidth(qint32 implicitWidth) {
2128
if (!this->window->isVisible()) {
2229
this->ProxyWindowBase::trySetWidth(implicitWidth);
@@ -44,6 +51,28 @@ void ProxyFloatingWindow::onMaximumSizeChanged() {
4451
emit this->maximumSizeChanged();
4552
}
4653

54+
QObject* ProxyFloatingWindow::parentWindow() const { return this->mParentWindow; }
55+
56+
void ProxyFloatingWindow::setParentWindow(QObject* window) {
57+
if (window == this->mParentWindow) return;
58+
59+
if (window) {
60+
if (auto* proxy = qobject_cast<ProxyWindowBase*>(window)) {
61+
this->mParentProxyWindow = proxy;
62+
} else if (auto* interface = qobject_cast<WindowInterface*>(window)) {
63+
this->mParentProxyWindow = interface->proxyWindow();
64+
} else {
65+
qmlWarning(this) << "parentWindow must be a quickshell window.";
66+
return;
67+
}
68+
69+
this->mParentWindow = window;
70+
} else {
71+
this->mParentWindow = nullptr;
72+
this->mParentProxyWindow = nullptr;
73+
}
74+
}
75+
4776
// FloatingWindowInterface
4877

4978
FloatingWindowInterface::FloatingWindowInterface(QObject* parent)
@@ -66,3 +95,9 @@ void FloatingWindowInterface::onReload(QObject* oldInstance) {
6695
}
6796

6897
ProxyWindowBase* FloatingWindowInterface::proxyWindow() const { return this->window; }
98+
99+
QObject* FloatingWindowInterface::parentWindow() const { return this->window->parentWindow(); }
100+
101+
void FloatingWindowInterface::setParentWindow(QObject* window) {
102+
this->window->setParentWindow(window);
103+
}

src/window/floatingwindow.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class ProxyFloatingWindow: public ProxyWindowBase {
1818
explicit ProxyFloatingWindow(QObject* parent = nullptr): ProxyWindowBase(parent) {}
1919

2020
void connectWindow() override;
21+
void postCompleteWindow() override;
22+
23+
[[nodiscard]] QObject* parentWindow() const;
24+
void setParentWindow(QObject* window);
2125

2226
// Setting geometry while the window is visible makes the content item shrink but not the window
2327
// which is awful so we disable it for floating windows.
@@ -34,6 +38,9 @@ class ProxyFloatingWindow: public ProxyWindowBase {
3438
void onMaximumSizeChanged();
3539
void onTitleChanged();
3640

41+
QObject* mParentWindow = nullptr;
42+
ProxyWindowBase* mParentProxyWindow = nullptr;
43+
3744
public:
3845
Q_OBJECT_BINDABLE_PROPERTY(
3946
ProxyFloatingWindow,
@@ -68,6 +75,11 @@ class FloatingWindowInterface: public WindowInterface {
6875
Q_PROPERTY(QSize minimumSize READ default WRITE default NOTIFY minimumSizeChanged BINDABLE bindableMinimumSize);
6976
/// Maximum window size given to the window system.
7077
Q_PROPERTY(QSize maximumSize READ default WRITE default NOTIFY maximumSizeChanged BINDABLE bindableMaximumSize);
78+
/// The parent window of this window. Setting this makes the window a child of the parent,
79+
/// which affects window stacking behavior.
80+
///
81+
/// > [!NOTE] This property cannot be changed after the window is visible.
82+
Q_PROPERTY(QObject* parentWindow READ parentWindow WRITE setParentWindow);
7183
// clang-format on
7284
QML_NAMED_ELEMENT(FloatingWindow);
7385

@@ -82,6 +94,9 @@ class FloatingWindowInterface: public WindowInterface {
8294
QBindable<QSize> bindableMaximumSize() { return &this->window->bMaximumSize; }
8395
QBindable<QString> bindableTitle() { return &this->window->bTitle; }
8496

97+
[[nodiscard]] QObject* parentWindow() const;
98+
void setParentWindow(QObject* window);
99+
85100
signals:
86101
void minimumSizeChanged();
87102
void maximumSizeChanged();

0 commit comments

Comments
 (0)