Open eliemichel opened 4 years ago
May have found a way for (A):
fn dupli_lens<T: Data>() -> impl Lens<T,(T,T)> {
lens::Id.map(
|d: &T| (d.clone(), d.clone()),
|d: &mut T, x: (T, T)| {
*d = x.0
},
)
}
Then after .vertical()
it's simply:
.lens(AppData::right.then(dupli_lens())
// or
.lens(dupli_lens())
.lens(AppData::right)
edit: And for (B):
fn second_lens<T: Data + Copy>() -> impl Lens<(Vector<T>,T),T> {
lens::Id.map(
|d: &(Vector<T>, T)| d.1,
|d: &mut (Vector<T>, T), x: T| {
d.1 = x
},
)
}
and so the label becomes a simpler impl Widget<u32>
:
.with_child(
Label::new(|item: &u32, _env: &_| {
format!("List item #{}", item)
})
.align_vertical(UnitPoint::LEFT)
.lens(second_lens())
)
Two questions:
second_lens()
? How can I do if T
is not copyable?
Hi, I am taking inspiration from the list example to build a slightly more complicated example but I run into a few conceptual issues. I am especially interested in the last part where list has shared data to be able to remove elements (redacted a bit for clarity):
(A) First, this list only depends on
AppData.right
, so I'd like it to be globally lensed to that, i.e. either add.lens(AppData.right)
at the end or replacelens::Id.map
withAppData.right.map
. This works:But is there a way to "automate" this unit
Vector<T> => (Vector<T>,Vector<T>)
? If I have several dynamic list I'll rewrite it many times.(B) Let's say now
AppData.right
is aVector<Stuff>
. I have animpl Wiget<Stuff>
object that I want to use in lieu of theLabel
of the example. How can I lens the state(Vector<Stuff>, Stuff)
into justStuff
more elegantly that by writing a dedicated lens from scratch?Once solved I would add it to the list.rs example I think it'd be worth sharing!