mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:44:26 +00:00
Select picture: the rows hold the painter by Rc, and EntryRows borrows entries and labels for separate lifetimes, so a row's thumbnail lives as long as the dressed row
This commit is contained in:
@@ -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<PictureFn<'a, T>>);
|
||||
pub struct SelectPicture<'a, T>(Rc<PictureFn<'a, T>>);
|
||||
|
||||
/// A copy shares the painter: each row's thumbnail holds one.
|
||||
impl<T> 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`.
|
||||
|
||||
@@ -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<T>],
|
||||
labels: &'a [Label],
|
||||
pub(super) struct EntryRows<'e, 'l, T> {
|
||||
entries: &'e [Entry<T>],
|
||||
labels: &'l [Label],
|
||||
}
|
||||
|
||||
impl<'a, T> EntryRows<'a, T> {
|
||||
pub(super) fn new(entries: &'a [Entry<T>], labels: &'a [Label]) -> Self {
|
||||
impl<'e, 'l, T> EntryRows<'e, 'l, T> {
|
||||
pub(super) fn new(entries: &'e [Entry<T>], labels: &'l [Label]) -> Self {
|
||||
EntryRows { entries, labels }
|
||||
}
|
||||
|
||||
@@ -26,12 +26,12 @@ impl<'a, T> EntryRows<'a, T> {
|
||||
at: usize,
|
||||
chosen: Option<usize>,
|
||||
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<T>] = self.entries;
|
||||
let entries: &'e [Entry<T>] = 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<T> DisplayOrder<usize> for EntryRows<'_, T> {
|
||||
impl<T> DisplayOrder<usize> for EntryRows<'_, '_, T> {
|
||||
fn len(&self) -> usize {
|
||||
self.entries.len()
|
||||
}
|
||||
@@ -82,4 +85,4 @@ impl<T> DisplayOrder<usize> for EntryRows<'_, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ItemSource<usize> for EntryRows<'_, T> {}
|
||||
impl<T> ItemSource<usize> for EntryRows<'_, '_, T> {}
|
||||
|
||||
Reference in New Issue
Block a user