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.64k stars 607 forks source link

Converting enum to string #6852

Open dorukotiv opened 1 day ago

dorukotiv commented 1 day ago

Feature Description

Currently, an enum that is defined in slint can not be given to the Text component as an input, a callback needs to be implemented in Rust to do the conversion.

Ideally, I would directly use the enum text as an input to the text.

Example:

enum Direction {
Right,
Left
}

Text {
text : Direction.Right;
}

that would be seen as "Right" on the screen.

Product Impact

We are a railway company building Remote Supervision Control Desktop application for a locomotive.

It would reduce the code and time.

tronical commented 1 day ago

I don't feel very comfortable with this. Other programming languages don't do this typically out of the box with enums and use reflection instead. And to make matters worse: this doesn't play well with translations.

Perhaps we need a way to more easily create mappings? Like a match on an enum that errors when not all variants are covered.

ogoffart commented 1 day ago

(in case you didn't know, it is converting to string for debugging purpose using the debug(...) function to print value in the console)

You can use @rust-attr to derive from one of the crate that provide enum-to-string conversion in rust for the exported rust enum.

But I agree with @tronical that this is not necessarily a good idea to have that in Slint itself.