1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 14:50:03 -04:00

More cleanup

This commit is contained in:
Konkitoman
2023-08-04 10:45:52 +03:00
parent 172be33b40
commit fbcb26827c
14 changed files with 268 additions and 295 deletions

View File

@@ -1,4 +1,5 @@
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))] #[cfg_attr(feature = "serde", serde(default))]
pub struct CodeEditor { pub struct CodeEditor {
@@ -36,7 +37,7 @@ impl super::Demo for CodeEditor {
impl super::View for CodeEditor { impl super::View for CodeEditor {
fn ui(&mut self, ui: &mut egui::Ui) { fn ui(&mut self, ui: &mut egui::Ui) {
let CodeEditor { language, code } = self; let Self { language, code } = self;
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.set_height(0.0); ui.set_height(0.0);

View File

@@ -16,7 +16,7 @@ impl super::Demo for DancingStrings {
.open(open) .open(open)
.default_size(vec2(512.0, 256.0)) .default_size(vec2(512.0, 256.0))
.vscroll(false) .vscroll(false)
.show(ctx, |ui| Self::default().ui(ui)); .show(ctx, |ui| self.ui(ui));
} }
} }

View File

@@ -1,7 +1,5 @@
use egui::{Context, Modifiers, ScrollArea, Ui}; use egui::{Context, Modifiers, ScrollArea, Ui};
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::sync::Arc;
use std::sync::RwLock;
use super::About; use super::About;
use super::Demo; use super::Demo;
@@ -14,7 +12,7 @@ use crate::is_mobile;
#[cfg_attr(feature = "serde", serde(default))] #[cfg_attr(feature = "serde", serde(default))]
struct Demos { struct Demos {
#[cfg_attr(feature = "serde", serde(skip))] #[cfg_attr(feature = "serde", serde(skip))]
demos: Vec<Box<dyn Demo + Sync + Send>>, demos: Vec<Box<dyn Demo>>,
open: BTreeSet<String>, open: BTreeSet<String>,
} }
@@ -47,7 +45,7 @@ impl Default for Demos {
} }
impl Demos { impl Demos {
pub fn from_demos(demos: Vec<Box<dyn Demo + Sync + Send>>) -> Self { pub fn from_demos(demos: Vec<Box<dyn Demo>>) -> Self {
let mut open = BTreeSet::new(); let mut open = BTreeSet::new();
open.insert( open.insert(
super::widget_gallery::WidgetGallery::default() super::widget_gallery::WidgetGallery::default()
@@ -151,20 +149,14 @@ fn set_open(open: &mut BTreeSet<String>, key: &'static str, is_open: bool) {
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))] #[cfg_attr(feature = "serde", serde(default))]
pub struct DemoWindowsData { pub struct DemoWindows {
about_is_open: bool, about_is_open: bool,
about: About, about: About,
demos: Demos, demos: Demos,
tests: Tests, tests: Tests,
} }
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
#[derive(Clone, Default)]
pub struct DemoWindows {
data: Arc<RwLock<DemoWindowsData>>,
}
impl Default for DemoWindowsData { impl Default for DemoWindows {
fn default() -> Self { fn default() -> Self {
Self { Self {
about_is_open: true, about_is_open: true,
@@ -186,36 +178,32 @@ impl DemoWindows {
} }
fn mobile_ui(&mut self, ctx: &Context) { fn mobile_ui(&mut self, ctx: &Context) {
let mut about_is_open = self.data.read().unwrap().about_is_open; if self.about_is_open {
if about_is_open {
let screen_size = ctx.input(|i| i.screen_rect.size()); let screen_size = ctx.input(|i| i.screen_rect.size());
let default_width = (screen_size.x - 20.0).min(400.0); let default_width = (screen_size.x - 20.0).min(400.0);
let close = Arc::new(RwLock::new(false)); let mut close = false;
let close_ = close.clone(); egui::Window::new(self.about.name())
let clone = self.clone();
egui::Window::new(self.data.read().unwrap().about.name())
.anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0]) .anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0])
.default_width(default_width) .default_width(default_width)
.default_height(ctx.available_rect().height() - 46.0) .default_height(ctx.available_rect().height() - 46.0)
.vscroll(true) .vscroll(true)
.open(&mut about_is_open) .open(&mut self.about_is_open)
.resizable(false) .resizable(false)
.collapsible(false) .collapsible(false)
.show(ctx, |ui| { .show(ctx, |ui| {
let close = close.clone(); self.about.ui(ui);
clone.data.write().unwrap().about.ui(ui);
ui.add_space(12.0); ui.add_space(12.0);
ui.vertical_centered_justified(|ui| { ui.vertical_centered_justified(|ui| {
if ui if ui
.button(egui::RichText::new("Continue to the demo!").size(20.0)) .button(egui::RichText::new("Continue to the demo!").size(20.0))
.clicked() .clicked()
{ {
*close.write().unwrap() = true; close = true;
} }
}); });
}); });
self.data.write().unwrap().about_is_open &= !*close_.read().unwrap(); self.about_is_open &= !close;
} else { } else {
self.mobile_top_bar(ctx); self.mobile_top_bar(ctx);
self.show_windows(ctx); self.show_windows(ctx);
@@ -288,22 +276,20 @@ impl DemoWindows {
/// Show the open windows. /// Show the open windows.
fn show_windows(&mut self, ctx: &Context) { fn show_windows(&mut self, ctx: &Context) {
let data = &mut *self.data.write().unwrap(); self.about.show(ctx, &mut self.about_is_open);
data.about.show(ctx, &mut data.about_is_open); self.demos.windows(ctx);
data.demos.windows(ctx); self.tests.windows(ctx);
data.tests.windows(ctx);
} }
fn demo_list_ui(&mut self, ui: &mut egui::Ui) { fn demo_list_ui(&mut self, ui: &mut egui::Ui) {
ScrollArea::vertical().show(ui, |ui| { ScrollArea::vertical().show(ui, |ui| {
ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| { ui.with_layout(egui::Layout::top_down_justified(egui::Align::LEFT), |ui| {
let data = &mut *self.data.write().unwrap(); ui.toggle_value(&mut self.about_is_open, self.about.name());
ui.toggle_value(&mut data.about_is_open, data.about.name());
ui.separator(); ui.separator();
data.demos.checkboxes(ui); self.demos.checkboxes(ui);
ui.separator(); ui.separator();
data.tests.checkboxes(ui); self.tests.checkboxes(ui);
ui.separator(); ui.separator();
if ui.button("Organize windows").clicked() { if ui.button("Organize windows").clicked() {

View File

@@ -120,9 +120,8 @@ impl super::View for DragAndDropDemo {
let id_source = "my_drag_and_drop_demo"; let id_source = "my_drag_and_drop_demo";
let mut source_col_row = None; let mut source_col_row = None;
let mut drop_col = None; let mut drop_col = None;
let columns = &mut self.columns; ui.columns(self.columns.len(), |uis| {
ui.columns(columns.len(), |uis| { for (col_idx, column) in self.columns.clone().into_iter().enumerate() {
for (col_idx, column) in columns.clone().into_iter().enumerate() {
let ui = &mut uis[col_idx]; let ui = &mut uis[col_idx];
let can_accept_what_is_being_dragged = true; // We accept anything being dragged (for now) ¯\_(ツ)_/¯ let can_accept_what_is_being_dragged = true; // We accept anything being dragged (for now) ¯\_(ツ)_/¯
let response = drop_target(ui, can_accept_what_is_being_dragged, |ui| { let response = drop_target(ui, can_accept_what_is_being_dragged, |ui| {
@@ -133,7 +132,7 @@ impl super::View for DragAndDropDemo {
let response = ui.add(Label::new(item).sense(Sense::click())); let response = ui.add(Label::new(item).sense(Sense::click()));
response.context_menu(|ui| { response.context_menu(|ui| {
if ui.button("Remove").clicked() { if ui.button("Remove").clicked() {
columns[col_idx].remove(row_idx); self.columns[col_idx].remove(row_idx);
ui.close_menu(); ui.close_menu();
} }
}); });
@@ -148,7 +147,7 @@ impl super::View for DragAndDropDemo {
let response = response.context_menu(|ui| { let response = response.context_menu(|ui| {
if ui.button("New Item").clicked() { if ui.button("New Item").clicked() {
columns[col_idx].push("New Item".to_owned()); self.columns[col_idx].push("New Item".to_owned());
ui.close_menu(); ui.close_menu();
} }
}); });
@@ -164,8 +163,8 @@ impl super::View for DragAndDropDemo {
if let Some(drop_col) = drop_col { if let Some(drop_col) = drop_col {
if ui.input(|i| i.pointer.any_released()) { if ui.input(|i| i.pointer.any_released()) {
// do the drop: // do the drop:
let item = columns[source_col].remove(source_row); let item = self.columns[source_col].remove(source_row);
columns[drop_col].push(item); self.columns[drop_col].push(item);
} }
} }
} }

View File

@@ -1,4 +1,5 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
pub struct FontBook { pub struct FontBook {
filter: String, filter: String,
font_id: egui::FontId, font_id: egui::FontId,

View File

@@ -14,7 +14,7 @@ impl super::Demo for Highlighting {
.open(open) .open(open)
.show(ctx, |ui| { .show(ctx, |ui| {
use super::View as _; use super::View as _;
Self::default().ui(ui); self.ui(ui);
}); });
} }
} }

View File

@@ -30,7 +30,8 @@ impl Default for Panel {
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#[derive(Default, PartialEq)]
#[derive(PartialEq, Default)]
pub struct PlotDemo { pub struct PlotDemo {
line_demo: LineDemo, line_demo: LineDemo,
marker_demo: MarkerDemo, marker_demo: MarkerDemo,

View File

@@ -55,8 +55,7 @@ impl super::Demo for Sliders {
impl super::View for Sliders { impl super::View for Sliders {
fn ui(&mut self, ui: &mut Ui) { fn ui(&mut self, ui: &mut Ui) {
{ let Self {
let Sliders {
min, min,
max, max,
logarithmic, logarithmic,
@@ -179,16 +178,13 @@ impl super::View for Sliders {
ui.add_space(8.0); ui.add_space(8.0);
ui.checkbox(clamp_to_range, "Clamp to range"); ui.checkbox(clamp_to_range, "Clamp to range");
ui.label( ui.label("If true, the slider will clamp incoming and outgoing values to the given range.");
"If true, the slider will clamp incoming and outgoing values to the given range.",
);
ui.label("If false, the slider can shows values outside its range, and you can manually enter values outside the range."); ui.label("If false, the slider can shows values outside its range, and you can manually enter values outside the range.");
ui.add_space(8.0); ui.add_space(8.0);
ui.checkbox(smart_aim, "Smart Aim"); ui.checkbox(smart_aim, "Smart Aim");
ui.label("Smart Aim will guide you towards round values when you drag the slider so you you are more likely to hit 250 than 247.23"); ui.label("Smart Aim will guide you towards round values when you drag the slider so you you are more likely to hit 250 than 247.23");
ui.add_space(8.0); ui.add_space(8.0);
}
ui.vertical_centered(|ui| { ui.vertical_centered(|ui| {
egui::reset_button(ui, self); egui::reset_button(ui, self);

View File

@@ -18,7 +18,7 @@ impl super::Demo for StripDemo {
.default_width(400.0) .default_width(400.0)
.show(ctx, |ui| { .show(ctx, |ui| {
use super::View as _; use super::View as _;
Self::default().ui(ui); self.ui(ui);
}); });
} }
} }

View File

@@ -51,7 +51,6 @@ const NUM_MANUAL_ROWS: usize = 20;
impl super::View for TableDemo { impl super::View for TableDemo {
fn ui(&mut self, ui: &mut egui::Ui) { fn ui(&mut self, ui: &mut egui::Ui) {
{
ui.vertical(|ui| { ui.vertical(|ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.checkbox(&mut self.striped, "Striped"); ui.checkbox(&mut self.striped, "Striped");
@@ -98,7 +97,6 @@ impl super::View for TableDemo {
}); });
ui.separator(); ui.separator();
}
// Leave room for the source code link after the table demo: // Leave room for the source code link after the table demo:
use egui_extras::{Size, StripBuilder}; use egui_extras::{Size, StripBuilder};

View File

@@ -1,5 +1,3 @@
use std::sync::{Arc, RwLock};
#[derive(Default)] #[derive(Default)]
pub struct CursorTest {} pub struct CursorTest {}
@@ -11,7 +9,7 @@ impl super::Demo for CursorTest {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) { fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name()).open(open).show(ctx, |ui| { egui::Window::new(self.name()).open(open).show(ctx, |ui| {
use super::View as _; use super::View as _;
Self::default().ui(ui); self.ui(ui);
}); });
} }
} }
@@ -43,7 +41,7 @@ impl super::Demo for IdTest {
fn show(&mut self, ctx: &egui::Context, open: &mut bool) { fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name()).open(open).show(ctx, |ui| { egui::Window::new(self.name()).open(open).show(ctx, |ui| {
use super::View as _; use super::View as _;
Self::default().ui(ui); self.ui(ui);
}); });
} }
} }
@@ -180,6 +178,7 @@ impl super::View for ManualLayoutTest {
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#[derive(PartialEq)] #[derive(PartialEq)]
pub struct TableTest { pub struct TableTest {
num_cols: usize, num_cols: usize,
@@ -216,14 +215,11 @@ impl super::Demo for TableTest {
impl super::View for TableTest { impl super::View for TableTest {
fn ui(&mut self, ui: &mut egui::Ui) { fn ui(&mut self, ui: &mut egui::Ui) {
{
ui.add( ui.add(
egui::Slider::new(&mut self.min_col_width, 0.0..=400.0) egui::Slider::new(&mut self.min_col_width, 0.0..=400.0).text("Minimum column width"),
.text("Minimum column width"),
); );
ui.add( ui.add(
egui::Slider::new(&mut self.max_col_width, 0.0..=400.0) egui::Slider::new(&mut self.max_col_width, 0.0..=400.0).text("Maximum column width"),
.text("Maximum column width"),
); );
ui.add(egui::Slider::new(&mut self.num_cols, 0..=5).text("Columns")); ui.add(egui::Slider::new(&mut self.num_cols, 0..=5).text("Columns"));
ui.add(egui::Slider::new(&mut self.num_rows, 0..=20).text("Rows")); ui.add(egui::Slider::new(&mut self.num_rows, 0..=20).text("Rows"));
@@ -300,7 +296,6 @@ impl super::View for TableTest {
ui.label("Fifth row, second column"); ui.label("Fifth row, second column");
ui.end_row(); ui.end_row();
}); });
}
ui.vertical_centered(|ui| { ui.vertical_centered(|ui| {
egui::reset_button(ui, self); egui::reset_button(ui, self);
@@ -383,15 +378,14 @@ impl super::View for InputTest {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#[derive(Clone)]
pub struct WindowResizeTest { pub struct WindowResizeTest {
text: Arc<RwLock<String>>, text: String,
} }
impl Default for WindowResizeTest { impl Default for WindowResizeTest {
fn default() -> Self { fn default() -> Self {
Self { Self {
text: Arc::new(RwLock::new(crate::LOREM_IPSUM_LONG.to_owned())), text: crate::LOREM_IPSUM_LONG.to_owned(),
} }
} }
} }
@@ -457,8 +451,6 @@ impl super::Demo for WindowResizeTest {
lorem_ipsum(ui, crate::LOREM_IPSUM); lorem_ipsum(ui, crate::LOREM_IPSUM);
}); });
let clone = self.clone();
Window::new("↔ resizable with TextEdit") Window::new("↔ resizable with TextEdit")
.open(open) .open(open)
.vscroll(false) .vscroll(false)
@@ -466,8 +458,7 @@ impl super::Demo for WindowResizeTest {
.default_height(300.0) .default_height(300.0)
.show(ctx, |ui| { .show(ctx, |ui| {
ui.label("Shows how you can fill an area with a widget."); ui.label("Shows how you can fill an area with a widget.");
let mut text = clone.text.write().unwrap(); ui.add_sized(ui.available_size(), TextEdit::multiline(&mut self.text));
ui.add_sized(ui.available_size(), TextEdit::multiline(&mut *text));
}); });
Window::new("↔ freely resized") Window::new("↔ freely resized")

View File

@@ -99,7 +99,7 @@ impl super::View for WidgetGallery {
impl WidgetGallery { impl WidgetGallery {
fn gallery_grid_contents(&mut self, ui: &mut egui::Ui) { fn gallery_grid_contents(&mut self, ui: &mut egui::Ui) {
let WidgetGallery { let Self {
enabled: _, enabled: _,
visible: _, visible: _,
boolean, boolean,

View File

@@ -37,7 +37,7 @@ impl super::Demo for WindowOptions {
} }
fn show(&mut self, ctx: &egui::Context, open: &mut bool) { fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
let WindowOptions { let Self {
title, title,
title_bar, title_bar,
closable, closable,
@@ -75,7 +75,7 @@ impl super::Demo for WindowOptions {
impl super::View for WindowOptions { impl super::View for WindowOptions {
fn ui(&mut self, ui: &mut egui::Ui) { fn ui(&mut self, ui: &mut egui::Ui) {
let WindowOptions { let Self {
title, title,
title_bar, title_bar,
closable, closable,

View File

@@ -14,7 +14,7 @@ impl super::Demo for WindowWithPanels {
.default_height(400.0) .default_height(400.0)
.vscroll(false) .vscroll(false)
.open(open); .open(open);
window.show(ctx, |ui| Self {}.ui(ui)); window.show(ctx, |ui| self.ui(ui));
} }
} }