From c6baeb6432a4db7433aab4fc8f89cc235473f11a Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 2 Dec 2020 20:50:26 +0800 Subject: ... --- include/cru/ui/style/Condition.hpp | 31 +++++++++++++++++++-- include/cru/ui/style/StyleRule.hpp | 57 ++++++++++++++++++++++++++++++++++++++ include/cru/ui/style/Styler.hpp | 8 ++++++ 3 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 include/cru/ui/style/StyleRule.hpp (limited to 'include/cru/ui') diff --git a/include/cru/ui/style/Condition.hpp b/include/cru/ui/style/Condition.hpp index 97d29287..c4fd2106 100644 --- a/include/cru/ui/style/Condition.hpp +++ b/include/cru/ui/style/Condition.hpp @@ -5,6 +5,7 @@ #include "cru/ui/controls/IClickableControl.hpp" #include "cru/ui/helper/ClickDetector.hpp" +#include #include #include #include @@ -15,18 +16,26 @@ class Condition : public Object { virtual std::vector ChangeOn( controls::Control* control) const = 0; virtual bool Judge(controls::Control* control) const = 0; + + virtual std::unique_ptr Clone() const = 0; }; class CompoundCondition : public Condition { public: - explicit CompoundCondition(std::vector conditions); + explicit CompoundCondition( + std::vector> conditions); + + const std::vector& GetConditions() const { + return readonly_conditions_; + } - const std::vector& GetConditions() const { return conditions_; } + std::vector> CloneConditions() const; std::vector ChangeOn(controls::Control* control) const override; private: - std::vector conditions_; + std::vector> conditions_; + std::vector readonly_conditions_; }; class AndCondition : public CompoundCondition { @@ -34,6 +43,10 @@ class AndCondition : public CompoundCondition { using CompoundCondition::CompoundCondition; bool Judge(controls::Control* control) const override; + + std::unique_ptr Clone() const override { + return std::make_unique(CloneConditions()); + } }; class OrCondition : public CompoundCondition { @@ -41,6 +54,10 @@ class OrCondition : public CompoundCondition { using CompoundCondition::CompoundCondition; bool Judge(controls::Control* control) const override; + + std::unique_ptr Clone() const override { + return std::make_unique(CloneConditions()); + } }; class FocusCondition : public Condition { @@ -50,6 +67,10 @@ class FocusCondition : public Condition { std::vector ChangeOn(controls::Control* control) const override; bool Judge(controls::Control* control) const override; + std::unique_ptr Clone() const override { + return std::make_unique(has_focus_); + } + private: bool has_focus_; }; @@ -61,6 +82,10 @@ class ClickStateCondition : public Condition { std::vector ChangeOn(controls::Control* control) const override; bool Judge(controls::Control* control) const override; + std::unique_ptr Clone() const override { + return std::make_unique(click_state_); + } + private: helper::ClickState click_state_; }; diff --git a/include/cru/ui/style/StyleRule.hpp b/include/cru/ui/style/StyleRule.hpp new file mode 100644 index 00000000..f1283e24 --- /dev/null +++ b/include/cru/ui/style/StyleRule.hpp @@ -0,0 +1,57 @@ +#pragma once +#include "Condition.hpp" +#include "Styler.hpp" +#include "cru/common/Base.hpp" +#include "cru/ui/Base.hpp" + +#include +#include +#include + +namespace cru::ui::style { +class StyleRule : public Object { + public: + StyleRule(std::unique_ptr condition, + std::unique_ptr styler, std::u16string name = {}); + + StyleRule(const StyleRule& other) + : condition_(other.condition_->Clone()), + styler_(other.styler_->Clone()), + name_(other.name_) {} + + StyleRule& operator=(const StyleRule& other) { + if (this != &other) { + condition_ = other.condition_->Clone(); + styler_ = other.styler_->Clone(); + name_ = other.name_; + } + return *this; + } + + CRU_DEFAULT_MOVE(StyleRule) + + ~StyleRule() override = default; + + public: + const std::u16string& GetName() const { return name_; } + Condition* GetCondition() const { return condition_.get(); } + Styler* GetStyler() const { return styler_.get(); } + + StyleRule WithNewCondition(std::unique_ptr condition, + std::u16string name = {}) const { + return StyleRule{std::move(condition), styler_->Clone(), std::move(name)}; + } + + StyleRule WithNewStyler(std::unique_ptr styler, + std::u16string name = {}) const { + return StyleRule{condition_->Clone(), std::move(styler), std::move(name)}; + } + + bool CheckAndApply(controls::Control* control) const; + + private: + std::unique_ptr condition_; + std::unique_ptr styler_; + std::u16string name_; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/Styler.hpp b/include/cru/ui/style/Styler.hpp index 89731033..4f4b18ba 100644 --- a/include/cru/ui/style/Styler.hpp +++ b/include/cru/ui/style/Styler.hpp @@ -3,10 +3,14 @@ #include "ApplyBorderStyleInfo.hpp" #include "cru/common/Base.hpp" +#include + namespace cru::ui::style { class Styler : public Object { public: virtual void Apply(controls::Control* control) const; + + virtual std::unique_ptr Clone() const = 0; }; class BorderStyler : public Styler { @@ -15,6 +19,10 @@ class BorderStyler : public Styler { void Apply(controls::Control* control) const override; + std::unique_ptr Clone() const override { + return std::make_unique(style_); + } + private: ApplyBorderStyleInfo style_; }; -- cgit v1.2.3