Skip to content

Commit

Permalink
fixed Clippy Warnings and Added Operate to the different widgets that…
Browse files Browse the repository at this point in the history
… hold Elements
  • Loading branch information
genusistimelord committed May 8, 2023
1 parent 37136fc commit 32b36b4
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 46 deletions.
18 changes: 6 additions & 12 deletions examples/cupertino/cupertino_spinner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ impl State {
async fn load() -> Result<State, ()> {
println!("Doing stuff...");
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
return Ok(Self {
Ok(Self {
hello: "Loaded!".to_string(),
});
})
}
}

Expand All @@ -53,16 +53,10 @@ impl Application for Spinner {
}

fn update(&mut self, message: Message) -> Command<Message> {
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()
Expand Down
18 changes: 9 additions & 9 deletions examples/split_scroller/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Application for MyApp {
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::RouterMessage(router_msg) => {
return self.router.update(router_msg).map(Message::RouterMessage);
self.router.update(router_msg).map(Message::RouterMessage)
}
}

Expand All @@ -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,
}
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -145,8 +145,8 @@ mod router {
}
pub fn view(&self) -> Element<Message> {
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),
}
}
}
Expand Down
33 changes: 30 additions & 3 deletions src/native/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -447,6 +448,32 @@ where
)
}

fn operate<'b>(
&'b self,
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<Message>,
) {
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,
Expand Down
22 changes: 13 additions & 9 deletions src/native/cupertino/cupertino_spinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Message, Renderer<B, T>> for CupertinoSpinner
impl<Message, B, T> Widget<Message, Renderer<B, T>> for CupertinoSpinner
where
B: Backend,
{
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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(),
})
}

Expand All @@ -197,7 +201,7 @@ where
return Status::Captured;
}

return Status::Ignored;
Status::Ignored
}
}

Expand All @@ -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
}
17 changes: 16 additions & 1 deletion src/native/floating_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -197,6 +200,18 @@ where
);
}

fn operate<'b>(
&'b self,
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<Message>,
) {
self.underlay
.as_widget()
.operate(&mut state.children[0], layout, renderer, operation);
}

fn overlay<'b>(
&'b mut self,
state: &'b mut Tree,
Expand Down
25 changes: 24 additions & 1 deletion src/native/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -224,6 +228,25 @@ where
})
}

fn operate(
&self,
state: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<Message>,
) {
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,
Expand Down
2 changes: 2 additions & 0 deletions src/native/menu/menu_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -209,6 +210,7 @@ struct MenuBounds {
check_bounds: Rectangle,
}
impl MenuBounds {
#[allow(clippy::too_many_arguments)]
fn new<Message, Renderer>(
menu_tree: &MenuTree<'_, Message, Renderer>,
item_width: ItemWidth,
Expand Down
28 changes: 26 additions & 2 deletions src/native/modal.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -233,6 +236,27 @@ where
.overlay(position),
)
}

fn operate<'b>(
&'b self,
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn Operation<Message>,
) {
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<Modal<'a, Content, Message, Renderer>>
Expand Down
6 changes: 5 additions & 1 deletion src/native/spinner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -108,7 +112,7 @@ fn fill_circle(
);
}

impl<'a, Message, Renderer> Widget<Message, Renderer> for Spinner<Renderer>
impl< Message, Renderer> Widget<Message, Renderer> for Spinner<Renderer>
where
Renderer: iced_native::Renderer,
Renderer::Theme: StyleSheet,
Expand Down
Loading

0 comments on commit 32b36b4

Please sign in to comment.