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 <QString>
7
8#include <cmath>
9
10namespace PhosphorLayout {
11
21enum class AspectRatioClass {
22 Any = 0,
23 Standard = 1,
24 Ultrawide = 2,
25 SuperUltrawide = 3,
26 Portrait = 4
27};
28
35namespace ScreenClassification {
36
37// Aspect ratio boundary thresholds (width / height).
38constexpr qreal PortraitMax = 1.0;
39constexpr qreal UltrawideMin = 1.9;
40constexpr qreal SuperUltrawideMin = 2.8;
41
42inline AspectRatioClass classify(qreal aspectRatio)
43{
44 // NaN never compares true under <, which would silently fall through every
45 // branch and land at SuperUltrawide. Treat non-finite as "no classification"
46 // so callers that compute aspect ratios from suspect inputs get a stable
47 // sentinel rather than a wildly wrong category.
48 if (!std::isfinite(aspectRatio) || aspectRatio <= 0.0) {
50 }
51 if (aspectRatio < PortraitMax) {
53 }
54 if (aspectRatio < UltrawideMin) {
56 }
57 if (aspectRatio < SuperUltrawideMin) {
59 }
61}
62
63inline AspectRatioClass classify(int width, int height)
64{
65 if (height <= 0 || width <= 0) {
67 }
68 return classify(static_cast<qreal>(width) / height);
69}
70
71inline QString toString(AspectRatioClass cls)
72{
73 switch (cls) {
75 return QStringLiteral("any");
77 return QStringLiteral("standard");
79 return QStringLiteral("ultrawide");
81 return QStringLiteral("super-ultrawide");
83 return QStringLiteral("portrait");
84 }
85 return QStringLiteral("any");
86}
87
88inline AspectRatioClass fromString(const QString& str)
89{
90 if (str == QLatin1String("standard")) {
92 }
93 if (str == QLatin1String("ultrawide")) {
95 }
96 if (str == QLatin1String("super-ultrawide")) {
98 }
99 if (str == QLatin1String("portrait")) {
101 }
103}
104
110inline qreal aspectRatioForClass(AspectRatioClass cls, qreal fallback = 16.0 / 9.0)
111{
112 switch (cls) {
114 return 16.0 / 9.0;
116 return 21.0 / 9.0;
118 return 32.0 / 9.0;
120 return 9.0 / 16.0;
122 return fallback;
123 }
124 // Unreachable under @c -Wswitch (every enumerator handled above). Kept
125 // to satisfy compilers that still warn about control reaching the end
126 // of a non-void function when the enum-covering switch exits the path.
127 return fallback;
128}
129
138inline bool matches(AspectRatioClass layoutClass, AspectRatioClass screenClass)
139{
140 if (layoutClass == AspectRatioClass::Any) {
141 return true;
142 }
143 return layoutClass == screenClass;
144}
145
146} // namespace ScreenClassification
147
148} // namespace PhosphorLayout
constexpr qreal SuperUltrawideMin
AR >= 2.8 → super-ultrawide.
Definition AspectRatioClass.h:40
bool matches(AspectRatioClass layoutClass, AspectRatioClass screenClass)
Check if a layout's aspect ratio class matches the given screen class.
Definition AspectRatioClass.h:138
QString toString(AspectRatioClass cls)
Definition AspectRatioClass.h:71
constexpr qreal UltrawideMin
AR ∈ [UltrawideMin, SuperUltrawideMin) → ultrawide.
Definition AspectRatioClass.h:39
AspectRatioClass fromString(const QString &str)
Definition AspectRatioClass.h:88
constexpr qreal PortraitMax
AR < 1.0 → portrait.
Definition AspectRatioClass.h:38
qreal aspectRatioForClass(AspectRatioClass cls, qreal fallback=16.0/9.0)
Representative aspect ratio for a class.
Definition AspectRatioClass.h:110
AspectRatioClass classify(qreal aspectRatio)
Definition AspectRatioClass.h:42
Definition AlgorithmMetadata.h:10
AspectRatioClass
Screen aspect-ratio classification.
Definition AspectRatioClass.h:21
@ Portrait
Rotated/vertical monitors (< 1.0)
@ Standard
~16:10 to ~16:9 (1.5 - 1.9)
@ Any
Suitable for all aspect ratios (default)