diff options
| author | crupest <crupest@outlook.com> | 2022-02-14 22:46:54 +0800 |
|---|---|---|
| committer | crupest <crupest@outlook.com> | 2022-02-14 22:46:54 +0800 |
| commit | 6593a25baa01687b9462f792ce91bd46909df229 (patch) | |
| tree | e9ee5ae8d67ef68fdfccca7e07c7d4b26f20c1d7 /src/theme_builder/components/properties/OptionalPropertyEditor.h | |
| parent | 091c0419a254a28f80963193b36bf0a7f30d6590 (diff) | |
| download | cru-6593a25baa01687b9462f792ce91bd46909df229.tar.gz cru-6593a25baa01687b9462f792ce91bd46909df229.tar.bz2 cru-6593a25baa01687b9462f792ce91bd46909df229.zip | |
...
Diffstat (limited to 'src/theme_builder/components/properties/OptionalPropertyEditor.h')
| -rw-r--r-- | src/theme_builder/components/properties/OptionalPropertyEditor.h | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/src/theme_builder/components/properties/OptionalPropertyEditor.h b/src/theme_builder/components/properties/OptionalPropertyEditor.h index e343727f..87ddcaa2 100644 --- a/src/theme_builder/components/properties/OptionalPropertyEditor.h +++ b/src/theme_builder/components/properties/OptionalPropertyEditor.h @@ -3,26 +3,59 @@ #include "cru/ui/controls/CheckBox.h" #include "cru/ui/controls/FlexLayout.h" +#include <optional> + namespace cru::theme_builder::components::properties { template <typename TEditor> class OptionalPropertyEditor : public ui::components::Component { public: + using PropertyType = typename TEditor::PropertyType; + OptionalPropertyEditor() { container_.AddChild(&check_box_); container_.AddChild(editor_->GetRootControl()); + + editor_.ChangeEvent()->AddHandler([this](std::nullptr_t) { + if (IsEnabled()) { + change_event_.Raise(nullptr); + } + }); } ~OptionalPropertyEditor() override { container_.RemoveFromParent(); } ui::controls::Control* GetRootControl() override { return &container_; } bool IsEnabled() const { return check_box_.IsChecked(); } - void SetEnabled(bool enabled) { check_box_.SetChecked(enabled); } + void SetEnabled(bool enabled, bool trigger_change = true) { + check_box_.SetChecked(enabled); + if (trigger_change) { + change_event_.Raise(nullptr); + } + } + + std::optional<PropertyType> GetValue() const { + return IsEnabled() ? editor_.GetValue() : std::nullopt; + } + + void SetValue(std::optional<PropertyType> value, bool trigger_change = true) { + if (value) { + SetEnabled(true, false); + editor_.SetValue(*value); + if (trigger_change) change_event_.Raise(nullptr); + } else { + SetEnabled(false, trigger_change); + } + } TEditor* GetEditor() { return &editor_; } + IEvent<std::nullptr_t>* ChangeEvent() { return &change_event_; } + private: ui::controls::FlexLayout container_; ui::controls::CheckBox check_box_; TEditor editor_; + + Event<std::nullptr_t> change_event_; }; } // namespace cru::theme_builder::components::properties |
