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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use tuix::*;
const STYLE: &str = r#"
popup {
background-color: #d2d2d2;
}
popup textbox {
child-space: 1s;
color: black;
background-color: #d2d2d2;
}
scroll_container>.scrollbar {
background-color: #464646;
width: 10px;
}
scroll_container:enabled>.scrollbar {
width: 10px;
transition: width 0.1 0.0;
}
scroll_container:disabled>.scrollbar {
width: 0px;
transition: width 0.1 0.0;
}
"#;
#[derive(Debug, Clone, PartialEq)]
enum CustomEvent {
ChangeColor(Color),
}
#[derive(Default)]
struct Container {
popup: Entity,
textbox: Entity,
}
impl Widget for Container {
type Ret = Entity;
type Data = ();
fn on_build(&mut self, state: &mut State, container: Entity) -> Self::Ret {
Label::new("Right click for popup.").build(state, container, |builder|
builder
.set_color(Color::black())
.set_left(Pixels(10.0))
.set_top(Pixels(10.0))
);
self.popup = Popup::new()
.build(state, container, |builder| {
builder
.set_width(Pixels(100.0))
.set_height(Pixels(200.0))
});
// let scroll = ScrollContainer::new()
// .build(state, self.popup, |builder|
// builder
// //.set_top(Stretch(1.0))
// //.set_bottom(Stretch(1.0))
// //.set_space(Stretch(1.0))
// );
// let list = List::new().build(state, self.popup, |builder|
// builder
// );
let list = Column::new().build(state, self.popup, |builder| builder);
// Spacer
Element::new().build(state, list, |builder|
builder
.set_height(Pixels(5.0))
);
self.textbox = Textbox::new("Red")
// .on_release(|_, state, button| {
// button.emit(state, CustomEvent::ChangeColor(Color::rgb(255, 0, 0)));
// button.emit(state, PopupEvent::Close);
// })
.build(state, list, |builder|
builder
);
Textbox::new("Green")
// .on_release(|_, state, button| {
// button.emit(state, CustomEvent::ChangeColor(Color::rgb(0, 255, 0)));
// button.emit(state, PopupEvent::Close);
// })
.build(state, list, |builder|
builder
);
Textbox::new("Blue")
// .on_release(|_, state, button| {
// button.emit(state, CustomEvent::ChangeColor(Color::rgb(0, 0, 255)));
// button.emit(state, PopupEvent::Close);
// })
.build(state, list, |builder|
builder
);
Textbox::new("Yellow")
// .on_release(|_, state, button| {
// button.emit(state, CustomEvent::ChangeColor(Color::rgb(255, 255, 0)));
// button.emit(state, PopupEvent::Close);
// })
.build(state, list, |builder|
builder
);
Textbox::new("Fuchsia")
// .on_release(|_, state, button| {
// button.emit(state, CustomEvent::ChangeColor(Color::rgb(255, 0, 255)));
// button.emit(state, PopupEvent::Close);
// })
.build(state, list, |builder|
builder
);
Textbox::new("Aqua")
// .on_release(|_, state, button| {
// button.emit(state, CustomEvent::ChangeColor(Color::rgb(0, 255, 255)));
// button.emit(state, PopupEvent::Close);
// })
.build(state, list, |builder|
builder
);
// Spacer
Element::new().build(state, list, |builder|
builder
.set_height(Pixels(5.0))
);
container.set_background_color(state, Color::white())
}
fn on_event(&mut self, state: &mut State, container: Entity, event: &mut Event) {
if let Some(window_event) = event.message.downcast() {
match window_event {
WindowEvent::MouseUp(button) if *button == MouseButton::Right => {
container.emit_to(state, self.popup, PopupEvent::OpenAtCursor);
//state.set_focus(self.textbox);
}
_=> {}
}
}
if let Some(custom_event) = event.message.downcast() {
match custom_event {
CustomEvent::ChangeColor(color) => {
container.set_background_color(state, *color);
}
}
}
}
}
fn main() {
let app = Application::new(
WindowDescription::new()
.with_title("Popup")
.with_inner_size(300, 300),
|state, window| {
window.set_background_color(state, Color::white());
state.add_theme(STYLE);
Container::default().build(state, window, |builder| builder);
},
);
app.run();
}