From 63c8d7b38aa5d30857a332aa7e18a763f4fb921d Mon Sep 17 00:00:00 2001
From: Ayush Singh <ayush@beagleboard.org>
Date: Tue, 5 Nov 2024 01:52:55 +0530
Subject: [PATCH] gui: Improve image selection code

- Use generic extra options list instead of treating custom image option
  as special.

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
---
 gui/src/main.rs                  | 20 ++++++++++++++------
 gui/src/pages/image_selection.rs | 32 ++++++++++++++++++++------------
 2 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/gui/src/main.rs b/gui/src/main.rs
index cb33eb1..0e420d4 100644
--- a/gui/src/main.rs
+++ b/gui/src/main.rs
@@ -371,13 +371,21 @@ impl BBImager {
                     images,
                     &self.search_bar,
                     &self.downloader,
-                    Some((
-                        "Format Sd Card",
-                        constants::FORMAT_ICON,
-                        BBImagerMessage::SelectImage(bb_imager::SelectedImage::Null(
+                    [
+                        pages::image_selection::ExtraImageEntry::new(
+                            "Custom Image",
+                            constants::FILE_ADD_ICON,
+                            BBImagerMessage::SelectLocalImage,
+                        ),
+                        pages::image_selection::ExtraImageEntry::new(
                             "Format Sd Card",
-                        )),
-                    )),
+                            constants::FORMAT_ICON,
+                            BBImagerMessage::SelectImage(bb_imager::SelectedImage::Null(
+                                "Format Sd Card",
+                            )),
+                        ),
+                    ]
+                    .into_iter(),
                 )
             }
             Screen::DestinationSelection => {
diff --git a/gui/src/pages/image_selection.rs b/gui/src/pages/image_selection.rs
index fa5464c..8fe512e 100644
--- a/gui/src/pages/image_selection.rs
+++ b/gui/src/pages/image_selection.rs
@@ -11,22 +11,33 @@ use crate::{
 
 const ICON_WIDTH: u16 = 80;
 
-pub fn view<'a, I>(
+pub(crate) struct ExtraImageEntry {
+    label: &'static str,
+    icon: &'static [u8],
+    msg: BBImagerMessage,
+}
+
+impl ExtraImageEntry {
+    pub(crate) const fn new(
+        label: &'static str,
+        icon: &'static [u8],
+        msg: BBImagerMessage,
+    ) -> Self {
+        Self { label, icon, msg }
+    }
+}
+
+pub fn view<'a, I, E>(
     images: I,
     search_bar: &'a str,
     downloader: &'a bb_imager::download::Downloader,
     // Allow optional format entry
-    format_entry: Option<(&'a str, &'static [u8], BBImagerMessage)>,
+    extra_entries: E,
 ) -> Element<'a, BBImagerMessage>
 where
     I: Iterator<Item = &'a helpers::Image>,
+    E: Iterator<Item = ExtraImageEntry>,
 {
-    let custom_image_btn = custom_btn(
-        "Use Custom Image",
-        constants::FILE_ADD_ICON,
-        BBImagerMessage::SelectLocalImage,
-    );
-
     let items = images
         .filter(|x| x.name.to_lowercase().contains(&search_bar.to_lowercase()))
         .map(|x| {
@@ -69,10 +80,7 @@ where
             ))
             .style(widget::button::secondary)
         })
-        .chain(match format_entry {
-            Some((label, icon, msg)) => vec![custom_image_btn, custom_btn(label, icon, msg)],
-            None => vec![custom_image_btn],
-        })
+        .chain(extra_entries.map(|x| custom_btn(x.label, x.icon, x.msg)))
         .map(Into::into);
 
     widget::column![
-- 
GitLab