Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
AspectRatioClass.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
6#include <QJsonValue>
7#include <QString>
8
9#include <cmath>
10
11namespace PhosphorLayout {
12
22enum class AspectRatioClass {
23 Any = 0,
24 Standard = 1,
25 Ultrawide = 2,
26 SuperUltrawide = 3,
27 Portrait = 4
28};
29
36namespace ScreenClassification {
37
38// Aspect ratio boundary thresholds (width / height).
39constexpr qreal PortraitMax = 1.0;
40constexpr qreal UltrawideMin = 1.9;
41constexpr qreal SuperUltrawideMin = 2.8;
42
43inline AspectRatioClass classify(qreal aspectRatio)
44{
45 // NaN never compares true under <, which would silently fall through every
46 // branch and land at SuperUltrawide. Treat non-finite as "no classification"
47 // so callers that compute aspect ratios from suspect inputs get a stable
48 // sentinel rather than a wildly wrong category.
49 if (!std::isfinite(aspectRatio) || aspectRatio <= 0.0) {
51 }
52 if (aspectRatio < PortraitMax) {
54 }
55 if (aspectRatio < UltrawideMin) {
57 }
58 if (aspectRatio < SuperUltrawideMin) {
60 }
62}
63
64inline AspectRatioClass classify(int width, int height)
65{
66 if (height <= 0 || width <= 0) {
68 }
69 return classify(static_cast<qreal>(width) / height);
70}
71
72inline QString toString(AspectRatioClass cls)
73{
74 switch (cls) {
76 return QStringLiteral("any");
78 return QStringLiteral("standard");
80 return QStringLiteral("ultrawide");
82 return QStringLiteral("super-ultrawide");
84 return QStringLiteral("portrait");
85 }
86 return QStringLiteral("any");
87}
88
89inline AspectRatioClass fromString(const QString& str)
90{
91 if (str == QLatin1String("standard")) {
93 }
94 if (str == QLatin1String("ultrawide")) {
96 }
97 if (str == QLatin1String("super-ultrawide")) {
99 }
100 if (str == QLatin1String("portrait")) {
102 }
104}
105
118inline AspectRatioClass fromJsonValue(const QJsonValue& value)
119{
120 if (value.isString()) {
121 return fromString(value.toString());
122 }
123 if (value.isDouble()) {
124 const int cls = value.toInt(static_cast<int>(AspectRatioClass::Any));
125 if (cls < 0 || cls > static_cast<int>(AspectRatioClass::Portrait)) {
127 }
128 return static_cast<AspectRatioClass>(cls);
129 }
131}
132
138inline qreal aspectRatioForClass(AspectRatioClass cls, qreal fallback = 16.0 / 9.0)
139{
140 switch (cls) {
142 return 16.0 / 9.0;
144 return 21.0 / 9.0;
146 return 32.0 / 9.0;
148 return 9.0 / 16.0;
150 return fallback;
151 }
152 // Unreachable under @c -Wswitch (every enumerator handled above). Kept
153 // to satisfy compilers that still warn about control reaching the end
154 // of a non-void function when the enum-covering switch exits the path.
155 return fallback;
156}
157
166inline bool matches(AspectRatioClass layoutClass, AspectRatioClass screenClass)
167{
168 if (layoutClass == AspectRatioClass::Any) {
169 return true;
170 }
171 return layoutClass == screenClass;
172}
173
174} // namespace ScreenClassification
175
176} // namespace PhosphorLayout
constexpr qreal SuperUltrawideMin
AR >= 2.8 → super-ultrawide.
Definition AspectRatioClass.h:41
bool matches(AspectRatioClass layoutClass, AspectRatioClass screenClass)
Check if a layout's aspect ratio class matches the given screen class.
Definition AspectRatioClass.h:166
QString toString(AspectRatioClass cls)
Definition AspectRatioClass.h:72
constexpr qreal UltrawideMin
AR ∈ [UltrawideMin, SuperUltrawideMin) → ultrawide.
Definition AspectRatioClass.h:40
AspectRatioClass fromString(const QString &str)
Definition AspectRatioClass.h:89
AspectRatioClass fromJsonValue(const QJsonValue &value)
Read a classification from either serialized JSON form.
Definition AspectRatioClass.h:118
constexpr qreal PortraitMax
AR < 1.0 → portrait.
Definition AspectRatioClass.h:39
qreal aspectRatioForClass(AspectRatioClass cls, qreal fallback=16.0/9.0)
Representative aspect ratio for a class.
Definition AspectRatioClass.h:138
AspectRatioClass classify(qreal aspectRatio)
Definition AspectRatioClass.h:43
Definition AlgorithmMetadata.h:10
AspectRatioClass
Screen aspect-ratio classification.
Definition AspectRatioClass.h:22
@ Portrait
Rotated/vertical monitors (< 1.0)
@ Standard
~16:10 to ~16:9 (1.5 - 1.9)
@ Any
Suitable for all aspect ratios (default)