Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
PhosphorCurve.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
7#include <PhosphorAnimation/phosphoranimation_export.h>
10
11#include <QtCore/QObject>
12#include <QtCore/QString>
13#include <QtQml/qqmlregistration.h>
14
15#include <atomic>
16#include <memory>
17
18namespace PhosphorAnimation {
19
20class CurveRegistry;
21
46class PHOSPHORANIMATION_EXPORT PhosphorCurve
47{
48 Q_GADGET
49 QML_VALUE_TYPE(phosphorCurve)
50 // No QML_STRUCTURED_VALUE — PhosphorCurve is opaque; the caller
51 // constructs it via factory functions (fromEasing/fromSpring/
52 // fromString) rather than a struct literal.
53
54 Q_PROPERTY(QString typeId READ typeId)
55
56public:
57 PhosphorCurve() = default;
58 explicit PhosphorCurve(std::shared_ptr<const Curve> curve)
59 : m_curve(std::move(curve))
60 {
61 }
62
64 std::shared_ptr<const Curve> curve() const
65 {
66 return m_curve;
67 }
68
71 QString typeId() const
72 {
73 return m_curve ? m_curve->typeId() : QString();
74 }
75
77 Q_INVOKABLE QString toString() const
78 {
79 return m_curve ? m_curve->toString() : QString();
80 }
81
83 Q_INVOKABLE bool isNull() const
84 {
85 return !m_curve;
86 }
87
88 // ─── Registry wiring ───
89
95 static void setDefaultRegistry(CurveRegistry* registry);
96
107 {
108 return s_registry.load(std::memory_order_acquire);
109 }
110
111 // ─── Factory helpers ───
112
117 Q_INVOKABLE static PhosphorCurve fromString(const QString& str);
118
121 Q_INVOKABLE static PhosphorCurve fromEasing(const PhosphorEasing& easing);
122
124 Q_INVOKABLE static PhosphorCurve fromSpring(const PhosphorSpring& spring);
125
126 // ─── Equality ───
127
131 bool operator==(const PhosphorCurve& other) const
132 {
133 if (m_curve == other.m_curve) {
134 return true;
135 }
136 if (!m_curve || !other.m_curve) {
137 return false;
138 }
139 return m_curve->equals(*other.m_curve);
140 }
141 bool operator!=(const PhosphorCurve& other) const
142 {
143 return !(*this == other);
144 }
145
146private:
147 // Atomic so concurrent QML loaders (multiple QQmlEngine instances
148 // on different threads — a background-prerender shell is the
149 // canonical case) cannot race on install-vs-read. Pointer loads
150 // are lock-free on every platform Qt supports; `relaxed` ordering
151 // is sufficient because the registry object's own initialisation
152 // is synchronised by the composition root's construction (the
153 // pointed-to `CurveRegistry` is already fully constructed before
154 // the publishing thread calls setDefaultRegistry).
155 static std::atomic<CurveRegistry*> s_registry;
156 std::shared_ptr<const Curve> m_curve;
157};
158
159} // namespace PhosphorAnimation
160
161Q_DECLARE_METATYPE(PhosphorAnimation::PhosphorCurve)
String-id <-> curve factory registry.
Definition CurveRegistry.h:22
Opaque QML value-type wrapper around shared_ptr<const Curve>.
Definition PhosphorCurve.h:47
static PhosphorCurve fromSpring(const PhosphorSpring &spring)
Wrap a Spring value as a polymorphic PhosphorCurve.
static PhosphorCurve fromEasing(const PhosphorEasing &easing)
Wrap an Easing value as a polymorphic PhosphorCurve.
bool isNull() const
Null-handle check for QML code: if (!curve.isNull()) ....
Definition PhosphorCurve.h:83
bool operator!=(const PhosphorCurve &other) const
Definition PhosphorCurve.h:141
static void setDefaultRegistry(CurveRegistry *registry)
Set the process-wide default CurveRegistry used by fromString.
bool operator==(const PhosphorCurve &other) const
Equality compares via Curve::equals so curves with floating-point parameters compare tightly rather t...
Definition PhosphorCurve.h:131
std::shared_ptr< const Curve > curve() const
The wrapped pointer. May be null on a default-constructed handle.
Definition PhosphorCurve.h:64
QString typeId() const
Stable curve type-id string ("cubic-bezier", "spring", "elastic-out", …).
Definition PhosphorCurve.h:71
static CurveRegistry * defaultRegistry()
Read-only view of the process-wide default CurveRegistry pointer installed via setDefaultRegistry.
Definition PhosphorCurve.h:106
static PhosphorCurve fromString(const QString &str)
Parse via CurveRegistry — handles every curve type the registry knows, including user-authored curves...
PhosphorCurve(std::shared_ptr< const Curve > curve)
Definition PhosphorCurve.h:58
QString toString() const
Serialize to wire format. Empty when null.
Definition PhosphorCurve.h:77
QML value-type wrapper around PhosphorAnimation::Easing.
Definition PhosphorEasing.h:38
QML value-type wrapper around PhosphorAnimation::Spring.
Definition PhosphorSpring.h:28
Definition AnimatedValue.h:31