mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-10-31 18:48:11 +08:00 
			
		
		
		
	Dropdown: Fix title not updated. (#851)
A bug was introduced by: https://github.com/ArthurSonzogni/FTXUI/pull/826 The checkbox label wasn't updated. Bug:https://github.com/ArthurSonzogni/FTXUI/issues/861
This commit is contained in:
		| @@ -5,8 +5,10 @@ | ||||
| #define FTXUI_UTIL_REF_HPP | ||||
|  | ||||
| #include <ftxui/screen/string.hpp> | ||||
| #include <memory> | ||||
| #include <string> | ||||
| #include <variant> | ||||
| #include <vector> | ||||
|  | ||||
| namespace ftxui { | ||||
|  | ||||
| @@ -104,42 +106,109 @@ class ConstStringRef : public ConstRef<std::string> { | ||||
| }; | ||||
|  | ||||
| /// @brief An adapter. Reference a list of strings. | ||||
| /// | ||||
| /// Supported input: | ||||
| /// - `std::vector<std::string>` | ||||
| /// - `std::vector<std::string>*` | ||||
| /// - `std::vector<std::wstring>*` | ||||
| /// - `Adapter*` | ||||
| /// - `std::unique_ptr<Adapter>` | ||||
| class ConstStringListRef { | ||||
|  public: | ||||
|   // Bring your own adapter: | ||||
|   class Adapter { | ||||
|    public: | ||||
|     Adapter() = default; | ||||
|     Adapter(const Adapter&) = default; | ||||
|     Adapter& operator=(const Adapter&) = default; | ||||
|     Adapter(Adapter&&) = default; | ||||
|     Adapter& operator=(Adapter&&) = default; | ||||
|     virtual ~Adapter() = default; | ||||
|     virtual size_t size() const = 0; | ||||
|     virtual std::string operator[](size_t i) const = 0; | ||||
|   }; | ||||
|   using Variant = std::variant<const std::vector<std::string>,    // | ||||
|                                const std::vector<std::string>*,   // | ||||
|                                const std::vector<std::wstring>*,  // | ||||
|                                Adapter*,                          // | ||||
|                                std::unique_ptr<Adapter>           // | ||||
|                                >; | ||||
|  | ||||
|   ConstStringListRef() = default; | ||||
|   ~ConstStringListRef() = default; | ||||
|   ConstStringListRef(ConstStringListRef&&) = delete; | ||||
|   ConstStringListRef& operator=(ConstStringListRef&&) = delete; | ||||
|   ConstStringListRef(const std::vector<std::string>* ref)  // NOLINT | ||||
|       : ref_(ref) {} | ||||
|   ConstStringListRef(const std::vector<std::wstring>* ref)  // NOLINT | ||||
|       : ref_wide_(ref) {} | ||||
|   ConstStringListRef(const ConstStringListRef& other) = default; | ||||
|   ConstStringListRef& operator=(const ConstStringListRef& other) = default; | ||||
|   ConstStringListRef& operator=(const ConstStringListRef&) = default; | ||||
|   ConstStringListRef& operator=(ConstStringListRef&&) = default; | ||||
|   ConstStringListRef(ConstStringListRef&&) = default; | ||||
|   ConstStringListRef(const ConstStringListRef&) = default; | ||||
|  | ||||
|   ConstStringListRef(std::vector<std::string> value)  // NOLINT | ||||
|   { | ||||
|     variant_ = std::make_shared<Variant>(value); | ||||
|   } | ||||
|   ConstStringListRef(const std::vector<std::string>* value)  // NOLINT | ||||
|   { | ||||
|     variant_ = std::make_shared<Variant>(value); | ||||
|   } | ||||
|   ConstStringListRef(const std::vector<std::wstring>* value)  // NOLINT | ||||
|   { | ||||
|     variant_ = std::make_shared<Variant>(value); | ||||
|   } | ||||
|   ConstStringListRef(Adapter* adapter)  // NOLINT | ||||
|   { | ||||
|     variant_ = std::make_shared<Variant>(adapter); | ||||
|   } | ||||
|   template <typename AdapterType> | ||||
|   ConstStringListRef(std::unique_ptr<AdapterType> adapter)  // NOLINT | ||||
|   { | ||||
|     variant_ = std::make_shared<Variant>( | ||||
|         static_cast<std::unique_ptr<Adapter>>(std::move(adapter))); | ||||
|   } | ||||
|  | ||||
|   size_t size() const { | ||||
|     if (ref_) { | ||||
|       return ref_->size(); | ||||
|     } | ||||
|     if (ref_wide_) { | ||||
|       return ref_wide_->size(); | ||||
|     } | ||||
|     return 0; | ||||
|     return variant_ ? std::visit(SizeVisitor(), *variant_) : 0; | ||||
|   } | ||||
|  | ||||
|   std::string operator[](size_t i) const { | ||||
|     if (ref_) { | ||||
|       return (*ref_)[i]; | ||||
|     } | ||||
|     if (ref_wide_) { | ||||
|       return to_string((*ref_wide_)[i]); | ||||
|     } | ||||
|     return ""; | ||||
|     return variant_ ? std::visit(IndexedGetter(i), *variant_) : ""; | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   const std::vector<std::string>* ref_ = nullptr; | ||||
|   const std::vector<std::wstring>* ref_wide_ = nullptr; | ||||
|   struct SizeVisitor { | ||||
|     size_t operator()(const std::vector<std::string>& v) const { | ||||
|       return v.size(); | ||||
|     } | ||||
|     size_t operator()(const std::vector<std::string>* v) const { | ||||
|       return v->size(); | ||||
|     } | ||||
|     size_t operator()(const std::vector<std::wstring>* v) const { | ||||
|       return v->size(); | ||||
|     } | ||||
|     size_t operator()(const Adapter* v) const { return v->size(); } | ||||
|     size_t operator()(const std::unique_ptr<Adapter>& v) const { | ||||
|       return v->size(); | ||||
|     } | ||||
|   }; | ||||
|  | ||||
|   struct IndexedGetter { | ||||
|     IndexedGetter(size_t index)  // NOLINT | ||||
|         : index_(index) {} | ||||
|     size_t index_; | ||||
|     std::string operator()(const std::vector<std::string>& v) const { | ||||
|       return v[index_]; | ||||
|     } | ||||
|     std::string operator()(const std::vector<std::string>* v) const { | ||||
|       return (*v)[index_]; | ||||
|     } | ||||
|     std::string operator()(const std::vector<std::wstring>* v) const { | ||||
|       return to_string((*v)[index_]); | ||||
|     } | ||||
|     std::string operator()(const Adapter* v) const { return (*v)[index_]; } | ||||
|     std::string operator()(const std::unique_ptr<Adapter>& v) const { | ||||
|       return (*v)[index_]; | ||||
|     } | ||||
|   }; | ||||
|  | ||||
|   std::shared_ptr<Variant> variant_; | ||||
| }; | ||||
|  | ||||
| }  // namespace ftxui | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Arthur Sonzogni
					Arthur Sonzogni