feat(wayland): create cross platform window interfaces

Internally this also refactors a ton of code around the wayland
layershell. Note that attachment failures are still broken and
platform interfaces are hardcoded.
This commit is contained in:
outfoxxed 2024-02-25 07:13:54 -08:00
parent 4a82949854
commit c2930783ea
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
20 changed files with 591 additions and 402 deletions

View file

@ -11,7 +11,9 @@ qt_add_executable(quickshell
watcher.cpp
region.cpp
persistentprops.cpp
shellwindow.cpp
windowinterface.cpp
floatingwindow.cpp
panelinterface.cpp
)
qt_add_qml_module(quickshell URI QuickShell)

View file

@ -0,0 +1,62 @@
#include "floatingwindow.hpp"
#include <qobject.h>
#include <qqmllist.h>
#include <qquickitem.h>
#include <qtypes.h>
#include "proxywindow.hpp"
#include "windowinterface.hpp"
void ProxyFloatingWindow::setWidth(qint32 width) {
if (this->window == nullptr || !this->window->isVisible()) {
this->ProxyWindowBase::setWidth(width);
}
}
void ProxyFloatingWindow::setHeight(qint32 height) {
if (this->window == nullptr || !this->window->isVisible()) {
this->ProxyWindowBase::setHeight(height);
}
}
// FloatingWindowInterface
FloatingWindowInterface::FloatingWindowInterface(QObject* parent)
: WindowInterface(parent)
, window(new ProxyFloatingWindow(this)) {
// clang-format off
QObject::connect(this->window, &ProxyWindowBase::windowConnected, this, &FloatingWindowInterface::windowConnected);
QObject::connect(this->window, &ProxyWindowBase::visibleChanged, this, &FloatingWindowInterface::visibleChanged);
QObject::connect(this->window, &ProxyWindowBase::heightChanged, this, &FloatingWindowInterface::heightChanged);
QObject::connect(this->window, &ProxyWindowBase::widthChanged, this, &FloatingWindowInterface::widthChanged);
QObject::connect(this->window, &ProxyWindowBase::screenChanged, this, &FloatingWindowInterface::screenChanged);
QObject::connect(this->window, &ProxyWindowBase::colorChanged, this, &FloatingWindowInterface::colorChanged);
QObject::connect(this->window, &ProxyWindowBase::maskChanged, this, &FloatingWindowInterface::maskChanged);
// clang-format on
}
void FloatingWindowInterface::onReload(QObject* oldInstance) {
auto* old = qobject_cast<FloatingWindowInterface*>(oldInstance);
this->window->onReload(old != nullptr ? old->window : nullptr);
}
QQmlListProperty<QObject> FloatingWindowInterface::data() { return this->window->data(); }
QQuickItem* FloatingWindowInterface::contentItem() const { return this->window->contentItem(); }
// NOLINTBEGIN
#define proxyPair(type, get, set) \
type FloatingWindowInterface::get() const { return this->window->get(); } \
void FloatingWindowInterface::set(type value) { this->window->set(value); }
proxyPair(bool, isVisible, setVisible);
proxyPair(qint32, width, setWidth);
proxyPair(qint32, height, setHeight);
proxyPair(QuickShellScreenInfo*, screen, setScreen);
proxyPair(QColor, color, setColor);
proxyPair(PendingRegion*, mask, setMask);
#undef proxyPair
#undef proxySet
#undef proxyGet
// NOLINTEND

View file

@ -0,0 +1,56 @@
#pragma once
#include <qobject.h>
#include <qtmetamacros.h>
#include "proxywindow.hpp"
class ProxyFloatingWindow: public ProxyWindowBase {
Q_OBJECT;
public:
explicit ProxyFloatingWindow(QObject* parent = nullptr): ProxyWindowBase(parent) {}
// Setting geometry while the window is visible makes the content item shrinks but not the window
// which is awful so we disable it for floating windows.
void setWidth(qint32 width) override;
void setHeight(qint32 height) override;
};
///! Standard floating window.
class FloatingWindowInterface: public WindowInterface {
Q_OBJECT;
QML_NAMED_ELEMENT(FloatingWindow);
public:
explicit FloatingWindowInterface(QObject* parent = nullptr);
void onReload(QObject* oldInstance) override;
[[nodiscard]] QQuickItem* contentItem() const override;
// NOLINTBEGIN
[[nodiscard]] bool isVisible() const override;
void setVisible(bool visible) override;
[[nodiscard]] qint32 width() const override;
void setWidth(qint32 width) override;
[[nodiscard]] qint32 height() const override;
void setHeight(qint32 height) override;
[[nodiscard]] QuickShellScreenInfo* screen() const override;
void setScreen(QuickShellScreenInfo* screen) override;
[[nodiscard]] QColor color() const override;
void setColor(QColor color) override;
[[nodiscard]] PendingRegion* mask() const override;
void setMask(PendingRegion* mask) override;
[[nodiscard]] QQmlListProperty<QObject> data() override;
// NOLINTEND
private:
ProxyFloatingWindow* window;
};

