add on_click to buttons

This commit is contained in:
hippoz 2022-03-20 03:05:21 +02:00
parent 736cef4667
commit 9c267cc87b
Signed by: hippoz
GPG key ID: 7C52899193467641
3 changed files with 10 additions and 0 deletions

View file

@ -40,6 +40,9 @@ void Button::on_focus_update(FocusUpdateEvent &event) {
void Button::on_activation_update(ActivationUpdateEvent &event) {
update_color();
if (event.get_activation_status() == false) {
on_click();
}
}
}

View file

@ -1,4 +1,5 @@
#include <string>
#include <functional>
#include "Widget.hpp"
#include "RGB.hpp"
#include "pango/pango-font.h"
@ -19,6 +20,8 @@ public:
void set_text(std::string text) { m_text = text; wants_repaint(); }
std::string &get_text() { return m_text; }
std::function<void()> on_click { [](){} };
protected:
void on_paint();
void on_init();
void on_focus_update(FocusUpdateEvent &event);

View file

@ -15,6 +15,10 @@ int main() {
main_widget.set_do_background_fill(true);
window.set_main_widget(&main_widget);
button.on_click = [&button]() {
button.set_text("clicked!");
};
main_widget.add_child(&button);
window.run(true);