aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-12-09 17:21:44 +0800
committercrupest <crupest@outlook.com>2020-12-09 17:21:44 +0800
commit9fcebe16b5ad4acc8b2e206dbe163e58b1f75cdf (patch)
treeab179c513081df6c9d5e35ee630b3575664e77c6 /src
parentf355356e4b022ed0fdebff3fac5daa88a1532132 (diff)
downloadcru-9fcebe16b5ad4acc8b2e206dbe163e58b1f75cdf.tar.gz
cru-9fcebe16b5ad4acc8b2e206dbe163e58b1f75cdf.tar.bz2
cru-9fcebe16b5ad4acc8b2e206dbe163e58b1f75cdf.zip
...
Diffstat (limited to 'src')
-rw-r--r--src/ui/controls/Button.cpp4
-rw-r--r--src/ui/controls/Control.cpp4
-rw-r--r--src/ui/controls/TextBox.cpp2
-rw-r--r--src/ui/style/StyleRuleSet.cpp74
4 files changed, 62 insertions, 22 deletions
diff --git a/src/ui/controls/Button.cpp b/src/ui/controls/Button.cpp
index 7858eadb..8bd9f93f 100644
--- a/src/ui/controls/Button.cpp
+++ b/src/ui/controls/Button.cpp
@@ -42,8 +42,8 @@ Button::Button() : click_detector_(this) {
}
});
- GetStyleRuleSet()->Set(
- UiManager::GetInstance()->GetThemeResources()->button_style);
+ GetStyleRuleSet()->SetParent(
+ &UiManager::GetInstance()->GetThemeResources()->button_style);
}
Button::~Button() = default;
diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp
index 1c4ffe51..29c2c46a 100644
--- a/src/ui/controls/Control.cpp
+++ b/src/ui/controls/Control.cpp
@@ -16,7 +16,9 @@ using platform::gui::IUiApplication;
using platform::gui::SystemCursorType;
Control::Control() {
- style_rule_set_ = std::make_unique<style::StyleRuleSet>(this);
+ style_rule_set_ = std::make_unique<style::StyleRuleSet>();
+ style_rule_set_bind_ =
+ std::make_unique<style::StyleRuleSetBind>(this, style_rule_set_.get());
MouseEnterEvent()->Direct()->AddHandler([this](event::MouseEventArgs&) {
this->is_mouse_over_ = true;
diff --git a/src/ui/controls/TextBox.cpp b/src/ui/controls/TextBox.cpp
index 0d14dbcf..d8317a8c 100644
--- a/src/ui/controls/TextBox.cpp
+++ b/src/ui/controls/TextBox.cpp
@@ -36,7 +36,7 @@ TextBox::TextBox()
border_render_object_->SetBorderEnabled(true);
- GetStyleRuleSet()->Set(theme_resources->text_box_style);
+ GetStyleRuleSet()->SetParent(&theme_resources->text_box_style);
}
TextBox::~TextBox() {}
diff --git a/src/ui/style/StyleRuleSet.cpp b/src/ui/style/StyleRuleSet.cpp
index 403fe114..24b88af9 100644
--- a/src/ui/style/StyleRuleSet.cpp
+++ b/src/ui/style/StyleRuleSet.cpp
@@ -1,17 +1,30 @@
#include "cru/ui/style/StyleRuleSet.hpp"
#include "cru/common/Event.hpp"
+#include "cru/ui/controls/Control.hpp"
#include "gsl/gsl_assert"
#include <unordered_set>
namespace cru::ui::style {
+StyleRuleSet::StyleRuleSet(StyleRuleSet* parent) { SetParent(parent); }
+
+void StyleRuleSet::SetParent(StyleRuleSet* parent) {
+ if (parent == parent_) return;
+ parent_change_event_guard_.Reset();
+ parent_ = parent;
+ if (parent != nullptr) {
+ parent_change_event_guard_.Reset(parent->ChangeEvent()->AddSpyOnlyHandler(
+ [this] { this->RaiseChangeEvent(); }));
+ }
+ RaiseChangeEvent();
+}
+
void StyleRuleSet::AddStyleRule(StyleRule rule, gsl::index index) {
Expects(index >= 0 && index <= GetSize());
rules_.insert(rules_.cbegin() + index, std::move(rule));
- UpdateChangeListener();
- UpdateStyle();
+ RaiseChangeEvent();
}
void StyleRuleSet::RemoveStyleRule(gsl::index index, gsl::index count) {
@@ -20,25 +33,48 @@ void StyleRuleSet::RemoveStyleRule(gsl::index index, gsl::index count) {
rules_.erase(rules_.cbegin() + index, rules_.cbegin() + index + count);
- UpdateChangeListener();
- UpdateStyle();
+ RaiseChangeEvent();
}
void StyleRuleSet::Set(const StyleRuleSet& other) {
rules_ = other.rules_;
- UpdateChangeListener();
- UpdateStyle();
+
+ RaiseChangeEvent();
+}
+
+StyleRuleSetBind::StyleRuleSetBind(controls::Control* control,
+ StyleRuleSet* ruleset)
+ : control_(control), ruleset_(ruleset) {
+ Expects(control);
+ Expects(ruleset);
+
+ ruleset->ChangeEvent()->AddSpyOnlyHandler([this] {
+ UpdateRuleSetChainCache();
+ UpdateChangeListener();
+ UpdateStyle();
+ });
}
-void StyleRuleSet::UpdateChangeListener() {
- if (control_ == nullptr) return;
+void StyleRuleSetBind::UpdateRuleSetChainCache() {
+ ruleset_chain_cache_.clear();
+ auto parent = ruleset_;
+ while (parent != nullptr) {
+ ruleset_chain_cache_.push_back(parent);
+ parent = parent->GetParent();
+ }
+}
+void StyleRuleSetBind::UpdateChangeListener() {
guard_.Clear();
std::unordered_set<IBaseEvent*> events;
- for (const auto& rule : rules_) {
- auto e = rule.GetCondition()->ChangeOn(control_);
- events.insert(e.cbegin(), e.cend());
+
+ // ruleset order does not matter
+ for (auto ruleset : ruleset_chain_cache_) {
+ for (const auto& rule : ruleset->GetRules()) {
+ auto e = rule.GetCondition()->ChangeOn(control_);
+ events.insert(e.cbegin(), e.cend());
+ }
}
for (auto e : events) {
@@ -46,13 +82,15 @@ void StyleRuleSet::UpdateChangeListener() {
}
}
-void StyleRuleSet::UpdateStyle() {
- if (control_ == nullptr) return;
-
- for (const auto& rule : rules_) {
- if (rule.GetCondition()->Judge(control_)) {
- rule.GetStyler()->Apply(control_);
- }
+void StyleRuleSetBind::UpdateStyle() {
+ // cache is parent last, but when calculate style, parent first, so iterate
+ // reverse.
+ for (auto iter = ruleset_chain_cache_.crbegin();
+ iter != ruleset_chain_cache_.crend(); ++iter) {
+ for (const auto& rule : (*iter)->GetRules())
+ if (rule.GetCondition()->Judge(control_)) {
+ rule.GetStyler()->Apply(control_);
+ }
}
}
} // namespace cru::ui::style