Skip to content

Commit

Permalink
Binding refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dancazarin committed Feb 10, 2025
1 parent c708b64 commit ee63563
Show file tree
Hide file tree
Showing 24 changed files with 283 additions and 255 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public:
// Button widget
rcnew Button{
rcnew Text{ "Click" },
// Using m_lifetime ensures that callbacks will be detached once the Component is deleted
onClick = m_lifetime |
// Using lifetime() ensures that callbacks will be detached once the Component is deleted
onClick = lifetime() |
[this]() {
// Notify bindings about the change
bindings->assign(m_label) = "Updated text";
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ public:
// Button widget
rcnew Button{
rcnew Text{ "Click" },
// Using m_lifetime ensures that callbacks will be detached once the Component is deleted
onClick = m_lifetime |
// Using lifetime() ensures that callbacks will be detached once the Component is deleted
onClick = lifetime() |
[this]() {
// Notify bindings about the change
bindings->assign(m_label) = "Updated text";
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/hello_world.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public:
rcnew Text{"Hello, world"}, // Display a text widget with "Hello, world"
rcnew Button{
rcnew Text{"Quit"}, // Button label
onClick = m_lifetime | []() { // Quit the application on button click
onClick = lifetime() | []() { // Quit the application on button click
windowApplication->quit();
},
},
Expand Down
48 changes: 24 additions & 24 deletions examples/calc/calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,28 @@ class CalcComponent final : public Component {
rcnew CalcBtn{
"CE",
Graphene::buttonColor = 0x9A202A_rgb,
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.clear();
},
},
rcnew CalcBtn{
"C",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.clear();
},
},
rcnew CalcBtn{
ICON_pi, // "π",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.constant(Number::parse("3.1415926535897932384626433832795"));
},
},
rcnew CalcBtn{
"",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.backspace();
},
Expand All @@ -153,29 +153,29 @@ class CalcComponent final : public Component {
rcnew CalcRow{
rcnew CalcBtn{
"1/x",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.operation(UnaryOperator::Reciprocal);
},
},
rcnew CalcBtn{
"",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.operation(UnaryOperator::Square);
},
},
rcnew CalcBtn{
ICON_radical, // "√x",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.operation(UnaryOperator::SquareRoot);
},
},
rcnew CalcBtn{
ICON_divide, // "÷",
Graphene::buttonColor = 0x6B7183_rgb,
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.operation(MultiplicativeOperator::Divide);
},
Expand All @@ -184,29 +184,29 @@ class CalcComponent final : public Component {
rcnew CalcRow{
rcnew CalcBtn{
"7",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.digit(7);
},
},
rcnew CalcBtn{
"8",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.digit(8);
},
},
rcnew CalcBtn{
"9",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.digit(9);
},
},
rcnew CalcBtn{
ICON_x, // "×",
Graphene::buttonColor = 0x6B7183_rgb,
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.operation(MultiplicativeOperator::Multiply);
},
Expand All @@ -215,29 +215,29 @@ class CalcComponent final : public Component {
rcnew CalcRow{
rcnew CalcBtn{
"4",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.digit(4);
},
},
rcnew CalcBtn{
"5",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.digit(5);
},
},
rcnew CalcBtn{
"6",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.digit(6);
},
},
rcnew CalcBtn{
ICON_minus, // "−",
Graphene::buttonColor = 0x6B7183_rgb,
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.operation(AdditiveOperator::Subtract);
},
Expand All @@ -246,29 +246,29 @@ class CalcComponent final : public Component {
rcnew CalcRow{
rcnew CalcBtn{
"1",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.digit(1);
},
},
rcnew CalcBtn{
"2",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.digit(2);
},
},
rcnew CalcBtn{
"3",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.digit(3);
},
},
rcnew CalcBtn{
ICON_plus, // "+",
Graphene::buttonColor = 0x6B7183_rgb,
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.operation(AdditiveOperator::Add);
},
Expand All @@ -277,29 +277,29 @@ class CalcComponent final : public Component {
rcnew CalcRow{
rcnew CalcBtn{
"±",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.changeSign();
},
},
rcnew CalcBtn{
"0",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.digit(0);
},
},
rcnew CalcBtn{
".",
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.decimalSep();
},
},
rcnew CalcBtn{
ICON_equal, // "=",
Graphene::buttonColor = 0x297227_rgb,
onClick = m_lifetime |
onClick = lifetime() |
[this] {
calc.solve();
},
Expand Down
6 changes: 3 additions & 3 deletions examples/showcase/src/Buttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ RC<Widget> ShowcaseButtons::build(RC<Notifications> notifications) {
rcnew Widget{
rcnew Button{
rcnew Text{ "Button 1" },
onClick = m_lifetime |
onClick = lifetime() |
[notifications]() {
notifications->show(rcnew Text{ "Button 1 clicked" });
},
},
rcnew Button{
rcnew Text{ "Disabled Button" },
disabled = true,
onClick = m_lifetime |
onClick = lifetime() |
[notifications]() {
notifications->show(rcnew Text{ "Disabled Button clicked" });
},
Expand Down Expand Up @@ -127,7 +127,7 @@ RC<Widget> ShowcaseButtons::build(RC<Notifications> notifications) {
},
repeatDelay = 0.2,
repeatInterval = 0.2,
onClick = m_lifetime |
onClick = lifetime() |
[this] {
m_clicked++;
bindings->notify(&m_clicked);
Expand Down
30 changes: 15 additions & 15 deletions examples/showcase/src/Dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Brisk {

static RC<Widget> osDialogButton(std::string text, Value<Trigger<>> fn) {
static RC<Widget> osDialogButton(std::string text, Listener<> fn) {
return rcnew HLayout{
rcnew Button{
rcnew Text{ std::move(text) },
Expand Down Expand Up @@ -42,7 +42,7 @@ RC<Widget> ShowcaseDialogs::build(RC<Notifications> notifications) {
rcnew HLayout{
rcnew Button{
rcnew Text{ "Open window" },
onClick = m_lifetime |
onClick = lifetime() |
[this]() {
RC<SmallComponent> comp = rcnew SmallComponent();
windowApplication->addWindow(comp->makeWindow());
Expand All @@ -51,7 +51,7 @@ RC<Widget> ShowcaseDialogs::build(RC<Notifications> notifications) {

rcnew Button{
rcnew Text{ "Open modal window" },
onClick = m_lifetime |
onClick = lifetime() |
[this]() {
RC<SmallComponent> comp = rcnew SmallComponent();
windowApplication->showModalWindow(comp->makeWindow());
Expand All @@ -61,7 +61,7 @@ RC<Widget> ShowcaseDialogs::build(RC<Notifications> notifications) {
rcnew HLayout{
rcnew Button{
rcnew Text{ "TextInputDialog" },
onClick = m_lifetime |
onClick = lifetime() |
[]() {
RC<TextInputDialog> dialog = rcnew TextInputDialog{ "Enter name", "World" };
windowApplication->showModalWindow(dialog->makeWindow());
Expand All @@ -78,7 +78,7 @@ RC<Widget> ShowcaseDialogs::build(RC<Notifications> notifications) {
rcnew HLayout{
rcnew Button{
rcnew Text{ "Open Dialog" },
onClick = m_lifetime |
onClick = lifetime() |
[this]() {
bindings->assign(m_popupDialog, true);
},
Expand All @@ -95,33 +95,33 @@ RC<Widget> ShowcaseDialogs::build(RC<Notifications> notifications) {

rcnew Text{ "OS dialogs (window/OSDialogs.hpp)", classes = { "section-header" } },
osDialogButton(
"Open URL", m_lifetime |
"Open URL", lifetime() |
[]() {
openURLInBrowser("https://www.brisklib.com/");
}),
osDialogButton(
"Open folder", m_lifetime |
"Open folder", lifetime() |
[]() {
openFolder(defaultFolder(DefaultFolder::Documents));
}),

osDialogButton(
"Message box (Info)", m_lifetime |
"Message box (Info)", lifetime() |
[]() {
showMessage("title", "message", MessageBoxType::Info);
}),
osDialogButton(
"Message box (Warning)", m_lifetime |
"Message box (Warning)", lifetime() |
[]() {
showMessage("title", "message", MessageBoxType::Warning);
}),
osDialogButton(
"Message box (Error)", m_lifetime |
"Message box (Error)", lifetime() |
[]() {
showMessage("title", "message", MessageBoxType::Error);
}),
osDialogButton(
"Dialog (OK, Cancel)", m_lifetime |
"Dialog (OK, Cancel)", lifetime() |
[this]() {
if (showDialog("title", "message", DialogButtons::OKCancel,
MessageBoxType::Info) == DialogResult::OK)
Expand All @@ -131,7 +131,7 @@ RC<Widget> ShowcaseDialogs::build(RC<Notifications> notifications) {
bindings->notify(&m_text);
}),
osDialogButton(
"Dialog (Yes, No, Cancel)", m_lifetime |
"Dialog (Yes, No, Cancel)", lifetime() |
[this]() {
if (DialogResult r = showDialog("title", "message",
DialogButtons::YesNoCancel,
Expand All @@ -145,7 +145,7 @@ RC<Widget> ShowcaseDialogs::build(RC<Notifications> notifications) {
bindings->notify(&m_text);
}),
osDialogButton(
"Open File", m_lifetime |
"Open File", lifetime() |
[this]() {
auto file = showOpenDialog({ { "*.txt", "Text files" } },
defaultFolder(DefaultFolder::Documents));
Expand All @@ -156,7 +156,7 @@ RC<Widget> ShowcaseDialogs::build(RC<Notifications> notifications) {
bindings->notify(&m_text);
}),
osDialogButton(
"Open Files", m_lifetime |
"Open Files", lifetime() |
[this]() {
auto files = showOpenDialogMulti({ { "*.txt", "Text files" }, anyFile() },
defaultFolder(DefaultFolder::Documents));
Expand All @@ -165,7 +165,7 @@ RC<Widget> ShowcaseDialogs::build(RC<Notifications> notifications) {
bindings->notify(&m_text);
}),
osDialogButton(
"Pick folder", m_lifetime |
"Pick folder", lifetime() |
[this]() {
auto folder = showFolderDialog(defaultFolder(DefaultFolder::Documents));
if (folder)
Expand Down
Loading

0 comments on commit ee63563

Please sign in to comment.