// SPDX-License-Identifier: GPL-3.0-or-later #include "core/window.h" #include "core/render.h" #include "core/schema.h" #include namespace xpl { namespace { QList numbers(const QString &value) { QList out; for (const QString &part : value.split(QLatin1Char(','), Qt::SkipEmptyParts)) { bool ok = false; const int n = part.trimmed().toInt(&ok); if (!ok) return {}; out.append(n); } return out; } // "help, min, max, close": names this build doesn't know are passed over. QList buttonList(const QString &value) { static const QHash names{ {QStringLiteral("menu"), CaptionButton::Menu}, {QStringLiteral("help"), CaptionButton::Help}, {QStringLiteral("min"), CaptionButton::Minimize}, {QStringLiteral("max"), CaptionButton::Maximize}, {QStringLiteral("close"), CaptionButton::Close}, }; QList out; for (const QString &name : value.split(QLatin1Char(','), Qt::SkipEmptyParts)) if (const auto it = names.constFind(name.trimmed().toLower()); it != names.cend() && !out.contains(*it)) out.append(*it); return out; } } // namespace QSize DecorationLayout::size(CaptionButton b) const { if (const auto it = sizes.constFind(int(b)); it != sizes.cend()) return *it; return b == CaptionButton::Menu ? QSize(iconSize, button.height()) : button; } int captionButtonPart(CaptionButton b) { const char *name = nullptr; switch (b) { case CaptionButton::Menu: return 0; case CaptionButton::Help: name = "helpbutton"; break; case CaptionButton::Minimize: name = "minbutton"; break; case CaptionButton::Maximize: name = "maxbutton"; break; case CaptionButton::Restore: name = "restorebutton"; break; case CaptionButton::Close: name = "closebutton"; break; } return schema::partId(u"window", QString::fromLatin1(name)); } // SM_CYSIZE - 4 tall, 2px apart, and as wide as the close button's art is in // proportion, rounded half up: Luna's 21x21 art gives 21x21, Aero Vista's // 24x12 gives 46x21 (measured on XP; the rule is WackyIdeas' "Calculating // the dimensions of a Windows XP Caption Button"). QSize styleButtonSize(int captionHeight, QSize closeArt) { const int height = qMax(8, captionHeight - 4); int width = height; if (closeArt.width() > 0 && closeArt.height() > 0) width = int(std::floor(double(closeArt.width()) / closeArt.height() * captionHeight - 4 + 0.5)); return QSize(qMax(8, width), height); } DecorationLayout decorationLayout(const NonClientMetrics &m, const Renderer *style) { DecorationLayout l; l.frame = m.borderWidth + 3; l.caption = m.captionHeight + 1; if (!style) { // SM_CXSIZE - 2 by SM_CYSIZE - 4; min and max touch, close 2px off. l.button = QSize(qMax(8, m.captionWidth - 2), qMax(8, m.captionHeight - 4)); l.titleGap = 2; return l; } const QString cls = QStringLiteral("window"); const PartRef close{cls, captionButtonPart(CaptionButton::Close), 1, {}}; l.button = styleButtonSize(m.captionHeight, style->stateImage(close).size()); // The style's (or the overrides') own say. const PartRef window{cls, 0, 0, {}}; auto text = [&](const PartRef &ref, const char *name) { return style->property(ref, QString::fromLatin1(name)).value_or(QString()); }; if (const QString v = text(window, "xplasmabuttonsleft"); !v.isNull()) l.left = buttonList(v); if (const QString v = text(window, "xplasmabuttonsright"); !v.isNull()) l.right = buttonList(v); if (const QList v = numbers(text(window, "xplasmabuttongap")); v.size() == 1 && v[0] >= 0) l.buttonGap = v[0]; if (const QList v = numbers(text(window, "xplasmabuttoninset")); v.size() == 2) l.inset = QPoint(v[0], v[1]); if (const QList v = numbers(text(window, "xplasmaiconsize")); v.size() == 1 && v[0] > 0) l.iconSize = v[0]; if (const QList v = numbers(text(window, "xplasmatitlegaps")); v.size() == 2) { l.titleGap = v[0]; l.titleEnd = v[1]; } const QString align = text({cls, schema::partId(u"window", u"caption"), 1, {}}, "contentalignment").toLower(); l.titleAlign = align == QLatin1String("center") ? Qt::AlignHCenter : align == QLatin1String("right") ? Qt::AlignRight : Qt::AlignLeft; for (CaptionButton b : {CaptionButton::Help, CaptionButton::Minimize, CaptionButton::Maximize, CaptionButton::Restore, CaptionButton::Close}) { const PartRef ref{cls, captionButtonPart(b), 1, {}}; if (const QList v = numbers(text(ref, "xplasmasize")); v.size() == 2 && v[0] > 0 && v[1] > 0) l.sizes.insert(int(b), QSize(v[0], v[1])); if (parseBool(text(ref, "xplasmaclipglyph"))) l.clipped.append(int(b)); } return l; } } // namespace xpl