[pve-devel] [PATCH installer 1/7] tui: fix setting content when using the `DiskSizeEditView` builder

Christoph Heiss c.heiss at proxmox.com
Wed Oct 4 16:42:12 CEST 2023


Previously, it would throw away all other settings (like `max_value`),
if a construct like

  DiskSizeEditView::new().max_value(8.0).content(8.0)

was used, due to simply replacing the inner view.
Instead, modify the inner view.

Signed-off-by: Christoph Heiss <c.heiss at proxmox.com>
---
 proxmox-tui-installer/src/views/mod.rs | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/proxmox-tui-installer/src/views/mod.rs b/proxmox-tui-installer/src/views/mod.rs
index aa24fa4..76f96a1 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, rc::Rc, str::FromStr};
+use std::{mem, net::IpAddr, rc::Rc, str::FromStr};
 
 use cursive::{
     event::{Event, EventResult},
@@ -190,8 +190,21 @@ impl DiskSizeEditView {
     }
 
     pub fn content(mut self, content: f64) -> Self {
-        if let Some(view) = self.view.get_child_mut(0).and_then(|v| v.downcast_mut()) {
-            *view = FloatEditView::new().content(content).full_width();
+        if let Some(view) = self
+            .view
+            .get_child_mut(0)
+            .and_then(|v| v.downcast_mut::<ResizedView<FloatEditView>>())
+        {
+            // We need actual ownership here of the inner `FloatEditView` to call `.content()` on
+            // it. Thus first swap it out with a dummy, modify it and swap it back in.
+            // This procedure ensures other settings (like `max_value`) is preserved on the inner
+            // view.
+
+            let mut inner = FloatEditView::new();
+            mem::swap(view.get_inner_mut(), &mut inner);
+
+            inner = inner.content(content);
+            mem::swap(view.get_inner_mut(), &mut inner);
         }
 
         self
-- 
2.42.0






More information about the pve-devel mailing list