Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-core/src/ui_components/text_input.rs
1use crate::elements::{ChildView, Clipped};
2use crate::{
3 elements::{Border, ConstrainedBox, Container, Element},
4 ui_components::components::{UiComponent, UiComponentStyles},
5 View, ViewHandle,
6};
7 
8pub struct TextInput<T: View> {
9 editor: ViewHandle<T>,
10 styles: UiComponentStyles,
11}
12 
13impl<T: View> UiComponent for TextInput<T> {
14 type ElementType = ConstrainedBox;
15 fn build(self) -> ConstrainedBox {
16 self.render_text_input()
17 }
18 
19 fn with_style(self, styles: UiComponentStyles) -> Self {
20 TextInput {
21 editor: self.editor,
22 styles: self.styles.merge(styles),
23 }
24 }
25}
26 
27impl<T: View> TextInput<T> {
28 pub fn new(editor: ViewHandle<T>, default_styles: UiComponentStyles) -> Self {
29 TextInput {
30 editor,
31 styles: default_styles,
32 }
33 }
34 
35 fn render_text_input(&self) -> ConstrainedBox {
36 let styles = self.styles;
37 
38 let mut container =
39 Container::new(Clipped::new(ChildView::new(&self.editor).finish()).finish());
40 // Setting up the border
41 if let Some(corner) = styles.border_radius {
42 container = container.with_corner_radius(corner);
43 }
44 
45 let mut border = Border::all(styles.border_width.unwrap_or_default());
46 if let Some(border_color) = styles.border_color {
47 border = border.with_border_fill(border_color);
48 }
49 container = container.with_border(border);
50 
51 // Position-related settings
52 if let Some(padding) = styles.padding {
53 container = container
54 .with_padding_left(padding.left)
55 .with_padding_top(padding.top)
56 .with_padding_right(padding.right)
57 .with_padding_bottom(padding.bottom);
58 }
59 if let Some(margin) = styles.margin {
60 container = container
61 .with_margin_left(margin.left)
62 .with_margin_top(margin.top)
63 .with_margin_right(margin.right)
64 .with_margin_bottom(margin.bottom);
65 }
66 
67 if let Some(background) = styles.background {
68 container = container.with_background(background);
69 }
70 
71 match (styles.height, styles.width) {
72 (None, None) => ConstrainedBox::new(container.finish()),
73 (_, _) => {
74 let mut constrained_box = ConstrainedBox::new(container.finish());
75 if let Some(height) = styles.height {
76 constrained_box = constrained_box.with_height(height);
77 }
78 if let Some(width) = styles.width {
79 constrained_box = constrained_box.with_width(width);
80 }
81 constrained_box
82 }
83 }
84 }
85}
86