1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use rust_on_rails::prelude::*;
use crate::elements::images::Icon;
use crate::elements::text::{Text, TextStyle};
use crate::layout::{Column, Offset, Padding, Row, Size, Stack};
use crate::PelicanUI;
/// A component displaying an amount in both USD and BTC formats.
///
/// The [`AmountDisplay`] component is used to show financial information, such as
/// a currency amount in both USD and Bitcoin (BTC). It consists of a column layout
/// containing a main text for the USD amount and a subtext for the BTC equivalent.
#[derive(Debug, Component)]
pub struct AmountDisplay(Column, Text, SubText);
impl OnEvent for AmountDisplay {}
impl AmountDisplay {
/// Creates a new [`AmountDisplay`] component.
///
/// This function initializes the [`AmountDisplay`] with a column layout,
/// a text component showing the USD amount, and a subtext component displaying
/// the BTC equivalent.
///
/// # Parameters
/// - `ctx`: A mutable reference to [`Context`].
///
/// # Returns
/// A new [`AmountDisplay`] component with default values.
///
/// # Example
/// ```rust
/// let amount_display = AmountDisplay::new(ctx);
/// ```
pub fn new(ctx: &mut Context) -> Self {
let font_size = ctx.get::<PelicanUI>().theme.fonts.size.title;
AmountDisplay (
Column::new(16.0, Offset::Center, Size::Fit, Padding(16.0, 64.0, 16.0, 64.0)),
Text::new(ctx, "$0.00", TextStyle::Heading, font_size, Align::Left),
SubText::new(ctx, "0.00000000 BTC")
)
}
/// Returns a mutable reference to the USD text in the [`AmountDisplay`].
pub fn usd(&mut self) -> &mut String { &mut self.1.text().spans[0].text }
/// Returns a mutable reference to the BTC text in the [`AmountDisplay`].
pub fn btc(&mut self) -> &mut String { &mut self.2.2.text().spans[0].text }
}
#[derive(Debug, Component)]
struct SubText(Row, Option<Image>, Text, #[skip] bool);
impl OnEvent for SubText {}
impl SubText {
fn new(ctx: &mut Context, btc: &'static str) -> Self {
let text_size = ctx.get::<PelicanUI>().theme.fonts.size.lg;
SubText(Row::center(8.0), None, Text::new(ctx, btc, TextStyle::Secondary, text_size, Align::Left), true)
}
fn set_error(&mut self, ctx: &mut Context, err: &'static str) {
let theme = &ctx.get::<PelicanUI>().theme;
let (color, text_size) = (theme.colors.status.danger, theme.fonts.size.lg);
self.1 = Some(Icon::new(ctx, "error", color, 24.0));
self.2 = Text::new(ctx, err, TextStyle::Error, text_size, Align::Left);
}
fn set_subtext(&mut self, ctx: &mut Context, txt: &'static str) {
let text_size = ctx.get::<PelicanUI>().theme.fonts.size.lg;
self.1 = None;
self.2 = Text::new(ctx, txt, TextStyle::Secondary, text_size, Align::Left);
}
fn error(&mut self) -> &mut bool {&mut self.3}
fn _text(&mut self) -> &mut String {&mut self.2.text().spans[0].text}
}
/// A component for inputting an amount in USD and viewing BTC equivalent.
#[derive(Debug, Component)]
pub struct AmountInput(Stack, AmountInputContent);
impl OnEvent for AmountInput {}
impl AmountInput {
/// Creates a new `AmountInput` component.
///
/// # Example
/// ```
/// let mut amount_input = AmountInput::new(ctx);
/// ```
pub fn new(ctx: &mut Context) -> Self {
AmountInput (
Stack(Offset::Center, Offset::Center, Size::Fit, Size::fill(), Padding::default()),
AmountInputContent::new(ctx),
)
}
/// Returns the USD input value as a string.
pub fn usd(&mut self) -> String { self.1.1.value() }
/// Returns a mutable reference to the BTC input value.
pub fn btc(&mut self) -> &mut f32 { &mut self.1.5 }
/// Returns a mutable reference to the error flag.
pub fn error(&mut self) -> &mut bool { self.1.2.error() }
/// Sets the minimum value for the amount input.
pub fn set_min(&mut self, a: f32) { self.1.3.0 = a; }
/// Sets the maximum value for the amount input.
pub fn set_max(&mut self, a: f32) { self.1.3.1 = a; }
/// Sets the price value for the amount input.
pub fn set_price(&mut self, a: f32) { self.1.4 = a; }
}
#[derive(Debug, Component)]
struct AmountInputContent(Column, Display, SubText, #[skip] (f32, f32), #[skip] f32, #[skip] f32);
// layout, display, subtext, (min, max fee), btc_price, btc input
impl AmountInputContent {
fn new(ctx: &mut Context) -> Self {
let subtext = if !crate::config::IS_MOBILE {"Type dollar amount."} else {"0.00001234 BTC"};
AmountInputContent (
Column::new(16.0, Offset::Center, Size::Fit, Padding(16.0, 64.0, 16.0, 64.0)),
Display::new(ctx),
SubText::new(ctx, subtext),
(0.0, 0.0), 0.0, 0.0
)
}
}
impl OnEvent for AmountInputContent {
fn on_event(&mut self, ctx: &mut Context, event: &mut dyn Event) -> bool {
if let Some(KeyboardEvent{state: KeyboardState::Pressed, key}) = event.downcast_ref() {
// Get font sizes from theme
let font_size = ctx.get::<PelicanUI>().theme.fonts.size;
// Remove commas from input string
let mut t = self.1.amount().text.replace(",", "");
// Count digits (excluding dots and commas)
let mut digit_count = t.chars().filter(|ch| ch.is_ascii_digit()).count();
// Handle key input
match key {
// Handle delete or backspace when input isn't empty
Key::Named(NamedKey::Delete | NamedKey::Backspace) if !t.is_empty() => match t.as_str() {
// Do nothing if the value is "$0"
"$0" => {},
// Reset to "0" if only one character remains
_ if t.len() == 1 => t = "0".to_string(),
// Remove the last character
_ => { t.pop(); }
},
// Handle regular character input
Key::Character(c) => {
// Only process if digit count is under the 8-digit cap
if digit_count < 8 {
// Get the first character from input
let character = c.chars().next().unwrap();
// Only continue if character is a number
if character.is_ascii_digit() {
match t.find('.') {
// Allow up to 2 digits after the decimal
Some(i) if t[i + 1..].len() < 2 => t.push(character),
None => match t.as_str() {
// Replace "0" with typed digit
"0" => t = character.to_string(),
// Append digit to current value
_ => t.push(character),
},
// Do nothing if 2 decimal digits already present
_ => {}
}
} else if character == '.' && !t.contains('.') && digit_count + 2 <= 8 {
// Allow decimal if not already present and projected total digits is less than 8
t.push('.');
}
}
}
// Ignore other key types
_ => {return true}
}
// Set placeholder zeroes after the decimal point
self.1.zeros().text = match t.find('.') {
Some(i) => match t[i + 1..].len() {
0 => "00",
1 => "0",
_ => "",
},
None => "",
}.to_string();
// Recalculate digit count (excluding dot/comma)
digit_count = t.chars().filter(|ch| ch.is_ascii_digit()).count();
// Split into dollar and cent portions
let (dollars, cents) = match t.find('.') {
Some(i) => (&t[..i], Some(&t[i..])),
None => (t.as_str(), None),
};
// Format dollar portion with commas every 3 chars
let formatted_dollars: String = dollars
.chars().rev().enumerate()
.flat_map(|(i, ch)| if i > 0 && i % 3 == 0 { vec![',', ch] } else { vec![ch] })
.collect::<Vec<_>>().into_iter().rev().collect();
// Combine formatted dollars and cents into final string
let t_formatted = format!("{}{}", formatted_dollars, cents.unwrap_or(""));
// Choose font size based on total digits (including decimal placeholders)
let total_digits = digit_count + self.1.zeros().text.len();
let size = if total_digits <= 5 { font_size.title } else { font_size.h1 };
// Set final text
self.1.amount().text = t_formatted.clone();
// Apply font size and line height to amount and zeros and currency symbol
self.1.amount().font_size = size;
self.1.amount().line_height = size * 1.25;
self.1.zeros().font_size = size;
self.1.zeros().line_height = size * 1.25;
self.1.currency().font_size = size;
self.1.currency().line_height = size * 1.25;
// Parse final amount as f64 for validation
let value = t_formatted.replace(",", "").parse::<f32>().unwrap_or(0.0);
// Display subtext or error message based on parsed value
println!("VAL: {:?}", value);
match t_formatted.as_str() {
"0" | "0." | "0.0" | "0.00" if !crate::config::IS_MOBILE => {
self.2.set_subtext(ctx, "Type dollar amount."); // Prompt input
self.2.3 = true; // Disable buttons
}
_ if value < self.3.0 => {
let error = format!("${:.2} minimum.", self.3.0);
self.2.set_error(ctx, Box::leak(error.into_boxed_str())); // Exceeds min -> show error
self.2.3 = true; // Disable buttons
}
_ if value > self.3.1 => {
let error = format!("${:.2} maximum.", self.3.1);
self.2.set_error(ctx, Box::leak(error.into_boxed_str())); // Exceeds max -> show error
self.2.3 = true; // Disable buttons
}
_ => {
self.5 = value*self.4;
let amount = format!("{:.8} BTC", self.5);
self.2.set_subtext(ctx, Box::leak(amount.into_boxed_str()));
self.2.3 = false; // Enable buttons
}
}
}
true
}
}
#[derive(Debug, Component)]
struct Display(Row, Text, Text, Text);
impl OnEvent for Display {}
impl Display {
pub fn new(ctx: &mut Context) -> Self {
let theme = &ctx.get::<PelicanUI>().theme;
let font_size = theme.fonts.size.title;
let (mc, dc) = (theme.colors.text.heading, theme.colors.text.secondary);
Display (
Row::center(0.0),
Text::new(ctx, "$", TextStyle::Label(mc), font_size, Align::Left),
Text::new(ctx, "0", TextStyle::Label(mc), font_size, Align::Left),
Text::new(ctx, "", TextStyle::Label(dc), font_size, Align::Left),
)
}
pub fn value(&mut self) -> String {self.amount().text.clone()+"."+&self.zeros().text}
pub fn amount(&mut self) -> &mut Span {&mut self.2.text().spans[0]}
pub fn zeros(&mut self) -> &mut Span {&mut self.3.text().spans[0]}
pub fn currency(&mut self) -> &mut Span {&mut self.1.text().spans[0]}
}