radio/
radio.rs

1use iced_native::{Color, Column, Container, Element, Length, Radio, Text};
2use iced_pancurses::{PancursesRenderer, Sandbox};
3
4struct MyState {
5    selected_color: ExampleColor,
6}
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum ExampleColor {
10    White,
11    Yellow,
12    Blue,
13    Red,
14}
15
16impl ExampleColor {
17    pub fn as_iced_color(&self) -> Color {
18        match self {
19            ExampleColor::White => Color::WHITE,
20            ExampleColor::Yellow => Color {
21                r: 1.,
22                g: 1.,
23                b: 0.,
24                a: 1.,
25            },
26            ExampleColor::Blue => Color {
27                r: 0.,
28                g: 0.,
29                b: 1.,
30                a: 1.,
31            },
32            ExampleColor::Red => Color {
33                r: 1.,
34                g: 0.,
35                b: 0.,
36                a: 1.,
37            },
38        }
39    }
40}
41
42impl Sandbox for MyState {
43    type Message = MyMessage;
44
45    fn view(&mut self) -> Element<'_, MyMessage, PancursesRenderer> {
46        Container::new(
47            Column::new()
48                .spacing(1)
49                .push(
50                    Text::new("Colored text")
51                        .width(Length::Shrink)
52                        .color(self.selected_color.as_iced_color()),
53                )
54                .push(
55                    Column::new()
56                        .push(Radio::new(
57                            ExampleColor::White,
58                            "White",
59                            Some(self.selected_color),
60                            |_| MyMessage::SelectColor(ExampleColor::White),
61                        ))
62                        .push(Radio::new(
63                            ExampleColor::Yellow,
64                            "Yellow",
65                            Some(self.selected_color),
66                            |_| MyMessage::SelectColor(ExampleColor::Yellow),
67                        ))
68                        .push(Radio::new(
69                            ExampleColor::Blue,
70                            "Blue",
71                            Some(self.selected_color),
72                            |_| MyMessage::SelectColor(ExampleColor::Blue),
73                        ))
74                        .push(Radio::new(
75                            ExampleColor::Red,
76                            "Red",
77                            Some(self.selected_color),
78                            |_| MyMessage::SelectColor(ExampleColor::Red),
79                        )),
80                ),
81        )
82        .width(Length::Fill)
83        .height(Length::Fill)
84        .center_x()
85        .center_y()
86        .into()
87    }
88
89    fn new() -> Self {
90        MyState {
91            selected_color: ExampleColor::White,
92        }
93    }
94
95    fn update(&mut self, messages: Vec<MyMessage>) {
96        messages.into_iter().for_each(|m| match m {
97            MyMessage::SelectColor(c) => self.selected_color = c,
98        });
99    }
100}
101
102#[derive(Debug, Clone, Copy)]
103pub enum MyMessage {
104    SelectColor(ExampleColor),
105}
106
107fn main() {
108    MyState::run()
109}