Skip to content

Commit ed8ce76

Browse files
committed
add content_wrapping
1 parent d5c8dc9 commit ed8ce76

File tree

9 files changed

+132
-87
lines changed

9 files changed

+132
-87
lines changed

src/ruis/util/content_wrapping.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
ruis - GUI framework
3+
4+
Copyright (C) 2012-2025 Ivan Gagis <[email protected]>
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
/* ================ LICENSE END ================ */
21+
22+
#include "content_wrapping.hpp"
23+
24+
using namespace ruis;
25+
26+
content_wrapping::content_wrapping(utki::shared_ref<container> content_container) :
27+
content_container(std::move(content_container))
28+
{}

src/ruis/util/content_wrapping.hpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
ruis - GUI framework
3+
4+
Copyright (C) 2012-2025 Ivan Gagis <[email protected]>
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
/* ================ LICENSE END ================ */
21+
22+
#pragma once
23+
24+
#include "../widget/container.hpp"
25+
26+
namespace ruis {
27+
28+
/**
29+
* @brief Content wrapping widget base.
30+
* The base class for widgets which are essentially containers,
31+
* but enchancing functionality of simple container widget,
32+
* for example, by adding padding to the contents or adding
33+
* some special control widgets on top of the content.
34+
*/
35+
class content_wrapping
36+
{
37+
protected:
38+
const utki::shared_ref<ruis::container> content_container;
39+
40+
content_wrapping(utki::shared_ref<container> content_container);
41+
42+
public:
43+
/**
44+
* @brief Get content container.
45+
* @return The content container. This is where the child widgets are stored.
46+
*/
47+
container& content()
48+
{
49+
return this->content_container.get();
50+
}
51+
52+
/**
53+
* @brief Get content container.
54+
* @return The content container. This is where the child widgets are stored.
55+
*/
56+
const container& content() const
57+
{
58+
return this->content_container.get();
59+
}
60+
};
61+
62+
} // namespace ruis

