|
| 1 | +use iced::widget; |
| 2 | + |
| 3 | +use super::PADDING; |
| 4 | +use super::{HeadingLevel, heading}; |
| 5 | + |
| 6 | +// pub struct ResultList<Message> { |
| 7 | +// selected_index: usize, |
| 8 | +// rows: Vec<ListRow<Message>>, |
| 9 | +// } |
| 10 | + |
| 11 | +pub struct ListRow<Message> { |
| 12 | + text: String, |
| 13 | + subtext: Option<String>, |
| 14 | + icon: Option<iced::widget::image::Handle>, |
| 15 | + on_activate: Option<Message>, |
| 16 | +} |
| 17 | + |
| 18 | +impl<Message> ListRow<Message> { |
| 19 | + pub fn new<T>(text: T) -> Self |
| 20 | + where |
| 21 | + T: ToString, |
| 22 | + { |
| 23 | + Self { |
| 24 | + text: text.to_string(), |
| 25 | + subtext: None, |
| 26 | + icon: None, |
| 27 | + on_activate: None, |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + pub fn subtext<T>(mut self, subtext: T) -> Self |
| 32 | + where |
| 33 | + T: ToString, |
| 34 | + { |
| 35 | + self.subtext = Some(subtext.to_string()); |
| 36 | + self |
| 37 | + } |
| 38 | + |
| 39 | + pub fn optional_subtext<T>(self, subtext: Option<T>) -> Self |
| 40 | + where |
| 41 | + T: ToString, |
| 42 | + { |
| 43 | + if let Some(text) = subtext { |
| 44 | + self.subtext(text) |
| 45 | + } else { |
| 46 | + self |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + pub fn icon(mut self, handle: widget::image::Handle) -> Self { |
| 51 | + self.icon = Some(handle); |
| 52 | + self |
| 53 | + } |
| 54 | + |
| 55 | + pub fn optional_icon(self, handle: Option<widget::image::Handle>) -> Self { |
| 56 | + if let Some(h) = handle { |
| 57 | + self.icon(h) |
| 58 | + } else { |
| 59 | + self |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// Message to output when selected and enter is pressed, or clicked. |
| 64 | + pub fn on_activate(mut self, msg: Message) -> Self { |
| 65 | + self.on_activate = Some(msg); |
| 66 | + self |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<'a, Message> From<ListRow<Message>> for iced::Element<'a, Message> |
| 71 | +where |
| 72 | + Message: Clone + 'a, |
| 73 | +{ |
| 74 | + fn from(value: ListRow<Message>) -> Self { |
| 75 | + let mut row_widget = widget::Row::new().padding(0); |
| 76 | + |
| 77 | + let icon_widget = widget::Responsive::new(move |size| { |
| 78 | + // wish i didnt have to clone |
| 79 | + if let Some(icon) = value.icon.clone() { |
| 80 | + let real_image = |
| 81 | + widget::image::Image::new(icon).content_fit(iced::ContentFit::Cover); |
| 82 | + |
| 83 | + widget::container(real_image) |
| 84 | + } else { |
| 85 | + // TODO. Get image from user icon theme |
| 86 | + widget::container(widget::image(widget::image::Handle::from_bytes( |
| 87 | + include_bytes!("../../assets/image-missing-symbolic.png").to_vec(), |
| 88 | + ))) |
| 89 | + } |
| 90 | + .clip(true) |
| 91 | + .width(size.height) |
| 92 | + .height(size.height) |
| 93 | + .padding(0) |
| 94 | + .align_y(iced::Alignment::Center) |
| 95 | + .align_x(iced::Alignment::Center) |
| 96 | + .into() |
| 97 | + }) |
| 98 | + .width(iced::Shrink) |
| 99 | + .height(iced::Length::Fixed(32.0)); // i dont like this |
| 100 | + |
| 101 | + row_widget = row_widget.push(icon_widget); |
| 102 | + row_widget = row_widget.push(widget::space().width(iced::Length::Fixed(PADDING))); |
| 103 | + |
| 104 | + // let colw = widget::Column::new(); |
| 105 | + |
| 106 | + let text_widget = widget::container( |
| 107 | + heading(HeadingLevel::H3, value.text, None) |
| 108 | + .align_x(iced::Left) |
| 109 | + .align_y(iced::Alignment::Center) |
| 110 | + .width(iced::Fill), |
| 111 | + ); |
| 112 | + row_widget = row_widget.push(text_widget); |
| 113 | + // let colw = colw.push(text_widget); |
| 114 | + |
| 115 | + let subtext_widget = widget::container( |
| 116 | + heading( |
| 117 | + HeadingLevel::Subheading, |
| 118 | + value.subtext.unwrap_or_default(), |
| 119 | + None, |
| 120 | + ) |
| 121 | + .align_x(iced::Right) |
| 122 | + .align_y(iced::Alignment::Center), |
| 123 | + ); |
| 124 | + row_widget = row_widget.push(subtext_widget); |
| 125 | + |
| 126 | + widget::container( |
| 127 | + widget::button(row_widget) |
| 128 | + .width(iced::Fill) |
| 129 | + .height(iced::Shrink) |
| 130 | + .on_press_maybe(value.on_activate) |
| 131 | + .style(widget::button::secondary), |
| 132 | + ) |
| 133 | + .padding(PADDING) |
| 134 | + .into() |
| 135 | + } |
| 136 | +} |
0 commit comments