View file

@ -0,0 +1 @@
#include "panelinterface.hpp" // NOLINT

View file

@ -1,16 +1,8 @@
#pragma once
#include <qobject.h>
#include <qqmlintegration.h>
#include <qqmllist.h>
#include <qquickwindow.h>
#include <qscreen.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include <qvariant.h>
#include <qwindow.h>
#include "proxywindow.hpp"
#include "windowinterface.hpp"
class Anchors {
Q_GADGET;
@ -87,7 +79,7 @@ Q_ENUM_NS(Enum);
/// The following snippet creates a white bar attached to the bottom of the screen.
///
/// ```qml
/// ShellWindow {
/// PanelWindow {
/// anchors {
/// left: true
/// bottom: true
@ -101,7 +93,7 @@ Q_ENUM_NS(Enum);
/// }
/// }
/// ```
class ProxyShellWindow: public ProxyWindowBase {
class PanelWindowInterface: public WindowInterface {
// clang-format off
Q_OBJECT;
/// Anchors attach a shell window to the sides of the screen.
@ -111,45 +103,36 @@ class ProxyShellWindow: public ProxyWindowBase {
/// > (width or height) will be forced to equal the screen width/height.
/// > Margins can be used to create anchored windows that are also disconnected from the monitor sides.
Q_PROPERTY(Anchors anchors READ anchors WRITE setAnchors NOTIFY anchorsChanged);
/// The amount of space reserved for the shell layer relative to its anchors.
///
/// > [!INFO] Some systems will require exactly 3 anchors to be attached for the exclusion zone to take
/// > effect.
Q_PROPERTY(qint32 exclusionZone READ exclusiveZone WRITE setExclusiveZone NOTIFY exclusionZoneChanged);
/// Defaults to `ExclusionMode.Normal`.
Q_PROPERTY(ExclusionMode::Enum exclusionMode READ exclusionMode WRITE setExclusionMode NOTIFY exclusionModeChanged);
/// Offsets from the sides of the screen.
///
/// > [!INFO] Only applies to edges with anchors
Q_PROPERTY(Margins margins READ margins WRITE setMargins NOTIFY marginsChanged);
/// The amount of space reserved for the shell layer relative to its anchors.
///
/// > [!INFO] Either 1 or 3 anchors are required for the zone to take effect.
Q_PROPERTY(qint32 exclusiveZone READ exclusiveZone WRITE setExclusiveZone NOTIFY exclusiveZoneChanged);
/// Defaults to `ExclusionMode.Auto`.
Q_PROPERTY(ExclusionMode::Enum exclusionMode READ exclusionMode WRITE setExclusionMode NOTIFY exclusionModeChanged);
// clang-format on
public:
explicit ProxyShellWindow(QObject* parent = nullptr): ProxyWindowBase(parent) {}
explicit PanelWindowInterface(QObject* parent = nullptr): WindowInterface(parent) {}
QQmlListProperty<QObject> data();
virtual void setAnchors(Anchors anchors) = 0;
[[nodiscard]] virtual Anchors anchors() const = 0;
virtual void setAnchors(Anchors anchors) = 0;
virtual void setExclusiveZone(qint32 zone) = 0;
[[nodiscard]] virtual qint32 exclusiveZone() const = 0;
virtual void setExclusionMode(ExclusionMode::Enum exclusionMode) = 0;
[[nodiscard]] virtual ExclusionMode::Enum exclusionMode() const = 0;
virtual void setMargins(Margins margins) = 0;
[[nodiscard]] virtual Margins margins() const = 0;
virtual void setMargins(Margins margins) = 0;
[[nodiscard]] virtual qint32 exclusiveZone() const = 0;
virtual void setExclusiveZone(qint32 exclusiveZone) = 0;
[[nodiscard]] virtual ExclusionMode::Enum exclusionMode() const = 0;
virtual void setExclusionMode(ExclusionMode::Enum exclusionMode) = 0;
signals:
void anchorsChanged();
void marginsChanged();
void exclusionZoneChanged();
void exclusiveZoneChanged();
void exclusionModeChanged();
protected:
ExclusionMode::Enum mExclusionMode = ExclusionMode::Normal;
qint32 mExclusionZone = 0;
Anchors mAnchors;
Margins mMargins;
};

View file

@ -203,14 +203,4 @@ QQmlListProperty<QObject> ProxyWindowBase::data() {
}
void ProxyWindowBase::onWidthChanged() { this->mContentItem->setWidth(this->width()); }
void ProxyWindowBase::onHeightChanged() { this->mContentItem->setHeight(this->height()); }
void ProxyFloatingWindow::setWidth(qint32 width) {
if (this->window == nullptr || !this->window->isVisible()) this->ProxyWindowBase::setWidth(width);
}
void ProxyFloatingWindow::setHeight(qint32 height) {
if (this->window == nullptr || !this->window->isVisible())
this->ProxyWindowBase::setHeight(height);
}

View file

@ -15,12 +15,12 @@
#include "qmlscreen.hpp"
#include "region.hpp"
#include "reload.hpp"
#include "windowinterface.hpp"
// Proxy to an actual window exposing a limited property set with the ability to
// transfer it to a new window.
///! Base class for reloadable windows
/// Base class for reloadable windows. See [ShellWindow] and [FloatingWindow]
///
/// [ShellWindow]: ../shellwindow
/// [FloatingWindow]: ../floatingwindow
@ -35,76 +35,14 @@ class ProxyWindowBase: public Reloadable {
/// > Use **only** if you know what you are doing.
Q_PROPERTY(QQuickWindow* _backingWindow READ backingWindow);
Q_PROPERTY(QQuickItem* contentItem READ contentItem);
/// If the window is shown or hidden. Defaults to true.
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 screen that the window currently occupies.
///
/// > [!INFO] This cannot be changed while the window is visible.
Q_PROPERTY(QuickShellScreenInfo* screen READ screen WRITE setScreen NOTIFY screenChanged);
/// 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);
/// The clickthrough mask. Defaults to null.
///
/// If non null then the clickable areas of the window will be determined by the provided region.
///
/// ```qml
/// ShellWindow {
/// // The mask region is set to `rect`, meaning only `rect` is clickable.
/// // All other clicks pass through the window to ones behind it.
/// mask: Region { item: rect }
///
/// Rectangle {
/// id: rect
///
/// anchors.centerIn: parent
/// width: 100
/// height: 100
/// }
/// }
/// ```
///
/// If the provided region's intersection mode is `Combine` (the default),
/// then the region will be used as is. Otherwise it will be applied on top of the window region.
///
/// For example, setting the intersection mode to `Xor` will invert the mask and make everything in
/// the mask region not clickable and pass through clicks inside it through the window.
///
/// ```qml
/// ShellWindow {
/// // The mask region is set to `rect`, but the intersection mode is set to `Xor`.
/// // This inverts the mask causing all clicks inside `rect` to be passed to the window
/// // behind this one.
/// mask: Region { item: rect; intersection: Intersection.Xor }
///
/// Rectangle {
/// id: rect
///
/// anchors.centerIn: parent
/// width: 100
/// height: 100
/// }
/// }
/// ```
Q_PROPERTY(PendingRegion* mask READ mask WRITE setMask NOTIFY maskChanged);
Q_PROPERTY(QQmlListProperty<QObject> data READ data);
Q_CLASSINFO("DefaultProperty", "data");
QML_ELEMENT;
QML_UNCREATABLE("use ShellWindow or FloatingWindow");
public:
explicit ProxyWindowBase(QObject* parent = nullptr);
@ -134,16 +72,16 @@ public:
[[nodiscard]] virtual qint32 height() const;
virtual void setHeight(qint32 height);
[[nodiscard]] virtual QuickShellScreenInfo* screen() const;
virtual void setScreen(QuickShellScreenInfo* screen);
[[nodiscard]] QuickShellScreenInfo* screen() const;
[[nodiscard]] QColor color() const;
void setColor(QColor color);
virtual void setColor(QColor color);
[[nodiscard]] PendingRegion* mask() const;
void setMask(PendingRegion* mask);
virtual void setMask(PendingRegion* mask);
QQmlListProperty<QObject> data();
[[nodiscard]] QQmlListProperty<QObject> data();
signals:
void windowConnected();
@ -173,18 +111,3 @@ protected:
private:
void updateMask();
};
// qt attempts to resize the window but fails because wayland
// and only resizes the graphics context which looks terrible.
///! Standard floating window.
class ProxyFloatingWindow: public ProxyWindowBase {
Q_OBJECT;
QML_NAMED_ELEMENT(FloatingWindow);
public:
// Setting geometry while the window is visible makes the content item shrink but not the window
// which is awful so we disable it for floating windows.
void setWidth(qint32 width) override;
void setHeight(qint32 height) override;
};

View file

@ -1 +0,0 @@
#include "shellwindow.hpp" // NOLINT

View file

@ -0,0 +1 @@
#include "windowinterface.hpp" // NOLINT

View file

@ -0,0 +1,124 @@
#pragma once
#include <qcolor.h>
#include <qobject.h>
#include <qqmlintegration.h>
#include <qqmllist.h>
#include <qquickitem.h>
#include <qtmetamacros.h>
#include <qtypes.h>
#include "qmlscreen.hpp"
#include "region.hpp"
#include "reload.hpp"
class WindowInterface: public Reloadable {
Q_OBJECT;
// clang-format off
Q_PROPERTY(QQuickItem* contentItem READ contentItem);
/// If the window is shown or hidden. Defaults to true.
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 screen that the window currently occupies.
///
/// > [!INFO] This cannot be changed after windowConnected.
Q_PROPERTY(QuickShellScreenInfo* screen READ screen WRITE setScreen NOTIFY screenChanged);
/// 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);
/// The clickthrough mask. Defaults to null.
///
/// If non null then the clickable areas of the window will be determined by the provided region.
///
/// ```qml
/// ShellWindow {
/// // The mask region is set to `rect`, meaning only `rect` is clickable.
/// // All other clicks pass through the window to ones behind it.
/// mask: Region { item: rect }
///
/// Rectangle {
/// id: rect
///
/// anchors.centerIn: parent
/// width: 100
/// height: 100
/// }
/// }
/// ```
///
/// If the provided region's intersection mode is `Combine` (the default),
/// then the region will be used as is. Otherwise it will be applied on top of the window region.
///
/// For example, setting the intersection mode to `Xor` will invert the mask and make everything in
/// the mask region not clickable and pass through clicks inside it through the window.
///
/// ```qml
/// ShellWindow {
/// // The mask region is set to `rect`, but the intersection mode is set to `Xor`.
/// // This inverts the mask causing all clicks inside `rect` to be passed to the window
/// // behind this one.
/// mask: Region { item: rect; intersection: Intersection.Xor }
///
/// Rectangle {
/// id: rect
///
/// anchors.centerIn: parent
/// width: 100
/// height: 100
/// }
/// }
/// ```
Q_PROPERTY(PendingRegion* mask READ mask WRITE setMask NOTIFY maskChanged);
Q_PROPERTY(QQmlListProperty<QObject> data READ data);
// clang-format on
Q_CLASSINFO("DefaultProperty", "data");
QML_NAMED_ELEMENT(QSWindow);
QML_UNCREATABLE("uncreatable base class");
public:
explicit WindowInterface(QObject* parent = nullptr): Reloadable(parent) {}
[[nodiscard]] virtual QQuickItem* contentItem() const = 0;
[[nodiscard]] virtual bool isVisible() const = 0;
virtual void setVisible(bool visible) = 0;
[[nodiscard]] virtual qint32 width() const = 0;
virtual void setWidth(qint32 width) = 0;
[[nodiscard]] virtual qint32 height() const = 0;
virtual void setHeight(qint32 height) = 0;
[[nodiscard]] virtual QuickShellScreenInfo* screen() const = 0;
virtual void setScreen(QuickShellScreenInfo* screen) = 0;
[[nodiscard]] virtual QColor color() const = 0;
virtual void setColor(QColor color) = 0;
[[nodiscard]] virtual PendingRegion* mask() const = 0;
virtual void setMask(PendingRegion* mask) = 0;
[[nodiscard]] virtual QQmlListProperty<QObject> data() = 0;
signals:
void windowConnected();
void visibleChanged();
void widthChanged();
void heightChanged();
void screenChanged();
void colorChanged();
void maskChanged();
};