Skip to content

Commit e1fee60

Browse files
committed
modernize tabbed_book test app
1 parent 1f57d26 commit e1fee60

File tree

9 files changed

+283
-50
lines changed

9 files changed

+283
-50
lines changed

src/ruis/widget/button/choice_group.cpp

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

2626
using namespace ruis;
2727

28+
choice_group::choice_group(
29+
utki::shared_ref<ruis::context> context, //
30+
all_parameters params
31+
) :
32+
widget(std::move(context), std::move(params.layout_params), std::move(params.widget_params)),
33+
// clang-format off
34+
container(this->context,
35+
{
36+
.container_params{
37+
.layout = layout::pile
38+
}
39+
},
40+
{}
41+
)
42+
// clang-format on
43+
{}
44+
2845
choice_group::choice_group(const utki::shared_ref<ruis::context>& c, const tml::forest& desc) :
2946
widget(c, desc),
3047
container(this->context, desc, layout::pile)

src/ruis/widget/button/choice_group.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ class choice_group : public container
4343
public:
4444
choice_group(const utki::shared_ref<ruis::context>& c, const tml::forest& desc);
4545

46+
struct all_parameters {
47+
layout_parameters layout_params;
48+
widget::parameters widget_params;
49+
container::parameters container_params;
50+
};
51+
52+
choice_group(
53+
utki::shared_ref<ruis::context> context, //
54+
all_parameters params
55+
);
56+
4657
choice_group(const choice_group&) = delete;
4758
choice_group& operator=(const choice_group&) = delete;
4859

@@ -62,4 +73,17 @@ class choice_group : public container
6273
void set_active_choice_button(std::weak_ptr<choice_button> rb);
6374
};
6475

76+
namespace make {
77+
inline utki::shared_ref<ruis::choice_group> choice_group(
78+
utki::shared_ref<ruis::context> context, //
79+
ruis::choice_group::all_parameters params
80+
)
81+
{
82+
return utki::make_shared<ruis::choice_group>(
83+
std::move(context), //
84+
std::move(params)
85+
);
86+
}
87+
} // namespace make
88+
6589
} // namespace ruis

src/ruis/widget/button/tab.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,19 @@ class tab :
7070
void on_pressed_change() override;
7171
};
7272

73+
namespace make {
74+
inline utki::shared_ref<ruis::tab> tab(
75+
utki::shared_ref<ruis::context> context, //
76+
ruis::tab::all_parameters params,
77+
utki::span<const utki::shared_ref<ruis::widget>> children
78+
)
79+
{
80+
return utki::make_shared<ruis::tab>(
81+
std::move(context), //
82+
std::move(params),
83+
children
84+
);
85+
}
86+
87+
} // namespace make
7388
} // namespace ruis

src/ruis/widget/button/tab_group.cpp

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

2828
using namespace ruis;
2929

30+
tab_group::tab_group(
31+
utki::shared_ref<ruis::context> context, //
32+
choice_group::all_parameters params
33+
) :
34+
widget(std::move(context), std::move(params.layout_params), std::move(params.widget_params)),
35+
choice_group(this->context, std::move(params))
36+
{
37+
// TODO: allow setting filler from params
38+
if (!this->filler) {
39+
this->set_filler(this->context.get().loader.load<res::image>("ruis_img_tabs_filler").to_shared_ptr());
40+
}
41+
}
42+
3043
tab_group::tab_group(const utki::shared_ref<ruis::context>& c, const tml::forest& desc) :
3144
widget(c, desc),
3245
choice_group(this->context, desc)

src/ruis/widget/button/tab_group.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ class tab_group :
3131
virtual public widget, //
3232
public choice_group
3333
{
34-
std::shared_ptr<res::image> filler;
34+
std::shared_ptr<res::image> filler; // TODO: ref to const?
3535
std::shared_ptr<const res::image::texture> filler_texture;
3636

3737
public:
3838
tab_group(const utki::shared_ref<ruis::context>& c, const tml::forest& desc);
3939

40+
tab_group(
41+
utki::shared_ref<ruis::context> context, //
42+
choice_group::all_parameters params // TODO: make own all_parameters struct
43+
);
44+
4045
tab_group(const tab_group&) = delete;
4146
tab_group& operator=(const tab_group&) = delete;
4247

@@ -54,4 +59,17 @@ class tab_group :
5459
void render(const ruis::matrix4& matrix) const override;
5560
};
5661

