From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 48EC891B8A for ; Wed, 4 Oct 2023 16:42:44 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 2FDE7F445 for ; Wed, 4 Oct 2023 16:42:44 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS for ; Wed, 4 Oct 2023 16:42:41 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 4E6BD44839 for ; Wed, 4 Oct 2023 16:42:41 +0200 (CEST) From: Christoph Heiss To: pve-devel@lists.proxmox.com Date: Wed, 4 Oct 2023 16:42:14 +0200 Message-ID: <20231004144232.327071-4-c.heiss@proxmox.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231004144232.327071-1-c.heiss@proxmox.com> References: <20231004144232.327071-1-c.heiss@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -1.332 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy ENA_SUBJ_ODD_CASE 2.6 Subject has odd case KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [PATCH installer 3/7] tui: add optional min-value constraint to `NumericEditView` and `DiskSizeEditView` X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 04 Oct 2023 14:42:44 -0000 Signed-off-by: Christoph Heiss --- 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 { view: EditView, + min_value: Option, max_value: Option, max_content_width: Option, 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, 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::>()) - { - 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::>()) + .map(|v| v.get_inner_mut()) + } } impl ViewWrapper for DiskSizeEditView { -- 2.42.0