Skip to content

Commit 7a38ff1

Browse files
committed
refactoring
1 parent af2d199 commit 7a38ff1

20 files changed

Lines changed: 154 additions & 57 deletions

File tree

res/ruis_res/themes/dark/main.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
ruis_tml_style_dark{
1+
ruis_tml_theme_dark{
22
file{style.tml}
33
}

res/ruis_res/themes/light/main.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
ruis_tml_style_light{
1+
ruis_tml_theme_light{
22
file{style.tml}
33
}

src/ruis/gui.cpp

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2525
#include "widget/input/character_input_widget.hpp"
2626
#include "widget/label/gap.hpp"
2727

28+
// TODO: remove when guit::init_standard_widgets() is removed
29+
#include "standard_widgets.hpp"
30+
2831
using namespace std::string_view_literals;
2932

3033
using namespace ruis;
@@ -37,47 +40,7 @@ gui::gui(utki::shared_ref<ruis::context> context) :
3740

3841
void gui::init_standard_widgets(const fsif::file& fi)
3942
{
40-
// mount default resource pack
41-
42-
std::vector<std::string> paths;
43-
44-
if (!fi.path().empty()) {
45-
paths.push_back(fi.path());
46-
}
47-
48-
paths.emplace_back("ruis_res/");
49-
50-
#if (M_OS == M_OS_LINUX && M_OS_NAME != M_OS_NAME_ANDROID) || (M_OS == M_OS_MACOSX && M_OS_NAME != M_OS_NAME_IOS) || \
51-
(M_OS == M_OS_UNIX)
52-
53-
unsigned soname =
54-
# include "../soname.txt"
55-
;
56-
57-
paths.push_back(utki::cat("/usr/local/share/ruis/res"sv, soname));
58-
paths.push_back(utki::cat("/usr/share/ruis/res"sv, soname));
59-
#endif
60-
61-
bool mounted = false;
62-
for (const auto& s : paths) {
63-
try {
64-
fi.set_path(s);
65-
this->context.get().loader().mount_res_pack(fi);
66-
} catch (std::runtime_error&) {
67-
continue;
68-
}
69-
70-
mounted = true;
71-
break;
72-
}
73-
74-
if (!mounted) {
75-
throw std::runtime_error("gui::init_standard_widgets(): could not mount default resource pack");
76-
}
77-
78-
// TODO: pass which style to load via parameter
79-
auto style_res = this->context.get().loader().load<ruis::res::tml>("ruis_tml_style_dark"sv);
80-
this->context.get().style().set(utki::make_shared<style_sheet>(style_res.get().forest));
43+
ruis::init_standard_widgets(this->context, fi);
8144
}
8245

8346
void gui::set_viewport(const ruis::rect& rect)

src/ruis/gui.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class gui final
108108
* widgets to be used by application.
109109
* @param fi - file interface to use for resource loading.
110110
*/
111-
// TODO: make free function taking context as argument
111+
[[deprecated("use free floating ruis::init_standard_widgets()")]]
112112
void init_standard_widgets(const fsif::file& fi);
113113

114114
/**

src/ruis/standard_widgets.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "standard_widgets.hpp"
2+
3+
#include "res/tml.hpp"
4+
5+
using namespace std::string_view_literals;
6+
7+
using namespace ruis;
8+
9+
void ruis::init_standard_widgets(
10+
ruis::context& context, //
11+
const fsif::file& fi,
12+
theme th
13+
)
14+
{
15+
// mount default resource pack
16+
17+
std::vector<std::string> paths;
18+
19+
if (!fi.path().empty()) {
20+
paths.push_back(fi.path());
21+
}
22+
23+
paths.emplace_back("ruis_res/");
24+
25+
#if (M_OS == M_OS_LINUX && M_OS_NAME != M_OS_NAME_ANDROID) || (M_OS == M_OS_MACOSX && M_OS_NAME != M_OS_NAME_IOS) || \
26+
(M_OS == M_OS_UNIX)
27+
28+
unsigned soname =
29+
# include "../soname.txt"
30+
;
31+
32+
paths.push_back(utki::cat("/usr/local/share/ruis/res"sv, soname));
33+
paths.push_back(utki::cat("/usr/share/ruis/res"sv, soname));
34+
#endif
35+
36+
bool mounted = false;
37+
for (const auto& s : paths) {
38+
try {
39+
fi.set_path(s);
40+
context.loader().mount_res_pack(fi);
41+
} catch (std::runtime_error&) {
42+
continue;
43+
}
44+
45+
mounted = true;
46+
break;
47+
}
48+
49+
if (!mounted) {
50+
throw std::runtime_error("init_standard_widgets(): could not mount default resource pack");
51+
}
52+
53+
auto theme_resource_id = [&]() {
54+
switch (th) {
55+
using enum theme;
56+
default:
57+
case dark:
58+
return "ruis_tml_theme_dark"sv;
59+
case light:
60+
return "ruis_tml_theme_light"sv;
61+
}
62+
}();
63+
64+
auto style_res = context.loader().load<ruis::res::tml>(theme_resource_id);
65+
context.style().set(utki::make_shared<style_sheet>(style_res.get().forest));
66+
}

src/ruis/standard_widgets.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "context.hpp"
4+
5+
namespace ruis {
6+
7+
enum class theme {
8+
dark,
9+
light
10+
};
11+
12+
void init_standard_widgets(
13+
ruis::context& context, //
14+
const fsif::file& fi,
15+
theme th = theme::dark
16+
);
17+
18+
} // namespace ruis

tests/align/src/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <ruis/layout/abstract_layout.hpp>
77
#include <ruis/widget/label/text.hpp>
88
#include <ruis/widget/label/rectangle.hpp>
9+
#include <ruis/standard_widgets.hpp>
910

1011
using namespace std::string_literals;
1112

@@ -326,7 +327,10 @@ class application : public ruisapp::application{
326327
}
327328
))
328329
{
329-
this->window.gui.init_standard_widgets(this->get_res_file("../../res/ruis_res/").get());
330+
ruis::init_standard_widgets(
331+
this->window.gui.context, //
332+
this->get_res_file("../../res/ruis_res/").get()
333+
);
330334

331335
this->window.gui.set_root(make_layout(this->window.gui.context));
332336

tests/app/src/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <ruis/widget/input/text_input_line.hpp>
2828
#include <ruis/widget/button/selection_box.hpp>
2929
#include <ruis/layout/linear_layout.hpp>
30+
#include <ruis/standard_widgets.hpp>
3031

3132
#include "root_widget.hpp"
3233
#include "cube_widget.hpp"
@@ -73,7 +74,10 @@ class application : public ruisapp::application{
7374

7475
auto& gui = this->window.gui;
7576

76-
gui.init_standard_widgets(this->get_res_file("../../res/ruis_res/").get());
77+
ruis::init_standard_widgets(
78+
gui.context, //
79+
this->get_res_file("../../res/ruis_res/").get()
80+
);
7781

7882
gui.context.get().loader().mount_res_pack(this->get_res_file("res/").get());
7983
// this->ResMan().MountResPack(ruis::ZipFile::New(fsif::FSFile::New("res.zip")));

tests/app2/src/application.cpp

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

2222
#include "application.hpp"
2323

24+
#include <ruis/standard_widgets.hpp>
2425
#include <ruis/widget/proxy/key_proxy.hpp>
2526
#include <ruisapp/application.hpp>
2627

@@ -32,7 +33,10 @@ application::application() :
3233
}),
3334
window(this->make_window({.dims = {1024, 800}}))
3435
{
35-
this->window.gui.init_standard_widgets(this->get_res_file("../../res/ruis_res/").get());
36+
ruis::init_standard_widgets(
37+
this->window.gui.context, //
38+
this->get_res_file("../../res/ruis_res/").get()
39+
);
3640

3741
this->window.gui.context.get().localization.get() =
3842
ruis::localization(tml::read(this->get_res_file("res/localization/en.tml").get()));

tests/aspect_ratio_proxy/src/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <ruis/widget/proxy/aspect_ratio_proxy.hpp>
77
#include <ruis/widget/label/text.hpp>
88
#include <ruis/widget/label/rectangle.hpp>
9+
#include <ruis/standard_widgets.hpp>
910

1011
using namespace std::string_literals;
1112

@@ -28,7 +29,10 @@ class application : public ruisapp::application{
2829
this->quit();
2930
};
3031

31-
this->window.gui.init_standard_widgets(this->get_res_file("../../res/ruis_res/").get());
32+
ruis::init_standard_widgets(
33+
this->window.gui.context, //
34+
this->get_res_file("../../res/ruis_res/").get()
35+
);
3236

3337
auto ctx = this->window.gui.context;
3438

0 commit comments

Comments
 (0)