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.68k stars 612 forks source link

Provide mock data shown in the preview but not compiled into the binary #3887

Open ogoffart opened 1 year ago

ogoffart commented 1 year ago

It may be interesting to have data that is only there for preview purpose but should be optimized out.

One way to do it right now is to pass --load-data to slint-viewer. But this doesn't work with the preview. How can we make it work with the preview?

A suggested alternative is to add a mock builtin function that can be used like so:

  in property <[string]> model: mock(["lorem", "ipsum", "dolor", "sit"]);

The mock function is just the identity function for the preview and the viewer, but yield to a default constructed empty value when generating rust or C++.

hunger commented 1 year ago

I like the idea, but am afraid that it would overwhelm the file with pretty useless data, especially when you have models with complex structs in it and want to have a wide range of data to test visually.

I do like the mock(...) idea though. Maybe it could refer to some external .json file instead?

melMass commented 1 year ago

Thanks for logging this issue!

A suggested alternative is to add a mock builtin function

My current approach is quite similar, I use a global object to store my widget's models and while in dev I add the default values there, this makes it work in preview too.

I like this approach but it means having to regularly comment out/uncomment these default values. Now that I think about it, maybe a temp solution could also be to "clear" the models from rust on init.

am afraid that it would overwhelm the file with pretty useless data

Agreed, maybe it could be some json file next to the slint file (named the same) where each keys is an identifier to a widget's property?

|_ui
|____my_widget.slint
|____my_widget.json
export component MyWidget {
    in property <[string]> names;

    for name[idx] in names:
            Text {
                text : "hello \{name} you are at position \{idx}"
            }
}
{
    "MyWidget.names": ["Bruno", "Olivier", "Tobias"]
}

or

{
    "MyWidget": {
            "names": ["Bruno", "Olivier", "Tobias"]
    }
}