feat: overhaul examples and clean things up

This commit is contained in:
Jonathan Kelley committed 2022-01-03 00:42:17 -05:00
1 parent 601632fc7e
commit 420a30e5d4
33 files changed
+1089 -1417

No files matched your search

+3 -3
View File
@@ -5,7 +5,6 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8" />
<!-- <meta name="viewport" content="width=device-width, initial-scale=1.0 maximum-scale=1.0 user-scalable=no" charset="utf-8" /> -->
</head>
@@ -13,6 +12,7 @@
<div id="main">
</div>
</body>
<script type="text/javascript" src="index.js"> </script>
<script type="text/javascript" src="index.js">
</script>
</html>
</html>
+1 -1
View File
@@ -191,7 +191,7 @@ impl DesktopController {
// create `first_menu`
let mut first_menu = MenuBar::new();
first_menu.add_native_item(MenuItem::About("Todos".to_string()));
first_menu.add_native_item(MenuItem::About("App".to_string()));
first_menu.add_native_item(MenuItem::Services);
first_menu.add_native_item(MenuItem::Separator);
first_menu.add_native_item(MenuItem::Hide);
+52 -31
View File
@@ -3,27 +3,16 @@ use std::{cell::Cell, future::Future, rc::Rc};
pub fn use_future<'a, T: 'static, F: Future<Output = T> + 'static>(
cx: &'a ScopeState,
f: impl FnOnce() -> F,
) -> (Option<&T>, FutureHandle<'a, T>) {
let state = cx.use_hook(|_| {
new_fut: impl FnOnce() -> F,
) -> &'a UseFuture<T> {
let state = cx.use_hook(move |_| {
//
let fut = f();
let slot = Rc::new(Cell::new(None));
let updater = cx.schedule_update();
let _slot = slot.clone();
let new_fut = async move {
let res = fut.await;
_slot.set(Some(res));
updater();
};
let task = cx.push_future(new_fut);
UseFutureInner {
needs_regen: true,
slot,
UseFuture {
update: cx.schedule_update(),
needs_regen: Cell::new(true),
slot: Rc::new(Cell::new(None)),
value: None,
task: Some(task),
task: None,
}
});
@@ -32,23 +21,55 @@ pub fn use_future<'a, T: 'static, F: Future<Output = T> + 'static>(
state.task = None;
}
(
state.value.as_ref(),
FutureHandle {
cx,
value: Cell::new(None),
},
)
if state.needs_regen.get() {
// We don't need regen anymore
state.needs_regen.set(false);
// Create the new future
let fut = new_fut();
// Clone in our cells
let slot = state.slot.clone();
let updater = state.update.clone();
state.task = Some(cx.push_future(async move {
let res = fut.await;
slot.set(Some(res));
updater();
}));
}
state
}
struct UseFutureInner<T> {
needs_regen: bool,
pub struct UseFuture<T> {
update: Rc<dyn Fn()>,
needs_regen: Cell<bool>,
value: Option<T>,
slot: Rc<Cell<Option<T>>>,
task: Option<TaskId>,
}
pub struct FutureHandle<'a, T> {
cx: &'a ScopeState,
value: Cell<Option<T>>,
impl<T> UseFuture<T> {
pub fn restart(&self) {
self.needs_regen.set(true);
(self.update)();
}
// clears the value in the future slot without starting the future over
pub fn clear(&self) -> Option<T> {
(self.update)();
self.slot.replace(None)
}
// Manually set the value in the future slot without starting the future over
pub fn set(&self, new_value: T) {
self.slot.set(Some(new_value));
self.needs_regen.set(true);
(self.update)();
}
pub fn value(&self) -> Option<&T> {
self.value.as_ref()
}
}
+1 -1
View File
@@ -42,7 +42,7 @@ pub fn Link<'a, R: Routable>(cx: Scope<'a, LinkProps<'a, R>>) -> Element {
href: "#",
class: format_args!("{}", cx.props.class.unwrap_or("")),
onclick: move |_| service.push_route(cx.props.to.clone()),
{&cx.props.children}
&cx.props.children
}
})
}
+1 -1
View File
@@ -194,7 +194,7 @@ impl<'a> TextRenderer<'a, '_> {
// when the page is loaded, the `querySelectorAll` will be used to collect all the nodes, and then add
// them interpreter's stack
if let (true, Some(id)) = (self.cfg.pre_render, node.try_mounted_id()) {
write!(f, " dio_el=\"{}\"", id)?;
write!(f, " dioxus-id=\"{}\"", id)?;
for _listener in el.listeners {
// todo: write the listeners
+2 -2
View File
@@ -55,13 +55,13 @@ impl WebsysDom {
// re-hydrate the page - only supports one virtualdom per page
if cfg.hydrate {
// Load all the elements into the arena
let node_list: NodeList = document.query_selector_all("dio_el").unwrap();
let node_list: NodeList = document.query_selector_all("dioxus-id").unwrap();
let len = node_list.length() as usize;
for x in 0..len {
let node: Node = node_list.get(x as u32).unwrap();
let el: &Element = node.dyn_ref::<Element>().unwrap();
let id: String = el.get_attribute("dio_el").unwrap();
let id: String = el.get_attribute("dioxus-id").unwrap();
let id = id.parse::<usize>().unwrap();
nodes[id] = Some(node);
}