Compare commits

...

1 Commits

Author SHA1 Message Date
38e36e8131 Allow specifying margins as fraactions
Absolute margin values pose a problem if you want to place a surface
certain distance away from a screen edge (in percents) but you also want
the compositor to place the surface on the active output.

At the moment, this issue is worked around by using kwin dbus api to
query the active output. This is not good.

This change is a take on allowing to specify margin values in fractions.
It can be used to drop a dbus call in krunner and assist us with porting
other components to layer shell, e.g. OSDs, they also need to be placed
one third away the bottom screen edge.
2025-12-08 10:01:32 +02:00
6 changed files with 382 additions and 17 deletions

View File

@ -17,6 +17,49 @@
using namespace LayerShellQt;
Margin::Margin()
: m_value(0)
{
}
Margin::Margin(int pixels)
: m_value(pixels)
{
}
Margin::Margin(qreal fraction)
: m_value(fraction)
{
}
Margin Margin::fromPixels(int pixels)
{
return Margin(pixels);
}
Margin Margin::fromFraction(qreal fraction)
{
return Margin(fraction);
}
std::optional<int> Margin::pixels() const
{
if (std::holds_alternative<int>(m_value)) {
return std::get<int>(m_value);
} else {
return std::nullopt;
}
}
std::optional<qreal> Margin::fraction() const
{
if (std::holds_alternative<qreal>(m_value)) {
return std::get<qreal>(m_value);
} else {
return std::nullopt;
}
}
class LayerShellQt::WindowPrivate
{
public:
@ -32,7 +75,10 @@ public:
Window::Anchor exclusiveEdge = Window::AnchorNone;
Window::KeyboardInteractivity keyboardInteractivity = Window::KeyboardInteractivityOnDemand;
Window::Layer layer = Window::LayerTop;
QMargins margins;
Margin leftMargin;
Margin topMargin;
Margin rightMargin;
Margin bottomMargin;
QSize desiredSize = QSize(0, 0);
Window::ScreenConfiguration screenConfiguration = Window::ScreenFromQWindow;
bool closeOnDismissed = true;
@ -89,15 +135,74 @@ Window::Anchor Window::exclusiveEdge() const
void Window::setMargins(const QMargins &margins)
{
if (d->margins != margins) {
d->margins = margins;
Q_EMIT marginsChanged();
}
setLeftMargin(Margin::fromPixels(margins.left()));
setTopMargin(Margin::fromPixels(margins.top()));
setRightMargin(Margin::fromPixels(margins.right()));
setBottomMargin(Margin::fromPixels(margins.bottom()));
}
QMargins Window::margins() const
{
return d->margins;
return QMargins(d->leftMargin.pixels().value_or(0),
d->topMargin.pixels().value_or(0),
d->rightMargin.pixels().value_or(0),
d->bottomMargin.pixels().value_or(0));
}
void Window::setLeftMargin(Margin margin)
{
if (d->leftMargin != margin) {
d->leftMargin = margin;
Q_EMIT leftMarginChanged();
Q_EMIT marginsChanged();
}
}
Margin Window::leftMargin() const
{
return d->leftMargin;
}
void Window::setTopMargin(Margin margin)
{
if (d->topMargin != margin) {
d->topMargin = margin;
Q_EMIT topMarginChanged();
Q_EMIT marginsChanged();
}
}
Margin Window::topMargin() const
{
return d->topMargin;
}
void Window::setRightMargin(Margin margin)
{
if (d->rightMargin != margin) {
d->rightMargin = margin;
Q_EMIT rightMarginChanged();
Q_EMIT marginsChanged();
}
}
Margin Window::rightMargin() const
{
return d->rightMargin;
}
void Window::setBottomMargin(Margin margin)
{
if (d->bottomMargin != margin) {
d->bottomMargin = margin;
Q_EMIT bottomMarginChanged();
Q_EMIT marginsChanged();
}
}
Margin Window::bottomMargin() const
{
return d->bottomMargin;
}
void Window::setDesiredSize(const QSize &size)

View File

@ -18,6 +18,34 @@ namespace LayerShellQt
{
class WindowPrivate;
/**
* The Margin type provides a way to specify how far a layer surface should be away from a screen edge.
*
* A margin can have an absolute value or a percent value. An absolute value indicates the distance
* in pixels. A percent value indicates the distance as a percentage of the output size, for example
* this can be used to tell the compositor that the surface should be one third of the output height from
* a screen edge, etc.
*/
class LAYERSHELLQT_EXPORT Margin
{
public:
static Margin fromPixels(int pixels);
static Margin fromFraction(qreal fraction);
Margin();
std::optional<int> pixels() const;
std::optional<qreal> fraction() const;
auto operator<=>(const Margin &other) const = default;
private:
Margin(int pixels);
Margin(qreal fraction);
std::variant<int, qreal> m_value;
};
class LAYERSHELLQT_EXPORT Window : public QObject
{
Q_OBJECT
@ -87,6 +115,18 @@ public:
void setMargins(const QMargins &margins);
QMargins margins() const;
void setLeftMargin(Margin margin);
Margin leftMargin() const;
void setTopMargin(Margin margin);
Margin topMargin() const;
void setRightMargin(Margin margin);
Margin rightMargin() const;
void setBottomMargin(Margin margin);
Margin bottomMargin() const;
void setDesiredSize(const QSize &size);
QSize desiredSize() const;
@ -145,6 +185,10 @@ Q_SIGNALS:
void exclusionZoneChanged();
void exclusiveEdgeChanged();
void marginsChanged();
void leftMarginChanged();
void topMarginChanged();
void rightMarginChanged();
void bottomMarginChanged();
void desiredSizeChanged();
void keyboardInteractivityChanged();
void layerChanged();

View File

@ -15,7 +15,7 @@
namespace LayerShellQt
{
QWaylandLayerShellIntegration::QWaylandLayerShellIntegration()
: QWaylandShellIntegrationTemplate<QWaylandLayerShellIntegration>(5)
: QWaylandShellIntegrationTemplate<QWaylandLayerShellIntegration>(6)
, m_xdgActivation(new QWaylandXdgActivationV1)
{
}

View File

@ -60,9 +60,24 @@ QWaylandLayerSurface::QWaylandLayerSurface(QWaylandLayerShellIntegration *shell,
setExclusiveEdge(m_interface->exclusiveEdge());
});
setMargins(m_interface->margins());
connect(m_interface, &Window::marginsChanged, this, [this]() {
setMargins(m_interface->margins());
setLeftMargin(m_interface->leftMargin());
connect(m_interface, &Window::leftMarginChanged, this, [this]() {
setLeftMargin(m_interface->leftMargin());
});
setTopMargin(m_interface->topMargin());
connect(m_interface, &Window::topMarginChanged, this, [this]() {
setTopMargin(m_interface->topMargin());
});
setRightMargin(m_interface->rightMargin());
connect(m_interface, &Window::rightMarginChanged, this, [this]() {
setRightMargin(m_interface->rightMargin());
});
setBottomMargin(m_interface->bottomMargin());
connect(m_interface, &Window::bottomMarginChanged, this, [this]() {
setBottomMargin(m_interface->bottomMargin());
});
connect(m_interface, &Window::desiredSizeChanged, this, [this]() {
@ -159,9 +174,68 @@ void QWaylandLayerSurface::setExclusiveEdge(uint32_t edge)
}
}
void QWaylandLayerSurface::setMargins(const QMargins &margins)
void QWaylandLayerSurface::setLeftMargin(const Margin &margin)
{
set_margin(margins.top(), margins.right(), margins.bottom(), margins.left());
if (zwlr_layer_surface_v1_get_version(object()) >= 6) {
if (const auto pixels = margin.pixels()) {
set_left_margin_units(*pixels);
} else if (const auto fraction = margin.fraction()) {
set_left_margin_fraction(wl_fixed_from_double(*fraction));
} else {
qCWarning(LAYERSHELLQT) << "Unspecified left margin for" << m_window->window();
}
} else {
const QMargins margins = m_interface->margins();
set_margin(margins.top(), margins.right(), margins.bottom(), margins.left());
}
}
void QWaylandLayerSurface::setTopMargin(const Margin &margin)
{
if (zwlr_layer_surface_v1_get_version(object()) >= 6) {
if (const auto pixels = margin.pixels()) {
set_top_margin_units(*pixels);
} else if (const auto fraction = margin.fraction()) {
set_top_margin_fraction(wl_fixed_from_double(*fraction));
} else {
qCWarning(LAYERSHELLQT) << "Unspecified top margin for" << m_window->window();
}
} else {
const QMargins margins = m_interface->margins();
set_margin(margins.top(), margins.right(), margins.bottom(), margins.left());
}
}
void QWaylandLayerSurface::setRightMargin(const Margin &margin)
{
if (zwlr_layer_surface_v1_get_version(object()) >= 6) {
if (const auto pixels = margin.pixels()) {
set_right_margin_units(*pixels);
} else if (const auto fraction = margin.fraction()) {
set_right_margin_fraction(wl_fixed_from_double(*fraction));
} else {
qCWarning(LAYERSHELLQT) << "Unspecified right margin for" << m_window->window();
}
} else {
const QMargins margins = m_interface->margins();
set_margin(margins.top(), margins.right(), margins.bottom(), margins.left());
}
}
void QWaylandLayerSurface::setBottomMargin(const Margin &margin)
{
if (zwlr_layer_surface_v1_get_version(object()) >= 6) {
if (const auto pixels = margin.pixels()) {
set_bottom_margin_units(*pixels);
} else if (const auto fraction = margin.fraction()) {
set_bottom_margin_fraction(wl_fixed_from_double(*fraction));
} else {
qCWarning(LAYERSHELLQT) << "Unspecified bottom margin for" << m_window->window();
}
} else {
const QMargins margins = m_interface->margins();
set_margin(margins.top(), margins.right(), margins.bottom(), margins.left());
}
}
void QWaylandLayerSurface::setKeyboardInteractivity(uint32_t interactivity)

View File

@ -19,6 +19,7 @@
namespace LayerShellQt
{
class Margin;
class Window;
class LAYERSHELLQT_EXPORT QWaylandLayerSurface : public QtWaylandClient::QWaylandShellSurface, public QtWayland::zwlr_layer_surface_v1
@ -38,7 +39,10 @@ public:
void setAnchor(uint32_t anchor);
void setExclusiveZone(int32_t zone);
void setExclusiveEdge(uint32_t edge);
void setMargins(const QMargins &margins);
void setLeftMargin(const Margin &margin);
void setTopMargin(const Margin &margin);
void setRightMargin(const Margin &margin);
void setBottomMargin(const Margin &margin);
void setKeyboardInteractivity(uint32_t interactivity);
void setLayer(uint32_t layer);

View File

@ -25,7 +25,7 @@
THIS SOFTWARE.
</copyright>
<interface name="zwlr_layer_shell_v1" version="5">
<interface name="zwlr_layer_shell_v1" version="6">
<description summary="create surfaces that are layers of the desktop">
Clients can use this interface to assign the surface_layer role to
wl_surfaces. Such surfaces are assigned to a "layer" of the output and
@ -100,7 +100,7 @@
</request>
</interface>
<interface name="zwlr_layer_surface_v1" version="5">
<interface name="zwlr_layer_surface_v1" version="6">
<description summary="layer metadata interface">
An interface that may be implemented by a wl_surface, for surfaces that
are designed to be rendered as a layer of a stacked desktop-like
@ -187,7 +187,7 @@
<arg name="zone" type="int"/>
</request>
<request name="set_margin">
<request name="set_margin" deprecated-since="6">
<description summary="sets a margin from the anchor point">
Requests that the surface be placed some distance away from the anchor
point on the output, in surface-local coordinates. Setting this value
@ -196,6 +196,10 @@
The exclusive zone includes the margin.
Margin is double-buffered, see wl_surface.commit.
This request is deprecated since version 6. Use set_left_margin_{units,fraction},
set_top_margin_{units,fraction}, set_right_margin_{units,fraction}, and
set_bottom_margin_{units,fraction} requests instead.
</description>
<arg name="top" type="int"/>
<arg name="right" type="int"/>
@ -401,7 +405,141 @@
The edge must be one the surface is anchored to, otherwise the
invalid_exclusive_edge protocol error will be raised.
</description>
<arg name="edge" type="uint"/>
<arg name="edge" type="uint" enum="anchor"/>
</request>
<!-- Version 6 additions -->
<request name="set_left_margin_units" since="6">
<description summary="set the left margin in surface local coordinates">
Sets the left margin in surface-local coordinates.
Margins specify that the surface should be placed some distance away
from the anchor point on the output. Setting a margin for an edge
you are not anchored to has no effect.
The left margin specified using set_left_margin_fraction will be
overwritten by this request.
Margins are double-buffered, see wl_surface.commit.
</description>
<arg name="margin" type="int"/>
</request>
<request name="set_left_margin_fraction" since="6">
<description summary="set the left margin as a fraction">
Sets the left margin as a fraction of the available area width. The
margin value is a number between 0 and 1.
Margins specify that the surface should be placed some distance away
from the anchor point on the output. Setting a margin for an edge
you are not anchored to has no effect.
The left margin specified using set_left_margin_units will be
overwritten by this request.
Margin is double-buffered, see wl_surface.commit.
</description>
<arg name="margin" type="fixed"/>
</request>
<request name="set_top_margin_units" since="6">
<description summary="set the top margin in surface local coordinates">
Sets the top margin in surface-local coordinates.
Margins specify that the surface should be placed some distance away
from the anchor point on the output. Setting a margin for an edge
you are not anchored to has no effect.
The top margin specified using set_top_margin_fraction will be
overwritten by this request.
Margins are double-buffered, see wl_surface.commit.
</description>
<arg name="margin" type="int"/>
</request>
<request name="set_top_margin_fraction" since="6">
<description summary="set the top margin as a fraction">
Sets the top margin as a fraction of the available area height. The
margin value is a number between 0 and 1.
Margins specify that the surface should be placed some distance away
from the anchor point on the output. Setting a margin for an edge
you are not anchored to has no effect.
The top margin specified using set_top_margin_units will be
overwritten by this request.
Margin is double-buffered, see wl_surface.commit.
</description>
<arg name="margin" type="fixed"/>
</request>
<request name="set_right_margin_units" since="6">
<description summary="set the right margin in surface local coordinates">
Sets the right margin in surface-local coordinates.
Margins specify that the surface should be placed some distance away
from the anchor point on the output. Setting a margin for an edge
you are not anchored to has no effect.
The right margin specified using set_right_margin_fraction will be
overwritten by this request.
Margins are double-buffered, see wl_surface.commit.
</description>
<arg name="margin" type="int"/>
</request>
<request name="set_right_margin_fraction" since="6">
<description summary="set the right margin as a fraction">
Sets the right margin as a fraction of the available area width. The
margin value is a number between 0 and 1.
Margins specify that the surface should be placed some distance away
from the anchor point on the output. Setting a margin for an edge
you are not anchored to has no effect.
The right margin specified using set_right_margin_units will be
overwritten by this request.
Margin is double-buffered, see wl_surface.commit.
</description>
<arg name="margin" type="fixed"/>
</request>
<request name="set_bottom_margin_units" since="6">
<description summary="set the bottom margin in surface local coordinates">
Sets the bottom margin in surface-local coordinates.
Margins specify that the surface should be placed some distance away
from the anchor point on the output. Setting a margin for an edge
you are not anchored to has no effect.
The bottom margin specified using set_bottom_margin_fraction will be
overwritten by this request.
Margins are double-buffered, see wl_surface.commit.
</description>
<arg name="margin" type="int"/>
</request>
<request name="set_bottom_margin_fraction" since="6">
<description summary="set the bottom margin as a fraction">
Sets the bottom margin as a fraction of the available area height. The
margin value is a number between 0 and 1.
Margins specify that the surface should be placed some distance away
from the anchor point on the output. Setting a margin for an edge
you are not anchored to has no effect.
The bottom margin specified using set_bottom_margin_units will be
overwritten by this request.
Margin is double-buffered, see wl_surface.commit.
</description>
<arg name="margin" type="fixed"/>
</request>
</interface>
</protocol>