diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/cru/common/Event.hpp | 2 | ||||
-rw-r--r-- | include/cru/ui/Base.hpp | 1 | ||||
-rw-r--r-- | include/cru/ui/controls/Control.hpp | 1 | ||||
-rw-r--r-- | include/cru/ui/style/StyleRuleSet.hpp | 40 |
4 files changed, 40 insertions, 4 deletions
diff --git a/include/cru/common/Event.hpp b/include/cru/common/Event.hpp index 7f7b4dd4..aa8fadbb 100644 --- a/include/cru/common/Event.hpp +++ b/include/cru/common/Event.hpp @@ -233,6 +233,8 @@ class EventRevokerGuard { EventRevoker Release() { return std::move(*revoker_.release()); } + void Reset() { revoker_.reset(); } + void Reset(EventRevoker&& revoker) { revoker_.reset(new EventRevoker(std::move(revoker))); } diff --git a/include/cru/ui/Base.hpp b/include/cru/ui/Base.hpp index 57beb723..b2939a0b 100644 --- a/include/cru/ui/Base.hpp +++ b/include/cru/ui/Base.hpp @@ -42,6 +42,7 @@ class RenderObject; namespace style { class StyleRuleSet; +class StyleRuleSetBind; } //-------------------- region: basic types -------------------- diff --git a/include/cru/ui/controls/Control.hpp b/include/cru/ui/controls/Control.hpp index 0d34bc63..341b6ef2 100644 --- a/include/cru/ui/controls/Control.hpp +++ b/include/cru/ui/controls/Control.hpp @@ -152,5 +152,6 @@ class Control : public Object { std::shared_ptr<platform::gui::ICursor> cursor_ = nullptr; std::unique_ptr<style::StyleRuleSet> style_rule_set_; + std::unique_ptr<style::StyleRuleSetBind> style_rule_set_bind_; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/style/StyleRuleSet.hpp b/include/cru/ui/style/StyleRuleSet.hpp index 3ec71730..ba3f8b4c 100644 --- a/include/cru/ui/style/StyleRuleSet.hpp +++ b/include/cru/ui/style/StyleRuleSet.hpp @@ -2,13 +2,14 @@ #include "StyleRule.hpp" #include "cru/common/Base.hpp" #include "cru/common/Event.hpp" -#include "gsl/gsl_assert" + +#include <cstddef> namespace cru::ui::style { class StyleRuleSet : public Object { public: - StyleRuleSet() : control_(nullptr) {} - explicit StyleRuleSet(controls::Control* control) : control_(control) {} + StyleRuleSet() = default; + explicit StyleRuleSet(StyleRuleSet* parent); CRU_DELETE_COPY(StyleRuleSet) CRU_DELETE_MOVE(StyleRuleSet) @@ -16,6 +17,9 @@ class StyleRuleSet : public Object { ~StyleRuleSet() override = default; public: + StyleRuleSet* GetParent() const { return parent_; } + void SetParent(StyleRuleSet* parent); + gsl::index GetSize() const { return static_cast<gsl::index>(rules_.size()); } const std::vector<StyleRule>& GetRules() const { return rules_; } @@ -41,14 +45,42 @@ class StyleRuleSet : public Object { const StyleRule& operator[](gsl::index index) const { return rules_[index]; } + // Triggered whenever a change happened to this (rule add or remove, parent + // change ...). Subscribe to this and update style change listeners and style. + IEvent<std::nullptr_t>* ChangeEvent() { return &change_event_; } + + private: + void RaiseChangeEvent() { change_event_.Raise(nullptr); } + + private: + Event<std::nullptr_t> change_event_; + + StyleRuleSet* parent_ = nullptr; + EventRevokerGuard parent_change_event_guard_; + + std::vector<StyleRule> rules_; +}; + +class StyleRuleSetBind { + public: + StyleRuleSetBind(controls::Control* control, StyleRuleSet* ruleset); + + CRU_DELETE_COPY(StyleRuleSetBind) + CRU_DELETE_MOVE(StyleRuleSetBind) + + ~StyleRuleSetBind() = default; + private: + void UpdateRuleSetChainCache(); void UpdateChangeListener(); void UpdateStyle(); private: controls::Control* control_; + StyleRuleSet* ruleset_; - std::vector<StyleRule> rules_; + // child first, parent last. + std::vector<StyleRuleSet*> ruleset_chain_cache_; EventRevokerListGuard guard_; }; |