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

Add support for filtering and mapping models in .slint directly #1328

Open
dilawar opened this issue Jun 9, 2022 · 10 comments
Open

Add support for filtering and mapping models in .slint directly #1328

dilawar opened this issue Jun 9, 2022 · 10 comments
Labels
a:language-slint Compiler for the .slint language (mO,bF) enhancement New feature or request

Comments

@dilawar
Copy link

dilawar commented Jun 9, 2022

I have a property<[FooMsg]> msgs that stores list messages I want to display in different tabs. I am not maintaining different properties for each tab. I can do that if the answer here is a No. Each message FooMsg has a field called tag that determines the Tab of the message.

Inside a Tab, Here is a psedo code that I use to filter the messages and display them.

for msg[i] in root.msgs {
   if msg.tag == root.tag { 
      Text {
         text : msg.text;
         row: i;
       }
   }
}

I was hoping for something like (pseudo code):

for msg in root.msg.filter( |x| x.tag == root.tag) {
   Text { text: msg.text, row : i };
}

Note that the value of i will be different in the second case. The second case is suitable for generating a grid-layout because i will take values that increase by 1 at each step and there won't be gaps.

In short, I am looking for a filter like function for the array before I can iterate over it. Suggestions?

@dilawar dilawar changed the title Possibility of filtering property<[foo]> before using for loop Filtering property<[foo]> foos before looping over it Jun 9, 2022
@tronical
Copy link
Member

tronical commented Jun 9, 2022

While this isn’t possible in pure Slint yet, you can do this if you provide your Mode from Rust and place it behind a FilterModel. Would that help?

@tronical
Copy link
Member

tronical commented Jun 9, 2022

FWIW it would be nice to be able to write a filter like in your example in pure Slint as well.

@ogoffart
Copy link
Member

ogoffart commented Jun 9, 2022

We just would need to find a good syntax for it.

// Like in rust
for xx in model.filter(|x| x.tag == root.tag) : Rectangle {
// like in JS
for xx in model.filter(x => x.tag == root.tag) : Rectangle {
// specialized syntax
for xx in model | #.tag == root.tag : Rectangle {

We also would probably need a map adapror syntax as well.
Maybe a sorting one too?

@tronical
Copy link
Member

tronical commented Jun 9, 2022

Right, and we'd need the map/filter/sort in C++ as well. This sounds very well doable. My order of preference in syntax is JS > Rust > Special ;-)

@dilawar
Copy link
Author

dilawar commented Jun 9, 2022

While this isn’t possible in pure Slint yet, you can do this if you provide your Mode from Rust and place it behind a FilterModel. Would that help?

Thanks! This might just work 🥳 .

@dilawar
Copy link
Author

dilawar commented Jun 9, 2022

I also think the JS syntax is easier on the eyes than Rust syntax (especially when you are also supporting JS and C++).

@dilawar
Copy link
Author

dilawar commented Jun 10, 2022

While this isn’t possible in pure Slint yet, you can do this if you provide your Mode from Rust and place it behind a FilterModel. Would that help?

Thanks again for the suggestion @tronical. It worked! Not sure if I am using it the most efficiently but for now it solves my problem.

        // Connect msglist to slint Window.
        let msglist_app = ModelRc::new(FilterModel::new(self.msglist.clone(), |s| s.code == "App"));
        self.window.unwrap().set_msglist(msglist_app);

@tronical
Copy link
Member

👍 We could leave this ticket open to track the ability to specify filters in .slint itself. If you're okay with that then I'll edit the title slightly to reflect the intent.

@dilawar
Copy link
Author

dilawar commented Jun 10, 2022

@tronical Sounds good.

@tronical tronical changed the title Filtering property<[foo]> foos before looping over it Add support for filtering and mapping models in .slint directly Jun 10, 2022
@tronical tronical added the enhancement New feature or request label Jun 10, 2022
@ogoffart ogoffart added the a:language-slint Compiler for the .slint language (mO,bF) label Jul 13, 2023
@ogoffart ogoffart mentioned this issue Jul 20, 2023
3 tasks
@jmgrosen
Copy link

Any other workarounds suggested for this? It seems like it makes maintaining the "one source of truth" that Slint seems to otherwise do pretty well break quickly without losing modularity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:language-slint Compiler for the .slint language (mO,bF) enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants