FastLED 3.9.15
Loading...
Searching...
No Matches
checkbox.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/function.h"
4#include "fl/stl/string.h"
5#include "fl/stl/compiler_control.h" // IWYU pragma: keep
6#include "fl/stl/noexcept.h"
8#include "fl/ui/element.h"
9#include "platforms/ui_defs.h"
10
11#ifndef FASTLED_HAS_UI_CHECKBOX
12#define FASTLED_HAS_UI_CHECKBOX 0
13#endif
14
15namespace fl {
16
17#if !FASTLED_HAS_UI_CHECKBOX
18
20 public:
21 UICheckboxImpl(const char *name, bool value = false) : mValue(value) {
22 FASTLED_UNUSED(name);
23 }
25 operator bool() const { return mValue; }
26 explicit operator int() const { return mValue ? 1 : 0; }
29 return *this;
30 }
32 setValue(value != 0);
33 return *this;
34 }
35 bool value() const { return mValue; }
36
37 // Stub method for group setting (does nothing on non-WASM platforms)
38 void setGroup(const fl::string& groupName) { FASTLED_UNUSED(groupName); }
39
40 private:
41 void setValue(bool value) { mValue = value; }
42 bool mValue;
43};
44
45#endif
46
47class UICheckbox : public UIElement {
48 public:
50 UICheckbox(const char *name, bool value = false) FL_NOEXCEPT;
52
53 operator bool() const FL_NOEXCEPT { return value(); }
54 explicit operator int() const FL_NOEXCEPT { return static_cast<int>(value()); }
56 mImpl = value;
57 return *this;
58 }
59 bool value() const FL_NOEXCEPT { return mImpl.value(); }
60
61 // Override setGroup to also update the implementation
62 void setGroup(const fl::string& groupName) FL_NOEXCEPT override {
63 UIElement::setGroup(groupName);
64 // Update the implementation's group if it has the method (WASM platforms)
65 mImpl.setGroup(groupName);
66 }
67
68
69 void onChanged(function<void(UICheckbox &)> callback) FL_NOEXCEPT {
70 mCallbacks.add(callback);
71 mListener.addToEngineEventsOnce();
72 }
74
75 protected:
77
80 // Don't register in constructor - prevents callbacks before owner is fully initialized
81 }
83 if (added) {
85 }
86 }
88 if (added) {
89 return;
90 }
92 added = true;
93 }
94 void onBeginFrame() FL_NOEXCEPT override;
95
96 private:
98 bool added = false;
99 };
100
101 private:
102 function_list<void(UICheckbox &)> mCallbacks;
103 bool mLastFrameValue = false;
106};
107
109
110} // namespace fl
static void removeListener(Listener *listener) FL_NOEXCEPT
static void addListener(Listener *listener, int priority=0) FL_NOEXCEPT
UICheckbox(const char *name, bool value=false) FL_NOEXCEPT
void setGroup(const fl::string &groupName) FL_NOEXCEPT override
Definition checkbox.h:62
Listener mListener
Definition checkbox.h:105
FL_NO_COPY(UICheckbox)
void clearCallbacks() FL_NOEXCEPT
Definition checkbox.h:73
function_list< void(UICheckbox &)> mCallbacks
Definition checkbox.h:102
bool mLastFrameValue
Definition checkbox.h:103
bool value() const FL_NOEXCEPT
Definition checkbox.h:59
void onChanged(function< void(UICheckbox &)> callback) FL_NOEXCEPT
Definition checkbox.h:69
UICheckboxImpl mImpl
Definition checkbox.h:76
bool mLastFrameValueValid
Definition checkbox.h:104
UICheckbox & operator=(bool value) FL_NOEXCEPT
Definition checkbox.h:55
void setValue(bool value)
Definition checkbox.h:41
~UICheckboxImpl() FL_NOEXCEPT
Definition checkbox.h:24
void setGroup(const fl::string &groupName)
Definition checkbox.h:38
bool value() const
Definition checkbox.h:35
UICheckboxImpl(const char *name, bool value=false)
Definition checkbox.h:21
UICheckboxImpl & operator=(int value) FL_NOEXCEPT
Definition checkbox.h:31
UICheckboxImpl & operator=(bool value) FL_NOEXCEPT
Definition checkbox.h:27
virtual void setGroup(const fl::string &groupName) FL_NOEXCEPT
Definition element.h:31
UIElement() FL_NOEXCEPT
#define FASTLED_UI_DEFINE_OPERATORS(UI_CLASS)
Definition element.h:40
Base definition for an LED controller.
Definition crgb.hpp:179
#define FASTLED_UNUSED(x)
#define FL_NOEXCEPT
UICheckbox * mOwner
Definition checkbox.h:97
~Listener() FL_NOEXCEPT
Definition checkbox.h:82
void addToEngineEventsOnce() FL_NOEXCEPT
Definition checkbox.h:87
void onBeginFrame() FL_NOEXCEPT override
Listener(UICheckbox *owner) FL_NOEXCEPT
Definition checkbox.h:79