src/ruis/widget/group/collapse_area.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ collapse_area::collapse_area(
5151
std::move(params.layout_params),
5252
std::move(params.widget_params)
5353
),
54+
content_wrapping(
55+
m::container(
56+
this->context,
57+
// clang-format off
58+
{
59+
.container_params = std::move(params.container_params)
60+
},
61+
// clang-format on
62+
std::move(contents)
63+
)
64+
),
5465
// clang-format off
5566
container(
5667
this->context,
@@ -127,25 +138,16 @@ collapse_area::collapse_area(
127138
)
128139
}
129140
),
130-
m::container(this->context,
131-
{
132-
.widget_params{
133-
.id = "ruis_content"s
134-
},
135-
.container_params = std::move(params.container_params)
136-
},
137-
std::move(contents)
138-
)
141+
this->content_container
139142
}
140143
),
141144
// clang-format on
142-
content_area(this->get_widget_as<container>("ruis_content")), //
143145
title_v(this->get_widget_as<container>("ruis_title"))
144146
{
145147
{
146148
auto& sw = this->get_widget_as<toggle_button>("ruis_switch");
147149
sw.pressed_change_handler = [this](button& tb) {
148-
auto& lp = this->content_area.get_layout_params();
150+
auto& lp = this->content().get_layout_params();
149151
if (tb.is_pressed()) {
150152
using namespace length_literals;
151153
lp.dims.y() = 0_px;

src/ruis/widget/group/collapse_area.hpp

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

2222
#pragma once
2323

24+
#include "../../util/content_wrapping.hpp"
2425
#include "../container.hpp"
25-
#include "../widget.hpp"
2626

2727
namespace ruis {
2828

29-
// TODO: derive from padding?
3029
// NOLINTNEXTLINE(bugprone-incorrect-enable-shared-from-this, "std::shared_from_this is public via widget")
3130
class collapse_area :
3231
virtual public widget, //
32+
public content_wrapping,
3333
private container
3434
{
35-
// TODO: use shared_ref
36-
// NOLINTNEXTLINE(clang-analyzer-webkit.NoUncountedMemberChecker, "false-positive")
37-
container& content_area;
3835
// TODO: use shared_ref
3936
// NOLINTNEXTLINE(clang-analyzer-webkit.NoUncountedMemberChecker, "false-positive")
4037
container& title_v;
@@ -53,11 +50,6 @@ class collapse_area :
5350
widget_list contents
5451
);
5552

56-
container& content() noexcept
57-
{
58-
return this->content_area;
59-
}
60-
6153
container& title() noexcept
6254
{
6355
return this->title_v;

src/ruis/widget/group/scroll_area.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class scroll_area : public container
4141
// offset from top left corner
4242
vector2 cur_scroll_pos = vector2(0);
4343

44-
// cached dimensions of the invisible contnets part, which goes beyond right and bottom edge of the scroll_area
44+
// cached dimensions of the invisible contents part, which goes beyond right and bottom edge of the scroll_area
4545
vector2 invisible_dims;
4646

4747
// cached scroll factor

src/ruis/widget/group/tiling_area.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,21 @@ tiling_area::tiling_area(
155155
std::move(params.widget_params) //
156156
),
157157
ruis::oriented({.vertical = false}),
158-
ruis::container(this->context, {}, {}),
159-
content_container(
158+
ruis::content_wrapping(
160159
ruis::make::container(
161160
this->context,
162161
{},
163162
std::move(children) //
164163
)
165164
),
165+
// clang-format off
166+
ruis::container(this->context,
167+
{},
168+
{
169+
this->content_container
170+
}
171+
),
172+
// clang-format on
166173
min_tile_size(this->context.get().units.pp_to_px(minimal_tile_size_pp)),
167174
dragger_size(this->context.get().units.pp_to_px(dragger_size_pp)),
168175
params([&]() {
@@ -174,8 +181,7 @@ tiling_area::tiling_area(
174181
return std::move(params.tiling_area_params);
175182
}())
176183
{
177-
this->ruis::container::push_back(this->content_container);
178-
this->content_container.get().move_to({0, 0});
184+
this->content().move_to({0, 0});
179185
}
180186

181187
void tiling_area::on_lay_out()

src/ruis/widget/group/tiling_area.hpp

Lines changed: 2 additions & 13 deletions
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
#pragma once
2323

24+
#include "../../util/content_wrapping.hpp"
2425
#include "../../util/oriented.hpp"
2526
#include "../container.hpp"
2627

@@ -33,11 +34,9 @@ Common borders of the contained tiles are draggable with mouse pointer.
3334
class tiling_area :
3435
virtual public widget, //
3536
public ruis::oriented,
37+
public content_wrapping,
3638
private ruis::container
3739
{
38-
// TODO: refactor: e.g. padding also has content container. Add some 'content_wrapping_widget' base class.
39-
utki::shared_ref<ruis::container> content_container;
40-
4140
public:
4241
const ruis::real min_tile_size;
4342
const ruis::real dragger_size;
@@ -62,16 +61,6 @@ class tiling_area :
6261
ruis::widget_list children
6362
);
6463

65-
ruis::container& content()
66-
{
67-
return this->content_container.get();
68-
}
69-
70-
const ruis::container& content() const
71-
{
72-
return this->content_container.get();
73-
}
74-
7564
void on_lay_out() override;
7665

7766
ruis::vector2 measure(const ruis::vector2& quotum) const override;

src/ruis/widget/label/padding.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ padding::padding(
3939
all_parameters params,
4040
widget_list children
4141
) :
42-
padding(
43-
// clang-format off
44-
m::container(std::move(context),
42+
widget( //
43+
std::move(context),
44+
std::move(params.layout_params),
45+
std::move(params.widget_params)
46+
),
47+
content_wrapping(
48+
m::container(this->context,
49+
// clang-format off
4550
{
4651
.container_params = [&](){
4752
// pile layout by default
@@ -51,37 +56,20 @@ padding::padding(
5156
return std::move(params.container_params);
5257
}()
5358
},
59+
// clang-format on
5460
std::move(children)
55-
),
56-
// clang-format on
57-
std::move(params.layout_params),
58-
std::move(params.widget_params),
59-
std::move(params.padding_params)
60-
)
61-
{}
62-
63-
padding::padding(
64-
utki::shared_ref<container> content_container,
65-
layout_parameters layout_params,
66-
widget::parameters widget_params,
67-
parameters padding_params
68-
):
69-
widget( //
70-
content_container.get().context,
71-
std::move(layout_params),
72-
std::move(widget_params)
61+
)
7362
),
7463
// clang-format off
7564
container(
7665
this->context,
7766
{},
7867
{
79-
content_container
68+
this->content_container
8069
}
8170
),
8271
// clang-format on
83-
params(std::move(padding_params)),
84-
inner_content(std::move(content_container))
72+
params(std::move(params.padding_params))
8573
{}
8674

8775
sides<real> padding::get_min_borders() const noexcept

src/ruis/widget/label/padding.hpp

Lines changed: 2 additions & 24 deletions
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
#pragma once
2323

24+
#include "../../util/content_wrapping.hpp"
2425
#include "../container.hpp"
2526

2627
namespace ruis {
@@ -30,6 +31,7 @@ namespace ruis {
3031
// NOLINTNEXTLINE(bugprone-incorrect-enable-shared-from-this, "false-positive")
3132
class padding :
3233
public virtual widget, //
34+
public content_wrapping,
3335
protected container
3436
{
3537
public:
@@ -40,8 +42,6 @@ class padding :
4042
private:
4143
parameters params;
4244

43-
utki::shared_ref<container> inner_content;
44-
4545
public:
4646
struct all_parameters {
4747
layout_parameters layout_params;
@@ -56,32 +56,10 @@ class padding :
5656
widget_list children
5757
);
5858

59-
private:
60-
padding(
61-
utki::shared_ref<container> content_container,
62-
layout_parameters layout_params,
63-
widget::parameters widget_params,
64-
parameters padding_params
65-
);
66-
6759
public:
6860
vec2 measure(const vec2& quotum) const override;
6961
void on_lay_out() override;
7062

71-
/**
72-
* @brief Get content container.
73-
* @return The content container. This is where the child widgets are stored.
74-
*/
75-
container& content()
76-
{
77-
return this->inner_content.get();
78-
}
79-
80-
const container& content() const
81-
{
82-
return this->inner_content.get();
83-
}
84-
8563
/**
8664
* @brief Set border settings.
8765
* @param borders - border values to set.

0 commit comments

Comments
 (0)