wip: apply formatting

This commit is contained in:
Jonathan Kelley committed 2021-07-12 13:03:24 -04:00
1 parent df8bb615de
commit a85b8c4b6b
5 files changed
+36 -41

No files matched your search

+4 -1
View File
@@ -46,12 +46,15 @@ core = []
futures = "0.3.15" futures = "0.3.15"
log = "0.4.14" log = "0.4.14"
num-format = "0.4.0" num-format = "0.4.0"
rand = "0.8.4"
separator = "0.4.1" separator = "0.4.1"
serde = { version = "1.0.126", features = ["derive"] } serde = { version = "1.0.126", features = ["derive"] }
surf = "2.2.0" surf = "2.2.0"
env_logger = "*" env_logger = "*"
async-std = { version = "1.9.0", features = ["attributes"] } async-std = { version = "1.9.0", features = ["attributes"] }
im-rc = "15.0.0"
rand = { version = "0.8.4", features = ["small_rng"] }
fxhash = "0.2.1"
[workspace] [workspace]
members = [ members = [
@@ -6,55 +6,45 @@
//! //!
//! //!
use dioxus::{events::on::MouseEvent, prelude::*};
use dioxus_html as dioxus_elements;
use fxhash::{FxBuildHasher, FxHasher32};
use std::rc::Rc; use std::rc::Rc;
use dioxus::events::on::MouseEvent;
use dioxus_core as dioxus;
use dioxus_core::prelude::*;
use dioxus_web::WebsysRenderer;
use dioxus_html as dioxus_elements;
fn main() { fn main() {
wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
console_error_panic_hook::set_once();
log::debug!("starting!"); log::debug!("starting!");
wasm_bindgen_futures::spawn_local(WebsysRenderer::start(App)); dioxus::desktop::launch(App, |c| c);
} }
// We use a special immutable hashmap to make hashmap operations efficient // We use a special immutable hashmap to make hashmap operations efficient
type RowList = im_rc::HashMap<usize, Rc<str>, FxBuildHasher>; type RowList = im_rc::HashMap<usize, Rc<str>, FxBuildHasher>;
// type RowList = im_rc::HashMap<usize, Rc<str>, nohash_hasher::BuildNoHashHasher<usize>>;
static App: FC<()> = |cx| { static App: FC<()> = |cx| {
let (items, set_items) = use_state_classic(cx, || RowList::default()); let items = use_state(cx, || RowList::default());
let (selection, set_selection) = use_state_classic(cx, || None as Option<usize>);
let create_rendered_rows = move |from, num| move |_| set_items(create_row_list(from, num)); let create_rendered_rows = move |from, num| move |_| items.set(create_row_list(from, num));
let append_1_000_rows = let append_1_000_rows =
move |_| set_items(create_row_list(items.len(), 1000).union(items.clone())); move |_| items.set(create_row_list(items.len(), 1000).union((*items).clone()));
let update_every_10th_row = move |_| { let update_every_10th_row = move |_| {
let mut new_items = items.clone(); let mut new_items = (*items).clone();
let mut small_rng = SmallRng::from_entropy(); let mut small_rng = SmallRng::from_entropy();
new_items new_items.iter_mut().step_by(10).for_each(|(_, val)| {
.iter_mut() *val = create_new_row_label(&mut String::with_capacity(30), &mut small_rng)
.step_by(10) });
.for_each(|(_, val)| *val = create_new_row_label(&mut small_rng)); items.set(new_items);
set_items(new_items);
}; };
let clear_rows = move |_| set_items(RowList::default()); let clear_rows = move |_| items.set(RowList::default());
let swap_rows = move |_| { let swap_rows = move |_| {
// this looks a bit ugly because we're using a hashmap instead of a vec // this looks a bit ugly because we're using a hashmap instead of a vec
if items.len() > 998 { if items.len() > 998 {
let mut new_items = items.clone(); let mut new_items = (*items).clone();
let a = new_items.get(&0).unwrap().clone(); let a = new_items.get(&0).unwrap().clone();
*new_items.get_mut(&0).unwrap() = new_items.get(&998).unwrap().clone(); *new_items.get_mut(&0).unwrap() = new_items.get(&998).unwrap().clone();
*new_items.get_mut(&998).unwrap() = a; *new_items.get_mut(&998).unwrap() = a;
set_items(new_items); items.set(new_items);
} }
}; };
@@ -83,7 +73,7 @@ static App: FC<()> = |cx| {
} }
} }
} }
table { table {
tbody { tbody {
{rows} {rows}
} }
@@ -93,23 +83,28 @@ static App: FC<()> = |cx| {
}) })
}; };
#[derive(Clone)]
struct RowController {}
#[derive(Props)] #[derive(Props)]
struct ActionButtonProps<F: Fn(MouseEvent)> { struct ActionButtonProps<F: Fn(MouseEvent)> {
name: &'static str, name: &'static str,
id: &'static str, id: &'static str,
action: F, action: F,
} }
fn ActionButton<F: Fn(MouseEvent)>(cx: Context<ActionButtonProps<F>>) -> VNode { fn ActionButton<F>(cx: Context<ActionButtonProps<F>>) -> VNode
where
F: Fn(MouseEvent),
{
cx.render(rsx! { cx.render(rsx! {
div { class: "col-sm-6 smallpad" div { class: "col-sm-6 smallpad"
button {class:"btn btn-primary btn-block", r#type: "button", id: "{cx.id}", onclick: {&cx.action}, button { class:"btn btn-primary btn-block", r#type: "button", id: "{cx.id}", onclick: {&cx.action},
"{cx.name}" "{cx.name}"
} }
} }
}) })
} }
#[derive(PartialEq, Props)] #[derive(PartialEq, Props)]
struct RowProps { struct RowProps {
row_id: usize, row_id: usize,
@@ -132,22 +127,25 @@ fn Row<'a>(cx: Context<'a, RowProps>) -> VNode {
}) })
} }
use fxhash::{FxBuildHasher, FxHasher32};
use rand::prelude::*; use rand::prelude::*;
fn create_new_row_label(rng: &mut SmallRng) -> Rc<str> { fn create_new_row_label(label: &mut String, rng: &mut SmallRng) -> Rc<str> {
let mut label = String::new();
label.push_str(ADJECTIVES.choose(rng).unwrap()); label.push_str(ADJECTIVES.choose(rng).unwrap());
label.push(' '); label.push(' ');
label.push_str(COLOURS.choose(rng).unwrap()); label.push_str(COLOURS.choose(rng).unwrap());
label.push(' '); label.push(' ');
label.push_str(NOUNS.choose(rng).unwrap()); label.push_str(NOUNS.choose(rng).unwrap());
Rc::from(label) Rc::from(label.as_ref())
} }
fn create_row_list(from: usize, num: usize) -> RowList { fn create_row_list(from: usize, num: usize) -> RowList {
let mut small_rng = SmallRng::from_entropy(); let mut small_rng = SmallRng::from_entropy();
let mut buf = String::with_capacity(35);
(from..num + from) (from..num + from)
.map(|f| (f, create_new_row_label(&mut small_rng))) .map(|f| {
let o = (f, create_new_row_label(&mut buf, &mut small_rng));
buf.clear();
o
})
.collect::<RowList>() .collect::<RowList>()
} }
-2
View File
@@ -100,7 +100,6 @@ static Example: FC<()> = |cx| {
// Using an "ID" associated with your data is a good idea. // Using an "ID" associated with your data is a good idea.
data.into_iter().map(|(k, v)| rsx!(li { key: "{k}" "{v}" })) data.into_iter().map(|(k, v)| rsx!(li { key: "{k}" "{v}" }))
}} }}
// Matching // Matching
// Matching will throw a Rust error about "no two closures are the same type" // Matching will throw a Rust error about "no two closures are the same type"
@@ -147,7 +146,6 @@ static Example: FC<()> = |cx| {
} }
} }
} }
// Components // Components
// Can accept any paths // Can accept any paths
-2
View File
@@ -1,3 +1 @@
fn main() {} fn main() {}
-2
View File
@@ -1,5 +1,3 @@
pub mod dioxus { pub mod dioxus {
pub mod builder { pub mod builder {
pub struct Builder; pub struct Builder;