From a66f4efaacad693eb1a119866e03321546b5561d Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 24 Apr 2020 18:47:14 +0200 Subject: [PATCH] Minor code cleanup and clippy fixes --- emigui/src/example_app.rs | 6 ++---- emigui/src/layout.rs | 12 +++--------- emigui/src/math.rs | 8 ++++---- emigui/src/mesher.rs | 6 +++--- emigui/src/region.rs | 32 ++++++++++++++------------------ emigui/src/scroll_area.rs | 5 +---- emigui/src/style.rs | 2 ++ emigui/src/widgets.rs | 16 ++++++++-------- emigui/src/window.rs | 13 +++++-------- 9 files changed, 42 insertions(+), 58 deletions(-) diff --git a/emigui/src/example_app.rs b/emigui/src/example_app.rs index b2c7731e..4dc30d4f 100644 --- a/emigui/src/example_app.rs +++ b/emigui/src/example_app.rs @@ -104,10 +104,8 @@ impl ExampleApp { region.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 { - if col.add(Button::new("Delete this")).clicked { - self.num_columns -= 1; - } + if i + 1 == self.num_columns && col.add(Button::new("Delete this")).clicked { + self.num_columns -= 1; } } }); diff --git a/emigui/src/layout.rs b/emigui/src/layout.rs index bd40d189..14fef003 100644 --- a/emigui/src/layout.rs +++ b/emigui/src/layout.rs @@ -24,10 +24,7 @@ pub struct GuiResponse { impl GuiResponse { /// Show some stuff if the item was hovered - pub fn tooltip(&mut self, add_contents: F) -> &mut Self - where - F: FnOnce(&mut Region), - { + pub fn tooltip(&mut self, add_contents: impl FnOnce(&mut Region)) -> &mut Self { if self.hovered { if let Some(mouse_pos) = self.ctx.input().mouse_pos { let window_pos = mouse_pos + vec2(16.0, 16.0); @@ -38,7 +35,7 @@ impl GuiResponse { } /// Show this text if the item was hovered - pub fn tooltip_text>(&mut self, text: S) -> &mut Self { + pub fn tooltip_text(&mut self, text: impl Into) -> &mut Self { self.tooltip(|popup| { popup.add(Label::new(text)); }) @@ -97,10 +94,7 @@ pub fn align_rect(rect: Rect, align: (Align, Align)) -> Rect { // TODO: move show_popup, and expand its features (default size, autosize, etc) /// Show a pop-over window -pub fn show_popup(ctx: &Arc, window_pos: Pos2, add_contents: F) -where - F: FnOnce(&mut Region), -{ +pub fn show_popup(ctx: &Arc, window_pos: Pos2, add_contents: impl FnOnce(&mut Region)) { let layer = Layer::Popup; let where_to_put_background = ctx.graphics.lock().layer(layer).len(); diff --git a/emigui/src/math.rs b/emigui/src/math.rs index 6c93d460..1454ded7 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -62,7 +62,7 @@ impl Vec2 { vec2(self.x.ceil(), self.y.ceil()) } - pub fn is_finite(&self) -> bool { + pub fn is_finite(self) -> bool { self.x.is_finite() && self.y.is_finite() } @@ -206,7 +206,7 @@ impl Pos2 { pos2(self.x.ceil(), self.y.ceil()) } - pub fn is_finite(&self) -> bool { + pub fn is_finite(self) -> bool { self.x.is_finite() && self.y.is_finite() } @@ -307,7 +307,7 @@ impl Rect { } pub fn from_min_max(min: Pos2, max: Pos2) -> Self { - Rect { min, max: max } + Rect { min, max } } pub fn from_min_size(min: Pos2, size: Vec2) -> Self { @@ -478,7 +478,7 @@ pub fn clamp(x: f32, min: f32, max: f32) -> f32 { /// For t=[0,1], returns [0,1] with a derivate of zero at both ends pub fn ease_in_ease_out(t: f32) -> f32 { - return 3.0 * t * t - 2.0 * t * t * t; + 3.0 * t * t - 2.0 * t * t * t } pub const TAU: f32 = 2.0 * std::f32::consts::PI; diff --git a/emigui/src/mesher.rs b/emigui/src/mesher.rs index 69eb2a29..bb371f64 100644 --- a/emigui/src/mesher.rs +++ b/emigui/src/mesher.rs @@ -121,7 +121,7 @@ impl Mesh { vertices: self.vertices[(min_vindex as usize)..=(max_vindex as usize)].to_vec(), }); } - return output; + output } } @@ -497,14 +497,14 @@ pub fn mesh_command( let mut top_left = Vertex { pos: pos + glyph.offset + vec2(*x_offset, 0.0), uv: glyph.min, - color: color, + color, }; top_left.pos.x = font.round_to_pixel(top_left.pos.x); // Pixel-perfection. top_left.pos.y = font.round_to_pixel(top_left.pos.y); // Pixel-perfection. let bottom_right = Vertex { pos: top_left.pos + glyph.size, uv: glyph.max, - color: color, + color, }; out_mesh.add_rect(top_left, bottom_right); } diff --git a/emigui/src/region.rs b/emigui/src/region.rs index 9fc10066..a10d47cc 100644 --- a/emigui/src/region.rs +++ b/emigui/src/region.rs @@ -237,10 +237,12 @@ impl Region { )) } - pub fn inner_layout(&mut self, dir: Direction, align: Align, add_contents: F) - where - F: FnOnce(&mut Region), - { + pub fn inner_layout( + &mut self, + dir: Direction, + align: Align, + add_contents: impl FnOnce(&mut Region), + ) { let child_rect = Rect::from_min_max(self.cursor, self.desired_rect.max()); let mut child_region = Region { dir, @@ -253,18 +255,12 @@ impl Region { } /// Start a region with horizontal layout - pub fn horizontal(&mut self, align: Align, add_contents: F) - where - F: FnOnce(&mut Region), - { + pub fn horizontal(&mut self, align: Align, add_contents: impl FnOnce(&mut Region)) { self.inner_layout(Direction::Horizontal, align, add_contents) } /// Start a region with vertical layout - pub fn vertical(&mut self, align: Align, add_contents: F) - where - F: FnOnce(&mut Region), - { + pub fn vertical(&mut self, align: Align, add_contents: impl FnOnce(&mut Region)) { self.inner_layout(Direction::Vertical, align, add_contents) } @@ -319,7 +315,7 @@ impl Region { // ------------------------------------------------------------------------ - pub fn add(&mut self, widget: W) -> GuiResponse { + pub fn add(&mut self, widget: impl Widget) -> GuiResponse { widget.add_to(self) } @@ -333,11 +329,11 @@ impl Region { self.add(Hyperlink::new(url)) } - pub fn collapsing(&mut self, text: S, add_contents: F) -> GuiResponse - where - S: Into, - F: FnOnce(&mut Region), - { + pub fn collapsing( + &mut self, + text: impl Into, + add_contents: impl FnOnce(&mut Region), + ) -> GuiResponse { CollapsingHeader::new(text).show(self, add_contents) } diff --git a/emigui/src/scroll_area.rs b/emigui/src/scroll_area.rs index 8b9a30e6..e161c40d 100644 --- a/emigui/src/scroll_area.rs +++ b/emigui/src/scroll_area.rs @@ -24,10 +24,7 @@ impl ScrollArea { } impl ScrollArea { - pub fn show(self, outer_region: &mut Region, add_contents: F) - where - F: FnOnce(&mut Region), - { + pub fn show(self, outer_region: &mut Region, add_contents: impl FnOnce(&mut Region)) { let ctx = outer_region.ctx().clone(); let scroll_area_id = outer_region.id.with("scroll_area"); diff --git a/emigui/src/style.rs b/emigui/src/style.rs index c0dfee32..ae2c3d49 100644 --- a/emigui/src/style.rs +++ b/emigui/src/style.rs @@ -1,3 +1,5 @@ +#![allow(clippy::if_same_then_else)] + use crate::{color::*, math::*, types::*}; #[derive(Clone, Copy, Debug, Serialize)] diff --git a/emigui/src/widgets.rs b/emigui/src/widgets.rs index 8f3ce342..8270f1b9 100644 --- a/emigui/src/widgets.rs +++ b/emigui/src/widgets.rs @@ -1,4 +1,4 @@ -#![allow(clippy::new_without_default_derive)] +#![allow(clippy::new_without_default)] use crate::{ layout::{Direction, GuiResponse}, @@ -21,7 +21,7 @@ pub struct Label { } impl Label { - pub fn new>(text: S) -> Self { + pub fn new(text: impl Into) -> Self { Label { text: text.into(), text_style: TextStyle::Body, @@ -88,7 +88,7 @@ impl Widget for Hyperlink { region.ctx().output.lock().cursor_icon = CursorIcon::PointingHand; } if interact.clicked { - region.ctx().output.lock().open_url = Some(self.url.clone()); + region.ctx().output.lock().open_url = Some(self.url); } if interact.hovered { @@ -121,7 +121,7 @@ pub struct Button { } impl Button { - pub fn new>(text: S) -> Self { + pub fn new(text: impl Into) -> Self { Button { text: text.into(), text_color: None, @@ -169,7 +169,7 @@ pub struct Checkbox<'a> { } impl<'a> Checkbox<'a> { - pub fn new>(checked: &'a mut bool, text: S) -> Self { + pub fn new(checked: &'a mut bool, text: impl Into) -> Self { Checkbox { checked, text: text.into(), @@ -240,7 +240,7 @@ pub struct RadioButton { } impl RadioButton { - pub fn new>(checked: bool, text: S) -> Self { + pub fn new(checked: bool, text: impl Into) -> Self { RadioButton { checked, text: text.into(), @@ -254,7 +254,7 @@ impl RadioButton { } } -pub fn radio>(checked: bool, text: S) -> RadioButton { +pub fn radio(checked: bool, text: impl Into) -> RadioButton { RadioButton::new(checked, text) } @@ -375,7 +375,7 @@ impl<'a> Slider<'a> { } } - pub fn text>(mut self, text: S) -> Self { + pub fn text(mut self, text: impl Into) -> Self { self.text = Some(text.into()); self } diff --git a/emigui/src/window.rs b/emigui/src/window.rs index 778b3fdb..1ba17362 100644 --- a/emigui/src/window.rs +++ b/emigui/src/window.rs @@ -55,7 +55,7 @@ impl Default for Window { } impl Window { - pub fn new>(title: S) -> Self { + pub fn new(title: impl Into) -> Self { Self { title: title.into(), ..Default::default() @@ -112,15 +112,12 @@ impl Window { } impl Window { - pub fn show(self, ctx: &Arc, add_contents: F) - where - F: FnOnce(&mut Region), - { + pub fn show(self, ctx: &Arc, add_contents: impl FnOnce(&mut Region)) { let style = ctx.style(); let window_padding = style.window_padding; - let default_pos = self.default_pos.unwrap_or(pos2(100.0, 100.0)); // TODO - let default_inner_size = self.default_size.unwrap_or(vec2(250.0, 250.0)); + let default_pos = self.default_pos.unwrap_or_else(|| pos2(100.0, 100.0)); // TODO + let default_inner_size = self.default_size.unwrap_or_else(|| vec2(250.0, 250.0)); let id = ctx.make_unique_id(&self.title, default_pos); @@ -236,7 +233,7 @@ impl Window { state = State { outer_pos: state.outer_pos, inner_size: new_inner_size, - outer_rect: outer_rect, + outer_rect, }; // Constrain to screen: