aboutsummaryrefslogtreecommitdiff
path: root/examples/name/src/main.rs
blob: deed1368ab84185f6ccb4a50efaac6eba60c009b (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use iced::advanced::text::Wrapping;
use iced::widget::operation::focus_next;
use iced::widget::{center, column, text_editor};
use iced::{Center, Element, Task};
use iced_selection::text;

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

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

#[derive(Debug, Clone)]
enum Message {
    UpdateText(text_editor::Action),
}

impl State {
    fn new() -> (Self, Task<Message>) {
        (Self::default(), focus_next())
    }

    fn update(&mut self, message: Message) {
        match message {
            Message::UpdateText(action) => {
                let is_edit = action.is_edit();

                self.content.perform(action);

                if is_edit {
                    self.name = self.content.text();
                }
            }
        };
    }

    fn view(&self) -> Element<'_, Message> {
        center(
            column![
                text!("Hello {}", &self.name).wrapping(Wrapping::None),
                text_editor(&self.content).on_action(Message::UpdateText)
            ]
            .spacing(10)
            .align_x(Center),
        )
        .into()
    }
}