Skip to content

Commit 6631c8e

Browse files
committed
style refactoring
1 parent d804056 commit 6631c8e

4 files changed

Lines changed: 159 additions & 33 deletions

File tree

src/ruis/style/style_provider.cpp

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ void style_provider::set(utki::shared_ref<style_sheet> ss)
3737

3838
std::vector<std::string_view> keys_to_remove;
3939

40+
// TODO: reload standard cache
41+
4042
// reload cache
41-
for (auto& pair : this->cache) {
43+
for (auto& pair : this->user_cache) {
4244
auto sv = pair.second.lock();
4345
if (!sv) {
4446
// the weak reference has expired
@@ -65,34 +67,55 @@ void style_provider::set(utki::shared_ref<style_sheet> ss)
6567

6668
// remove unused cache entries
6769
for (const auto& k : keys_to_remove) {
68-
auto i = this->cache.find(k);
69-
this->cache.erase(i);
70+
auto i = this->user_cache.find(k);
71+
this->user_cache.erase(i);
72+
}
73+
}
74+
75+
std::shared_ptr<const style_provider::style_value_base> style_provider::get_from_cache(style id) const
76+
{
77+
auto& w = this->standard_cache[id];
78+
auto p = w.lock();
79+
if (!p) {
80+
return nullptr;
7081
}
82+
83+
return p;
7184
}
7285

7386
std::shared_ptr<const style_provider::style_value_base> style_provider::get_from_cache(std::string_view id) const
7487
{
75-
auto i = this->cache.find(id);
76-
if (i == this->cache.end()) {
88+
auto i = this->user_cache.find(id);
89+
if (i == this->user_cache.end()) {
7790
return nullptr;
7891
}
7992

8093
auto p = i->second.lock();
8194
if (!p) {
82-
this->cache.erase(i);
95+
this->user_cache.erase(i);
8396
}
8497

8598
return p;
8699
}
87100

101+
void style_provider::store_to_cache(
102+
style id, //
103+
std::weak_ptr<style_value_base> v
104+
) const
105+
{
106+
utki::assert(this->standard_cache[id].expired());
107+
108+
this->standard_cache[id] = std::move(v);
109+
}
110+
88111
void style_provider::store_to_cache(
89112
std::string_view id, //
90113
std::weak_ptr<style_value_base> v
91114
) const
92115
{
93-
utki::assert(!utki::contains(this->cache, id));
116+
utki::assert(!utki::contains(this->user_cache, id));
94117

95-
[[maybe_unused]] auto res = this->cache.insert(std::make_pair(
118+
[[maybe_unused]] auto res = this->user_cache.insert(std::make_pair(
96119
std::string(id), //
97120
std::move(v)
98121
));
@@ -103,45 +126,45 @@ void style_provider::store_to_cache(
103126

104127
styled<color> style_provider::get_color_background() const
105128
{
106-
return this->get<color>("color_background"sv);
129+
return this->get<color>(style::color_background);
107130
}
108131

109132
styled<color> style_provider::get_color_middleground() const
110133
{
111-
return this->get<color>("color_middleground"sv);
134+
return this->get<color>(style::color_middleground);
112135
}
113136

114137
styled<color> style_provider::get_color_foreground() const
115138
{
116-
return this->get<color>("color_foreground"sv);
139+
return this->get<color>(style::color_foreground);
117140
}
118141

119142
styled<color> style_provider::get_color_text_normal() const
120143
{
121-
return this->get<color>("color_text_normal"sv);
144+
return this->get<color>(style::color_text_normal);
122145
}
123146

124147
styled<color> style_provider::get_color_highlight() const
125148
{
126-
return this->get<color>("color_highlight"sv);
149+
return this->get<color>(style::color_highlight);
127150
}
128151

129152
styled<color> style_provider::get_color_cursor() const
130153
{
131-
return this->get<color>("color_cursor"sv);
154+
return this->get<color>(style::color_cursor);
132155
}
133156

134157
styled<layout::dimension> style_provider::get_dim_indent_tree_view_item() const
135158
{
136-
return this->get<layout::dimension>("dim_tree_view_item_indent"sv);
159+
return this->get<layout::dimension>(style::dim_tree_view_item_indent);
137160
}
138161

139162
styled<length> style_provider::get_font_size_normal() const
140163
{
141-
return this->get<length>("font_size_normal"sv);
164+
return this->get<length>(style::font_size_normal);
142165
}
143166

144167
styled<res::font> style_provider::get_font_face_normal() const
145168
{
146-
return this->get<res::font>("font_face_normal"sv);
169+
return this->get<res::font>(style::font_face_normal);
147170
}

src/ruis/style/style_provider.hpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,17 @@ class style_provider
6969
virtual ~style_value_base() = default;
7070
};
7171

72-
mutable std::map<std::string, std::weak_ptr<style_value_base>, std::less<>> cache;
72+
mutable utki::enum_array<std::weak_ptr<style_value_base>, style> standard_cache;
73+
mutable std::map<std::string, std::weak_ptr<style_value_base>, std::less<>> user_cache;
7374

75+
std::shared_ptr<const style_value_base> get_from_cache(style id) const;
7476
std::shared_ptr<const style_value_base> get_from_cache(std::string_view id) const;
7577

78+
void store_to_cache(
79+
style id, //
80+
std::weak_ptr<style_value_base> v
81+
) const;
82+
7683
void store_to_cache(
7784
std::string_view id, //
7885
std::weak_ptr<style_value_base> v
@@ -84,6 +91,9 @@ class style_provider
8491
private:
8592
utki::shared_ref<ruis::style_sheet> cur_style_sheet;
8693

94+
template <typename value_type>
95+
styled<value_type> get(style id) const;
96+
8797
public:
8898
style_provider(utki::shared_ref<ruis::resource_loader> loader);
8999

@@ -114,6 +124,34 @@ class style_provider
114124

115125
namespace ruis {
116126

127+
template <typename value_type>
128+
styled<value_type> style_provider::get(style id) const
129+
{
130+
if (auto svb = this->get_from_cache(id)) {
131+
if (auto sv = std::dynamic_pointer_cast<const typename styled<value_type>::style_value>(svb)) {
132+
return {utki::shared_ref<const typename styled<value_type>::style_value>(std::move(sv))};
133+
}
134+
throw std::invalid_argument("style::get(id): requested value_type does not match the one stored in cache");
135+
}
136+
const auto& desc = this->cur_style_sheet.get().get(id);
137+
138+
if (desc.empty()) {
139+
throw std::invalid_argument(
140+
utki::cat("style_provider::get(style): no style value with id ", unsigned(id), " in the style sheet")
141+
);
142+
}
143+
144+
auto ret = utki::make_shared<typename styled<value_type>::style_value>(
145+
desc, //
146+
this->res_loader.get()
147+
);
148+
this->store_to_cache(
149+
id, //
150+
ret.to_shared_ptr()
151+
);
152+
return {ret};
153+
}
154+
117155
template <typename value_type>
118156
styled<value_type> style_provider::get(std::string_view id) const
119157
{

src/ruis/style/style_sheet.cpp

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,39 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2424
using namespace std::string_view_literals;
2525
using namespace ruis;
2626

27-
style_sheet::style_sheet(tml::forest desc) :
28-
id_to_description_map(parse(std::move(desc)))
29-
{}
27+
style style_sheet::name_to_style(std::string_view name)
28+
{
29+
if (name == "color_background"sv) {
30+
return style::color_background;
31+
} else if (name == "color_middleground"sv) {
32+
return style::color_middleground;
33+
} else if (name == "color_foreground"sv) {
34+
return style::color_foreground;
35+
} else if (name == "color_text_normal"sv) {
36+
return style::color_text_normal;
37+
} else if (name == "color_text_selection_bg"sv) {
38+
return style::color_text_selection_bg;
39+
} else if (name == "color_highlight"sv) {
40+
return style::color_highlight;
41+
} else if (name == "color_cursor"sv) {
42+
return style::color_cursor;
43+
} else if (name == "dim_tree_view_item_indent"sv) {
44+
return style::dim_tree_view_item_indent;
45+
} else if (name == "font_size_normal"sv) {
46+
return style::font_size_normal;
47+
} else if (name == "font_face_normal"sv) {
48+
return style::font_face_normal;
49+
}
50+
51+
throw std::invalid_argument(utki::cat("style_sheet::name_to_style(name): unknown style name: ", name));
52+
}
3053

31-
std::map<std::string, tml::forest, std::less<>> style_sheet::parse(tml::forest desc)
54+
style_sheet::style_sheet(tml::forest desc)
55+
{
56+
this->parse(std::move(desc));
57+
}
58+
59+
void style_sheet::parse(tml::forest desc)
3260
{
3361
if (desc.empty()) {
3462
throw std::invalid_argument("style_sheet::parse(desc): empty style sheet description supplied");
@@ -52,21 +80,20 @@ std::map<std::string, tml::forest, std::less<>> style_sheet::parse(tml::forest d
5280
));
5381
}
5482

55-
std::map<std::string, tml::forest, std::less<>> ret;
56-
5783
for (auto& d : utki::skip_front<1>(desc)) {
58-
// TODO: why "ruis" and "user" are stored into same place?
59-
if (d.value.string == "ruis"sv || d.value.string == "user"sv) {
84+
if (d.value.string == "ruis"sv) {
6085
for (auto& s : d.children) {
61-
ret.insert_or_assign(
86+
this->standard_styles[style_sheet::name_to_style(s.value.string)] = std::move(s.children);
87+
}
88+
} else if (d.value.string == "user"sv) {
89+
for (auto& s : d.children) {
90+
this->user_styles.insert_or_assign(
6291
std::move(s.value.string), //
6392
std::move(s.children)
6493
);
6594
}
6695
}
6796
}
68-
69-
return ret;
7097
}
7198

7299
style_sheet style_sheet::load(const fsif::file& fi)
@@ -76,8 +103,8 @@ style_sheet style_sheet::load(const fsif::file& fi)
76103

77104
const tml::forest* style_sheet::get(std::string_view style_id) const noexcept
78105
{
79-
auto i = this->id_to_description_map.find(style_id);
80-
if (i == this->id_to_description_map.end()) {
106+
auto i = this->user_styles.find(style_id);
107+
if (i == this->user_styles.end()) {
81108
return nullptr;
82109
}
83110

src/ruis/style/style_sheet.hpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,34 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
#pragma once
2323

2424
#include <tml/tree.hpp>
25+
#include <utki/enum_array.hpp>
2526

2627
namespace ruis {
2728

29+
/**
30+
* @brief Style value id.
31+
* Ruis standard style values.
32+
*/
33+
enum class style {
34+
color_background,
35+
color_middleground,
36+
color_foreground,
37+
38+
color_text_normal,
39+
color_text_selection_bg,
40+
41+
color_highlight,
42+
43+
color_cursor,
44+
45+
dim_tree_view_item_indent,
46+
47+
font_size_normal,
48+
font_face_normal,
49+
50+
enum_size
51+
};
52+
2853
/**
2954
* @brief Style sheet.
3055
* TODO: write more description
@@ -45,9 +70,12 @@ namespace ruis {
4570
*/
4671
class style_sheet
4772
{
48-
std::map<std::string, tml::forest, std::less<>> id_to_description_map;
73+
utki::enum_array<tml::forest, style> standard_styles;
74+
std::map<std::string, tml::forest, std::less<>> user_styles;
75+
76+
void parse(tml::forest desc);
4977

50-
static std::map<std::string, tml::forest, std::less<>> parse(tml::forest desc);
78+
static style name_to_style(std::string_view name);
5179

5280
public:
5381
style_sheet() = default;
@@ -62,6 +90,16 @@ class style_sheet
6290
*/
6391
const tml::forest* get(std::string_view style_id) const noexcept;
6492

93+
/**
94+
* @brief Get standard style value description.
95+
* @param style_id - id of the standard style value.
96+
* @return reference to the standard style value description.
97+
*/
98+
const tml::forest& get(style style_id) const noexcept
99+
{
100+
return this->standard_styles[style_id];
101+
}
102+
65103
static style_sheet load(const fsif::file& fi);
66104
};
67105

0 commit comments

Comments
 (0)