Skip to content

Commit 892c622

Browse files
committed
theme selection in test app
1 parent 3a1c846 commit 892c622

3 files changed

Lines changed: 137 additions & 64 deletions

File tree

src/ruis/standard_widgets.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ using namespace std::string_view_literals;
2727

2828
using namespace ruis;
2929

30+
std::string_view ruis::to_resource_id(theme th) noexcept
31+
{
32+
switch (th) {
33+
using enum theme;
34+
case dark:
35+
return "ruis_tml_theme_dark"sv;
36+
case light:
37+
return "ruis_tml_theme_light"sv;
38+
}
39+
40+
return "ruis_tml_theme_dark"sv;
41+
}
42+
3043
void ruis::init_standard_widgets(
3144
ruis::context& context, //
3245
const fsif::file& fi,
@@ -74,17 +87,6 @@ void ruis::init_standard_widgets(
7487
throw std::runtime_error("init_standard_widgets(): could not mount default resource pack");
7588
}
7689

77-
auto theme_resource_id = [&]() {
78-
switch (th) {
79-
using enum theme;
80-
default:
81-
case dark:
82-
return "ruis_tml_theme_dark"sv;
83-
case light:
84-
return "ruis_tml_theme_light"sv;
85-
}
86-
}();
87-
88-
auto style_res = context.loader().load<ruis::res::tml>(theme_resource_id);
90+
auto style_res = context.loader().load<ruis::res::tml>(to_resource_id(th));
8991
context.style().set(utki::make_shared<style_sheet>(style_res.get().forest));
9092
}

src/ruis/standard_widgets.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2121

2222
#pragma once
2323

24+
#include <string_view>
25+
2426
#include "context.hpp"
2527

