Function rui::list

source ·
pub fn list<ID: Hash, V: View, F: Fn(&ID) -> V + 'static>(
    ids: Vec<ID>,
    f: F
) -> List<ID, F>
Expand description

Displays a list of items all of which are represented by the same View. See examples/list.rs.

ids is a Vec of items that implement Hash.

f is a function called to generate a View for each item.

For example:

rui(list(vec![1, 2, 3], |i| {
    hstack((
        circle(),
        text(&format!("{:?}", i))
    ))
}));
Examples found in repository?
examples/any_view.rs (lines 4-11)
3
4
5
6
7
8
9
10
11
12
fn main() {
    rui(list(vec![7, 42], |i| {
        if *i == 7 {
            any_view(circle())
        } else {
            any_view(rectangle())
        }
        .padding(Auto)
    }));
}
More examples
Hide additional examples
examples/list.rs (lines 8-10)
3
4
5
6
7
8
9
10
11
fn main() {
    let data = vec!["John", "Paul", "George", "Ringo"];

    let ids = (0usize..data.len()).collect();

    rui(list(ids, move |id| {
        hstack((circle(), data[*id].to_string()))
    }));
}
examples/todo_list.rs (lines 22-25)
17
18
19
20
21
22
23
24
25
26
27
fn todo_list(todos: impl Binding<Vec<String>>) -> impl View {
    with_cx(move |cx| {
        let len = todos.with(cx, |todos| todos.len());
        let ids = (0usize..len).collect();

        list(ids, move |id| {
            let id = *id;
            with_cx(move |cx| todos.with(cx, |todos| todos[id].clone()))
        })
    })
}
examples/counter_list.rs (lines 6-22)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() {
    rui(state(Counters::default, |counters, cx| {
        vstack((
            list(cx[counters].ids(), move |&i| {
                with_cx(move |cx| {
                    let count = bind(counters, CounterLens(i));
                    hstack((
                        format!("{}", count.get(cx)).padding(Auto),
                        button("increment", move |cx| {
                            *count.get_mut(cx) += 1;
                        })
                        .padding(Auto),
                        button("decrement", move |cx| {
                            *count.get_mut(cx) -= 1;
                        })
                        .padding(Auto),
                        button("remove", move |cx| cx[counters].remove_counter(i)).padding(Auto),
                    ))
                })
            }),
            format!("total: {}", cx[counters].sum_counters()).padding(Auto),
            button("add counter", move |cx| cx[counters].add_counter()).padding(Auto),
        ))
    }));
}