use crate::density;
use crate::theme::{self, spacing, surface, FontScale};
use crate::widgets::{meta_text, scaled_text, selectable_row_style};
use iced::widget::{button, column, container, opaque, row};
use iced::{Element, Length};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Suggestion {
pub label: String,
pub hint: Option<String>,
}
pub fn suggestions<'a, Message: Clone + 'a>(
heading: Option<String>,
rows: &[Suggestion],
highlighted: Option<usize>,
on_choose: impl Fn(usize) -> Message,
empty: Option<String>,
mono: bool,
scale: FontScale,
) -> Option<Element<'a, Message>> {
if rows.is_empty() && empty.is_none() {
return None;
}
let font = if mono { theme::mono_font() } else { iced::Font::DEFAULT };
let mut list = column![].width(Length::Fill).spacing(1);
if let Some(heading) = heading {
list = list.push(
container(meta_text(heading, density::META_TEXT_BASE, scale).font(font))
.padding([spacing::XS as u16, spacing::SM as u16]),
);
}
if rows.is_empty() {
if let Some(empty) = empty {
list = list.push(
container(meta_text(empty, density::META_TEXT_BASE, scale))
.padding([spacing::XS as u16, spacing::SM as u16]),
);
}
}
for (index, item) in rows.iter().enumerate() {
let selected = highlighted == Some(index);
let mut content = row![scaled_text(item.label.clone(), density::META_TEXT_BASE, scale)
.font(font)
.width(Length::Fill)]
.height(Length::Fill)
.align_y(iced::Alignment::Center)
.spacing(spacing::MD);
if let Some(hint) = &item.hint {
content = content.push(meta_text(hint.clone(), density::META_TEXT_BASE, scale));
}
list = list.push(
button(content)
.on_press(on_choose(index))
.width(Length::Fill)
.height(Length::Fixed(density::row_height(scale)))
.padding([0, spacing::SM as u16])
.style(move |t: &iced::Theme, status| selectable_row_style(t, status, selected)),
);
}
let panel = container(list)
.padding(spacing::XS)
.width(Length::Fill)
.style(|_t: &iced::Theme| container::Style {
background: Some(iced::Background::Color(surface::sidebar())),
border: iced::Border {
color: surface::card_border(),
width: 1.0,
radius: density::inner_radius().into(),
},
..container::Style::default()
});
Some(opaque(panel))
}