aboutsummaryrefslogtreecommitdiff
path: root/examples/name/src/main.rs
blob: 2a733d5dbe57b03d648a56743d3b0ba1999c93f6 (plain)
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
use iced::widget::{center, column, text_input};
use iced::{Center, Element};
use iced_selection::text;

fn main() -> iced::Result {
    iced::run(State::update, State::view)
}

#[derive(Default)]
struct State {
    name: String,
}

#[derive(Debug, Clone)]
enum Message {
    UpdateText(String),
}

impl State {
    fn update(&mut self, message: Message) {
        match message {
            Message::UpdateText(name) => self.name = name,
        };
    }

    fn view(&self) -> Element<'_, Message> {
        center(
            column![
                text!("Hello {}", &self.name),
                text_input("Type your name here...", &self.name)
                    .on_input(Message::UpdateText)
            ]
            .spacing(10)
            .align_x(Center),
        )
        .into()
    }
}