[pve-devel] [PATCH installer 3/7] tui: add optional min-value constraint to `NumericEditView` and `DiskSizeEditView`

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


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

diff --git a/proxmox-tui-installer/src/views/mod.rs b/proxmox-tui-installer/src/views/mod.rs
index 7efd487..0fe715e 100644
--- a/proxmox-tui-installer/src/views/mod.rs
+++ b/proxmox-tui-installer/src/views/mod.rs
@@ -60,6 +60,7 @@ where
 
 pub struct NumericEditView<T> {
     view: EditView,
+    min_value: Option<T>,
     max_value: Option<T>,
     max_content_width: Option<usize>,
     allow_empty: bool,
@@ -72,6 +73,7 @@ where
     pub fn new() -> Self {
         Self {
             view: EditView::new().content("0"),
+            min_value: None,
             max_value: None,
             max_content_width: None,
             allow_empty: false,
@@ -81,12 +83,20 @@ where
     pub fn new_empty() -> Self {
         Self {
             view: EditView::new(),
+            min_value: None,
             max_value: None,
             max_content_width: None,
             allow_empty: true,
         }
     }
 
+    pub fn with_range(min: T, max: T) -> Self {
+        let mut view = Self::new();
+        view.min_value = Some(min);
+        view.max_value = Some(max);
+        view
+    }
+
     pub fn max_value(mut self, max: T) -> Self {
         self.max_value = Some(max);
         self
@@ -113,12 +123,19 @@ where
         }
     }
 
+    pub fn set_min_value(&mut self, min: T) {
+        self.min_value = Some(min);
+    }
+
     pub fn set_max_value(&mut self, max: T) {
         self.max_value = Some(max);
     }
 
     fn in_range(&self, value: T) -> bool {
-        !self.max_value.map_or(false, |max| value >= max)
+        let too_small = self.min_value.map_or(false, |min| value < min);
+        let too_large = self.max_value.map_or(false, |max| value > max);
+
+        !too_small && !too_large
     }
 
     fn check_bounds(&mut self, original: Rc<String>, result: EventResult) -> EventResult {
@@ -226,6 +243,10 @@ impl DiskSizeEditView {
         Self { view }
     }
 
+    pub fn with_range(min: f64, max: f64) -> Self {
+        Self::new().min_value(min).max_value(max)
+    }
+
     pub fn content(mut self, content: f64) -> Self {
         if let Some(view) = self
             .view
@@ -255,13 +276,17 @@ impl DiskSizeEditView {
         }
     }
 
+    pub fn min_value(mut self, min: f64) -> Self {
+        if let Some(view) = self.get_inner_mut() {
+            view.set_min_value(min);
+        }
+
+        self
+    }
+
     pub fn max_value(mut self, max: f64) -> Self {
-        if let Some(view) = self
-            .view
-            .get_child_mut(0)
-            .and_then(|v| v.downcast_mut::<ResizedView<FloatEditView>>())
-        {
-            view.get_inner_mut().set_max_value(max);
+        if let Some(view) = self.get_inner_mut() {
+            view.set_max_value(max);
         }
 
         self
@@ -281,6 +306,13 @@ impl DiskSizeEditView {
             None => Err(NumericEditViewError::InvalidView),
         }
     }
+
+    fn get_inner_mut(&mut self) -> Option<&mut FloatEditView> {
+        self.view
+            .get_child_mut(0)
+            .and_then(|v| v.downcast_mut::<ResizedView<FloatEditView>>())
+            .map(|v| v.get_inner_mut())
+    }
 }
 
 impl ViewWrapper for DiskSizeEditView {
-- 
2.42.0






More information about the pve-devel mailing list