blob: fae9ab5f8ce7ff15b569bbda65a5211773dd4989 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
#pragma once
#include "StyleRule.h"
#include "cru/base/Base.h"
#include "cru/base/Event.h"
#include "cru/ui/model/IListChangeNotify.h"
#include <cstddef>
namespace cru::ui::style {
/**
* \brief A set of style rules.
* \remarks Note this class is different from other constructs in style. It is
* mutable and has reference semantics. Change of it will be notified by
* StyleRuleSet::ChangeEvent.
*/
class CRU_UI_API StyleRuleSet : public Object, public model::IListChangeNotify {
public:
StyleRuleSet() = default;
explicit StyleRuleSet(std::shared_ptr<StyleRuleSet> parent);
CRU_DELETE_COPY(StyleRuleSet)
CRU_DELETE_MOVE(StyleRuleSet)
~StyleRuleSet() override = default;
public:
std::shared_ptr<StyleRuleSet> GetParent() const { return parent_; }
void SetParent(std::shared_ptr<StyleRuleSet> parent);
Index GetSize() const { return static_cast<Index>(rules_.size()); }
const std::vector<StyleRule>& GetRules() const { return rules_; }
void AddStyleRule(StyleRule rule) {
AddStyleRule(std::move(rule), GetSize());
}
void AddStyleRule(StyleRule rule, Index index);
void RemoveStyleRule(Index index, Index count = 1);
const StyleRule& GetStyleRule(Index index) const { return rules_[index]; }
void SetStyleRule(Index index, StyleRule rule);
void Clear() { RemoveStyleRule(0, GetSize()); }
void Set(const StyleRuleSet& other, bool set_parent = false);
const StyleRule& operator[](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_; }
IEvent<model::ListChange>* ListChangeEvent() override {
return &list_change_event_;
}
private:
void RaiseChangeEvent() { change_event_.Raise(nullptr); }
void RaiseChangeEvent(model::ListChange list_change) {
list_change_event_.Raise(list_change);
change_event_.Raise(nullptr);
}
private:
Event<std::nullptr_t> change_event_;
Event<model::ListChange> list_change_event_;
std::shared_ptr<StyleRuleSet> parent_ = nullptr;
EventRevokerGuard parent_change_event_guard_;
std::vector<StyleRule> rules_;
};
class CRU_UI_API StyleRuleSetBind {
public:
StyleRuleSetBind(controls::Control* control,
std::shared_ptr<StyleRuleSet> 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<StyleRuleSet> ruleset_;
// child first, parent last.
std::vector<StyleRuleSet*> ruleset_chain_cache_;
EventRevokerListGuard guard_;
};
} // namespace cru::ui::style
|