Commit d6094ab1 authored by Janet Blackquill's avatar Janet Blackquill 🌈 Committed by Janet Blackquill
Browse files

Add RtL support

This makes KDecoration2 able to handle right to left;
by properly mirroring buttons.

BUG: 432390
(cherry picked from commit 4123ec8c)
parent fd757a61
Pipeline #150371 passed with stage
in 45 seconds
......@@ -9,6 +9,7 @@
#include "decorationsettings.h"
#include <QDebug>
#include <QGuiApplication>
namespace KDecoration2
{
......@@ -57,18 +58,33 @@ void DecorationButtonGroup::Private::updateLayout()
}
setGeometry(QRectF(pos, QSizeF(width, height)));
// now position all buttons
qreal position = pos.x();
const auto &constButtons = buttons;
for (auto button : constButtons) {
QGuiApplication* app = qobject_cast<QGuiApplication*>(QCoreApplication::instance());
const auto layoutDirection = app ? app->layoutDirection() : Qt::LeftToRight;
qreal leftPosition = pos.x();
qreal rightPosition = pos.x() + width;
if (layoutDirection == Qt::LeftToRight) for (auto button : qAsConst(buttons)) {
if (!button->isVisible()) {
continue;
}
const auto size = button->size();
const auto buttonPos = QPointF(leftPosition, pos.y());
button->setGeometry(QRectF(buttonPos, size));
leftPosition += size.width() + spacing;
} else if (layoutDirection == Qt::RightToLeft) for (auto button : qAsConst(buttons)) {
if (!button->isVisible()) {
continue;
}
const QSizeF size = button->size();
// TODO: center
button->setGeometry(QRectF(QPointF(position, pos.y()), size));
position += size.width() + spacing;
const auto size = button->size();
const auto buttonPos = QPointF(rightPosition - size.width(), pos.y());
button->setGeometry(QRectF(buttonPos, size));
rightPosition -= size.width() + spacing;
} else {
qCritical() << "There's an unhandled layout direction! This is likely an issue of KDecoration2 not being updated to handle it\n"
<< "or the application having an invalid layout direction set. Either way, this is a critical bug.";
}
s_layoutRecursion = false;
}
......@@ -84,9 +100,14 @@ DecorationButtonGroup::DecorationButtonGroup(DecorationButtonGroup::Position typ
: QObject(parent)
, d(new Private(parent, this))
{
QGuiApplication* app = qobject_cast<QGuiApplication*>(QCoreApplication::instance());
const auto layoutDirection = app ? app->layoutDirection() : Qt::LeftToRight;
auto settings = parent->settings();
auto createButtons = [=] {
const auto &buttons = (type == Position::Left) ? settings->decorationButtonsLeft() : settings->decorationButtonsRight();
const auto &buttons =
(type == Position::Left) ?
(layoutDirection == Qt::LeftToRight ? settings->decorationButtonsLeft() : settings->decorationButtonsRight()) :
(layoutDirection == Qt::LeftToRight ? settings->decorationButtonsRight() : settings->decorationButtonsLeft());
for (DecorationButtonType type : buttons) {
if (DecorationButton *b = buttonCreator(type, parent, this)) {
addButton(QPointer<DecorationButton>(b));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment