mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:27:35 +00:00
Try out a signal-like system like this:
auto* b = new Button; b->onClick = [] (Button&) { printf("The button was clicked!\n"); };
This commit is contained in:
parent
a3fb19fe9c
commit
7a0a7abc52
4 changed files with 10 additions and 0 deletions
|
@ -87,5 +87,8 @@ void Button::mouseUpEvent(MouseEvent& event)
|
||||||
|
|
||||||
update();
|
update();
|
||||||
Widget::mouseUpEvent(event);
|
Widget::mouseUpEvent(event);
|
||||||
|
|
||||||
|
if (onClick)
|
||||||
|
onClick(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,8 @@ public:
|
||||||
String caption() const { return m_caption; }
|
String caption() const { return m_caption; }
|
||||||
void setCaption(String&&);
|
void setCaption(String&&);
|
||||||
|
|
||||||
|
std::function<void(Button&)> onClick;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void paintEvent(PaintEvent&) override;
|
virtual void paintEvent(PaintEvent&) override;
|
||||||
virtual void mouseDownEvent(MouseEvent&) override;
|
virtual void mouseDownEvent(MouseEvent&) override;
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include "Rect.h"
|
#include "Rect.h"
|
||||||
#include "Color.h"
|
#include "Color.h"
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
class Window;
|
class Window;
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,10 @@ int main(int argc, char** argv)
|
||||||
b->setWindowRelativeRect({ 0, 20, 100, 20 });
|
b->setWindowRelativeRect({ 0, 20, 100, 20 });
|
||||||
b->setCaption("Button");
|
b->setCaption("Button");
|
||||||
|
|
||||||
|
b->onClick = [] (Button& button) {
|
||||||
|
printf("Button %p clicked!\n", &button);
|
||||||
|
};
|
||||||
|
|
||||||
auto* c = new CheckBox(widgetTestWindowWidget);
|
auto* c = new CheckBox(widgetTestWindowWidget);
|
||||||
c->setWindowRelativeRect({ 0, 40, 100, 20 });
|
c->setWindowRelativeRect({ 0, 40, 100, 20 });
|
||||||
c->setCaption("CheckBox");
|
c->setCaption("CheckBox");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue