[pve-devel] [PATCH installer 2/2] tui: add "Deselect all" button to zfs/btrfs raid disk select

Christoph Heiss c.heiss at proxmox.com
Mon Jul 10 15:26:26 CEST 2023


Another puzzle piece in the quest to align the TUI installer more to
its GUI counterpart.

Like the GUI installer, it will only be shown if >3 disks are
detected on the system.

Signed-off-by: Christoph Heiss <c.heiss at proxmox.com>
---
 proxmox-tui-installer/src/views/bootdisk.rs | 46 ++++++++++++++++-----
 proxmox-tui-installer/src/views/mod.rs      | 17 ++++++++
 2 files changed, 53 insertions(+), 10 deletions(-)

diff --git a/proxmox-tui-installer/src/views/bootdisk.rs b/proxmox-tui-installer/src/views/bootdisk.rs
index 4970655..552cbc4 100644
--- a/proxmox-tui-installer/src/views/bootdisk.rs
+++ b/proxmox-tui-installer/src/views/bootdisk.rs
@@ -3,7 +3,8 @@ use std::{cell::RefCell, marker::PhantomData, rc::Rc};
 use cursive::{
     view::{Nameable, Resizable, ViewWrapper},
     views::{
-        Button, Dialog, DummyView, LinearLayout, NamedView, Panel, ScrollView, SelectView, TextView,
+        Button, Dialog, DummyView, LinearLayout, NamedView, PaddedView, Panel, ScrollView,
+        SelectView, TextView,
     },
     Cursive, View,
 };
@@ -293,10 +294,34 @@ impl<T: View> MultiDiskOptionsView<T> {
             );
         }

-        let disk_select_view = LinearLayout::vertical()
+        let mut disk_select_view = LinearLayout::vertical()
             .child(TextView::new("Disk setup").center())
             .child(DummyView)
-            .child(ScrollView::new(disk_form));
+            .child(ScrollView::new(disk_form.with_name("multidisk-disk-form")));
+
+        if avail_disks.len() > 3 {
+            let do_not_use_index = selectable_disks.len() - 1;
+            let deselect_all_button = Button::new("Deselect all", move |siv| {
+                siv.call_on_name("multidisk-disk-form", |view: &mut FormView| {
+                    view.call_on_childs(&|v: &mut SelectView<Option<Disk>>| {
+                        // As there is no .on_select() callback defined on the
+                        // SelectView's, the returned callback here can be safely
+                        // ignored.
+                        v.set_selection(do_not_use_index);
+                    });
+                });
+            });
+
+            disk_select_view.add_child(PaddedView::lrtb(
+                0,
+                0,
+                1,
+                0,
+                LinearLayout::horizontal()
+                    .child(DummyView.full_width())
+                    .child(deselect_all_button),
+            ));
+        }

         let options_view = LinearLayout::vertical()
             .child(TextView::new("Advanced options").center())
@@ -335,13 +360,14 @@ impl<T: View> MultiDiskOptionsView<T> {

         let disk_form = self
             .view
-            .get_child(view_top_index)?
-            .downcast_ref::<LinearLayout>()?
-            .get_child(0)?
-            .downcast_ref::<LinearLayout>()?
-            .get_child(2)?
-            .downcast_ref::<ScrollView<FormView>>()?
-            .get_inner();
+            .get_child_mut(view_top_index)?
+            .downcast_mut::<LinearLayout>()?
+            .get_child_mut(0)?
+            .downcast_mut::<LinearLayout>()?
+            .get_child_mut(2)?
+            .downcast_mut::<ScrollView<NamedView<FormView>>>()?
+            .get_inner_mut()
+            .get_mut();

         let mut selected_disks = Vec::new();

diff --git a/proxmox-tui-installer/src/views/mod.rs b/proxmox-tui-installer/src/views/mod.rs
index b1a1a13..aa78289 100644
--- a/proxmox-tui-installer/src/views/mod.rs
+++ b/proxmox-tui-installer/src/views/mod.rs
@@ -326,6 +326,15 @@ impl FormView {
             .downcast_ref::<T>()
     }

+    pub fn get_child_mut<T: View>(&mut self, index: usize) -> Option<&mut T> {
+        self.view
+            .get_child_mut(1)?
+            .downcast_mut::<ResizedView<LinearLayout>>()?
+            .get_inner_mut()
+            .get_child_mut(index)?
+            .downcast_mut::<T>()
+    }
+
     pub fn get_value<T, R>(&self, index: usize) -> Option<R>
     where
         T: View + FormViewGetValue<R>,
@@ -346,6 +355,14 @@ impl FormView {
         }
     }

+    pub fn call_on_childs<T: View>(&mut self, callback: &dyn Fn(&mut T)) {
+        for i in 0..self.len() {
+            if let Some(v) = self.get_child_mut::<T>(i) {
+                callback(v);
+            }
+        }
+    }
+
     pub fn len(&self) -> usize {
         self.view
             .get_child(1)
--
2.41.0






More information about the pve-devel mailing list