From 32b36b4f761f6b0e5a1ccb1fb8ed9b0e56801148 Mon Sep 17 00:00:00 2001 From: genusistimelord Date: Mon, 8 May 2023 16:46:21 -0400 Subject: [PATCH] fixed Clippy Warnings and Added Operate to the different widgets that hold Elements --- .../cupertino/cupertino_spinner/src/main.rs | 18 ++++------ examples/split_scroller/src/main.rs | 18 +++++----- src/native/card.rs | 33 +++++++++++++++++-- src/native/cupertino/cupertino_spinner.rs | 22 ++++++++----- src/native/floating_element.rs | 17 +++++++++- src/native/grid.rs | 25 +++++++++++++- src/native/menu/menu_inner.rs | 2 ++ src/native/modal.rs | 28 ++++++++++++++-- src/native/spinner.rs | 6 +++- src/native/wrap.rs | 25 ++++++++++++-- src/style/color_picker.rs | 8 ++--- 11 files changed, 156 insertions(+), 46 deletions(-) diff --git a/examples/cupertino/cupertino_spinner/src/main.rs b/examples/cupertino/cupertino_spinner/src/main.rs index 385b862b..364fd127 100644 --- a/examples/cupertino/cupertino_spinner/src/main.rs +++ b/examples/cupertino/cupertino_spinner/src/main.rs @@ -29,9 +29,9 @@ impl State { async fn load() -> Result { println!("Doing stuff..."); tokio::time::sleep(tokio::time::Duration::from_secs(3)).await; - return Ok(Self { + Ok(Self { hello: "Loaded!".to_string(), - }); + }) } } @@ -53,16 +53,10 @@ impl Application for Spinner { } fn update(&mut self, message: Message) -> Command { - match self { - Spinner::Loading => match message { - Message::Loaded(Ok(state)) => { - *self = Spinner::Loaded(State { hello: state.hello }); - } - - _ => (), - }, - - _ => (), + if let Spinner::Loading = self { + if let Message::Loaded(Ok(state)) = message { + *self = Spinner::Loaded(State { hello: state.hello }); + } } Command::none() diff --git a/examples/split_scroller/src/main.rs b/examples/split_scroller/src/main.rs index 5235ff22..48f51de0 100644 --- a/examples/split_scroller/src/main.rs +++ b/examples/split_scroller/src/main.rs @@ -37,7 +37,7 @@ impl Application for MyApp { fn update(&mut self, message: Message) -> Command { match message { Message::RouterMessage(router_msg) => { - return self.router.update(router_msg).map(Message::RouterMessage); + self.router.update(router_msg).map(Message::RouterMessage) } } @@ -63,8 +63,8 @@ mod router { #[derive(Debug, Clone)] pub enum Message { - SplittedMessage(splitted::Message), - DemoMessage(demo::Message), + Splitted(splitted::Message), + Demo(demo::Message), GoToDemo, GoToSplitted, } @@ -111,14 +111,14 @@ mod router { Message::GoToSplitted => { self.next_state(ViewState::splitted()); } - Message::SplittedMessage(msg) => { + Message::Splitted(msg) => { if let ViewState::Splitted { state } = &mut self.state { - return state.update(msg).map(Message::SplittedMessage); + return state.update(msg).map(Message::Splitted); } } - Message::DemoMessage(demo_msg) => { + Message::Demo(demo_msg) => { if let ViewState::Demo { state } = &mut self.state { - return state.update(demo_msg).map(Message::DemoMessage); + return state.update(demo_msg).map(Message::Demo); } } } @@ -145,8 +145,8 @@ mod router { } pub fn view(&self) -> Element { match self { - Self::Splitted { state } => state.view().map(Message::SplittedMessage), - Self::Demo { state } => state.view().map(Message::DemoMessage), + Self::Splitted { state } => state.view().map(Message::Splitted), + Self::Demo { state } => state.view().map(Message::Demo), } } } diff --git a/src/native/card.rs b/src/native/card.rs index d8cb8dc3..7ce13722 100644 --- a/src/native/card.rs +++ b/src/native/card.rs @@ -5,10 +5,11 @@ use iced_native::{ alignment::{Horizontal, Vertical}, event, mouse, renderer::{self, BorderRadius}, - touch, Alignment, Clipboard, Color, Event, Layout, Length, Padding, Point, Rectangle, Shell, - Size, + touch, + widget::{Operation, Tree}, + Alignment, Clipboard, Color, Element, Event, Layout, Length, Padding, Point, Rectangle, Shell, + Size, Widget, }; -use iced_native::{widget::Tree, Element, Widget}; use crate::graphics::icons::Icon; pub use crate::style::card::{Appearance, StyleSheet}; @@ -447,6 +448,32 @@ where ) } + fn operate<'b>( + &'b self, + state: &'b mut Tree, + layout: Layout<'_>, + renderer: &Renderer, + operation: &mut dyn Operation, + ) { + let mut children = layout.children(); + let head_layout = children.next().expect("Missing Head Layout"); + let body_layout = children.next().expect("Missing Body Layout"); + let foot_layout = children.next().expect("Missing Footer Layout"); + + self.head + .as_widget() + .operate(&mut state.children[0], head_layout, renderer, operation); + self.body + .as_widget() + .operate(&mut state.children[1], body_layout, renderer, operation); + + if let Some(footer) = &self.foot { + footer + .as_widget() + .operate(&mut state.children[2], foot_layout, renderer, operation); + }; + } + fn draw( &self, state: &Tree, diff --git a/src/native/cupertino/cupertino_spinner.rs b/src/native/cupertino/cupertino_spinner.rs index 77d9208e..a3530895 100644 --- a/src/native/cupertino/cupertino_spinner.rs +++ b/src/native/cupertino/cupertino_spinner.rs @@ -63,17 +63,20 @@ impl Default for CupertinoSpinner { impl CupertinoSpinner { /// Creates a new [`CupertinoSpinner`] widget. + #[must_use] pub fn new() -> Self { Self::default() } /// Sets the width of the [`CupertinoSpinner`](CupertinoSpinner). + #[must_use] pub fn width(mut self, width: Length) -> Self { self.width = width; self } /// Sets the height of the [`CupertinoSpinner`](CupertinoSpinner). + #[must_use] pub fn height(mut self, height: Length) -> Self { self.height = height; self @@ -82,13 +85,14 @@ impl CupertinoSpinner { /// Sets the radius of the [`CupertinoSpinner`](CupertinoSpinner). /// NOTE: While you _can_ tweak the radius, the scale may be all out of whack if not using a /// number close to the default of `20.0`. + #[must_use] pub fn radius(mut self, radius: f32) -> Self { self.radius = radius; self } } -impl<'a, Message, B, T> Widget> for CupertinoSpinner +impl Widget> for CupertinoSpinner where B: Backend, { @@ -127,17 +131,17 @@ where let mut hands: Vec<(Path, _)> = vec![]; - for i in 0..HAND_COUNT { + for i in ALPHAS { hands.push(( Path::line(Point::new(0.0, radius / 3.0), Point::new(0.0, radius / 1.5)), move || -> Stroke { // The `60.0` is to shift the original black to dark grey // gen_stroke( width, - Color::from_rgba(0.0, 0.0, 0.0, ALPHAS[i] as f32 / (60.0 + 147.0)), + Color::from_rgba(0.0, 0.0, 0.0, f32::from(i) / (60.0 + 147.0)), ) }, - )) + )); } frame.translate(Vector::new(center.x, center.y)); @@ -171,7 +175,7 @@ where State::new(SpinnerState { now: time::OffsetDateTime::now_local() .unwrap_or_else(|_| time::OffsetDateTime::now_utc()), - spinner: Default::default(), + spinner: Cache::default(), }) } @@ -197,7 +201,7 @@ where return Status::Captured; } - return Status::Ignored; + Status::Ignored } } @@ -211,18 +215,18 @@ where } fn gen_stroke(width: f32, color: Color) -> Stroke<'static> { - return Stroke { + Stroke { width, style: stroke::Style::Solid(color), line_cap: LineCap::Round, ..Stroke::default() - }; + } } const K: f32 = PI * 2.0; fn hand_rotation(n: u16, total: u16) -> f32 { - let turns = n as f32 / total as f32; + let turns = f32::from(n) / f32::from(total); K * turns } diff --git a/src/native/floating_element.rs b/src/native/floating_element.rs index 8f29e9a6..d15d918d 100644 --- a/src/native/floating_element.rs +++ b/src/native/floating_element.rs @@ -4,7 +4,10 @@ use iced_native::{ event, mouse, overlay, Clipboard, Event, Layout, Length, Point, Rectangle, Shell, }; -use iced_native::{widget::Tree, Element, Widget}; +use iced_native::{ + widget::{Operation, Tree}, + Element, Widget, +}; pub mod anchor; pub use anchor::Anchor; @@ -197,6 +200,18 @@ where ); } + fn operate<'b>( + &'b self, + state: &'b mut Tree, + layout: Layout<'_>, + renderer: &Renderer, + operation: &mut dyn Operation, + ) { + self.underlay + .as_widget() + .operate(&mut state.children[0], layout, renderer, operation); + } + fn overlay<'b>( &'b mut self, state: &'b mut Tree, diff --git a/src/native/grid.rs b/src/native/grid.rs index b915f6b9..780e26ae 100644 --- a/src/native/grid.rs +++ b/src/native/grid.rs @@ -4,7 +4,11 @@ use iced_native::{ event, layout::Node, mouse, Clipboard, Event, Layout, Length, Point, Rectangle, Shell, Size, }; -use iced_native::{overlay, widget::Tree, Element, Widget}; +use iced_native::{ + overlay, + widget::{Operation, Tree}, + Element, Widget, +}; /// A container that distributes its contents in a grid. /// @@ -224,6 +228,25 @@ where }) } + fn operate( + &self, + state: &mut Tree, + layout: Layout<'_>, + renderer: &Renderer, + operation: &mut dyn Operation, + ) { + for ((element, state), layout) in self + .elements + .iter() + .zip(&mut state.children) + .zip(layout.children()) + { + element + .as_widget() + .operate(state, layout, renderer, operation); + } + } + fn draw( &self, state: &iced_native::widget::Tree, diff --git a/src/native/menu/menu_inner.rs b/src/native/menu/menu_inner.rs index d9261176..74fda32d 100644 --- a/src/native/menu/menu_inner.rs +++ b/src/native/menu/menu_inner.rs @@ -68,6 +68,7 @@ pub(super) enum Direction { /// Adaptive open direction #[derive(Debug)] +#[allow(clippy::struct_excessive_bools)] struct Aod { // whether or not to use aod horizontal: bool, @@ -209,6 +210,7 @@ struct MenuBounds { check_bounds: Rectangle, } impl MenuBounds { + #[allow(clippy::too_many_arguments)] fn new( menu_tree: &MenuTree<'_, Message, Renderer>, item_width: ItemWidth, diff --git a/src/native/modal.rs b/src/native/modal.rs index 9a7852c3..1469fdac 100644 --- a/src/native/modal.rs +++ b/src/native/modal.rs @@ -1,8 +1,11 @@ //! A modal for showing elements as an overlay on top of another. //! //! *This API requires the following crate features to be activated: modal* -use iced_native::{event, mouse, Clipboard, Event, Layout, Length, Point, Rectangle, Shell}; -use iced_native::{widget::Tree, Element, Widget}; +use iced_native::{ + event, mouse, + widget::{Operation, Tree}, + Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Widget, +}; use super::overlay::modal::ModalOverlay; @@ -233,6 +236,27 @@ where .overlay(position), ) } + + fn operate<'b>( + &'b self, + state: &'b mut Tree, + layout: Layout<'_>, + renderer: &Renderer, + operation: &mut dyn Operation, + ) { + if self.show_modal { + let content = (self.content)(); + content.as_widget().diff(&mut state.children[1]); + + content + .as_widget() + .operate(&mut state.children[1], layout, renderer, operation); + } else { + self.underlay + .as_widget() + .operate(&mut state.children[0], layout, renderer, operation); + } + } } impl<'a, Content, Message, Renderer> From> diff --git a/src/native/spinner.rs b/src/native/spinner.rs index f41d7441..c4a01cb9 100644 --- a/src/native/spinner.rs +++ b/src/native/spinner.rs @@ -54,23 +54,27 @@ where Renderer::Theme: StyleSheet, { /// Creates a new [`Spinner`] widget. + #[must_use] pub fn new() -> Self { Self::default() } /// Sets the width of the [`Spinner`](Spinner). + #[must_use] pub fn width(mut self, width: Length) -> Self { self.width = width; self } /// Sets the height of the [`Spinner`](Spinner). + #[must_use] pub fn height(mut self, height: Length) -> Self { self.height = height; self } /// Sets the circle radius of the spinning circle. + #[must_use] pub fn circle_radius(mut self, radius: f32) -> Self { self.circle_radius = radius; self @@ -108,7 +112,7 @@ fn fill_circle( ); } -impl<'a, Message, Renderer> Widget for Spinner +impl< Message, Renderer> Widget for Spinner where Renderer: iced_native::Renderer, Renderer::Theme: StyleSheet, diff --git a/src/native/wrap.rs b/src/native/wrap.rs index be636fea..06aaeafd 100644 --- a/src/native/wrap.rs +++ b/src/native/wrap.rs @@ -5,9 +5,11 @@ use iced_native::{ event, layout::{Limits, Node}, - mouse, Alignment, Clipboard, Event, Layout, Length, Padding, Point, Rectangle, Shell, Size, + mouse, + widget::{tree::Tree, Operation}, + Alignment, Clipboard, Element, Event, Layout, Length, Padding, Point, Rectangle, Shell, Size, + Widget, }; -use iced_native::{widget::tree::Tree, Element, Widget}; use std::marker::PhantomData; @@ -274,6 +276,25 @@ where ); } } + + fn operate( + &self, + state: &mut Tree, + layout: Layout<'_>, + renderer: &Renderer, + operation: &mut dyn Operation, + ) { + for ((element, state), layout) in self + .elements + .iter() + .zip(&mut state.children) + .zip(layout.children()) + { + element + .as_widget() + .operate(state, layout, renderer, operation); + } + } } impl<'a, Message, Renderer> From> diff --git a/src/style/color_picker.rs b/src/style/color_picker.rs index aa3b1d17..994c9eb3 100644 --- a/src/style/color_picker.rs +++ b/src/style/color_picker.rs @@ -73,15 +73,11 @@ impl StyleSheet for Theme { } fn selected(&self, style: Self::Style) -> Appearance { - Appearance { - ..self.active(style) - } + self.active(style) } fn hovered(&self, style: Self::Style) -> Appearance { - Appearance { - ..self.active(style) - } + self.active(style) } fn focused(&self, style: Self::Style) -> Appearance {