Add text input section to tour

This commit is contained in:
Héctor Ramón Jiménez 2019-11-02 02:17:00 +01:00
parent c427ce0cec
commit f3baae9228

View file

@ -1,7 +1,7 @@
use iced::{ use iced::{
button, scrollable, slider, text::HorizontalAlignment, Align, Application, button, scrollable, slider, text::HorizontalAlignment, text_input, Align,
Background, Button, Checkbox, Color, Column, Element, Image, Justify, Application, Background, Button, Checkbox, Color, Column, Element, Image,
Length, Radio, Row, Scrollable, Slider, Text, Justify, Length, Radio, Row, Scrollable, Slider, Text, TextInput,
}; };
pub fn main() { pub fn main() {
@ -101,7 +101,7 @@ impl Application for Tour {
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone)]
pub enum Message { pub enum Message {
BackPressed, BackPressed,
NextPressed, NextPressed,
@ -139,6 +139,10 @@ impl Steps {
slider: slider::State::new(), slider: slider::State::new(),
}, },
Step::Scrollable, Step::Scrollable,
Step::TextInput {
value: String::new(),
state: text_input::State::new(),
},
Step::Debugger, Step::Debugger,
Step::End, Step::End,
], ],
@ -201,11 +205,15 @@ enum Step {
slider: slider::State, slider: slider::State,
}, },
Scrollable, Scrollable,
TextInput {
value: String,
state: text_input::State,
},
Debugger, Debugger,
End, End,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone)]
pub enum StepMessage { pub enum StepMessage {
SliderChanged(f32), SliderChanged(f32),
LayoutChanged(Layout), LayoutChanged(Layout),
@ -214,6 +222,7 @@ pub enum StepMessage {
TextColorChanged(Color), TextColorChanged(Color),
LanguageSelected(Language), LanguageSelected(Language),
ImageWidthChanged(f32), ImageWidthChanged(f32),
InputChanged(String),
DebugToggled(bool), DebugToggled(bool),
} }
@ -260,6 +269,11 @@ impl<'a> Step {
*width = new_width.round() as u16; *width = new_width.round() as u16;
} }
} }
StepMessage::InputChanged(new_value) => {
if let Step::TextInput { value, .. } = self {
*value = new_value;
}
}
}; };
} }
@ -272,6 +286,7 @@ impl<'a> Step {
Step::Image { .. } => true, Step::Image { .. } => true,
Step::RowsAndColumns { .. } => true, Step::RowsAndColumns { .. } => true,
Step::Scrollable => true, Step::Scrollable => true,
Step::TextInput { value, .. } => !value.is_empty(),
Step::Debugger => true, Step::Debugger => true,
Step::End => false, Step::End => false,
} }
@ -297,6 +312,9 @@ impl<'a> Step {
Self::rows_and_columns(*layout, spacing_slider, *spacing).into() Self::rows_and_columns(*layout, spacing_slider, *spacing).into()
} }
Step::Scrollable => Self::scrollable().into(), Step::Scrollable => Self::scrollable().into(),
Step::TextInput { value, state } => {
Self::text_input(value, state).into()
}
Step::Debugger => Self::debugger(debug).into(), Step::Debugger => Self::debugger(debug).into(),
Step::End => Self::end().into(), Step::End => Self::end().into(),
} }
@ -562,6 +580,38 @@ impl<'a> Step {
) )
} }
fn text_input(
value: &str,
state: &'a mut text_input::State,
) -> Column<'a, StepMessage> {
Self::container("Text input")
.push(Text::new(
"Use a text input to ask for different kinds of information.",
))
.push(
TextInput::new(
state,
"Type something to continue...",
value,
StepMessage::InputChanged,
)
.padding(10)
.size(30),
)
.push(Text::new(
"A text input produces a message every time it changes. It is \
very easy to keep track of its contents:",
))
.push(
Text::new(if value.is_empty() {
"You have not typed anything yet..."
} else {
value
})
.horizontal_alignment(HorizontalAlignment::Center),
)
}
fn debugger(debug: bool) -> Column<'a, StepMessage> { fn debugger(debug: bool) -> Column<'a, StepMessage> {
Self::container("Debugger") Self::container("Debugger")
.push(Text::new( .push(Text::new(