Skip to content

Commit 49aad9a

Browse files
refactor(ui): merge widget_parent into widget
1 parent e8fb6ce commit 49aad9a

File tree

6 files changed

+22
-30
lines changed

6 files changed

+22
-30
lines changed

src/shell/contextmenu/menu_widget.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ enum class popup_direction {
4343
bottom_right,
4444
};
4545

46-
struct menu_widget : public ui::widget_parent_flex {
47-
using super = ui::widget_parent_flex;
46+
struct menu_widget : public ui::widget_flex {
47+
using super = ui::widget_flex;
4848
float bg_padding_vertical = 6;
4949
std::shared_ptr<ui::rect_widget> bg;
5050
menu menu_data;

src/ui/ui.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void render_target::start_loop() {
2525
}
2626
}
2727
std::expected<bool, std::string> render_target::init() {
28-
root = std::make_shared<widget_parent>();
28+
root = std::make_shared<widget>();
2929

3030
std::ignore = init_global();
3131

src/ui/ui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace ui {
1616
struct render_target {
17-
std::shared_ptr<widget_parent> root;
17+
std::shared_ptr<widget> root;
1818
GLFWwindow *window;
1919

2020
NVGcontext *nvg;

src/ui/widget.cc

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
#include "ui.h"
33
#include <chrono>
44
#include <thread>
5-
void ui::widget_parent::render(nanovg_context ctx) {
6-
widget::render(ctx);
5+
void ui::widget::render(nanovg_context ctx) {
76
float orig_offset_x = ctx.offset_x, orig_offset_y = ctx.offset_y;
87
for (auto &child : children) {
98
ctx.offset_x = *x + orig_offset_x;
@@ -16,8 +15,11 @@ void ui::widget_parent::render(nanovg_context ctx) {
1615
ctx.offset_x = orig_offset_x;
1716
ctx.offset_y = orig_offset_y;
1817
}
19-
void ui::widget_parent::update(update_context &ctx) {
20-
widget::update(ctx);
18+
void ui::widget::update(update_context &ctx) {
19+
for (auto anim : anim_floats) {
20+
anim->update(ctx.delta_t);
21+
}
22+
2123
float orig_offset_x = ctx.offset_x, orig_offset_y = ctx.offset_y;
2224

2325
for (auto &child : children) {
@@ -29,15 +31,10 @@ void ui::widget_parent::update(update_context &ctx) {
2931
ctx.offset_x = orig_offset_x;
3032
ctx.offset_y = orig_offset_y;
3133
}
32-
void ui::widget_parent::add_child(std::shared_ptr<widget> child) {
34+
void ui::widget::add_child(std::shared_ptr<widget> child) {
3335
children.push_back(std::move(child));
3436
}
3537

36-
void ui::widget::update(update_context &ctx) {
37-
for (auto anim : anim_floats) {
38-
anim->update(ctx.delta_t);
39-
}
40-
}
4138
bool ui::update_context::hovered(widget *w, bool hittest) const {
4239
if (hittest && !hovered_widgets.empty())
4340
return false;
@@ -46,8 +43,8 @@ bool ui::update_context::hovered(widget *w, bool hittest) const {
4643
}
4744
float ui::widget::measure_height(update_context &ctx) { return height->dest(); }
4845
float ui::widget::measure_width(update_context &ctx) { return width->dest(); }
49-
void ui::widget_parent_flex::update(update_context &ctx) {
50-
widget_parent::update(ctx);
46+
void ui::widget_flex::update(update_context &ctx) {
47+
widget::update(ctx);
5148
float x = 0, y = 0;
5249
float target_width = 0, target_height = 0;
5350

@@ -124,4 +121,3 @@ bool ui::widget::check_hit(const update_context &ctx) {
124121
ctx.mouse_y >= (y->dest() + ctx.offset_y) &&
125122
ctx.mouse_y <= (y->dest() + height->dest() + ctx.offset_y);
126123
}
127-
void ui::widget::render(nanovg_context ctx) {}

src/ui/widget.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ struct update_context {
5454
All the widgets in the tree should be wrapped in a shared_ptr.
5555
If you want to use a widget in multiple places, you should create a new instance
5656
for each place.
57+
58+
It is responsible for updating and rendering its children
59+
It also sets the offset for the children
60+
It's like `posision: relative` in CSS
61+
While all other widgets are like `position: absolute`
5762
*/
5863
struct widget : std::enable_shared_from_this<widget> {
5964
std::vector<sp_anim_float> anim_floats{};
@@ -77,18 +82,9 @@ struct widget : std::enable_shared_from_this<widget> {
7782
}
7883

7984
virtual bool check_hit(const update_context &ctx);
80-
};
8185

82-
// A widget that renders its children
83-
// It is responsible for updating and rendering its children
84-
// It also sets the offset for the children
85-
// It's like `posision: relative` in CSS
86-
// While all other widgets are like `position: absolute`
87-
struct widget_parent : public widget {
88-
std::vector<std::shared_ptr<widget>> children;
89-
void render(nanovg_context ctx) override;
90-
void update(update_context &ctx) override;
9186
void add_child(std::shared_ptr<widget> child);
87+
std::vector<std::shared_ptr<widget>> children;
9288
template <typename T, typename... Args>
9389
inline void emplace_child(Args &&...args) {
9490
children.emplace_back(std::make_shared<T>(std::forward<Args>(args)...));
@@ -115,7 +111,7 @@ struct widget_parent : public widget {
115111
};
116112

117113
// A widget with child which lays out children in a row or column
118-
struct widget_parent_flex : public widget_parent {
114+
struct widget_flex : public widget {
119115
float gap = 0;
120116
bool horizontal = false;
121117
bool auto_size = true;

src/ui_test/test.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ struct menu_item_widget : public ui::widget {
145145
}
146146
};
147147

148-
struct menu_widget : public ui::widget_parent_flex {
149-
using super = ui::widget_parent_flex;
148+
struct menu_widget : public ui::widget_flex {
149+
using super = ui::widget_flex;
150150
float bg_padding_vertical = 6;
151151
float anchor_x = 0, anchor_y = 0;
152152
std::unique_ptr<ui::acrylic_background_widget> bg;

0 commit comments

Comments
 (0)