slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
17.69k stars 613 forks source link

Interpreter: Add support for enumerations #4777

Open tronical opened 8 months ago

tronical commented 8 months ago

In the public API for the interpreter in Rust and C++, enumerations are not supported.

Internally, they are implemented as a pair of enum name as string and enum variant as string.

Acceptance criteria for this issue is to be able to read and write properties declared in Slint that hold an enum declared in Slint, from Rust and C++ API. After implementing this, follow-up issues should be created for language bindings (JavaScript, Python) to add support there, too. When completed, #4778 can be reverted again.

For support in JavaScript and Python, it might make sense to include the exported enums as objects in the corresponding module objects the JS/Python load_file APIs create, and let each object have the enumeration variants as properties. Their value could be a fully qualified string, or something else - whatever the interpreter API accepts at that point.

ogoffart commented 8 months ago

My suggestion:

We can also simplify some code in the viewer that tries to serialize/deserialize enum values: https://github.com/slint-ui/slint/blob/6576a62d86acd94c877710fbe25753a16eac921c/tools/viewer/main.rs#L318

tronical commented 8 months ago

Add some code in the interpreter to convert Value::String to Type::Enumeration, that takes care of the setters

Ok, so given

enum Foo {
    Bar,
    Baz
}

and

in-out property <Foo> blub;

I suppose I should be able to assign the string "Bar" to it (via API, not in .slint of course)?

For the getter, right, we could just return a string.