forked from iced-rs/iced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
104 lines (91 loc) · 2.35 KB
/
main.rs
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
94
95
96
97
98
99
100
101
102
103
104
use iced::widget::{column, container, pick_list, scrollable, vertical_space};
use iced::{Alignment, Element, Length, Sandbox, Settings};
pub fn main() -> iced::Result {
Example::run(Settings::default())
}
#[derive(Default)]
struct Example {
selected_language: Option<Language>,
}
#[derive(Debug, Clone, Copy)]
enum Message {
LanguageSelected(Language),
}
impl Sandbox for Example {
type Message = Message;
fn new() -> Self {
Self::default()
}
fn title(&self) -> String {
String::from("Pick list - Iced")
}
fn update(&mut self, message: Message) {
match message {
Message::LanguageSelected(language) => {
self.selected_language = Some(language);
}
}
}
fn view(&self) -> Element<Message> {
let pick_list = pick_list(
&Language::ALL[..],
self.selected_language,
Message::LanguageSelected,
)
.placeholder("Choose a language...");
let content = column![
vertical_space(600),
"Which is your favorite language?",
pick_list,
vertical_space(600),
]
.width(Length::Fill)
.align_items(Alignment::Center)
.spacing(10);
container(scrollable(content))
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Language {
#[default]
Rust,
Elm,
Ruby,
Haskell,
C,
Javascript,
Other,
}
impl Language {
const ALL: [Language; 7] = [
Language::C,
Language::Elm,
Language::Ruby,
Language::Haskell,
Language::Rust,
Language::Javascript,
Language::Other,
];
}
impl std::fmt::Display for Language {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Language::Rust => "Rust",
Language::Elm => "Elm",
Language::Ruby => "Ruby",
Language::Haskell => "Haskell",
Language::C => "C",
Language::Javascript => "Javascript",
Language::Other => "Some other language",
}
)
}
}