Allows storing (typesafe) arbitrary data together with each child. No functional changes for existing code.
Signed-off-by: Christoph Heiss <[email protected]> --- Changes v1 -> v2: * no changes proxmox-tui-installer/src/main.rs | 2 +- proxmox-tui-installer/src/views/bootdisk.rs | 6 ++-- proxmox-tui-installer/src/views/mod.rs | 33 +++++++++++++++++---- proxmox-tui-installer/src/views/network.rs | 2 +- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/proxmox-tui-installer/src/main.rs b/proxmox-tui-installer/src/main.rs index b24f90b..fce9fc2 100644 --- a/proxmox-tui-installer/src/main.rs +++ b/proxmox-tui-installer/src/main.rs @@ -420,7 +420,7 @@ fn password_dialog(siv: &mut Cursive) -> InstallerView { let state = siv.user_data::<InstallerState>().unwrap(); let options = &state.options.password; - let inner = FormView::new() + let inner = FormView::<()>::new() .child( "Root password [at least 8 characters]", EditView::new().secret(), diff --git a/proxmox-tui-installer/src/views/bootdisk.rs b/proxmox-tui-installer/src/views/bootdisk.rs index 9f6d235..3566814 100644 --- a/proxmox-tui-installer/src/views/bootdisk.rs +++ b/proxmox-tui-installer/src/views/bootdisk.rs @@ -47,7 +47,7 @@ impl BootdiskOptionsView { pub fn new(siv: &mut Cursive, runinfo: &RuntimeInfo, options: &BootdiskOptions) -> Self { let advanced_options = Arc::new(Mutex::new(options.clone())); - let bootdisk_form = FormView::new() + let bootdisk_form = FormView::<()>::new() .child( "Target harddisk", target_bootdisk_selectview( @@ -152,7 +152,7 @@ impl AdvancedBootdiskOptionsView { let mut view = LinearLayout::vertical() .child(DummyView.full_width()) - .child(FormView::new().child("Filesystem", fstype_select)) + .child(FormView::<()>::new().child("Filesystem", fstype_select)) .child(DummyView.full_width()); // Create the appropriate (inner) advanced options view @@ -493,7 +493,7 @@ impl<T: View> MultiDiskOptionsView<T> { selectable_disks.push(("-- do not use --".to_owned(), None)); - let mut disk_form = FormView::new(); + let mut disk_form = FormView::<()>::new(); for (i, _) in avail_disks.iter().enumerate() { disk_form.add_child( &format!("Harddisk {i}"), diff --git a/proxmox-tui-installer/src/views/mod.rs b/proxmox-tui-installer/src/views/mod.rs index 43ca999..5723fed 100644 --- a/proxmox-tui-installer/src/views/mod.rs +++ b/proxmox-tui-installer/src/views/mod.rs @@ -1,4 +1,4 @@ -use std::{net::IpAddr, str::FromStr, sync::Arc}; +use std::{collections::HashMap, net::IpAddr, str::FromStr, sync::Arc}; use cursive::{ Printer, Rect, Vec2, View, @@ -414,17 +414,21 @@ impl FormViewGetValue<f64> for DiskSizeEditView { } } -pub struct FormView { +pub struct FormView<UDT = ()> { view: LinearLayout, + user_data: HashMap<usize, UDT>, } -impl FormView { +impl<UDT> FormView<UDT> { pub fn new() -> Self { let view = LinearLayout::horizontal() .child(LinearLayout::vertical().full_width()) .child(LinearLayout::vertical().full_width()); - Self { view } + Self { + view, + user_data: HashMap::new(), + } } pub fn add_child(&mut self, label: &str, view: impl View) { @@ -432,6 +436,12 @@ impl FormView { self.add_to_column(1, view); } + pub fn add_child_with_data(&mut self, label: &str, view: impl View, data: UDT) { + self.add_to_column(0, TextView::new(format!("{label}: ")).no_wrap()); + self.add_to_column(1, view); + self.user_data.insert(self.len() - 1, data); + } + pub fn child(mut self, label: &str, view: impl View) -> Self { self.add_child(label, view); self @@ -453,6 +463,19 @@ impl FormView { .downcast_ref::<T>() } + pub fn get_child_with_data<T: View>(&self, index: usize) -> Option<(&T, &UDT)> { + let view = self + .view + .get_child(1)? + .downcast_ref::<ResizedView<LinearLayout>>()? + .get_inner() + .get_child(index)? + .downcast_ref::<T>()?; + + let data = self.user_data.get(&index)?; + Some((view, data)) + } + pub fn get_child_mut<T: View>(&mut self, index: usize) -> Option<&mut T> { self.view .get_child_mut(1)? @@ -509,7 +532,7 @@ impl FormView { } } -impl ViewWrapper for FormView { +impl<UDT: Send + Sync + 'static> ViewWrapper for FormView<UDT> { cursive::wrap_impl!(self.view: LinearLayout); fn wrap_important_area(&self, size: Vec2) -> Rect { diff --git a/proxmox-tui-installer/src/views/network.rs b/proxmox-tui-installer/src/views/network.rs index 5e3e258..960c25e 100644 --- a/proxmox-tui-installer/src/views/network.rs +++ b/proxmox-tui-installer/src/views/network.rs @@ -33,7 +33,7 @@ impl NetworkOptionsView { ifaces_selection.set_selection(selected); - let view = FormView::new() + let form = FormView::<()>::new() .child("Management interface", ifaces_selection) .child( "Hostname (FQDN)", -- 2.51.0 _______________________________________________ pve-devel mailing list [email protected] https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
