gui: trim text input strings

This is applied to all forms except those concerning fingerprint labels
and the new GUI labels feature.
This commit is contained in:
jp1ac4 2023-10-03 13:36:56 +01:00
parent d3f2931375
commit 9895b493f1
No known key found for this signature in database
GPG Key ID: A7ACD32423568D7B
7 changed files with 41 additions and 19 deletions

View File

@ -770,7 +770,7 @@ pub fn update_spend_view<'a>(
.spacing(10)
.push(text("Insert updated PSBT:").bold())
.push(
form::Form::new("PSBT", updated, move |msg| {
form::Form::new_trimmed("PSBT", updated, move |msg| {
Message::ImportSpend(ImportSpendMessage::PsbtEdited(msg))
})
.warning("Please enter the correct base64 encoded PSBT")

View File

@ -27,7 +27,7 @@ pub fn import_psbt_view<'a>(
.spacing(10)
.push(text("Insert PSBT:").bold())
.push(
form::Form::new("PSBT", imported, move |msg| {
form::Form::new_trimmed("PSBT", imported, move |msg| {
Message::ImportSpend(ImportSpendMessage::PsbtEdited(msg))
})
.warning("Please enter a base64 encoded PSBT")

View File

@ -66,7 +66,7 @@ pub fn recovery<'a>(
.push(text("Destination").bold())
.push(
Container::new(
form::Form::new("Address", address, move |msg| {
form::Form::new_trimmed("Address", address, move |msg| {
Message::CreateSpend(CreateSpendMessage::RecipientEdited(
0, "address", msg,
))
@ -81,7 +81,7 @@ pub fn recovery<'a>(
.push(text("Feerate").bold())
.push(
Container::new(
form::Form::new("42 (sats/vbyte)", feerate, move |msg| {
form::Form::new_trimmed("42 (sats/vbyte)", feerate, move |msg| {
Message::CreateSpend(CreateSpendMessage::FeerateEdited(msg))
})
.warning("Invalid feerate")

View File

@ -247,7 +247,7 @@ pub fn bitcoind_edit<'a>(
Column::new()
.push(text("Cookie file path:").bold().small())
.push(
form::Form::new("Cookie file path", cookie_path, |value| {
form::Form::new_trimmed("Cookie file path", cookie_path, |value| {
SettingsEditMessage::FieldEdited("cookie_file_path", value)
})
.warning("Please enter a valid filesystem path")
@ -260,7 +260,7 @@ pub fn bitcoind_edit<'a>(
Column::new()
.push(text("Socket address:").bold().small())
.push(
form::Form::new("Socket address:", addr, |value| {
form::Form::new_trimmed("Socket address:", addr, |value| {
SettingsEditMessage::FieldEdited("socket_address", value)
})
.warning("Please enter a valid address")
@ -456,7 +456,7 @@ pub fn rescan<'a>(
Row::new()
.push(text("Year:").bold().small())
.push(
form::Form::new("2022", year, |value| {
form::Form::new_trimmed("2022", year, |value| {
SettingsEditMessage::FieldEdited("rescan_year", value)
})
.size(20)
@ -464,7 +464,7 @@ pub fn rescan<'a>(
)
.push(text("Month:").bold().small())
.push(
form::Form::new("12", month, |value| {
form::Form::new_trimmed("12", month, |value| {
SettingsEditMessage::FieldEdited("rescan_month", value)
})
.size(20)
@ -472,7 +472,7 @@ pub fn rescan<'a>(
)
.push(text("Day:").bold().small())
.push(
form::Form::new("31", day, |value| {
form::Form::new_trimmed("31", day, |value| {
SettingsEditMessage::FieldEdited("rescan_day", value)
})
.size(20)

View File

@ -141,9 +141,13 @@ pub fn create_spend_tx<'a>(
.push(Container::new(p1_bold("Feerate")).padding(10))
.spacing(10)
.push(
form::Form::new("42 (in sats/vbyte)", feerate, move |msg| {
Message::CreateSpend(CreateSpendMessage::FeerateEdited(msg))
})
form::Form::new_trimmed(
"42 (in sats/vbyte)",
feerate,
move |msg| {
Message::CreateSpend(CreateSpendMessage::FeerateEdited(msg))
},
)
.warning("Invalid feerate")
.size(20)
.padding(10),
@ -265,7 +269,7 @@ pub fn recipient_view<'a>(
.width(Length::Fixed(80.0)),
)
.push(
form::Form::new("Address", address, move |msg| {
form::Form::new_trimmed("Address", address, move |msg| {
CreateSpendMessage::RecipientEdited(index, "address", msg)
})
.warning("Invalid address (maybe it is for another network?)")
@ -284,7 +288,7 @@ pub fn recipient_view<'a>(
.width(Length::Fixed(80.0)),
)
.push(
form::Form::new("0.001 (in BTC)", amount, move |msg| {
form::Form::new_trimmed("0.001 (in BTC)", amount, move |msg| {
CreateSpendMessage::RecipientEdited(index, "amount", msg)
})
.warning(

View File

@ -389,7 +389,7 @@ pub fn import_descriptor<'a>(
let col_descriptor = Column::new()
.push(text("Descriptor:").bold())
.push(
form::Form::new("Descriptor", imported_descriptor, |msg| {
form::Form::new_trimmed("Descriptor", imported_descriptor, |msg| {
Message::DefineDescriptor(message::DefineDescriptor::ImportDescriptor(msg))
})
.warning("Incompatible descriptor.")
@ -808,7 +808,7 @@ pub fn define_bitcoin<'a>(
let col_address = Column::new()
.push(text("Address:").bold())
.push(
form::Form::new("Address", address, |msg| {
form::Form::new_trimmed("Address", address, |msg| {
Message::DefineBitcoind(message::DefineBitcoind::AddressEdited(msg))
})
.warning("Please enter correct address")
@ -820,7 +820,7 @@ pub fn define_bitcoin<'a>(
let col_cookie = Column::new()
.push(text("Cookie path:").bold())
.push(
form::Form::new("Cookie path", cookie_path, |msg| {
form::Form::new_trimmed("Cookie path", cookie_path, |msg| {
Message::DefineBitcoind(message::DefineBitcoind::CookiePathEdited(msg))
})
.warning("Please enter correct path")
@ -1467,7 +1467,7 @@ pub fn edit_key_modal<'a>(
.push(
Row::new()
.push(
form::Form::new("Extended public key", form_xpub, |msg| {
form::Form::new_trimmed("Extended public key", form_xpub, |msg| {
Message::DefineDescriptor(
message::DefineDescriptor::KeyModal(
message::ImportKeyModal::XPubEdited(msg),
@ -1581,7 +1581,7 @@ pub fn edit_sequence_modal<'a>(sequence: &form::Value<String>) -> Element<'a, Me
Row::new()
.push(
Container::new(
form::Form::new("ex: 1000", sequence, |v| {
form::Form::new_trimmed("ex: 1000", sequence, |v| {
Message::DefineDescriptor(message::DefineDescriptor::SequenceModal(
message::SequenceModal::SequenceEdited(v),
))

View File

@ -44,6 +44,24 @@ where
}
}
/// Creates a new [`Form`] that trims input values before applying the `on_change` function.
///
/// It expects:
/// - a placeholder
/// - the current value
/// - a function that produces a message when the [`Form`] changes
pub fn new_trimmed<F>(placeholder: &str, value: &Value<String>, on_change: F) -> Self
where
F: 'static + Fn(String) -> Message,
{
Self {
input: text_input::TextInput::new(placeholder, &value.value)
.on_input(move |s| on_change(s.trim().to_string())),
warning: None,
valid: value.valid,
}
}
/// Sets the [`Form`] with a warning message
pub fn warning(mut self, warning: &'a str) -> Self {
self.warning = Some(warning);