quickshell/src/cpp/proxywindow.hpp

114 lines
3.5 KiB
C++
Raw Normal View History

2024-02-01 09:29:45 +00:00
#pragma once
#include <qcolor.h>
2024-02-04 12:58:58 +00:00
#include <qevent.h>
2024-02-01 09:29:45 +00:00
#include <qobject.h>
#include <qqmllist.h>
#include <qqmlparserstatus.h>
#include <qquickitem.h>
2024-02-01 09:29:45 +00:00
#include <qquickwindow.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include "scavenge.hpp"
// Proxy to an actual window exposing a limited property set with the ability to
// transfer it to a new window.
// Detaching a window and touching any property is a use after free.
//
// NOTE: setting an `id` in qml will point to the proxy window and not the real window so things
// like anchors must use `item`.
2024-02-01 09:29:45 +00:00
class ProxyWindowBase: public Scavenger {
Q_OBJECT;
/// The content item of the window.
Q_PROPERTY(QQuickItem* item READ item CONSTANT);
/// The visibility of the window.
///
/// > [!INFO] Windows are not visible by default so you will need to set this to make the window
/// appear.
2024-02-04 12:58:58 +00:00
Q_PROPERTY(bool visible READ isVisible WRITE setVisible NOTIFY visibleChanged);
Q_PROPERTY(qint32 width READ width WRITE setWidth NOTIFY widthChanged);
Q_PROPERTY(qint32 height READ height WRITE setHeight NOTIFY heightChanged);
/// The background color of the window. Defaults to white.
///
/// > [!WARNING] This seems to behave weirdly when using transparent colors on some systems.
/// > Using a colored content item over a transparent window is the recommended way to work around this:
/// > ```qml
/// > ProxyWindow {
/// > Rectangle {
/// > anchors.fill: parent
/// > color: "#20ffffff"
/// >
/// > // your content here
/// > }
/// > }
/// > ```
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged);
2024-02-01 09:29:45 +00:00
Q_PROPERTY(QQmlListProperty<QObject> data READ data);
Q_CLASSINFO("DefaultProperty", "data");
protected:
void earlyInit(QObject* old) override;
2024-02-04 12:58:58 +00:00
QQuickWindow* window = nullptr;
2024-02-01 09:29:45 +00:00
public:
explicit ProxyWindowBase(QObject* parent = nullptr): Scavenger(parent) {}
~ProxyWindowBase() override;
ProxyWindowBase(ProxyWindowBase&) = delete;
ProxyWindowBase(ProxyWindowBase&&) = delete;
void operator=(ProxyWindowBase&) = delete;
void operator=(ProxyWindowBase&&) = delete;
// Disown the backing window and delete all its children.
2024-02-04 12:58:58 +00:00
virtual QQuickWindow* disownWindow();
2024-02-01 09:29:45 +00:00
QQuickItem* item();
2024-02-04 12:58:58 +00:00
virtual bool isVisible();
2024-02-01 09:29:45 +00:00
virtual void setVisible(bool value);
2024-02-04 12:58:58 +00:00
virtual qint32 width();
2024-02-01 09:29:45 +00:00
virtual void setWidth(qint32 value);
2024-02-04 12:58:58 +00:00
virtual qint32 height();
2024-02-01 09:29:45 +00:00
virtual void setHeight(qint32 value);
QColor color();
void setColor(QColor value);
QQmlListProperty<QObject> data();
2024-02-04 12:58:58 +00:00
signals:
void visibleChanged(bool visible);
void widthChanged(qint32 width);
void heightChanged(qint32 width);
void colorChanged(QColor color);
2024-02-04 12:58:58 +00:00
2024-02-01 09:29:45 +00:00
private:
static QQmlListProperty<QObject> dataBacker(QQmlListProperty<QObject>* prop);
static void dataAppend(QQmlListProperty<QObject>* prop, QObject* obj);
static qsizetype dataCount(QQmlListProperty<QObject>* prop);
static QObject* dataAt(QQmlListProperty<QObject>* prop, qsizetype i);
static void dataClear(QQmlListProperty<QObject>* prop);
static void dataReplace(QQmlListProperty<QObject>* prop, qsizetype i, QObject* obj);
static void dataRemoveLast(QQmlListProperty<QObject>* prop);
};
// qt attempts to resize the window but fails because wayland
// and only resizes the graphics context which looks terrible.
class ProxyFloatingWindow: public ProxyWindowBase {
Q_OBJECT;
QML_ELEMENT;
public:
void earlyInit(QObject* old) override;
void componentComplete() override;
2024-02-01 09:29:45 +00:00
void setWidth(qint32 value) override;
void setHeight(qint32 value) override;
private:
bool geometryLocked = false;
};