62+
namespace make {
63+
inline utki::shared_ref<ruis::tab_group> tab_group(
64+
utki::shared_ref<ruis::context> context,
65+
choice_group::all_parameters params
66+
)
67+
{
68+
return utki::make_shared<ruis::tab_group>(
69+
std::move(context), //
70+
std::move(params)
71+
);
72+
}
73+
} // namespace make
74+
5775
} // namespace ruis

src/ruis/widget/group/tabbed_book.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,82 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
2222
#include "tabbed_book.hpp"
2323

2424
#include "../button/tab.hpp"
25+
#include "../button/tab_group.hpp"
26+
27+
using namespace std::string_literals;
2528

2629
using namespace ruis;
2730

31+
namespace m {
32+
using namespace ruis::make;
33+
} // namespace m
34+
35+
tabbed_book::tabbed_book(
36+
utki::shared_ref<ruis::context> context, //
37+
all_parameters params
38+
) :
39+
widget(
40+
std::move(context), //
41+
std::move(params.layout_params),
42+
std::move(params.widget_params)
43+
),
44+
// clang-format off
45+
container(this->context,
46+
{
47+
.container_params{
48+
.layout = ruis::layout::column
49+
}
50+
},
51+
{
52+
m::tab_group(this->context,
53+
{
54+
.layout_params{
55+
.dims{ruis::dim::fill, ruis::dim::min}
56+
},
57+
.widget_params{
58+
.id = "ruis_tab_group"s
59+
}
60+
}
61+
),
62+
m::book(this->context,
63+
{
64+
.layout_params{
65+
.dims{ruis::dim::fill, ruis::dim::max},
66+
.weight = 1
67+
},
68+
.widget_params{
69+
.id = "ruis_book"s
70+
}
71+
}
72+
)
73+
}
74+
),
75+
// clang-format on
76+
tab_group(this->get_widget_as<ruis::tab_group>("ruis_tab_group")),
77+
book(this->get_widget_as<ruis::book>("ruis_book"))
78+
{
79+
// on page tear out, remove corresponding tab
80+
this->book.pages_change_handler = [this](ruis::book& b, const page& p) {
81+
auto i = this->find_pair(p);
82+
if (i != this->tab_page_pairs.end()) {
83+
ASSERT(i->tab)
84+
this->activate_another_tab(*i->tab);
85+
i->tab->remove_from_parent();
86+
this->tab_page_pairs.erase(i);
87+
}
88+
};
89+
90+
// on page programmatic activate we need to activate the corresponding tab as well
91+
this->book.active_page_change_handler = [this](ruis::book& b) {
92+
ASSERT(b.get_active_page())
93+
auto i = this->find_pair(*b.get_active_page());
94+
if (i != this->tab_page_pairs.end()) {
95+
ASSERT(i->tab)
96+
i->tab->activate();
97+
}
98+
};
99+
}
100+
28101
tabbed_book::tabbed_book(const utki::shared_ref<ruis::context>& context, const tml::forest& desc) :
29102
ruis::widget(context, desc),
30103
ruis::container(this->context, tml::read(R"(

src/ruis/widget/group/tabbed_book.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ class tabbed_book :
5353
public:
5454
tabbed_book(const utki::shared_ref<ruis::context>& context, const tml::forest& desc);
5555

56+
struct all_parameters {
57+
layout_parameters layout_params;
58+
widget::parameters widget_params;
59+
};
60+
61+
tabbed_book(
62+
utki::shared_ref<ruis::context> context, //
63+
all_parameters params
64+
);
65+
5666
void add(const utki::shared_ref<tab>& tab, const utki::shared_ref<ruis::page>& page);
5767

5868
const ruis::book& get_book() const noexcept
@@ -75,4 +85,17 @@ class tabbed_book :
7585
using ruis::container::on_enabled_change;
7686
};
7787

88+
namespace make {
89+
inline utki::shared_ref<ruis::tabbed_book> tabbed_book(
90+
utki::shared_ref<ruis::context> context, //
91+
ruis::tabbed_book::all_parameters params
92+
)
93+
{
94+
return utki::make_shared<ruis::tabbed_book>(
95+
std::move(context), //
96+
std::move(params)
97+
);
98+
}
99+
} // namespace make
100+
78101
} // namespace ruis

tests/tabbed_book/res/test.gui

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)