Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow two-way binding to struct members #814

Open
Futsch1 opened this issue Jan 7, 2022 · 2 comments
Open

Allow two-way binding to struct members #814

Futsch1 opened this issue Jan 7, 2022 · 2 comments
Labels
a:tool classes & property system runtime core classes (SharedVector,SharedString) and property system (mO,bS) enhancement New feature or request

Comments

@Futsch1
Copy link
Contributor

Futsch1 commented Jan 7, 2022

It is currently only possible to bind a current-value or checked attribute of a widget to a property. This requires creating single properties for each control in order to process their value in the host application or to create a struct and manually fill the values in a .60 callback.

It would be convenient to be able to map attributes to members of a struct property, like:

struct Settings {
    value_1: bool,
    value_2: bool
}

Window {
    property <Setttings> settings;

    Checkbox {
        text: "Value 1";
        checked <=> settings.value_1;
    }
    Checkbox {
        text: "Value 2";
        checked <=> settings.value_2;
    }

}
@ogoffart
Copy link
Member

ogoffart commented Jan 8, 2022

I have been thinking allowing two way binding with converstion.
As long as the compiler can invert the expression.
For example

foo <=> bar + 1;   // can be inverted to foo-1;
foo <=> -bar;  // can be inverted to -foo;
foo <=> 5 * bar; // can be inverted to foo / 5

(then implementation wise, in the generated code, there would be in the impl Property

pub fn link_two_way_with_conversion(prop1: Pin<&Property<T>>, prop2: Pin<&Property<U>>, conv1: impl Fn(U)->T, conv2: impl Fn(T)->U)

)

But unfortunately this is not enough because

foo <=> bar.xxx; // (with bar a struct that has a xxx field)

cannot be inverted....
... unless we pass the previous value in the conversion function:

pub fn link_two_way_with_conversion(prop1: Pin<&Property<T>>, prop2: Pin<&Property<U>>, conv1: impl Fn(U, &T)->T, conv2: impl Fn(T, &U)->U)

@ogoffart ogoffart added the enhancement New feature or request label Jan 8, 2022
@FlorentBecker
Copy link

FlorentBecker commented May 4, 2022

Since it's structs (aka product types) you're dealing with, you can always inline them. That is, it's always possible to compile this:

struct Settings {
    value_1: bool,
    value_2: bool
}

Window {
    property <Setttings> preferences;

    Checkbox {
        text: "Value 1";
        checked <=> preferences.value_1;
    }
    Checkbox {
        text: "Value 2";
        checked <=> preferences.value_2;
    }
}

into that:

Window {
    property<bool> preferences__value_1;
    property<bool> preferences__value_2;

    callback get_preferences() -> Settings;
    get_preferences => {
         return { value_1: self.preferences__value_1, value_2: self.preferences__value_2}
    }

   callback set_preferences(Settings);
   set_preferences(s) => {
        self.preferences__value_1 = s.value_1; self.preferences__value_2 = s.value_2;
   }

    Checkbox {
        text: "Value 1";
        checked <=> preferences__value_1;
    }
    Checkbox {
        text: "Value 2";
        checked <=> preferences__value_2;
    }

}

Then rewrite reads and writes of preferences into calls to get_preferences and set_preferences, which the compiler is bound to do at some point anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:tool classes & property system runtime core classes (SharedVector,SharedString) and property system (mO,bS) enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants