2021-01-25 19:52:47 +00:00
|
|
|
use egui::*;
|
|
|
|
|
2021-09-29 06:45:13 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
#[cfg_attr(feature = "serde", serde(default))]
|
2021-01-25 19:52:47 +00:00
|
|
|
pub struct LayoutTest {
|
|
|
|
// Identical to contents of `egui::Layout`
|
2021-09-07 18:37:50 +00:00
|
|
|
layout: LayoutSettings,
|
2021-01-25 19:52:47 +00:00
|
|
|
|
|
|
|
// Extra for testing wrapping:
|
|
|
|
wrap_column_width: f32,
|
|
|
|
wrap_row_height: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for LayoutTest {
|
|
|
|
fn default() -> Self {
|
2021-09-07 18:37:50 +00:00
|
|
|
Self {
|
|
|
|
layout: LayoutSettings::top_down(),
|
|
|
|
wrap_column_width: 150.0,
|
|
|
|
wrap_row_height: 20.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
2021-09-29 06:45:13 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
#[cfg_attr(feature = "serde", serde(default))]
|
2021-09-07 18:37:50 +00:00
|
|
|
pub struct LayoutSettings {
|
|
|
|
// Similar to the contents of `egui::Layout`
|
|
|
|
main_dir: Direction,
|
|
|
|
main_wrap: bool,
|
|
|
|
cross_align: Align,
|
|
|
|
cross_justify: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for LayoutSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::top_down()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LayoutSettings {
|
|
|
|
fn top_down() -> Self {
|
2021-01-25 19:52:47 +00:00
|
|
|
Self {
|
|
|
|
main_dir: Direction::TopDown,
|
|
|
|
main_wrap: false,
|
|
|
|
cross_align: Align::Min,
|
|
|
|
cross_justify: false,
|
|
|
|
}
|
|
|
|
}
|
2021-09-07 18:37:50 +00:00
|
|
|
fn top_down_justified_centered() -> Self {
|
|
|
|
Self {
|
|
|
|
main_dir: Direction::TopDown,
|
|
|
|
main_wrap: false,
|
|
|
|
cross_align: Align::Center,
|
|
|
|
cross_justify: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn horizontal_wrapped() -> Self {
|
|
|
|
Self {
|
|
|
|
main_dir: Direction::LeftToRight,
|
|
|
|
main_wrap: true,
|
|
|
|
cross_align: Align::Center,
|
|
|
|
cross_justify: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&self) -> Layout {
|
|
|
|
Layout::from_main_dir_and_cross_align(self.main_dir, self.cross_align)
|
|
|
|
.with_main_wrap(self.main_wrap)
|
|
|
|
.with_cross_justify(self.cross_justify)
|
|
|
|
}
|
2021-01-25 19:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl super::Demo for LayoutTest {
|
2021-02-20 08:18:23 +00:00
|
|
|
fn name(&self) -> &'static str {
|
2021-02-06 15:54:38 +00:00
|
|
|
"Layout Test"
|
2021-01-25 19:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn show(&mut self, ctx: &egui::CtxRef, open: &mut bool) {
|
|
|
|
egui::Window::new(self.name())
|
|
|
|
.open(open)
|
|
|
|
.resizable(false)
|
|
|
|
.show(ctx, |ui| {
|
|
|
|
use super::View;
|
|
|
|
self.ui(ui);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl super::View for LayoutTest {
|
|
|
|
fn ui(&mut self, ui: &mut Ui) {
|
|
|
|
ui.label("Tests and demonstrates the limits of the egui layouts");
|
|
|
|
self.content_ui(ui);
|
|
|
|
Resize::default()
|
2021-09-07 18:37:50 +00:00
|
|
|
.default_size([150.0, 200.0])
|
2021-01-25 19:52:47 +00:00
|
|
|
.show(ui, |ui| {
|
2021-09-07 18:37:50 +00:00
|
|
|
if self.layout.main_wrap {
|
|
|
|
if self.layout.main_dir.is_horizontal() {
|
2021-01-25 19:52:47 +00:00
|
|
|
ui.allocate_ui(
|
2021-08-28 14:02:16 +00:00
|
|
|
vec2(ui.available_size_before_wrap().x, self.wrap_row_height),
|
2021-09-07 18:37:50 +00:00
|
|
|
|ui| ui.with_layout(self.layout.layout(), demo_ui),
|
2021-01-25 19:52:47 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
ui.allocate_ui(
|
2021-08-28 14:02:16 +00:00
|
|
|
vec2(self.wrap_column_width, ui.available_size_before_wrap().y),
|
2021-09-07 18:37:50 +00:00
|
|
|
|ui| ui.with_layout(self.layout.layout(), demo_ui),
|
2021-01-25 19:52:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-07 18:37:50 +00:00
|
|
|
ui.with_layout(self.layout.layout(), demo_ui);
|
2021-01-25 19:52:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
ui.label("Resize to see effect");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LayoutTest {
|
|
|
|
pub fn content_ui(&mut self, ui: &mut Ui) {
|
|
|
|
ui.horizontal(|ui| {
|
2021-09-07 18:37:50 +00:00
|
|
|
ui.selectable_value(&mut self.layout, LayoutSettings::top_down(), "Top-down");
|
|
|
|
ui.selectable_value(
|
|
|
|
&mut self.layout,
|
|
|
|
LayoutSettings::top_down_justified_centered(),
|
|
|
|
"Top-down, centered and justified",
|
|
|
|
);
|
|
|
|
ui.selectable_value(
|
|
|
|
&mut self.layout,
|
|
|
|
LayoutSettings::horizontal_wrapped(),
|
|
|
|
"Horizontal wrapped",
|
|
|
|
);
|
2021-01-25 19:52:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ui.horizontal(|ui| {
|
|
|
|
ui.label("Main Direction:");
|
|
|
|
for &dir in &[
|
|
|
|
Direction::LeftToRight,
|
|
|
|
Direction::RightToLeft,
|
|
|
|
Direction::TopDown,
|
|
|
|
Direction::BottomUp,
|
|
|
|
] {
|
2021-09-07 18:37:50 +00:00
|
|
|
ui.radio_value(&mut self.layout.main_dir, dir, format!("{:?}", dir));
|
2021-01-25 19:52:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ui.horizontal(|ui| {
|
2021-09-07 18:37:50 +00:00
|
|
|
ui.checkbox(&mut self.layout.main_wrap, "Main wrap")
|
2021-01-25 19:52:47 +00:00
|
|
|
.on_hover_text("Wrap when next widget doesn't fit the current row/column");
|
|
|
|
|
2021-09-07 18:37:50 +00:00
|
|
|
if self.layout.main_wrap {
|
|
|
|
if self.layout.main_dir.is_horizontal() {
|
2021-03-27 15:07:18 +00:00
|
|
|
ui.add(Slider::new(&mut self.wrap_row_height, 0.0..=200.0).text("Row height"));
|
2021-01-25 19:52:47 +00:00
|
|
|
} else {
|
|
|
|
ui.add(
|
2021-03-27 15:07:18 +00:00
|
|
|
Slider::new(&mut self.wrap_column_width, 0.0..=200.0).text("Column width"),
|
2021-01-25 19:52:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ui.horizontal(|ui| {
|
|
|
|
ui.label("Cross Align:");
|
|
|
|
for &align in &[Align::Min, Align::Center, Align::Max] {
|
2021-09-07 18:37:50 +00:00
|
|
|
ui.radio_value(&mut self.layout.cross_align, align, format!("{:?}", align));
|
2021-01-25 19:52:47 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-07 18:37:50 +00:00
|
|
|
ui.checkbox(&mut self.layout.cross_justify, "Cross Justified")
|
2021-01-25 19:52:47 +00:00
|
|
|
.on_hover_text("Try to fill full width/height (e.g. buttons)");
|
|
|
|
}
|
2021-03-23 18:53:31 +00:00
|
|
|
}
|
2021-01-25 19:52:47 +00:00
|
|
|
|
2021-03-23 18:53:31 +00:00
|
|
|
fn demo_ui(ui: &mut Ui) {
|
2021-09-07 18:37:50 +00:00
|
|
|
ui.add(egui::Label::new("Wrapping text followed by example widgets:").wrap(true));
|
|
|
|
let mut dummy = false;
|
|
|
|
ui.checkbox(&mut dummy, "checkbox");
|
|
|
|
ui.radio_value(&mut dummy, false, "radio");
|
|
|
|
let _ = ui.button("button");
|
2021-01-25 19:52:47 +00:00
|
|
|
}
|