Closed takagiy closed 2 years ago
Oh absolutely. Tell you what, make an MVCE and I'll show you the solution (it involves using shared elements).
Thank you for your speedy help. I tried to cut the redundant part of my code and fix it to work. After that, the program (below) worked correctly without segfault. Although I didn't came up with what's wrong in my original code, now I probably know how to use the library for that purpose. So it seems this issue can be closed for now. I'll try to refactor original code with reference to the working code. If I have some problem then, I may ask you again. Thank you very much.
#include <elements.hpp>
#include <memory>
#include <deque>
using namespace cycfi::elements;
namespace e = cycfi::elements;
struct container_data;
struct child_data {
float start, length;
container_data* parent;
element_ptr make_view();
};
struct container_data {
std::deque<child_data> children;
std::shared_ptr<layer_composite> children_element;
element_ptr view_element;
view* global_view;
void add_child(child_data &&c) {
c.parent = this;
this->children.emplace_back(std::move(c));
this->children_element->push_back(this->children.back().make_view());
this->global_view->refresh(*this->view_element);
}
element_ptr make_view() {
auto bg = fixed_size({2000, 100}, box(colors::white));
this->children_element = share(layer_composite());
this->children_element->push_back(share(bg));
for(auto& c: this->children) {
c.parent = this;
this->children_element->push_back(c.make_view());
}
this->view_element = share(hscroller(hold(this->children_element)));
return this->view_element;
}
};
element_ptr child_data::make_view() {
auto button = layered_button(rbox(colors::blue), rbox(colors::azure));
button.on_click = [&, this](bool down) {
if (down) {
puts("clicked");
this->parent->add_child(child_data{this->start + 200, 100});
}
};
auto b = fixed_size({this->length, 100}, button);
return share(left_margin(this->start, b));
}
int main(int argc, char* argv[])
{
container_data container{std::deque<child_data>{child_data{0, 100}, child_data{200, 100}}};
app _app(argc, argv, "Test Project", "dev.neqo.test1");
window _win(_app.name());
_win.on_close = [&_app]() { _app.stop(); };
view view_(_win);
view_.content(
hold(container.make_view())
);
container.global_view = &view_;
_app.run();
return 0;
}
Hello. I would like to make an element which dynamically add child elment to the layer by user interaction like clicking some button. I tried to use
layer_composite::push_back()
in theon_click
callback for that but it didn't work and caused SEGV. (Maybe reallocating the vector invalidated the pointers) I'm afraid that I cannot find out the proper solution by myself. Could this library provide a way for such purpose?