Minor code cleanup and clippy fixes

This commit is contained in:
Emil Ernerfeldt 2020-04-24 18:47:14 +02:00
parent ddc34d654b
commit a66f4efaac
9 changed files with 42 additions and 58 deletions

View file

@ -104,10 +104,8 @@ impl ExampleApp {
region.columns(self.num_columns, |cols| { region.columns(self.num_columns, |cols| {
for (i, col) in cols.iter_mut().enumerate() { for (i, col) in cols.iter_mut().enumerate() {
col.add(label!("Column {} out of {}", i + 1, self.num_columns)); col.add(label!("Column {} out of {}", i + 1, self.num_columns));
if i + 1 == self.num_columns { if i + 1 == self.num_columns && col.add(Button::new("Delete this")).clicked {
if col.add(Button::new("Delete this")).clicked { self.num_columns -= 1;
self.num_columns -= 1;
}
} }
} }
}); });

View file

@ -24,10 +24,7 @@ pub struct GuiResponse {
impl GuiResponse { impl GuiResponse {
/// Show some stuff if the item was hovered /// Show some stuff if the item was hovered
pub fn tooltip<F>(&mut self, add_contents: F) -> &mut Self pub fn tooltip(&mut self, add_contents: impl FnOnce(&mut Region)) -> &mut Self {
where
F: FnOnce(&mut Region),
{
if self.hovered { if self.hovered {
if let Some(mouse_pos) = self.ctx.input().mouse_pos { if let Some(mouse_pos) = self.ctx.input().mouse_pos {
let window_pos = mouse_pos + vec2(16.0, 16.0); let window_pos = mouse_pos + vec2(16.0, 16.0);
@ -38,7 +35,7 @@ impl GuiResponse {
} }
/// Show this text if the item was hovered /// Show this text if the item was hovered
pub fn tooltip_text<S: Into<String>>(&mut self, text: S) -> &mut Self { pub fn tooltip_text(&mut self, text: impl Into<String>) -> &mut Self {
self.tooltip(|popup| { self.tooltip(|popup| {
popup.add(Label::new(text)); 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) // TODO: move show_popup, and expand its features (default size, autosize, etc)
/// Show a pop-over window /// Show a pop-over window
pub fn show_popup<F>(ctx: &Arc<Context>, window_pos: Pos2, add_contents: F) pub fn show_popup(ctx: &Arc<Context>, window_pos: Pos2, add_contents: impl FnOnce(&mut Region)) {
where
F: FnOnce(&mut Region),
{
let layer = Layer::Popup; let layer = Layer::Popup;
let where_to_put_background = ctx.graphics.lock().layer(layer).len(); let where_to_put_background = ctx.graphics.lock().layer(layer).len();

View file

@ -62,7 +62,7 @@ impl Vec2 {
vec2(self.x.ceil(), self.y.ceil()) 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() self.x.is_finite() && self.y.is_finite()
} }
@ -206,7 +206,7 @@ impl Pos2 {
pos2(self.x.ceil(), self.y.ceil()) 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() self.x.is_finite() && self.y.is_finite()
} }
@ -307,7 +307,7 @@ impl Rect {
} }
pub fn from_min_max(min: Pos2, max: Pos2) -> Self { 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 { 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 /// For t=[0,1], returns [0,1] with a derivate of zero at both ends
pub fn ease_in_ease_out(t: f32) -> f32 { 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; pub const TAU: f32 = 2.0 * std::f32::consts::PI;

View file

@ -121,7 +121,7 @@ impl Mesh {
vertices: self.vertices[(min_vindex as usize)..=(max_vindex as usize)].to_vec(), 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 { let mut top_left = Vertex {
pos: pos + glyph.offset + vec2(*x_offset, 0.0), pos: pos + glyph.offset + vec2(*x_offset, 0.0),
uv: glyph.min, uv: glyph.min,
color: color, color,
}; };
top_left.pos.x = font.round_to_pixel(top_left.pos.x); // Pixel-perfection. 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. top_left.pos.y = font.round_to_pixel(top_left.pos.y); // Pixel-perfection.
let bottom_right = Vertex { let bottom_right = Vertex {
pos: top_left.pos + glyph.size, pos: top_left.pos + glyph.size,
uv: glyph.max, uv: glyph.max,
color: color, color,
}; };
out_mesh.add_rect(top_left, bottom_right); out_mesh.add_rect(top_left, bottom_right);
} }

View file

@ -237,10 +237,12 @@ impl Region {
)) ))
} }
pub fn inner_layout<F>(&mut self, dir: Direction, align: Align, add_contents: F) pub fn inner_layout(
where &mut self,
F: FnOnce(&mut Region), dir: Direction,
{ align: Align,
add_contents: impl FnOnce(&mut Region),
) {
let child_rect = Rect::from_min_max(self.cursor, self.desired_rect.max()); let child_rect = Rect::from_min_max(self.cursor, self.desired_rect.max());
let mut child_region = Region { let mut child_region = Region {
dir, dir,
@ -253,18 +255,12 @@ impl Region {
} }
/// Start a region with horizontal layout /// Start a region with horizontal layout
pub fn horizontal<F>(&mut self, align: Align, add_contents: F) pub fn horizontal(&mut self, align: Align, add_contents: impl FnOnce(&mut Region)) {
where
F: FnOnce(&mut Region),
{
self.inner_layout(Direction::Horizontal, align, add_contents) self.inner_layout(Direction::Horizontal, align, add_contents)
} }
/// Start a region with vertical layout /// Start a region with vertical layout
pub fn vertical<F>(&mut self, align: Align, add_contents: F) pub fn vertical(&mut self, align: Align, add_contents: impl FnOnce(&mut Region)) {
where
F: FnOnce(&mut Region),
{
self.inner_layout(Direction::Vertical, align, add_contents) self.inner_layout(Direction::Vertical, align, add_contents)
} }
@ -319,7 +315,7 @@ impl Region {
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
pub fn add<W: Widget>(&mut self, widget: W) -> GuiResponse { pub fn add(&mut self, widget: impl Widget) -> GuiResponse {
widget.add_to(self) widget.add_to(self)
} }
@ -333,11 +329,11 @@ impl Region {
self.add(Hyperlink::new(url)) self.add(Hyperlink::new(url))
} }
pub fn collapsing<S, F>(&mut self, text: S, add_contents: F) -> GuiResponse pub fn collapsing(
where &mut self,
S: Into<String>, text: impl Into<String>,
F: FnOnce(&mut Region), add_contents: impl FnOnce(&mut Region),
{ ) -> GuiResponse {
CollapsingHeader::new(text).show(self, add_contents) CollapsingHeader::new(text).show(self, add_contents)
} }

View file

@ -24,10 +24,7 @@ impl ScrollArea {
} }
impl ScrollArea { impl ScrollArea {
pub fn show<F>(self, outer_region: &mut Region, add_contents: F) pub fn show(self, outer_region: &mut Region, add_contents: impl FnOnce(&mut Region)) {
where
F: FnOnce(&mut Region),
{
let ctx = outer_region.ctx().clone(); let ctx = outer_region.ctx().clone();
let scroll_area_id = outer_region.id.with("scroll_area"); let scroll_area_id = outer_region.id.with("scroll_area");

View file

@ -1,3 +1,5 @@
#![allow(clippy::if_same_then_else)]
use crate::{color::*, math::*, types::*}; use crate::{color::*, math::*, types::*};
#[derive(Clone, Copy, Debug, Serialize)] #[derive(Clone, Copy, Debug, Serialize)]

View file

@ -1,4 +1,4 @@
#![allow(clippy::new_without_default_derive)] #![allow(clippy::new_without_default)]
use crate::{ use crate::{
layout::{Direction, GuiResponse}, layout::{Direction, GuiResponse},
@ -21,7 +21,7 @@ pub struct Label {
} }
impl Label { impl Label {
pub fn new<S: Into<String>>(text: S) -> Self { pub fn new(text: impl Into<String>) -> Self {
Label { Label {
text: text.into(), text: text.into(),
text_style: TextStyle::Body, text_style: TextStyle::Body,
@ -88,7 +88,7 @@ impl Widget for Hyperlink {
region.ctx().output.lock().cursor_icon = CursorIcon::PointingHand; region.ctx().output.lock().cursor_icon = CursorIcon::PointingHand;
} }
if interact.clicked { 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 { if interact.hovered {
@ -121,7 +121,7 @@ pub struct Button {
} }
impl Button { impl Button {
pub fn new<S: Into<String>>(text: S) -> Self { pub fn new(text: impl Into<String>) -> Self {
Button { Button {
text: text.into(), text: text.into(),
text_color: None, text_color: None,
@ -169,7 +169,7 @@ pub struct Checkbox<'a> {
} }
impl<'a> Checkbox<'a> { impl<'a> Checkbox<'a> {
pub fn new<S: Into<String>>(checked: &'a mut bool, text: S) -> Self { pub fn new(checked: &'a mut bool, text: impl Into<String>) -> Self {
Checkbox { Checkbox {
checked, checked,
text: text.into(), text: text.into(),
@ -240,7 +240,7 @@ pub struct RadioButton {
} }
impl RadioButton { impl RadioButton {
pub fn new<S: Into<String>>(checked: bool, text: S) -> Self { pub fn new(checked: bool, text: impl Into<String>) -> Self {
RadioButton { RadioButton {
checked, checked,
text: text.into(), text: text.into(),
@ -254,7 +254,7 @@ impl RadioButton {
} }
} }
pub fn radio<S: Into<String>>(checked: bool, text: S) -> RadioButton { pub fn radio(checked: bool, text: impl Into<String>) -> RadioButton {
RadioButton::new(checked, text) RadioButton::new(checked, text)
} }
@ -375,7 +375,7 @@ impl<'a> Slider<'a> {
} }
} }
pub fn text<S: Into<String>>(mut self, text: S) -> Self { pub fn text(mut self, text: impl Into<String>) -> Self {
self.text = Some(text.into()); self.text = Some(text.into());
self self
} }

View file

@ -55,7 +55,7 @@ impl Default for Window {
} }
impl Window { impl Window {
pub fn new<S: Into<String>>(title: S) -> Self { pub fn new(title: impl Into<String>) -> Self {
Self { Self {
title: title.into(), title: title.into(),
..Default::default() ..Default::default()
@ -112,15 +112,12 @@ impl Window {
} }
impl Window { impl Window {
pub fn show<F>(self, ctx: &Arc<Context>, add_contents: F) pub fn show(self, ctx: &Arc<Context>, add_contents: impl FnOnce(&mut Region)) {
where
F: FnOnce(&mut Region),
{
let style = ctx.style(); let style = ctx.style();
let window_padding = style.window_padding; let window_padding = style.window_padding;
let default_pos = self.default_pos.unwrap_or(pos2(100.0, 100.0)); // TODO let default_pos = self.default_pos.unwrap_or_else(|| pos2(100.0, 100.0)); // TODO
let default_inner_size = self.default_size.unwrap_or(vec2(250.0, 250.0)); 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); let id = ctx.make_unique_id(&self.title, default_pos);
@ -236,7 +233,7 @@ impl Window {
state = State { state = State {
outer_pos: state.outer_pos, outer_pos: state.outer_pos,
inner_size: new_inner_size, inner_size: new_inner_size,
outer_rect: outer_rect, outer_rect,
}; };
// Constrain to screen: // Constrain to screen: