summaryrefslogtreecommitdiffstats
path: root/examples/text_input/src/main.rs
blob: b25ed7e18ce43bfbb814deb75892a022f9f2cd57 (plain) (blame)
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
use iced::widget::{checkbox, column, container, text_input};
use iced::{Element, Font, Length, Sandbox, Settings};

const ICON_FONT: Font = Font::External {
    name: "Icons",
    bytes: include_bytes!("../fonts/icons.ttf"),
};

pub fn main() -> iced::Result {
    Example::run(Settings::default())
}

#[derive(Default)]
struct Example {
    value: String,
    is_showing_handle: bool,
}

#[derive(Debug, Clone)]
enum Message {
    Changed(String),
    ToggleHandle(bool),
}

impl Sandbox for Example {
    type Message = Message;

    fn new() -> Self {
        Self::default()
    }

    fn title(&self) -> String {
        String::from("Text Input - Iced")
    }

    fn update(&mut self, message: Message) {
        match message {
            Message::Changed(value) => self.value = value,
            Message::ToggleHandle(_) => {
                self.is_showing_handle = !self.is_showing_handle
            }
        }
    }

    fn view(&self) -> Element<Message> {
        let checkbox =
            checkbox("Handle", self.is_showing_handle, Message::ToggleHandle)
                .spacing(5)
                .text_size(16);

        let mut text_input =
            text_input("Placeholder", self.value.as_str(), Message::Changed);

        if self.is_showing_handle {
            text_input = text_input.handle(text_input::Handle {
                font: ICON_FONT,
                text: String::from('\u{e900}'),
                size: Some(18),
                position: text_input::HandlePosition::Right,
            });
        }

        let content = column!["What is blazing fast?", text_input, checkbox]
            .width(Length::Units(200))
            .spacing(10);

        container(content)
            .width(Length::Fill)
            .height(Length::Fill)
            .center_x()
            .center_y()
            .into()
    }

    fn theme(&self) -> iced::Theme {
        iced::Theme::default()
    }

    fn style(&self) -> iced::theme::Application {
        iced::theme::Application::default()
    }

    fn scale_factor(&self) -> f64 {
        1.0
    }

    fn run(settings: Settings<()>) -> Result<(), iced::Error>
    where
        Self: 'static + Sized,
    {
        <Self as iced::Application>::run(settings)
    }
}