-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathdrop_down.rs
101 lines (87 loc) · 2.43 KB
/
drop_down.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
// This example demonstrates how to use the [`DropDown`] widget
//
// It was written by wiiznokes <[email protected]>
use std::fmt::Display;
use iced::{
widget::{Button, Column, Row, Text},
Element, Length,
};
use iced_aw::{drop_down, DropDown};
fn main() -> iced::Result {
iced::application(
"ContextMenu example",
DropDownExample::update,
DropDownExample::view,
)
.font(iced_fonts::REQUIRED_FONT_BYTES)
.run()
}
#[derive(Clone, Debug, Default)]
enum Choice {
#[default]
Choice1,
Choice2,
Choice3,
Choice4,
}
const CHOICES: [Choice; 4] = [
Choice::Choice1,
Choice::Choice2,
Choice::Choice3,
Choice::Choice4,
];
#[derive(Clone, Debug)]
enum Message {
Select(Choice),
Dismiss,
Expand,
}
#[derive(Default)]
struct DropDownExample {
selected: Choice,
expanded: bool,
}
impl DropDownExample {
fn update(&mut self, message: Message) {
match message {
Message::Select(choice) => {
self.selected = choice;
self.expanded = false;
}
Message::Dismiss => self.expanded = false,
Message::Expand => self.expanded = !self.expanded,
}
}
fn view(&self) -> Element<'_, Message> {
let underlay = Row::new()
.push(Text::new(format!("Selected: {}", self.selected)))
.push(Button::new(Text::new("expand")).on_press(Message::Expand));
let overlay = Column::with_children(CHOICES.map(|choice| {
Row::new()
.push(Text::new(choice.to_string()))
.push(Button::new(Text::new("choose")).on_press(Message::Select(choice)))
.into()
}));
let drop_down = DropDown::new(underlay, overlay, self.expanded)
.width(Length::Fill)
.on_dismiss(Message::Dismiss)
.alignment(drop_down::Alignment::Bottom);
Column::new()
.padding(20)
.width(Length::Fill)
.height(Length::Fill)
.align_x(iced::Alignment::Center)
.push(drop_down)
.into()
}
}
impl Display for Choice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Choice::Choice1 => write!(f, "1"),
Choice::Choice2 => write!(f, "2"),
Choice::Choice3 => write!(f, "3"),
Choice::Choice4 => write!(f, "4"),
}
}
}