Skip to content

Commit 3b78e57

Browse files
committed
refactor: Cleaned up some code
1 parent 833e124 commit 3b78e57

File tree

1 file changed

+46
-53
lines changed

1 file changed

+46
-53
lines changed

elements-namespace/src/lib.rs

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ builder_constructors! {
8989
};
9090
}
9191

92+
// I am still bad at Macros so I created this element directly
9293
#[allow(non_camel_case_types)]
9394
pub struct image;
9495

@@ -111,68 +112,56 @@ impl image {
111112
}
112113
}
113114

114-
pub trait GlobalAttributes {
115-
fn prevent_default<'a>(&self, cx: NodeFactory<'a>, val: Arguments) -> Attribute<'a> {
116-
cx.attr("dioxus-prevent-default", val, None, false)
117-
}
118-
}
119-
120-
pub trait SvgAttributes {
121-
fn prevent_default<'a>(&self, cx: NodeFactory<'a>, val: Arguments) -> Attribute<'a> {
122-
cx.attr("dioxus-prevent-default", val, None, false)
123-
}
124-
}
125-
126115
pub mod on {
127116
use dioxus_core::*;
128117
use dioxus_html::on::{MouseData, MouseEvent, WheelData, WheelEvent};
129118

130119
use bumpalo::boxed::Box as BumpBox;
131120

132121
macro_rules! event_directory {
133-
( $(
134-
$( #[$attr:meta] )*
135-
$wrapper:ident($data:ident): [
136-
$(
137-
$( #[$method_attr:meta] )*
138-
$name:ident
139-
)*
140-
];
141-
)* ) => {
142-
$(
122+
( $(
123+
$( #[$attr:meta] )*
124+
$wrapper:ident($data:ident): [
125+
$(
126+
$( #[$method_attr:meta] )*
127+
$name:ident
128+
)*
129+
];
130+
)* ) => {
143131
$(
144-
$(#[$method_attr])*
145-
pub fn $name<'a>(
146-
factory: NodeFactory<'a>,
147-
mut callback: impl FnMut($wrapper) + 'a,
148-
// mut callback: impl FnMut(UiEvent<$data>) + 'a,
149-
) -> Listener<'a>
150-
{
151-
let bump = &factory.bump();
152-
153-
// we can't allocate unsized in bumpalo's box, so we need to craft the box manually
154-
// safety: this is essentially the same as calling Box::new() but manually
155-
// The box is attached to the lifetime of the bumpalo allocator
156-
let cb: &mut dyn FnMut(AnyEvent) = bump.alloc(move |evt: AnyEvent| {
157-
let event = evt.downcast::<$data>().unwrap();
158-
callback(event)
159-
});
160-
161-
let callback: BumpBox<dyn FnMut(AnyEvent) + 'a> = unsafe { BumpBox::from_raw(cb) };
162-
163-
// ie oncopy
164-
let event_name = stringify!($name);
165-
166-
// ie copy
167-
let shortname: &'static str = &event_name[2..];
168-
169-
let handler = bump.alloc(std::cell::RefCell::new(Some(callback)));
170-
factory.listener(shortname, handler)
171-
}
132+
$(
133+
$(#[$method_attr])*
134+
pub fn $name<'a>(
135+
factory: NodeFactory<'a>,
136+
mut callback: impl FnMut($wrapper) + 'a,
137+
// mut callback: impl FnMut(UiEvent<$data>) + 'a,
138+
) -> Listener<'a>
139+
{
140+
let bump = &factory.bump();
141+
142+
// we can't allocate unsized in bumpalo's box, so we need to craft the box manually
143+
// safety: this is essentially the same as calling Box::new() but manually
144+
// The box is attached to the lifetime of the bumpalo allocator
145+
let cb: &mut dyn FnMut(AnyEvent) = bump.alloc(move |evt: AnyEvent| {
146+
let event = evt.downcast::<$data>().unwrap();
147+
callback(event)
148+
});
149+
150+
let callback: BumpBox<dyn FnMut(AnyEvent) + 'a> = unsafe { BumpBox::from_raw(cb) };
151+
152+
// ie oncopy
153+
let event_name = stringify!($name);
154+
155+
// ie copy
156+
let shortname: &'static str = &event_name[2..];
157+
158+
let handler = bump.alloc(std::cell::RefCell::new(Some(callback)));
159+
factory.listener(shortname, handler)
160+
}
161+
)*
172162
)*
173-
)*
174-
};
175-
}
163+
};
164+
}
176165

177166
event_directory! {
178167
MouseEvent(MouseData): [
@@ -186,3 +175,7 @@ pub mod on {
186175
];
187176
}
188177
}
178+
179+
pub trait GlobalAttributes {}
180+
181+
pub trait SvgAttributes {}

0 commit comments

Comments
 (0)