Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
PhosphorSpring.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>
8
9#include <QtCore/QObject>
10#include <QtCore/QString>
11#include <QtQml/qqmlregistration.h>
12
13namespace PhosphorAnimation {
14
27class PHOSPHORANIMATION_EXPORT PhosphorSpring
28{
29 Q_GADGET
30 QML_VALUE_TYPE(phosphorSpring)
31 QML_STRUCTURED_VALUE
32
33 Q_PROPERTY(qreal omega READ omega WRITE setOmega)
34 Q_PROPERTY(qreal zeta READ zeta WRITE setZeta)
35
36public:
37 PhosphorSpring() = default;
39 explicit PhosphorSpring(const Spring& value)
40 : m_value(value)
41 {
42 }
43 PhosphorSpring(qreal omega, qreal zeta)
44 : m_value(omega, zeta)
45 {
46 }
47
53 const Spring& value() const
54 {
55 return m_value;
56 }
57
58 // ─── Property delegates ───
59
60 qreal omega() const
61 {
62 return m_value.omega;
63 }
64 void setOmega(qreal v)
65 {
66 // Re-construct through the Spring ctor so bounds-clamping is
67 // shared with the C++ path. Direct field writes bypass the
68 // clamp which would let a settings UI sneak a pathological
69 // value past the contract.
70 m_value = Spring(v, m_value.zeta);
71 }
72 qreal zeta() const
73 {
74 return m_value.zeta;
75 }
76 void setZeta(qreal v)
77 {
78 m_value = Spring(m_value.omega, v);
79 }
80
81 // ─── Serialization ───
82
84 Q_INVOKABLE QString toString() const
85 {
86 return m_value.toString();
87 }
88
91 Q_INVOKABLE static PhosphorSpring fromString(const QString& str)
92 {
93 return PhosphorSpring(Spring::fromString(str));
94 }
95
96 // ─── Presets ───
97
99 Q_INVOKABLE static PhosphorSpring snappy()
100 {
101 return PhosphorSpring(Spring::snappy());
102 }
104 Q_INVOKABLE static PhosphorSpring smooth()
105 {
106 return PhosphorSpring(Spring::smooth());
107 }
109 Q_INVOKABLE static PhosphorSpring bouncy()
110 {
111 return PhosphorSpring(Spring::bouncy());
112 }
113
114 // ─── Equality ───
115
116 bool operator==(const PhosphorSpring& other) const
117 {
118 return m_value == other.m_value;
119 }
120 bool operator!=(const PhosphorSpring& other) const
121 {
122 return !(*this == other);
123 }
124
125private:
126 Spring m_value;
127};
128
129} // namespace PhosphorAnimation
130
131Q_DECLARE_METATYPE(PhosphorAnimation::PhosphorSpring)
QML value-type wrapper around PhosphorAnimation::Spring.
Definition PhosphorSpring.h:28
bool operator==(const PhosphorSpring &other) const
Definition PhosphorSpring.h:116
PhosphorSpring(const Spring &value)
Implicit-conversion ctor for core-library code.
Definition PhosphorSpring.h:39
PhosphorSpring(qreal omega, qreal zeta)
Definition PhosphorSpring.h:43
const Spring & value() const
Read-only access to the underlying value.
Definition PhosphorSpring.h:53
qreal omega() const
Definition PhosphorSpring.h:60
QString toString() const
Canonical wire format from PhosphorAnimation::Spring::toString.
Definition PhosphorSpring.h:84
static PhosphorSpring smooth()
Critically damped. No overshoot, firm approach.
Definition PhosphorSpring.h:104
void setZeta(qreal v)
Definition PhosphorSpring.h:76
static PhosphorSpring fromString(const QString &str)
Parse "spring:omega,zeta" or "omega,zeta".
Definition PhosphorSpring.h:91
void setOmega(qreal v)
Definition PhosphorSpring.h:64
qreal zeta() const
Definition PhosphorSpring.h:72
static PhosphorSpring bouncy()
Visible bounce. Good for attention-grabbing feedback.
Definition PhosphorSpring.h:109
bool operator!=(const PhosphorSpring &other) const
Definition PhosphorSpring.h:120
static PhosphorSpring snappy()
Responsive, slight overshoot. Good default for window snap.
Definition PhosphorSpring.h:99
Damped harmonic oscillator.
Definition Spring.h:19
Definition AnimatedValue.h:31