From 12afc356eed0ac781703f20194b41ecf19467818 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 10 Oct 2020 13:04:40 +0200 Subject: [PATCH] Misc code cleanup: use more ui.checkbox, ui.label, ui.button etc --- egui/src/context.rs | 57 +++++++++++++-------------------- egui/src/demos/app.rs | 22 ++++++------- egui/src/demos/demo_window.rs | 26 +++++++-------- egui/src/demos/fractal_clock.rs | 4 +-- egui/src/demos/sliders.rs | 2 +- egui/src/demos/widgets.rs | 16 ++++----- egui/src/introspection.rs | 7 ++-- egui/src/menu.rs | 2 +- egui/src/paint/command.rs | 2 +- egui/src/style.rs | 29 ++++++++--------- egui/src/ui.rs | 8 +++++ 11 files changed, 83 insertions(+), 92 deletions(-) diff --git a/egui/src/context.rs b/egui/src/context.rs index 03430cb3a..f23ad5666 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -536,23 +536,21 @@ impl Context { .show(ui, |ui| ui.input().clone().ui(ui)); ui.collapsing("Stats", |ui| { - ui.add(label!( + ui.label(format!( "Screen size: {} x {} points, pixels_per_point: {:?}", ui.input().screen_size.x, ui.input().screen_size.y, ui.input().pixels_per_point, )); - ui.add(label!("Painting:").text_style(TextStyle::Heading)); + ui.heading("Painting:"); self.paint_stats.lock().ui(ui); }); } pub fn memory_ui(&self, ui: &mut crate::Ui) { - use crate::widgets::*; - if ui - .add(Button::new("Reset all")) + .button("Reset all") .on_hover_text("Reset all Egui state") .clicked { @@ -560,50 +558,48 @@ impl Context { } ui.horizontal(|ui| { - ui.add(label!( + ui.label(format!( "{} areas (window positions)", self.memory().areas.count() )); - if ui.add(Button::new("Reset")).clicked { + if ui.button("Reset").clicked { self.memory().areas = Default::default(); } }); ui.horizontal(|ui| { - ui.add(label!( + ui.label(format!( "{} collapsing headers", self.memory().collapsing_headers.len() )); - if ui.add(Button::new("Reset")).clicked { + if ui.button("Reset").clicked { self.memory().collapsing_headers = Default::default(); } }); ui.horizontal(|ui| { - ui.add(label!("{} menu bars", self.memory().menu_bar.len())); - if ui.add(Button::new("Reset")).clicked { + ui.label(format!("{} menu bars", self.memory().menu_bar.len())); + if ui.button("Reset").clicked { self.memory().menu_bar = Default::default(); } }); ui.horizontal(|ui| { - ui.add(label!("{} scroll areas", self.memory().scroll_areas.len())); - if ui.add(Button::new("Reset")).clicked { + ui.label(format!("{} scroll areas", self.memory().scroll_areas.len())); + if ui.button("Reset").clicked { self.memory().scroll_areas = Default::default(); } }); ui.horizontal(|ui| { - ui.add(label!("{} resize areas", self.memory().resize.len())); - if ui.add(Button::new("Reset")).clicked { + ui.label(format!("{} resize areas", self.memory().resize.len())); + if ui.button("Reset").clicked { self.memory().resize = Default::default(); } }); ui.shrink_width_to_current(); // don't let the text below grow this window wider - ui.add( - label!("NOTE: the position of this window cannot be reset from within itself."), // .auto_shrink(), - ); + ui.label("NOTE: the position of this window cannot be reset from within itself."); } } @@ -624,31 +620,24 @@ impl paint::PaintOptions { debug_paint_clip_rects, debug_ignore_clip_rects, } = self; - use crate::widgets::*; - ui.add(Checkbox::new(anti_alias, "Antialias")); - ui.add(Checkbox::new( + ui.checkbox(anti_alias, "Antialias"); + ui.checkbox( coarse_tessellation_culling, "Do coarse culling in the tessellator", - )); - ui.add(Checkbox::new( - debug_paint_clip_rects, - "Paint clip rectangles (debug)", - )); - ui.add(Checkbox::new( - debug_ignore_clip_rects, - "Ignore clip rectangles (debug)", - )); + ); + ui.checkbox(debug_paint_clip_rects, "Paint clip rectangles (debug)"); + ui.checkbox(debug_ignore_clip_rects, "Ignore clip rectangles (debug)"); } } impl PaintStats { pub fn ui(&self, ui: &mut Ui) { - ui.add(label!("Jobs: {}", self.num_jobs)) + ui.label(format!("Jobs: {}", self.num_jobs)) .on_hover_text("Number of separate clip rectangles"); - ui.add(label!("Primitives: {}", self.num_primitives)) + ui.label(format!("Primitives: {}", self.num_primitives)) .on_hover_text("Boxes, circles, text areas etc"); - ui.add(label!("Vertices: {}", self.num_vertices)); - ui.add(label!("Triangles: {}", self.num_triangles)); + ui.label(format!("Vertices: {}", self.num_vertices)); + ui.label(format!("Triangles: {}", self.num_triangles)); } } diff --git a/egui/src/demos/app.rs b/egui/src/demos/app.rs index 8068a200f..6bc5c5f38 100644 --- a/egui/src/demos/app.rs +++ b/egui/src/demos/app.rs @@ -331,8 +331,8 @@ impl DemoApp { ui.hyperlink("https://github.com/emilk/egui"); }); } else { - ui.add(label!("Egui").text_style(TextStyle::Heading)); - if ui.add(Button::new("Quit")).clicked { + ui.heading("Egui"); + if ui.button("Quit").clicked { backend.quit(); return; } @@ -466,11 +466,11 @@ impl OpenWindows { fn show_menu_bar(ui: &mut Ui, windows: &mut OpenWindows, env: &DemoEnvironment) { menu::bar(ui, |ui| { menu::menu(ui, "File", |ui| { - if ui.add(Button::new("Reorganize windows")).clicked { + if ui.button("Reorganize windows").clicked { ui.ctx().memory().reset_areas(); } if ui - .add(Button::new("Clear entire Egui memory")) + .button("Clear entire Egui memory") .on_hover_text("Forget scroll, collapsibles etc") .clicked { @@ -486,16 +486,16 @@ fn show_menu_bar(ui: &mut Ui, windows: &mut OpenWindows, env: &DemoEnvironment) memory, resize, } = windows; - ui.add(Checkbox::new(demo, "Demo")); - ui.add(Checkbox::new(fractal_clock, "Fractal Clock")); + ui.checkbox(demo, "Demo"); + ui.checkbox(fractal_clock, "Fractal Clock"); ui.separator(); - ui.add(Checkbox::new(settings, "Settings")); - ui.add(Checkbox::new(inspection, "Inspection")); - ui.add(Checkbox::new(memory, "Memory")); - ui.add(Checkbox::new(resize, "Resize examples")); + ui.checkbox(settings, "Settings"); + ui.checkbox(inspection, "Inspection"); + ui.checkbox(memory, "Memory"); + ui.checkbox(resize, "Resize examples"); }); menu::menu(ui, "About", |ui| { - ui.add(label!("This is Egui")); + ui.label("This is Egui"); ui.add(Hyperlink::new("https://github.com/emilk/egui").text("Egui home page")); }); diff --git a/egui/src/demos/demo_window.rs b/egui/src/demos/demo_window.rs index 82338811b..28cd69226 100644 --- a/egui/src/demos/demo_window.rs +++ b/egui/src/demos/demo_window.rs @@ -32,9 +32,7 @@ impl Default for DemoWindow { impl DemoWindow { pub fn ui(&mut self, ui: &mut Ui) { ui.collapsing("About Egui", |ui| { - ui.add(label!( - "Egui is an experimental immediate mode GUI written in Rust." - )); + ui.label("Egui is an experimental immediate mode GUI written in Rust."); ui.horizontal(|ui| { ui.label("Project home page:"); @@ -67,7 +65,7 @@ impl DemoWindow { ui.columns(self.num_columns, |cols| { for (i, col) in cols.iter_mut().enumerate() { col.add(label!("Column {} out of {}", i + 1, self.num_columns)); - if i + 1 == self.num_columns && col.add(Button::new("Delete this")).clicked { + if i + 1 == self.num_columns && col.button("Delete this").clicked { self.num_columns -= 1; } } @@ -94,8 +92,8 @@ impl DemoWindow { .default_open(false) .show(ui, |ui| { Resize::default().default_height(100.0).show(ui, |ui| { - ui.add(label!("This ui can be resized!")); - ui.add(label!("Just pull the handle on the bottom right")); + ui.label("This ui can be resized!"); + ui.label("Just pull the handle on the bottom right"); }); }); @@ -139,8 +137,8 @@ impl DemoWindow { ui.label("\ Most widgets don't need unique names, but are tracked \ based on their position on screen. For instance, buttons:"); - ui.add(Button::new("Button")); - ui.add(Button::new("Button")); + let _ = ui.button("Button"); + let _ = ui.button("Button"); }); } } @@ -292,7 +290,7 @@ impl Painting { ui.horizontal(|ui| { self.stroke.ui(ui, "Stroke"); - if ui.add(Button::new("Clear")).clicked { + if ui.button("Clear").clicked { self.lines.clear(); } }); @@ -381,12 +379,12 @@ impl LayoutDemo { } pub fn content_ui(&mut self, ui: &mut Ui) { - // ui.add(label!("Available space: {:?}", ui.available().size())); - if ui.add(Button::new("Reset")).clicked { + // ui.label(format!("Available space: {:?}", ui.available().size())); + if ui.button("Reset").clicked { *self = Default::default(); } ui.separator(); - ui.add(label!("Direction:")); + ui.label("Direction:"); // TODO: enum iter @@ -399,11 +397,11 @@ impl LayoutDemo { } } - ui.add(Checkbox::new(&mut self.reversed, "Reversed")); + ui.checkbox(&mut self.reversed, "Reversed"); ui.separator(); - ui.add(label!("Align:")); + ui.label("Align:"); for &align in &[Align::Min, Align::Center, Align::Max] { if ui diff --git a/egui/src/demos/fractal_clock.rs b/egui/src/demos/fractal_clock.rs index 672a8c071..c4a9119f4 100644 --- a/egui/src/demos/fractal_clock.rs +++ b/egui/src/demos/fractal_clock.rs @@ -82,14 +82,14 @@ impl FractalClock { )); }; - ui.add(Checkbox::new(&mut self.paused, "Paused")); + ui.checkbox(&mut self.paused, "Paused"); ui.add(Slider::f32(&mut self.zoom, 0.0..=1.0).text("zoom")); ui.add(Slider::f32(&mut self.start_line_width, 0.0..=5.0).text("Start line width")); ui.add(Slider::usize(&mut self.depth, 0..=14).text("depth")); ui.add(Slider::f32(&mut self.length_factor, 0.0..=1.0).text("length factor")); ui.add(Slider::f32(&mut self.luminance_factor, 0.0..=1.0).text("luminance factor")); ui.add(Slider::f32(&mut self.width_factor, 0.0..=1.0).text("width factor")); - if ui.add(Button::new("Reset")).clicked { + if ui.button("Reset").clicked { *self = Default::default(); } diff --git a/egui/src/demos/sliders.rs b/egui/src/demos/sliders.rs index 7a951e570..012f3c466 100644 --- a/egui/src/demos/sliders.rs +++ b/egui/src/demos/sliders.rs @@ -69,7 +69,7 @@ impl Sliders { ui.label("Sliders will automatically figure out how many decimals to show."); - if ui.add(Button::new("Assign PI")).clicked { + if ui.button("Assign PI").clicked { self.value = std::f64::consts::PI; } } diff --git a/egui/src/demos/widgets.rs b/egui/src/demos/widgets.rs index df363adb4..617d9df61 100644 --- a/egui/src/demos/widgets.rs +++ b/egui/src/demos/widgets.rs @@ -51,13 +51,13 @@ impl Widgets { ui.horizontal(|ui| { ui.style_mut().spacing.item_spacing.x = 0.0; - ui.add(label!("Text can have ").text_color(srgba(110, 255, 110, 255))); - ui.add(label!("color ").text_color(srgba(128, 140, 255, 255))); - ui.add(label!("and tooltips.")).on_hover_text( + ui.add(Label::new("Text can have ").text_color(srgba(110, 255, 110, 255))); + ui.add(Label::new("color ").text_color(srgba(128, 140, 255, 255))); + ui.add(Label::new("and tooltips.")).on_hover_text( "This is a multiline tooltip that demonstrates that you can easily add tooltips to any element.\nThis is the second line.\nThis is the third.", ); }); - ui.add(label!("Tooltips can be more than just simple text.")) + ui.label("Tooltips can be more than just simple text.") .on_hover_ui(|ui| { ui.heading("The name of the tooltip"); ui.horizontal(|ui| { @@ -67,7 +67,7 @@ impl Widgets { let _ = ui.button("A button you can never press"); }); - ui.add(label!("Ευρηκα! τ = 2×π")) + ui.label("Ευρηκα! τ = 2×π") .on_hover_text("The current font supports only a few non-latin characters and Egui does not currently support right-to-left text."); ui.horizontal(|ui| { @@ -82,7 +82,7 @@ impl Widgets { ui.radio_value(&mut self.radio, Enum::Third, "Third"); }); - ui.add(Checkbox::new(&mut self.button_enabled, "Button enabled")); + ui.checkbox(&mut self.button_enabled, "Button enabled"); ui.horizontal(|ui| { if ui @@ -133,7 +133,7 @@ impl Widgets { ui.separator(); ui.horizontal(|ui| { - ui.add(label!("Single line text input:")); + ui.label("Single line text input:"); ui.add( TextEdit::new(&mut self.single_line_text_input) .multiline(false) @@ -141,7 +141,7 @@ impl Widgets { ); }); // TODO: .on_hover_text("Enter text to edit me") - ui.add(label!("Multiline text input:")); + ui.label("Multiline text input:"); ui.add(TextEdit::new(&mut self.multiline_text_input).id_source("multiline")); ui.separator(); diff --git a/egui/src/introspection.rs b/egui/src/introspection.rs index b6162cb28..e864d19ff 100644 --- a/egui/src/introspection.rs +++ b/egui/src/introspection.rs @@ -8,10 +8,9 @@ use crate::{ impl Texture { pub fn ui(&self, ui: &mut Ui) { - ui.add(label!( + ui.label(format!( "Texture size: {} x {} (hover to zoom)", - self.width, - self.height + self.width, self.height )); if self.width <= 1 || self.height <= 1 { return; @@ -65,7 +64,7 @@ impl paint::FontDefinitions { .text(format!("{:?}", text_style)), ); } - if ui.add(Button::new("Reset fonts")).clicked { + if ui.button("Reset fonts").clicked { *self = paint::FontDefinitions::with_pixels_per_point(self.pixels_per_point); } } diff --git a/egui/src/menu.rs b/egui/src/menu.rs index 626d9f402..334928d28 100644 --- a/egui/src/menu.rs +++ b/egui/src/menu.rs @@ -7,7 +7,7 @@ //! //! menu::bar(ui, |ui| { //! menu::menu(ui, "File", |ui| { -//! if ui.add(Button::new("Open")).clicked { +//! if ui.button("Open").clicked { //! // ... //! } //! }); diff --git a/egui/src/paint/command.rs b/egui/src/paint/command.rs index 5de3da943..968b7bc35 100644 --- a/egui/src/paint/command.rs +++ b/egui/src/paint/command.rs @@ -110,7 +110,7 @@ impl PaintCmd { } } -#[derive(Clone, Copy, Debug, Default)] +#[derive(Clone, Copy, Debug, Default, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Stroke { pub width: f32, diff --git a/egui/src/style.rs b/egui/src/style.rs index af175d6d6..732502dee 100644 --- a/egui/src/style.rs +++ b/egui/src/style.rs @@ -8,7 +8,7 @@ use crate::{ }; /// Specifies the look and feel of a `Ui`. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Style { /// Default `TextStyle` for normal text (i.e. for `Label` and `TextEdit`). @@ -37,7 +37,7 @@ impl Style { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Spacing { /// Horizontal and vertical spacing between widgets @@ -86,7 +86,7 @@ impl Spacing { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Interaction { /// Mouse must be the close to the side of a window to resize @@ -96,7 +96,7 @@ pub struct Interaction { pub resize_grab_radius_corner: f32, } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Visuals { pub widgets: Widgets, @@ -132,7 +132,7 @@ impl Visuals { } } -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct Widgets { /// For an interactive widget that is being interacted with @@ -162,7 +162,7 @@ impl Widgets { } /// bg = background, fg = foreground. -#[derive(Clone, Copy, Debug)] +#[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct WidgetVisuals { /// Background color of widget @@ -291,7 +291,7 @@ use crate::{widgets::*, Ui}; impl Style { pub fn ui(&mut self, ui: &mut crate::Ui) { - if ui.add(Button::new("Reset")).clicked { + if ui.button("Reset").clicked { *self = Default::default(); } @@ -317,7 +317,7 @@ impl Style { impl Spacing { pub fn ui(&mut self, ui: &mut crate::Ui) { - if ui.add(Button::new("Reset")).clicked { + if ui.button("Reset").clicked { *self = Default::default(); } @@ -346,7 +346,7 @@ impl Spacing { impl Interaction { pub fn ui(&mut self, ui: &mut crate::Ui) { - if ui.add(Button::new("Reset")).clicked { + if ui.button("Reset").clicked { *self = Default::default(); } @@ -364,7 +364,7 @@ impl Interaction { impl Widgets { pub fn ui(&mut self, ui: &mut crate::Ui) { - if ui.add(Button::new("Reset")).clicked { + if ui.button("Reset").clicked { *self = Default::default(); } @@ -404,7 +404,7 @@ impl WidgetVisuals { impl Visuals { pub fn ui(&mut self, ui: &mut crate::Ui) { - if ui.add(Button::new("Reset")).clicked { + if ui.button("Reset").clicked { *self = Default::default(); } @@ -428,11 +428,8 @@ impl Visuals { ui.add(Slider::f32(text_cursor_width, 0.0..=2.0).text("text_cursor_width")); ui.add(Slider::f32(clip_rect_margin, 0.0..=20.0).text("clip_rect_margin")); - ui.add(Checkbox::new( - debug_widget_rects, - "Paint debug rectangles around widgets", - )); - ui.add(Checkbox::new(debug_resize, "Debug Resize")); + ui.checkbox(debug_widget_rects, "Paint debug rectangles around widgets"); + ui.checkbox(debug_resize, "Debug Resize"); } } diff --git a/egui/src/ui.rs b/egui/src/ui.rs index cf0fd6639..92db92837 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -146,18 +146,26 @@ impl Ui { self.painter().layer() } + /// The `Input` of the `Context` associated with the `Ui`. + /// Equivalent to `.ctx().input()`. pub fn input(&self) -> &InputState { self.ctx().input() } + /// The `Memory` of the `Context` associated with the `Ui`. + /// Equivalent to `.ctx().memory()`. pub fn memory(&self) -> parking_lot::MutexGuard<'_, Memory> { self.ctx().memory() } + /// The `Output` of the `Context` associated with the `Ui`. + /// Equivalent to `.ctx().output()`. pub fn output(&self) -> parking_lot::MutexGuard<'_, Output> { self.ctx().output() } + /// The `Fonts` of the `Context` associated with the `Ui`. + /// Equivalent to `.ctx().fonts()`. pub fn fonts(&self) -> &Fonts { self.ctx().fonts() }