Definition of the internal class that was previously in dropdown_internal.h
| 12 | |
| 13 | // Definition of the internal class that was previously in dropdown_internal.h |
| 14 | class JsonUiDropdownInternal : public JsonUiInternal { |
| 15 | private: |
| 16 | fl::vector<fl::string> mOptions; |
| 17 | size_t mSelectedIndex; |
| 18 | |
| 19 | public: |
| 20 | // Constructor: Initializes the base JsonUiInternal with name and sets initial values. |
| 21 | JsonUiDropdownInternal(const fl::string& name, const fl::vector<fl::string>& options, size_t selectedIndex = 0) |
| 22 | FL_NOEXCEPT : JsonUiInternal(name), mOptions(options), mSelectedIndex(selectedIndex) {} |
| 23 | |
| 24 | // Override toJson to serialize the dropdown's data directly. |
| 25 | void toJson(fl::json& json) const FL_NOEXCEPT override { |
| 26 | json.set("name", name()); |
| 27 | json.set("type", "dropdown"); |
| 28 | json.set("group", groupName()); |
| 29 | json.set("id", id()); |
| 30 | json.set("value", static_cast<int>(mSelectedIndex)); |
| 31 | |
| 32 | fl::json optionsArray = fl::json::array(); |
| 33 | for (const auto& option : mOptions) { |
| 34 | optionsArray.push_back(option); |
| 35 | } |
| 36 | json.set("options", optionsArray); |
| 37 | } |
| 38 | |
| 39 | // Override updateInternal to handle updates from JSON. |
| 40 | void updateInternal(const fl::json& json) FL_NOEXCEPT override { |
| 41 | int index = json | 0; |
| 42 | if (index >= 0 && static_cast<size_t>(index) < mOptions.size()) { |
| 43 | mSelectedIndex = static_cast<size_t>(index); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Accessors for the dropdown state. |
| 48 | const fl::vector<fl::string>& options() const FL_NOEXCEPT { return mOptions; } |
| 49 | size_t selectedIndex() const FL_NOEXCEPT { return mSelectedIndex; } |
| 50 | void setSelectedIndex(size_t index) FL_NOEXCEPT { |
| 51 | if (index < mOptions.size()) { |
| 52 | mSelectedIndex = index; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | fl::string value() const FL_NOEXCEPT { |
| 57 | if (mSelectedIndex < mOptions.size()) { |
| 58 | return mOptions[mSelectedIndex]; |
| 59 | } |
| 60 | return fl::string(); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | // Constructor with array of options and count |
| 65 | JsonDropdownImpl::JsonDropdownImpl(const fl::string &name, const fl::string* options, size_t count) FL_NOEXCEPT { |