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| {
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;
}
}
});

View file

@ -24,10 +24,7 @@ pub struct GuiResponse {
impl GuiResponse {
/// Show some stuff if the item was hovered
pub fn tooltip<F>(&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<S: Into<String>>(&mut self, text: S) -> &mut Self {
pub fn tooltip_text(&mut self, text: impl Into<String>) -> &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<F>(ctx: &Arc<Context>, window_pos: Pos2, add_contents: F)
where
F: FnOnce(&mut Region),
{
pub fn show_popup(ctx: &Arc<Context>, window_pos: Pos2, add_contents: impl FnOnce(&mut Region)) {
let layer = Layer::Popup;
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())
}
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;

View file

@ -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);
}

View file

@ -237,10 +237,12 @@ impl Region {
))
}
pub fn inner_layout<F>(&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<F>(&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<F>(&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<W: Widget>(&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<S, F>(&mut self, text: S, add_contents: F) -> GuiResponse
where
S: Into<String>,
F: FnOnce(&mut Region),
{
pub fn collapsing(
&mut self,
text: impl Into<String>,
add_contents: impl FnOnce(&mut Region),
) -> GuiResponse {
CollapsingHeader::new(text).show(self, add_contents)
}

View file

@ -24,10 +24,7 @@ impl ScrollArea {
}
impl ScrollArea {
pub fn show<F>(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");

View file

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

View file

@ -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<S: Into<String>>(text: S) -> Self {
pub fn new(text: impl Into<String>) -> 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<S: Into<String>>(text: S) -> Self {
pub fn new(text: impl Into<String>) -> Self {
Button {
text: text.into(),
text_color: None,
@ -169,7 +169,7 @@ pub struct 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 {
checked,
text: text.into(),
@ -240,7 +240,7 @@ pub struct 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 {
checked,
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)
}
@ -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
}

View file

@ -55,7 +55,7 @@ impl Default for Window {
}
impl Window {
pub fn new<S: Into<String>>(title: S) -> Self {
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
..Default::default()
@ -112,15 +112,12 @@ impl Window {
}
impl Window {
pub fn show<F>(self, ctx: &Arc<Context>, add_contents: F)
where
F: FnOnce(&mut Region),
{
pub fn show(self, ctx: &Arc<Context>, 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: