Skip to content

Commit

Permalink
Run cargo clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Sep 28, 2023
1 parent 16d19b9 commit 951948f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 25 deletions.
9 changes: 6 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ impl<T> Context<T> {

// Create the window layout
let root_layout = self.tree[root].layout_key.unwrap();
let mut style = Style::default();
style.size = Size::from_points(size.width as _, size.height as _);
let style = Style {
size: Size::from_points(size.width as _, size.height as _),
..Default::default()
};

let window = self.taffy.new_with_children(style, &[root_layout]).unwrap();

// Compute the layout of the taffy tree.
Expand All @@ -130,7 +133,7 @@ impl<T> Context<T> {
for item in self.tree.iter_mut(root) {
match item {
ItemMut::Node { node, level: _ } => {
let mut layout = self.taffy.layout(node.layout_key.unwrap()).unwrap().clone();
let mut layout = *self.taffy.layout(node.layout_key.unwrap()).unwrap();
if let Some(parent_layout) = stack.last() {
layout.location.x += parent_layout.location.x;
layout.location.y += parent_layout.location.y;
Expand Down
8 changes: 5 additions & 3 deletions src/element/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use taffy::{
style::{AlignItems, Dimension, FlexDirection, JustifyContent, LengthPercentage},
};

type Handler<T> = Box<dyn FnMut(&mut Context<T>, event::MouseEvent)>;

#[derive(Clone, Copy, PartialEq, Eq)]
/// Element attribute kind.
pub enum AttributeKind {
Expand Down Expand Up @@ -56,13 +58,13 @@ pub enum AttributeValue<T> {
JustifyContent(JustifyContent),

/// Click handler attribute value.
OnClick(Box<dyn FnMut(&mut Context<T>, event::MouseEvent)>),
OnClick(Handler<T>),

/// Mouse in handler attribute value.
OnMouseIn(Box<dyn FnMut(&mut Context<T>, event::MouseEvent)>),
OnMouseIn(Handler<T>),

// Mouse out handler attribute value.
OnMouseOut(Box<dyn FnMut(&mut Context<T>, event::MouseEvent)>),
OnMouseOut(Handler<T>),

/// Color attribute value.
Color(Color4f),
Expand Down
2 changes: 1 addition & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<T> Node<T> {
*data = Some(TextData { text_blob, font });
data.as_ref().unwrap().text_blob.as_ref().unwrap()
};
let bounds = text_blob.bounds().clone();
let bounds = *text_blob.bounds();

// TODO this is a measure func for paragraphs
taffy
Expand Down
14 changes: 10 additions & 4 deletions src/render.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{Context, NodeKey, Window};
use slotmap::{DefaultKey, SlotMap};

use std::{
collections::HashMap,
future::Future,
Expand All @@ -13,14 +14,19 @@ use winit::{
window::WindowId,
};

#[derive(Clone, Copy, Debug)]
pub struct UpdateError;

type Update<T> = Box<dyn FnOnce(&mut Context<T>) + Send>;

pub struct Updater<T> {
tx: mpsc::Sender<UserEvent<T>>,
}

impl<T> Updater<T> {
/// Send an update to the UI tree.
pub fn update(&self, f: Box<dyn FnOnce(&mut Context<T>) + Send>) -> Result<(), ()> {
self.tx.send(UserEvent::Update(f)).map_err(|_| ())
pub fn update(&self, f: Update<T>) -> Result<(), UpdateError> {
self.tx.send(UserEvent::Update(f)).map_err(|_| UpdateError)
}
}

Expand All @@ -41,13 +47,13 @@ impl<T> Scope<T> {
}

/// Send an update to the UI tree.
pub fn update(&self, f: Box<dyn FnOnce(&mut Context<T>) + Send>) -> Result<(), ()> {
pub fn update(&self, f: Update<T>) -> Result<(), UpdateError> {
self.updater.update(f)
}
}

pub(crate) enum UserEvent<T> {
Update(Box<dyn FnOnce(&mut Context<T>) + Send>),
Update(Update<T>),
FrameRequest,
}

Expand Down
2 changes: 1 addition & 1 deletion src/window/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Builder {
})
.unwrap()
})
.map_err(|error| Error::Display(error))?;
.map_err(Error::Display)?;
let window = window.ok_or(Error::Window)?;
let raw_window_handle = window.raw_window_handle();

Expand Down
24 changes: 11 additions & 13 deletions src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ impl Window {
root: NodeKey,
event: WindowEvent,
) -> Result<Option<ControlFlow>, Error> {
// TODO
#[allow(deprecated)]
match event {
WindowEvent::CloseRequested => {
return Ok(Some(ControlFlow::Exit));
Expand Down Expand Up @@ -200,25 +202,21 @@ impl Window {
..
} => {
if let Some(pos) = self.cursor_pos {
match delta {
MouseScrollDelta::PixelDelta(px_delta) => {
let pos = Point::new(pos.x, pos.y);
if let Some(target) = tree.tree.target_with_filter(
root,
Point::new(pos.x, pos.y),
|node| {
if let MouseScrollDelta::PixelDelta(px_delta) = delta {
let pos = Point::new(pos.x, pos.y);
if let Some(target) =
tree.tree
.target_with_filter(root, Point::new(pos.x, pos.y), |node| {
if let NodeData::Element(ref elem) = node.data {
elem.overflow_y() == Some(Overflow::Scroll)
} else {
false
}
},
) {
let mut node = tree.node(target);
node.scroll(Size::new(px_delta.x, px_delta.y));
}
})
{
let mut node = tree.node(target);
node.scroll(Size::new(px_delta.x, px_delta.y));
}
_ => {}
}
}
}
Expand Down

0 comments on commit 951948f

Please sign in to comment.