Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
motion_preferences.hpp
Go to the documentation of this file.
1#pragma once
2
17
18#include <functional>
19#include <mutex>
20#include <optional>
21#include <string>
22#include <string_view>
23
24namespace pulp::view {
25
33
39class MotionPreferences {
40public:
42 static MotionPreferences& instance();
43
45 static MotionPolicy current() { return instance().policy(); }
46
48 static double current_duration_scale() {
49 return instance().duration_scale();
50 }
51
55
59 double duration_scale() const;
60
64
67 void set_override(std::optional<MotionPolicy> p);
68
70 bool has_override() const noexcept;
71
75 bool poll();
76
79 void on_policy_changed(std::function<void(MotionPolicy)> callback);
80
84
85private:
86 MotionPreferences();
87 ~MotionPreferences();
88 MotionPreferences(const MotionPreferences&) = delete;
89 MotionPreferences& operator=(const MotionPreferences&) = delete;
90
92 static MotionPolicy detect_system_policy();
93
95 MotionPolicy policy_locked() const;
96
100 void notify_changed(MotionPolicy p);
101
103 mutable std::mutex mtx_;
104 MotionPolicy last_os_ = MotionPolicy::Full;
105 std::optional<MotionPolicy> override_;
106 double duration_scale_ = 1.0;
107 std::function<void(MotionPolicy)> callback_;
108};
109
110// ── Free-function convenience shims ──────────────────────────────────
111//
112// Animation primitives use these to keep the call site cheap and avoid
113// pulling the full singleton header into hot paths in the future.
114
117inline float apply_motion_policy_to_duration(float configured_seconds) {
118 auto& prefs = MotionPreferences::instance();
119 switch (prefs.policy()) {
121 return 0.0f;
123 return configured_seconds *
124 static_cast<float>(prefs.duration_scale());
126 default:
127 return configured_seconds;
128 }
129}
130
136
139 switch (p) {
140 case MotionPolicy::Off: return "off";
141 case MotionPolicy::Reduced: return "reduced";
143 default: return "full";
144 }
145}
146
148inline MotionPolicy motion_policy_from_string(const std::string_view s) {
149 if (s == "off") return MotionPolicy::Off;
150 if (s == "reduced") return MotionPolicy::Reduced;
151 return MotionPolicy::Full;
152}
153
154} // namespace pulp::view
void reset_for_tests()
Reset state for tests: clear override, reset duration_scale to 1.0, drop callbacks,...
static MotionPolicy current()
Convenience: the effective policy right now.
Definition motion_preferences.hpp:45
void set_override(std::optional< MotionPolicy > p)
Override the OS-reported policy.
MotionPolicy policy() const
Current effective policy.
static MotionPreferences & instance()
Process-wide instance. Reads the OS value on first construction.
void on_policy_changed(std::function< void(MotionPolicy)> callback)
Register a callback that fires whenever the effective policy changes (via poll() or set_override()).
double duration_scale() const
Current duration scale (default 1.0).
bool poll()
Poll the OS for a changed reduced-motion setting.
void set_duration_scale(double scale)
Set the duration scale.
bool has_override() const noexcept
Returns true if an override is currently in effect.
static double current_duration_scale()
Convenience: the effective duration scale right now.
Definition motion_preferences.hpp:48
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
float apply_motion_policy_to_duration(float configured_seconds)
Apply the current MotionPolicy to a configured duration.
Definition motion_preferences.hpp:117
const char * motion_policy_to_string(MotionPolicy p)
Returns the current policy string for fixture headers.
Definition motion_preferences.hpp:138
bool motion_policy_is_off()
Returns true when the current policy says "do not animate" — i.e.
Definition motion_preferences.hpp:133
@ s
Definition input_events.hpp:41
@ p
Definition input_events.hpp:40
@ scale
Definition css_animation.hpp:126
MotionPolicy motion_policy_from_string(const std::string_view s)
Parse a policy string (case-sensitive). Returns Full on unknown.
Definition motion_preferences.hpp:148
MotionPolicy
Reduced-motion policy.
Definition motion_preferences.hpp:28
@ Reduced
Animate, but scale duration by duration_scale.
Definition motion_preferences.hpp:30
@ Full
Animate normally.
Definition motion_preferences.hpp:29
@ Off
Skip animation; jump to the target value.
Definition motion_preferences.hpp:31
STL namespace.