From 74bb9cd27242b9320f99ff4d2b50c3051576cc14 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 8 Feb 2022 16:53:51 +0800 Subject: ... --- include/cru/ui/style/ApplyBorderStyleInfo.h | 30 ++++++ include/cru/ui/style/ApplyBorderStyleInfo.hpp | 30 ------ include/cru/ui/style/Condition.h | 133 ++++++++++++++++++++++++++ include/cru/ui/style/Condition.hpp | 133 -------------------------- include/cru/ui/style/StyleRule.h | 49 ++++++++++ include/cru/ui/style/StyleRule.hpp | 49 ---------- include/cru/ui/style/StyleRuleSet.h | 80 ++++++++++++++++ include/cru/ui/style/StyleRuleSet.hpp | 80 ---------------- include/cru/ui/style/Styler.h | 90 +++++++++++++++++ include/cru/ui/style/Styler.hpp | 90 ----------------- 10 files changed, 382 insertions(+), 382 deletions(-) create mode 100644 include/cru/ui/style/ApplyBorderStyleInfo.h delete mode 100644 include/cru/ui/style/ApplyBorderStyleInfo.hpp create mode 100644 include/cru/ui/style/Condition.h delete mode 100644 include/cru/ui/style/Condition.hpp create mode 100644 include/cru/ui/style/StyleRule.h delete mode 100644 include/cru/ui/style/StyleRule.hpp create mode 100644 include/cru/ui/style/StyleRuleSet.h delete mode 100644 include/cru/ui/style/StyleRuleSet.hpp create mode 100644 include/cru/ui/style/Styler.h delete mode 100644 include/cru/ui/style/Styler.hpp (limited to 'include/cru/ui/style') diff --git a/include/cru/ui/style/ApplyBorderStyleInfo.h b/include/cru/ui/style/ApplyBorderStyleInfo.h new file mode 100644 index 00000000..2e6753fc --- /dev/null +++ b/include/cru/ui/style/ApplyBorderStyleInfo.h @@ -0,0 +1,30 @@ +#pragma once +#include "../Base.h" + +#include + +namespace cru::ui::style { +struct ApplyBorderStyleInfo { + ApplyBorderStyleInfo() = default; + + explicit ApplyBorderStyleInfo( + std::optional> border_brush, + std::optional border_thickness = std::nullopt, + std::optional border_radius = std::nullopt, + std::optional> + foreground_brush = std::nullopt, + std::optional> + background_brush = std::nullopt) + : border_brush(std::move(border_brush)), + border_thickness(std::move(border_thickness)), + border_radius(std::move(border_radius)), + foreground_brush(std::move(foreground_brush)), + background_brush(std::move(background_brush)) {} + + std::optional> border_brush; + std::optional border_thickness; + std::optional border_radius; + std::optional> foreground_brush; + std::optional> background_brush; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/ApplyBorderStyleInfo.hpp b/include/cru/ui/style/ApplyBorderStyleInfo.hpp deleted file mode 100644 index 7ae5b2c5..00000000 --- a/include/cru/ui/style/ApplyBorderStyleInfo.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -#include "../Base.hpp" - -#include - -namespace cru::ui::style { -struct ApplyBorderStyleInfo { - ApplyBorderStyleInfo() = default; - - explicit ApplyBorderStyleInfo( - std::optional> border_brush, - std::optional border_thickness = std::nullopt, - std::optional border_radius = std::nullopt, - std::optional> - foreground_brush = std::nullopt, - std::optional> - background_brush = std::nullopt) - : border_brush(std::move(border_brush)), - border_thickness(std::move(border_thickness)), - border_radius(std::move(border_radius)), - foreground_brush(std::move(foreground_brush)), - background_brush(std::move(background_brush)) {} - - std::optional> border_brush; - std::optional border_thickness; - std::optional border_radius; - std::optional> foreground_brush; - std::optional> background_brush; -}; -} // namespace cru::ui::style diff --git a/include/cru/ui/style/Condition.h b/include/cru/ui/style/Condition.h new file mode 100644 index 00000000..221a7428 --- /dev/null +++ b/include/cru/ui/style/Condition.h @@ -0,0 +1,133 @@ +#pragma once +#include "../Base.h" +#include "cru/common/Base.h" +#include "cru/common/ClonablePtr.h" +#include "cru/common/Event.h" +#include "cru/ui/controls/IClickableControl.h" +#include "cru/ui/helper/ClickDetector.h" + +#include +#include +#include +#include + +namespace cru::ui::style { +class Condition : public Object { + public: + virtual std::vector ChangeOn( + controls::Control* control) const = 0; + virtual bool Judge(controls::Control* control) const = 0; + + virtual Condition* Clone() const = 0; +}; + +class NoCondition : public Condition { + public: + static ClonablePtr Create() { + return ClonablePtr(new NoCondition); + }; + + std::vector ChangeOn(controls::Control*) const override { + return {}; + } + + bool Judge(controls::Control*) const override { return true; } + + NoCondition* Clone() const override { return new NoCondition; } +}; + +class CompoundCondition : public Condition { + public: + explicit CompoundCondition(std::vector> conditions); + + std::vector ChangeOn(controls::Control* control) const override; + + protected: + std::vector> conditions_; +}; + +class AndCondition : public CompoundCondition { + public: + static ClonablePtr Create( + std::vector> conditions) { + return ClonablePtr(new AndCondition(std::move(conditions))); + } + + using CompoundCondition::CompoundCondition; + + bool Judge(controls::Control* control) const override; + + AndCondition* Clone() const override { return new AndCondition(conditions_); } +}; + +class OrCondition : public CompoundCondition { + public: + static ClonablePtr Create( + std::vector> conditions) { + return ClonablePtr(new OrCondition(std::move(conditions))); + } + + using CompoundCondition::CompoundCondition; + + bool Judge(controls::Control* control) const override; + + OrCondition* Clone() const override { return new OrCondition(conditions_); } +}; + +class FocusCondition : public Condition { + public: + static ClonablePtr Create(bool has_focus) { + return ClonablePtr(new FocusCondition(has_focus)); + } + + explicit FocusCondition(bool has_focus); + + std::vector ChangeOn(controls::Control* control) const override; + bool Judge(controls::Control* control) const override; + + FocusCondition* Clone() const override { + return new FocusCondition(has_focus_); + } + + private: + bool has_focus_; +}; + +class HoverCondition : public Condition { + public: + static ClonablePtr Create(bool hover) { + return ClonablePtr(new HoverCondition(hover)); + } + + explicit HoverCondition(bool hover) : hover_(hover) {} + + std::vector ChangeOn(controls::Control* control) const override; + bool Judge(controls::Control* control) const override; + + HoverCondition* Clone() const override { return new HoverCondition(hover_); } + + private: + bool hover_; +}; + +class ClickStateCondition : public Condition { + public: + static ClonablePtr Create( + helper::ClickState click_state) { + return ClonablePtr( + new ClickStateCondition(click_state)); + } + + explicit ClickStateCondition(helper::ClickState click_state); + + std::vector ChangeOn(controls::Control* control) const override; + bool Judge(controls::Control* control) const override; + + ClickStateCondition* Clone() const override { + return new ClickStateCondition(click_state_); + } + + private: + helper::ClickState click_state_; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/Condition.hpp b/include/cru/ui/style/Condition.hpp deleted file mode 100644 index 89da81f6..00000000 --- a/include/cru/ui/style/Condition.hpp +++ /dev/null @@ -1,133 +0,0 @@ -#pragma once -#include "../Base.hpp" -#include "cru/common/Base.hpp" -#include "cru/common/ClonablePtr.hpp" -#include "cru/common/Event.hpp" -#include "cru/ui/controls/IClickableControl.hpp" -#include "cru/ui/helper/ClickDetector.hpp" - -#include -#include -#include -#include - -namespace cru::ui::style { -class Condition : public Object { - public: - virtual std::vector ChangeOn( - controls::Control* control) const = 0; - virtual bool Judge(controls::Control* control) const = 0; - - virtual Condition* Clone() const = 0; -}; - -class NoCondition : public Condition { - public: - static ClonablePtr Create() { - return ClonablePtr(new NoCondition); - }; - - std::vector ChangeOn(controls::Control*) const override { - return {}; - } - - bool Judge(controls::Control*) const override { return true; } - - NoCondition* Clone() const override { return new NoCondition; } -}; - -class CompoundCondition : public Condition { - public: - explicit CompoundCondition(std::vector> conditions); - - std::vector ChangeOn(controls::Control* control) const override; - - protected: - std::vector> conditions_; -}; - -class AndCondition : public CompoundCondition { - public: - static ClonablePtr Create( - std::vector> conditions) { - return ClonablePtr(new AndCondition(std::move(conditions))); - } - - using CompoundCondition::CompoundCondition; - - bool Judge(controls::Control* control) const override; - - AndCondition* Clone() const override { return new AndCondition(conditions_); } -}; - -class OrCondition : public CompoundCondition { - public: - static ClonablePtr Create( - std::vector> conditions) { - return ClonablePtr(new OrCondition(std::move(conditions))); - } - - using CompoundCondition::CompoundCondition; - - bool Judge(controls::Control* control) const override; - - OrCondition* Clone() const override { return new OrCondition(conditions_); } -}; - -class FocusCondition : public Condition { - public: - static ClonablePtr Create(bool has_focus) { - return ClonablePtr(new FocusCondition(has_focus)); - } - - explicit FocusCondition(bool has_focus); - - std::vector ChangeOn(controls::Control* control) const override; - bool Judge(controls::Control* control) const override; - - FocusCondition* Clone() const override { - return new FocusCondition(has_focus_); - } - - private: - bool has_focus_; -}; - -class HoverCondition : public Condition { - public: - static ClonablePtr Create(bool hover) { - return ClonablePtr(new HoverCondition(hover)); - } - - explicit HoverCondition(bool hover) : hover_(hover) {} - - std::vector ChangeOn(controls::Control* control) const override; - bool Judge(controls::Control* control) const override; - - HoverCondition* Clone() const override { return new HoverCondition(hover_); } - - private: - bool hover_; -}; - -class ClickStateCondition : public Condition { - public: - static ClonablePtr Create( - helper::ClickState click_state) { - return ClonablePtr( - new ClickStateCondition(click_state)); - } - - explicit ClickStateCondition(helper::ClickState click_state); - - std::vector ChangeOn(controls::Control* control) const override; - bool Judge(controls::Control* control) const override; - - ClickStateCondition* Clone() const override { - return new ClickStateCondition(click_state_); - } - - private: - helper::ClickState click_state_; -}; -} // namespace cru::ui::style diff --git a/include/cru/ui/style/StyleRule.h b/include/cru/ui/style/StyleRule.h new file mode 100644 index 00000000..68b239f3 --- /dev/null +++ b/include/cru/ui/style/StyleRule.h @@ -0,0 +1,49 @@ +#pragma once +#include "../Base.h" +#include "Condition.h" +#include "Styler.h" +#include "cru/common/ClonablePtr.h" + +#include +#include + +namespace cru::ui::style { +class StyleRule : public Object { + public: + static ClonablePtr Create(ClonablePtr condition, + ClonablePtr styler, + String name = {}) { + return ClonablePtr(new StyleRule( + std::move(condition), std::move(styler), std::move(name))); + } + + StyleRule(ClonablePtr condition, ClonablePtr styler, + String name = {}); + + CRU_DEFAULT_COPY(StyleRule) + CRU_DEFAULT_MOVE(StyleRule) + + ~StyleRule() override = default; + + public: + String GetName() const { return name_; } + Condition* GetCondition() const { return condition_.get(); } + Styler* GetStyler() const { return styler_.get(); } + + StyleRule WithNewCondition(ClonablePtr condition, + String name = {}) const { + return StyleRule{std::move(condition), styler_, std::move(name)}; + } + + StyleRule WithNewStyler(ClonablePtr styler, String name = {}) const { + return StyleRule{condition_, std::move(styler), std::move(name)}; + } + + bool CheckAndApply(controls::Control* control) const; + + private: + ClonablePtr condition_; + ClonablePtr styler_; + String name_; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/StyleRule.hpp b/include/cru/ui/style/StyleRule.hpp deleted file mode 100644 index 743aa759..00000000 --- a/include/cru/ui/style/StyleRule.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once -#include "../Base.hpp" -#include "Condition.hpp" -#include "Styler.hpp" -#include "cru/common/ClonablePtr.hpp" - -#include -#include - -namespace cru::ui::style { -class StyleRule : public Object { - public: - static ClonablePtr Create(ClonablePtr condition, - ClonablePtr styler, - String name = {}) { - return ClonablePtr(new StyleRule( - std::move(condition), std::move(styler), std::move(name))); - } - - StyleRule(ClonablePtr condition, ClonablePtr styler, - String name = {}); - - CRU_DEFAULT_COPY(StyleRule) - CRU_DEFAULT_MOVE(StyleRule) - - ~StyleRule() override = default; - - public: - String GetName() const { return name_; } - Condition* GetCondition() const { return condition_.get(); } - Styler* GetStyler() const { return styler_.get(); } - - StyleRule WithNewCondition(ClonablePtr condition, - String name = {}) const { - return StyleRule{std::move(condition), styler_, std::move(name)}; - } - - StyleRule WithNewStyler(ClonablePtr styler, String name = {}) const { - return StyleRule{condition_, std::move(styler), std::move(name)}; - } - - bool CheckAndApply(controls::Control* control) const; - - private: - ClonablePtr condition_; - ClonablePtr styler_; - String name_; -}; -} // namespace cru::ui::style diff --git a/include/cru/ui/style/StyleRuleSet.h b/include/cru/ui/style/StyleRuleSet.h new file mode 100644 index 00000000..34d0fad4 --- /dev/null +++ b/include/cru/ui/style/StyleRuleSet.h @@ -0,0 +1,80 @@ +#pragma once +#include "StyleRule.h" +#include "cru/common/Base.h" +#include "cru/common/Event.h" + +#include + +namespace cru::ui::style { +class StyleRuleSet : public Object { + public: + StyleRuleSet() = default; + explicit StyleRuleSet(std::shared_ptr parent); + + CRU_DELETE_COPY(StyleRuleSet) + CRU_DELETE_MOVE(StyleRuleSet) + + ~StyleRuleSet() override = default; + + public: + std::shared_ptr GetParent() const { return parent_; } + void SetParent(std::shared_ptr parent); + + gsl::index GetSize() const { return static_cast(rules_.size()); } + const std::vector& GetRules() const { return rules_; } + + void AddStyleRule(StyleRule rule) { + AddStyleRule(std::move(rule), GetSize()); + } + + void AddStyleRule(StyleRule rule, gsl::index index); + + void RemoveStyleRule(gsl::index index, gsl::index count = 1); + + void Clear() { RemoveStyleRule(0, GetSize()); } + + void Set(const StyleRuleSet& other, bool set_parent = false); + + 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* ChangeEvent() { return &change_event_; } + + private: + void RaiseChangeEvent() { change_event_.Raise(nullptr); } + + private: + Event change_event_; + + std::shared_ptr parent_ = nullptr; + EventRevokerGuard parent_change_event_guard_; + + std::vector rules_; +}; + +class StyleRuleSetBind { + public: + StyleRuleSetBind(controls::Control* control, + std::shared_ptr ruleset); + + CRU_DELETE_COPY(StyleRuleSetBind) + CRU_DELETE_MOVE(StyleRuleSetBind) + + ~StyleRuleSetBind() = default; + + private: + void UpdateRuleSetChainCache(); + void UpdateChangeListener(); + void UpdateStyle(); + + private: + controls::Control* control_; + std::shared_ptr ruleset_; + + // child first, parent last. + std::vector ruleset_chain_cache_; + + EventRevokerListGuard guard_; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/StyleRuleSet.hpp b/include/cru/ui/style/StyleRuleSet.hpp deleted file mode 100644 index 32b02d78..00000000 --- a/include/cru/ui/style/StyleRuleSet.hpp +++ /dev/null @@ -1,80 +0,0 @@ -#pragma once -#include "StyleRule.hpp" -#include "cru/common/Base.hpp" -#include "cru/common/Event.hpp" - -#include - -namespace cru::ui::style { -class StyleRuleSet : public Object { - public: - StyleRuleSet() = default; - explicit StyleRuleSet(std::shared_ptr parent); - - CRU_DELETE_COPY(StyleRuleSet) - CRU_DELETE_MOVE(StyleRuleSet) - - ~StyleRuleSet() override = default; - - public: - std::shared_ptr GetParent() const { return parent_; } - void SetParent(std::shared_ptr parent); - - gsl::index GetSize() const { return static_cast(rules_.size()); } - const std::vector& GetRules() const { return rules_; } - - void AddStyleRule(StyleRule rule) { - AddStyleRule(std::move(rule), GetSize()); - } - - void AddStyleRule(StyleRule rule, gsl::index index); - - void RemoveStyleRule(gsl::index index, gsl::index count = 1); - - void Clear() { RemoveStyleRule(0, GetSize()); } - - void Set(const StyleRuleSet& other, bool set_parent = false); - - 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* ChangeEvent() { return &change_event_; } - - private: - void RaiseChangeEvent() { change_event_.Raise(nullptr); } - - private: - Event change_event_; - - std::shared_ptr parent_ = nullptr; - EventRevokerGuard parent_change_event_guard_; - - std::vector rules_; -}; - -class StyleRuleSetBind { - public: - StyleRuleSetBind(controls::Control* control, - std::shared_ptr ruleset); - - CRU_DELETE_COPY(StyleRuleSetBind) - CRU_DELETE_MOVE(StyleRuleSetBind) - - ~StyleRuleSetBind() = default; - - private: - void UpdateRuleSetChainCache(); - void UpdateChangeListener(); - void UpdateStyle(); - - private: - controls::Control* control_; - std::shared_ptr ruleset_; - - // child first, parent last. - std::vector ruleset_chain_cache_; - - EventRevokerListGuard guard_; -}; -} // namespace cru::ui::style diff --git a/include/cru/ui/style/Styler.h b/include/cru/ui/style/Styler.h new file mode 100644 index 00000000..9bdec294 --- /dev/null +++ b/include/cru/ui/style/Styler.h @@ -0,0 +1,90 @@ +#pragma once +#include "../Base.h" +#include "ApplyBorderStyleInfo.h" +#include "cru/common/Base.h" +#include "cru/common/ClonablePtr.h" +#include "cru/platform/gui/Cursor.h" +#include "cru/ui/controls/Control.h" + +#include +#include + +namespace cru::ui::style { +class Styler : public Object { + public: + virtual void Apply(controls::Control* control) const = 0; + + virtual Styler* Clone() const = 0; +}; + +class CompoundStyler : public Styler { + public: + template + static ClonablePtr Create(ClonablePtr... s) { + return ClonablePtr( + new CompoundStyler(std::vector>{std::move(s)...})); + } + + static ClonablePtr Create( + std::vector> stylers) { + return ClonablePtr(new CompoundStyler(std::move(stylers))); + } + + explicit CompoundStyler(std::vector> stylers) + : stylers_(std::move(stylers)) {} + + void Apply(controls::Control* control) const override { + for (const auto& styler : stylers_) { + styler->Apply(control); + } + } + + virtual CompoundStyler* Clone() const override { + return new CompoundStyler(stylers_); + } + + private: + std::vector> stylers_; +}; + +class BorderStyler : public Styler { + public: + static ClonablePtr Create() { + return ClonablePtr(new BorderStyler()); + } + + static ClonablePtr Create(ApplyBorderStyleInfo style) { + return ClonablePtr(new BorderStyler(std::move(style))); + } + + BorderStyler() = default; + explicit BorderStyler(ApplyBorderStyleInfo style); + + void Apply(controls::Control* control) const override; + + BorderStyler* Clone() const override { return new BorderStyler(style_); } + + private: + ApplyBorderStyleInfo style_; +}; + +class CursorStyler : public Styler { + public: + static ClonablePtr Create( + std::shared_ptr cursor) { + return ClonablePtr(new CursorStyler(std::move(cursor))); + } + + static ClonablePtr Create(platform::gui::SystemCursorType type); + + explicit CursorStyler(std::shared_ptr cursor) + : cursor_(std::move(cursor)) {} + + void Apply(controls::Control* control) const override; + + CursorStyler* Clone() const override { return new CursorStyler(cursor_); } + + private: + std::shared_ptr cursor_; +}; +} // namespace cru::ui::style diff --git a/include/cru/ui/style/Styler.hpp b/include/cru/ui/style/Styler.hpp deleted file mode 100644 index 3bf0b027..00000000 --- a/include/cru/ui/style/Styler.hpp +++ /dev/null @@ -1,90 +0,0 @@ -#pragma once -#include "../Base.hpp" -#include "ApplyBorderStyleInfo.hpp" -#include "cru/common/Base.hpp" -#include "cru/common/ClonablePtr.hpp" -#include "cru/platform/gui/Cursor.hpp" -#include "cru/ui/controls/Control.hpp" - -#include -#include - -namespace cru::ui::style { -class Styler : public Object { - public: - virtual void Apply(controls::Control* control) const = 0; - - virtual Styler* Clone() const = 0; -}; - -class CompoundStyler : public Styler { - public: - template - static ClonablePtr Create(ClonablePtr... s) { - return ClonablePtr( - new CompoundStyler(std::vector>{std::move(s)...})); - } - - static ClonablePtr Create( - std::vector> stylers) { - return ClonablePtr(new CompoundStyler(std::move(stylers))); - } - - explicit CompoundStyler(std::vector> stylers) - : stylers_(std::move(stylers)) {} - - void Apply(controls::Control* control) const override { - for (const auto& styler : stylers_) { - styler->Apply(control); - } - } - - virtual CompoundStyler* Clone() const override { - return new CompoundStyler(stylers_); - } - - private: - std::vector> stylers_; -}; - -class BorderStyler : public Styler { - public: - static ClonablePtr Create() { - return ClonablePtr(new BorderStyler()); - } - - static ClonablePtr Create(ApplyBorderStyleInfo style) { - return ClonablePtr(new BorderStyler(std::move(style))); - } - - BorderStyler() = default; - explicit BorderStyler(ApplyBorderStyleInfo style); - - void Apply(controls::Control* control) const override; - - BorderStyler* Clone() const override { return new BorderStyler(style_); } - - private: - ApplyBorderStyleInfo style_; -}; - -class CursorStyler : public Styler { - public: - static ClonablePtr Create( - std::shared_ptr cursor) { - return ClonablePtr(new CursorStyler(std::move(cursor))); - } - - static ClonablePtr Create(platform::gui::SystemCursorType type); - - explicit CursorStyler(std::shared_ptr cursor) - : cursor_(std::move(cursor)) {} - - void Apply(controls::Control* control) const override; - - CursorStyler* Clone() const override { return new CursorStyler(cursor_); } - - private: - std::shared_ptr cursor_; -}; -} // namespace cru::ui::style -- cgit v1.2.3