diff --git a/crates/ui-widgets/src/pickers/dress.rs b/crates/ui-widgets/src/pickers/dress.rs index 0ef26a22b..34c055718 100644 --- a/crates/ui-widgets/src/pickers/dress.rs +++ b/crates/ui-widgets/src/pickers/dress.rs @@ -3,6 +3,7 @@ use crate::list::ListRow; use crate::primitives::ContentPainterCx; use egui::Rect; +use std::rc::Rc; /// What a picker adds to an option's row: the host's content for an option (a detail line, a trailing shortcut text, a /// thumbnail painter). `Select::decorate` and `ComboBox::decorate` take the closure. @@ -24,14 +25,21 @@ impl<'a, T> RowDress<'a, T> { /// The picture of an option (an arrowhead, a line style): one painter draws it in the option's list row and on the /// closed face. `Select::picture` takes the closure. -pub struct SelectPicture<'a, T>(Box>); +pub struct SelectPicture<'a, T>(Rc>); + +/// A copy shares the painter: each row's thumbnail holds one. +impl Clone for SelectPicture<'_, T> { + fn clone(&self) -> Self { + SelectPicture(Rc::clone(&self.0)) + } +} type PictureFn<'a, T> = dyn Fn(&T, &ContentPainterCx, Rect) + 'a; impl<'a, T> SelectPicture<'a, T> { /// The picture `paint` draws for an option, given its value, into the rect the row or face gives it. pub fn new(paint: impl Fn(&T, &ContentPainterCx, Rect) + 'a) -> Self { - SelectPicture(Box::new(paint)) + SelectPicture(Rc::new(paint)) } /// Paints the picture of `value` into `rect`. diff --git a/crates/ui-widgets/src/pickers/rows.rs b/crates/ui-widgets/src/pickers/rows.rs index 14ecc1542..61c67899c 100644 --- a/crates/ui-widgets/src/pickers/rows.rs +++ b/crates/ui-widgets/src/pickers/rows.rs @@ -9,13 +9,13 @@ use std::borrow::Cow; /// The entries as a collection source, keyed by entry position: the entries are fixed while the list is open, and the /// list's filter leaves out the ones that do not match. -pub(super) struct EntryRows<'a, T> { - entries: &'a [Entry], - labels: &'a [Label], +pub(super) struct EntryRows<'e, 'l, T> { + entries: &'e [Entry], + labels: &'l [Label], } -impl<'a, T> EntryRows<'a, T> { - pub(super) fn new(entries: &'a [Entry], labels: &'a [Label]) -> Self { +impl<'e, 'l, T> EntryRows<'e, 'l, T> { + pub(super) fn new(entries: &'e [Entry], labels: &'l [Label]) -> Self { EntryRows { entries, labels } } @@ -26,12 +26,12 @@ impl<'a, T> EntryRows<'a, T> { at: usize, chosen: Option, dress: Option<&RowDress<'r, T>>, - picture: Option<&'r SelectPicture<'r, T>>, + picture: Option<&SelectPicture<'r, T>>, ) -> ListRow<'r> where - 'a: 'r, + 'e: 'r, { - let entries: &'a [Entry] = self.entries; + let entries: &'e [Entry] = self.entries; let entry = &entries[at]; let Some(value) = entry.value() else { return if entry.is_heading() { ListRow::heading(self.labels[at].clone()) } else { ListRow::spacer() }; @@ -42,7 +42,10 @@ impl<'a, T> EntryRows<'a, T> { None => row, }; let row = match picture { - Some(picture) => row.thumb(ThumbSize::Small, move |cx, rect| picture.paint(value, cx, rect)), + Some(picture) => { + let picture = picture.clone(); + row.thumb(ThumbSize::Small, move |cx, rect| picture.paint(value, cx, rect)) + } None => row, }; let row = match entry.why_disabled() { @@ -56,7 +59,7 @@ impl<'a, T> EntryRows<'a, T> { } } -impl DisplayOrder for EntryRows<'_, T> { +impl DisplayOrder for EntryRows<'_, '_, T> { fn len(&self) -> usize { self.entries.len() } @@ -82,4 +85,4 @@ impl DisplayOrder for EntryRows<'_, T> { } } -impl ItemSource for EntryRows<'_, T> {} +impl ItemSource for EntryRows<'_, '_, T> {}