diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-10-13 23:01:06 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-10-13 23:01:06 +0200 |
commit | 7a0a7abc52e7aad185b581481d91395e18d6c178 (patch) | |
tree | b18b78f7155375069a710e832c149b137aff5f64 | |
parent | a3fb19fe9c7f810bc997977eadb6ddb63e45cf93 (diff) | |
download | serenity-7a0a7abc52e7aad185b581481d91395e18d6c178.zip |
Try out a signal-like system like this:
auto* b = new Button;
b->onClick = [] (Button&) {
printf("The button was clicked!\n");
};
-rw-r--r-- | Widgets/Button.cpp | 3 | ||||
-rw-r--r-- | Widgets/Button.h | 2 | ||||
-rw-r--r-- | Widgets/Widget.h | 1 | ||||
-rw-r--r-- | Widgets/test.cpp | 4 |
4 files changed, 10 insertions, 0 deletions
diff --git a/Widgets/Button.cpp b/Widgets/Button.cpp index 9313ed143b..10ec97aa50 100644 --- a/Widgets/Button.cpp +++ b/Widgets/Button.cpp @@ -87,5 +87,8 @@ void Button::mouseUpEvent(MouseEvent& event) update(); Widget::mouseUpEvent(event); + + if (onClick) + onClick(*this); } diff --git a/Widgets/Button.h b/Widgets/Button.h index 98c63596eb..09ad9e0458 100644 --- a/Widgets/Button.h +++ b/Widgets/Button.h @@ -11,6 +11,8 @@ public: String caption() const { return m_caption; } void setCaption(String&&); + std::function<void(Button&)> onClick; + private: virtual void paintEvent(PaintEvent&) override; virtual void mouseDownEvent(MouseEvent&) override; diff --git a/Widgets/Widget.h b/Widgets/Widget.h index d592e91375..e025dc3aed 100644 --- a/Widgets/Widget.h +++ b/Widgets/Widget.h @@ -5,6 +5,7 @@ #include "Rect.h" #include "Color.h" #include <AK/String.h> +#include <functional> class Window; diff --git a/Widgets/test.cpp b/Widgets/test.cpp index dab26bb5da..5e8f403779 100644 --- a/Widgets/test.cpp +++ b/Widgets/test.cpp @@ -63,6 +63,10 @@ int main(int argc, char** argv) b->setWindowRelativeRect({ 0, 20, 100, 20 }); b->setCaption("Button"); + b->onClick = [] (Button& button) { + printf("Button %p clicked!\n", &button); + }; + auto* c = new CheckBox(widgetTestWindowWidget); c->setWindowRelativeRect({ 0, 40, 100, 20 }); c->setCaption("CheckBox"); |