2628
namespace ruis {
@@ -30,6 +32,8 @@ enum class theme {
3032
light
3133
};
3234

35+
std::string_view to_resource_id(theme th) noexcept;
36+
3337
void init_standard_widgets(
3438
ruis::context& context, //
3539
const fsif::file& fi,

tests/touch/src/scroll_area_page.cpp

Lines changed: 119 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,88 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2121

2222
#include "scroll_area_page.hpp"
2323

24+
#include <ruis/res/tml.hpp>
25+
#include <ruis/standard_widgets.hpp>
26+
#include <ruis/style/style_sheet.hpp>
2427
#include <ruis/widget/button/impl/rectangle_push_button.hpp>
28+
#include <ruis/widget/button/selection_box.hpp>
2529
#include <ruis/widget/group/touch/scroll_area.hpp>
2630
#include <ruis/widget/label/text.hpp>
2731
#include <ruis/widget/slider/scroll_bar.hpp>
2832

2933
#include "style.hpp"
3034

3135
using namespace std::string_literals;
36+
using namespace std::string_view_literals;
3237

3338
using namespace ruis::length_literals;
3439

3540
namespace {
3641

42+
void apply_theme(
43+
ruis::context& c, //
44+
ruis::theme theme
45+
)
46+
{
47+
auto style_res = c.loader().load<ruis::res::tml>(ruis::to_resource_id(theme));
48+
c.style().set(utki::make_shared<ruis::style_sheet>(style_res.get().forest));
49+
}
50+
51+
class theme_selection_provider : public ruis::list_provider
52+
{
53+
std::vector<std::u32string> items;
54+
55+
public:
56+
theme_selection_provider(utki::shared_ref<ruis::context> context) :
57+
ruis::list_provider(std::move(context)),
58+
items{U"Dark"s, U"Light"s}
59+
{}
60+
61+
size_t count() const noexcept override
62+
{
63+
return this->items.size();
64+
}
65+
66+
utki::shared_ref<ruis::widget> get_widget(size_t index) override
67+
{
68+
return m::text(this->context, {}, this->items.at(index));
69+
}
70+
};
71+
3772
ruis::widget_list make_scroll_area_page_contents(utki::shared_ref<ruis::context> c)
3873
{
3974
// clang-format off
40-
auto button_1 = m::push_button(c,
41-
{
42-
.layout_params{
43-
.dims = {ruis::dim::fill, 200_pp}
44-
}
45-
},
46-
{
47-
m::text(c, {}, U"Button 1"s)
48-
}
49-
);
75+
auto theme_selector = m::selection_box(c,
76+
{
77+
.layout_params{
78+
.dims = {ruis::dim::max, ruis::dim::min}
79+
},
80+
.list_params{
81+
.provider = utki::make_shared<theme_selection_provider>(c)
82+
}
83+
}
84+
);
85+
// clang-format on
86+
87+
theme_selector.get().set_selection(size_t(ruis::theme::dark));
88+
theme_selector.get().selection_handler = [](ruis::selection_box& sb) {
89+
apply_theme(
90+
sb.context.get(), //
91+
ruis::theme(sb.get_selection())
92+
);
93+
};
94+
95+
// clang-format off
96+
auto button_1 = m::push_button(c,
97+
{
98+
.layout_params{
99+
.dims = {ruis::dim::fill, 200_pp}
100+
}
101+
},
102+
{
103+
m::text(c, {}, U"Button 1"s)
104+
}
105+
);
50106
// clang-format on
51107

52108
button_1.get().click_handler = [](ruis::push_button&) {
@@ -56,26 +112,37 @@ ruis::widget_list make_scroll_area_page_contents(utki::shared_ref<ruis::context>
56112
};
57113

58114
// clang-format off
59-
return {
60-
button_1,
61-
m::scroll_bar(c,
62-
{
63-
.layout_params{
64-
.dims{50_pp, 1000_pp}
65-
},
66-
.fraction_params{
67-
.fraction = ruis::real(0.25)
68-
},
69-
.fraction_band_params{
70-
.band_fraction = ruis::real(0.2)
71-
},
72-
.oriented_params{
73-
.vertical = true
74-
}
75-
}
76-
),
77-
m::text(c, {}, U"some text"s)
78-
};
115+
return {
116+
m::row(c,
117+
{
118+
.layout_params{
119+
.dims = {ruis::dim::fill, ruis::dim::min}
120+
}
121+
},
122+
{
123+
m::text(c, {}, U"Theme:"s),
124+
theme_selector
125+
}
126+
),
127+
button_1,
128+
m::scroll_bar(c,
129+
{
130+
.layout_params{
131+
.dims{50_pp, 1000_pp}
132+
},
133+
.fraction_params{
134+
.fraction = ruis::real(0.25)
135+
},
136+
.fraction_band_params{
137+
.band_fraction = ruis::real(0.2)
138+
},
139+
.oriented_params{
140+
.vertical = true
141+
}
142+
}
143+
),
144+
m::text(c, {}, U"some text"s)
145+
};
79146
// clang-format on
80147
}
81148

@@ -86,31 +153,31 @@ class scroll_area_page :
86153
public:
87154
scroll_area_page(utki::shared_ref<ruis::context> c) :
88155
// clang-format off
89-
ruis::widget(
90-
std::move(c),
91-
{},
92-
{
93-
.clip = true
94-
}
95-
),
156+
ruis::widget(
157+
std::move(c),
158+
{},
159+
{
160+
.clip = true
161+
}
162+
),
96163
// clang-format on
97164
ruis::page(this->context, {}),
98165
// clang-format off
99-
ruis::touch::scroll_area(
100-
this->context,
101-
{},
102-
{
103-
m::column(
104-
this->context,
105-
{
106-
.layout_params{
107-
.dims = {ruis::dim::fill, ruis::dim::min}
108-
}
109-
},
110-
make_scroll_area_page_contents(this->context)
111-
)
112-
}
113-
)
166+
ruis::touch::scroll_area(
167+
this->context,
168+
{},
169+
{
170+
m::column(
171+
this->context,
172+
{
173+
.layout_params{
174+
.dims = {ruis::dim::fill, ruis::dim::min}
175+
}
176+
},
177+
make_scroll_area_page_contents(this->context)
178+
)
179+
}
180+
)
114181
// clang-format on
115182
{}
116183
};

0 commit comments

Comments
 (0)