test many widgets at the same time

This commit is contained in:
hippoz 2022-05-10 16:17:12 +03:00
parent 3039646b6b
commit fcb4556a4e
Signed by: hippoz
GPG key ID: 7C52899193467641
4 changed files with 21 additions and 20 deletions

View file

@ -14,7 +14,7 @@ private:
std::string m_text; std::string m_text;
public: public:
Button(std::string text) Button(std::string text)
: Widget() : Widget(WidgetType::Button)
, m_text(text) {} , m_text(text) {}
void set_text(std::string text) { m_text = text; wants_repaint(); } void set_text(std::string text) { m_text = text; wants_repaint(); }
@ -22,8 +22,6 @@ public:
std::function<void()> on_click { [](){} }; std::function<void()> on_click { [](){} };
protected: protected:
WidgetType m_type { WidgetType::Button };
void on_paint(); void on_paint();
void on_init(); void on_init();
void on_focus_update(FocusUpdateEvent &event); void on_focus_update(FocusUpdateEvent &event);

View file

@ -10,7 +10,7 @@ private:
std::string m_text; std::string m_text;
public: public:
Label(std::string text) Label(std::string text)
: Widget() : Widget(WidgetType::Label)
, m_text(text) {} , m_text(text) {}
~Label() {} ~Label() {}
@ -18,8 +18,6 @@ public:
std::string &text() { return m_text; } std::string &text() { return m_text; }
void set_text(std::string text) { m_text = text; wants_repaint(); } void set_text(std::string text) { m_text = text; wants_repaint(); }
protected: protected:
WidgetType m_type { WidgetType::Label };
void on_paint(); void on_paint();
void on_init(); void on_init();
}; };

View file

@ -48,6 +48,9 @@ private:
public: public:
Widget() {} Widget() {}
Widget(WidgetType type)
: m_type(type) {}
Widget(ControlWidgetType type) Widget(ControlWidgetType type)
: m_control_type(type) { : m_control_type(type) {
m_type = WidgetType::Control; m_type = WidgetType::Control;
@ -107,7 +110,7 @@ public:
} }
template<typename T, class... Args> template<typename T, class... Args>
std::shared_ptr<T> add_child(Args&&... args) { std::shared_ptr<T> add(Args&&... args) {
std::shared_ptr<T> child = std::make_shared<T>(std::forward<Args>(args)...); std::shared_ptr<T> child = std::make_shared<T>(std::forward<Args>(args)...);
add_child(child); add_child(child);
return child; return child;

View file

@ -6,6 +6,9 @@
#include "Label.hpp" #include "Label.hpp"
#include "Layout.hpp" #include "Layout.hpp"
#include "src/DocumentLayout.hpp" #include "src/DocumentLayout.hpp"
#include <iostream>
#include <memory>
#include <string>
int main() { int main() {
Raven::Window window {}; Raven::Window window {};
@ -13,22 +16,21 @@ int main() {
auto main_widget = window.set_main_widget<Raven::Widget>(); auto main_widget = window.set_main_widget<Raven::Widget>();
main_widget->set_layout<Raven::DocumentLayout>(6.0); main_widget->set_layout<Raven::DocumentLayout>(6.0);
auto add_button = main_widget->add_child<Raven::Button>("add");
auto subtract_button = main_widget->add_child<Raven::Button>("subtract");
main_widget->add_child<Raven::Widget>(Raven::ControlWidgetType::NewRow);
auto label = main_widget->add_child<Raven::Label>("0");
int number = 0; int number = 0;
add_button->on_click = [&]() { for (int i = 0; i < 100; i++) {
auto button = main_widget->add<Raven::Button>("0");
button->on_click = [&]() {
number++; number++;
label->set_text(std::to_string(number));
};
subtract_button->on_click = [&]() { for (auto& c : main_widget->children()) {
number--; if (c->type() == Raven::WidgetType::Button) {
label->set_text(std::to_string(number)); auto button_child = std::static_pointer_cast<Raven::Button>(c);
button_child->set_text(std::to_string(number));
}
}
}; };
}
window.spawn_window(); window.spawn_window();
window.run(true); window.run(true);