Skip to content

Commit d7b8f53

Browse files
committed
Merge #992: [GUI] Limit amount to 8 decimal digits
9415dc9 new_amount_btc (pythcoiner) Pull request description: This PR replace #977 and fixes #798 I've followed jp1ac4 advice and create a `Form::new_amount_btc()` method that act like `Form::new_trimmed()` ~w/ few more (filtering) features:~ ~- allow only input of "0123456789,." characters~ ~- "," is replaced by "."~ ~- only one "." separator~ ~- maximum 8 digit after separator~ ~these features works for keyboard input or paste~ ~i've tryied to add a "0" in case the String start with a "." but this make the cursor have a bad location and i do not find a way to control the cursor location w/ iced, so i revert this feature~ ACKs for top commit: jp1ac4: ACK 9415dc9. edouardparis: ACK 9415dc9 Tree-SHA512: c8387e5fd9c3c030a71d0ba4a7aa3cd7f834215000f04e4e4d6b689c6bc9bf9dcecbf8b0799882f00f92d5bb8a656714f5149479e7f25e608a0ce96326a6e630
2 parents 6f6c35d + 9415dc9 commit d7b8f53

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

gui/src/app/view/spend/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,9 @@ pub fn create_spend_tx<'a>(
319319

320320
pub fn recipient_view<'a>(
321321
index: usize,
322-
address: &form::Value<String>,
323-
amount: &form::Value<String>,
324-
label: &form::Value<String>,
322+
address: &'a form::Value<String>,
323+
amount: &'a form::Value<String>,
324+
label: &'a form::Value<String>,
325325
is_max_selected: bool,
326326
) -> Element<'a, CreateSpendMessage> {
327327
Container::new(
@@ -395,7 +395,7 @@ pub fn recipient_view<'a>(
395395
None
396396
})
397397
.push_maybe(if !is_max_selected {
398-
Some(form::Form::new_trimmed("0.001 (in BTC)", amount, move |msg| {
398+
Some(form::Form::new_amount_btc("0.001 (in BTC)", amount, move |msg| {
399399
CreateSpendMessage::RecipientEdited(index, "amount", msg)
400400
})
401401
.warning(

gui/ui/src/component/form.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use bitcoin::Denomination;
12
use iced::{widget::text_input, Length};
23

34
use crate::{color, component::text, theme, util::Collection, widget::*};
@@ -62,6 +63,29 @@ where
6263
}
6364
}
6465

66+
/// Creates a new [`Form`] that restrict input values to valid btc amount before applying the
67+
/// `on_change` function.
68+
/// It expects:
69+
/// - a placeholder
70+
/// - the current value
71+
/// - a function that produces a message when the [`Form`] changes
72+
pub fn new_amount_btc<F>(placeholder: &str, value: &'a Value<String>, on_change: F) -> Self
73+
where
74+
F: 'static + Fn(String) -> Message,
75+
{
76+
Self {
77+
input: text_input::TextInput::new(placeholder, &value.value).on_input(move |s| {
78+
if bitcoin::Amount::from_str_in(&s, Denomination::Bitcoin).is_ok() || s.is_empty() {
79+
on_change(s)
80+
} else {
81+
on_change(value.value.clone())
82+
}
83+
}),
84+
warning: None,
85+
valid: value.valid,
86+
}
87+
}
88+
6589
/// Sets the [`Form`] with a warning message
6690
pub fn warning(mut self, warning: &'a str) -> Self {
6791
self.warning = Some(warning);

0 commit comments

Comments
 (0)