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
use egui::{Button, Checkbox, Color32, Frame, Label, ScrollArea, Slider, TextEdit, Widget};
use egui_flex::{Flex, FlexAlign, FlexItem};
use hello_egui_utils_dev::run;
use std::num::NonZeroUsize;
#[allow(clippy::too_many_lines)]
fn main() {
run!(|ui| {
ScrollArea::vertical().show(ui, |ui| {
let available_rect = ui.max_rect();
for _ in 0..50 {
// The scope and the set max height is *essential* to get egui_flex work well
// in a scroll area. Since egui_flex checks the available space and limits each items
// size to that, we need to give it some space to work with.
ui.scope(|ui| {
ui.set_max_height(available_rect.height());
ui.label(format!("Item {:?}", ui.id()));
ui.ctx().debug_painter().debug_rect(
ui.available_rect_before_wrap(),
Color32::RED,
"",
);
ui.ctx().options_mut(|opts| {
opts.max_passes = NonZeroUsize::new(1).unwrap();
});
ui.horizontal_top(|ui| {
let items = vec![
"I",
"can have veeeeeeeeeeeeery long",
"and",
"very",
"short",
"and",
"multi\nline",
"or\neven\nmore\nlines\n\n\nhi",
"and",
"even",
"some middle length",
"items",
];
Flex::new()
.w_full()
.align_items(egui_flex::FlexAlign::Stretch)
.align_items_content(egui::Align2::CENTER_CENTER)
.wrap(true)
.show(ui, |flex| {
flex.add_ui(
FlexItem::default()
.grow(1.0)
.frame(Frame::group(flex.ui().style())),
|ui| {
ui.label("Hello");
},
);
for item in items {
flex.add_ui(
FlexItem::default()
.grow(1.0)
.frame(Frame::group(flex.ui().style())),
|ui| {
Label::new(item).wrap().ui(ui);
},
);
}
flex.add_ui(
FlexItem::default()
.grow(1.0)
.basis(200.0)
.frame(Frame::group(flex.ui().style())),
|ui| {
TextEdit::singleline(&mut String::new())
.desired_width(ui.available_width())
.ui(ui);
},
);
flex.add_ui(
FlexItem::default()
.grow(1.0)
.basis(80.0)
.frame(Frame::group(flex.ui().style())),
|ui| {
ui.add(Label::new("I have flex basis 80").wrap());
},
);
for align in &[
FlexAlign::Start,
FlexAlign::End,
FlexAlign::Center,
FlexAlign::Stretch,
] {
flex.add_ui(
FlexItem::default()
.grow(1.0)
.align_self(*align)
.frame(Frame::group(flex.ui().style())),
|ui| {
ui.add(
Label::new(format!("I have align-self: {align:?}"))
.wrap(),
);
},
);
}
flex.add_ui(FlexItem::new().grow(1.0).basis(150.0), |ui| {
ui.style_mut().spacing.slider_width =
ui.available_width() - 50.0;
Slider::new(&mut 0.0, 0.0..=1000.0).ui(ui);
});
flex.add_flex(
FlexItem::default()
.grow(1.0)
.frame(egui::Frame::group(flex.ui().style())),
Flex::vertical()
.align_content(egui_flex::FlexAlignContent::Stretch)
.grow_items(1.0),
|flex| {
flex.add(FlexItem::default().grow(1.0), Button::new("btn"));
flex.add(
FlexItem::default(),
Button::new("Very long button"),
);
flex.add_flex(
FlexItem::default().grow(1.0),
Flex::horizontal()
.align_content(egui_flex::FlexAlignContent::Stretch)
.grow_items(1.0),
|flex| {
flex.add(
FlexItem::default().grow(1.0),
Button::new("btn"),
);
flex.add(
FlexItem::default(),
Button::new("Very long button"),
);
},
);
},
);
flex.add(
FlexItem::new().grow(1.0),
Button::new("Very long button"),
);
flex.add(FlexItem::new().grow(1.0), Button::new("Button"));
flex.add(
FlexItem::new().grow(1.0),
Button::new("Button wefoijfgiweopjg"),
);
flex.add(FlexItem::new().grow(1.0), Button::new("Button"));
flex.add(FlexItem::new(), Button::new("Simple Button"));
flex.add(FlexItem::new(), Checkbox::new(&mut false, "Checkbox"));
});
});
});
}
});
});
}