Skip to content

Commit

Permalink
Revert "Revert "update CLI and web""
Browse files Browse the repository at this point in the history
This reverts commit f80dd97.
  • Loading branch information
sameer committed Feb 5, 2024
1 parent d7f6a9c commit 8ecce82
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 320 deletions.
365 changes: 165 additions & 200 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ g-code = "0.3"
codespan-reporting = "0.11"
structopt = "0.3"
roxmltree = "0.19"
svgtypes = "0.12"
svgtypes = "0.13"
serde_json = "1"

[[bin]]
Expand Down
5 changes: 4 additions & 1 deletion lib/src/converter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ impl<'a, T: Turtle> visit::XmlVisitor for ConversionVisitor<'a, T> {
self.terrarium.line(true, x, y);
}
} else {
warn!("There is a polyline node containing no actual path: {:?}", node);
warn!(
"There is a polyline node containing no actual path: {:?}",
node
);
}
}

Expand Down
9 changes: 2 additions & 7 deletions lib/src/turtle/g_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,8 @@ impl<'input> Turtle for GCodeTurtle<'input> {

fn move_to(&mut self, to: Point<f64>) {
self.tool_off();
self.program.append(
&mut command!(RapidPositioning {
X: to.x,
Y: to.y,
})
.into_token_vec(),
);
self.program
.append(&mut command!(RapidPositioning { X: to.x, Y: to.y, }).into_token_vec());
}

fn line_to(&mut self, to: Point<f64>) {
Expand Down
4 changes: 2 additions & 2 deletions web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ codespan = "0.11"
serde = "1"
paste = "1"
log = "0.4"
svgtypes = "0.12"
svgtypes = "0.13"
serde_json = "1"
thiserror = "1.0"
zip = { version = "0.6", default-features = false }

yew = { version ="0.21", features = ["csr"] }
yewdux = "0.9.4"
yewdux = "0.10"
web-sys = { version = "0.3", features = [] }
wasm-logger = "0.2"
gloo-file = { version = "0.3", features = ["futures"] }
Expand Down
28 changes: 16 additions & 12 deletions web/src/forms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
use web_sys::{window, Event, FileList, HtmlElement, HtmlInputElement, Response};
use yew::prelude::*;
use yewdux::{functional::use_store, prelude::Dispatch};
use yewdux::{functional::use_store, use_dispatch};

use crate::{
state::{AppState, FormState, Svg},
Expand All @@ -28,7 +28,7 @@ use inputs::*;

#[function_component(SettingsForm)]
pub fn settings_form() -> Html {
let app_dispatch = Dispatch::<AppState>::new();
let app_dispatch = use_dispatch::<AppState>();
let (form_state, form_dispatch) = use_store::<FormState>();

let disabled = form_state.tolerance.is_err()
Expand Down Expand Up @@ -205,8 +205,8 @@ pub fn settings_form() -> Html {

#[function_component(ImportExportModal)]
pub fn import_export_modal() -> Html {
let app_dispatch = Dispatch::<AppState>::new();
let form_dispatch = Dispatch::<FormState>::new();
let app_dispatch = use_dispatch::<AppState>();
let form_dispatch = use_dispatch::<FormState>();

let import_state = use_state(|| Option::<Result<Settings, String>>::None);

Expand Down Expand Up @@ -349,12 +349,12 @@ pub fn import_export_modal() -> Html {

#[function_component(SvgForm)]
pub fn svg_form() -> Html {
let app_dispatch = Dispatch::<AppState>::new();
let app_dispatch = use_dispatch::<AppState>();

let file_upload_state = use_mut_ref(Vec::default);
let file_upload_state_cloned = file_upload_state.clone();
let file_upload_onchange =
app_dispatch.reduce_mut_future_callback_with(move |app, file_list: FileList| {
app_dispatch.future_callback_with(move |app, file_list: FileList| {
let file_upload_state_cloned = file_upload_state_cloned.clone();
Box::pin(async move {
let mut results = Vec::with_capacity(file_list.length() as usize);
Expand Down Expand Up @@ -384,7 +384,9 @@ pub fn svg_form() -> Html {
.borrow_mut()
.push(result.clone().map(|_| ()));
}
app.svgs.extend(results.drain(..).filter_map(Result::ok));
app.reduce_mut(|app| {
app.svgs.extend(results.drain(..).filter_map(Result::ok));
});
})
});

Expand Down Expand Up @@ -420,7 +422,7 @@ pub fn svg_form() -> Html {
let url_input_parsed = url_input_parsed.clone();
let url_add_loading = url_add_loading.clone();

app_dispatch.reduce_mut_future_callback_with(move |app, _| {
app_dispatch.future_callback_with(move |app, _| {
let url_input_state = url_input_state.clone();
let url_input_parsed = url_input_parsed.clone();
let url_add_loading = url_add_loading.clone();
Expand All @@ -447,10 +449,12 @@ pub fn svg_form() -> Html {
&response_url, err
))));
} else {
app.svgs.push(Svg {
content: text,
filename: response_url,
dimensions: [None; 2],
app.reduce_mut(|app| {
app.svgs.push(Svg {
content: text,
filename: response_url,
dimensions: [None; 2],
});
});
};
}
Expand Down
197 changes: 101 additions & 96 deletions web/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ use forms::*;
use state::*;
use ui::*;
use util::*;
use yewdux::prelude::{use_store, Dispatch};
use yewdux::{prelude::use_store, use_dispatch, YewduxRoot};
use zip::{write::FileOptions, CompressionMethod, ZipWriter};

#[function_component]
fn App(_props: &()) -> Html {
let generating = use_state_eq(|| false);
let generating_setter = generating.setter();

let form_dispatch = Dispatch::<FormState>::new();
let form_dispatch = use_dispatch::<FormState>();
let (app_store, app_dispatch) = use_store::<AppState>();

// TODO: come up with a less awkward way to do this.
Expand All @@ -54,7 +54,7 @@ fn App(_props: &()) -> Html {
let opts = FileOptions::default().compression_method(CompressionMethod::Stored);

if app_store.svgs.len() > 1 {
zip.add_directory("svg2gcode_output", opts.clone()).unwrap();
zip.add_directory("svg2gcode_output", opts).unwrap();
}

for svg in app_store.svgs.iter() {
Expand Down Expand Up @@ -109,36 +109,39 @@ fn App(_props: &()) -> Html {
Path::new(svg.filename.as_str()).with_extension("gcode")
};

if app_store.svgs.len() > 1 {
zip.start_file(filepath.to_string_lossy(), opts.clone())
.unwrap();
match app_store.svgs.len() {
0 => unreachable!(),
1 => {
let gcode = {
let mut acc = String::new();
format_gcode_fmt(
&program,
FormatOptions {
checksums: app_store.settings.postprocess.checksums,
line_numbers: app_store.settings.postprocess.line_numbers,
..Default::default()
},
&mut acc,
)
.unwrap();
acc
};
prompt_download(filepath, gcode.as_bytes());
}
_multiple => {
zip.start_file(filepath.to_string_lossy(), opts).unwrap();

format_gcode_io(
&program,
FormatOptions {
checksums: app_store.settings.postprocess.checksums,
line_numbers: app_store.settings.postprocess.line_numbers,
..Default::default()
},
&mut zip,
)
.unwrap();
} else if app_store.svgs.len() == 1 {
let gcode = {
let mut acc = String::new();
format_gcode_fmt(
format_gcode_io(
&program,
FormatOptions {
checksums: app_store.settings.postprocess.checksums,
line_numbers: app_store.settings.postprocess.line_numbers,
..Default::default()
},
&mut acc,
&mut zip,
)
.unwrap();
acc
};
prompt_download(filepath, gcode.as_bytes());
}
}
}

Expand All @@ -160,81 +163,83 @@ fn App(_props: &()) -> Html {
};

html! {
<div class="container">
<div class={classes!("column")}>
<h1>
{ "svg2gcode" }
</h1>
<p>
{ env!("CARGO_PKG_DESCRIPTION") }
</p>
<SvgForm/>
<ButtonGroup>
<Button
title="Generate G-Code"
style={ButtonStyle::Primary}
loading={*generating}
icon={
html_nested! (
<Icon name={IconName::Download} />
)
}
disabled={generate_disabled}
onclick={generate_onclick}
/>
<HyperlinkButton
title="Settings"
style={ButtonStyle::Default}
icon={IconName::Edit}
href="#settings"
/>
</ButtonGroup>
<div class={classes!("card-container", "columns")}>
{
for app_store.svgs.iter().enumerate().map(|(i, svg)| {
let svg_base64 = base64::engine::general_purpose::STANDARD_NO_PAD.encode(svg.content.as_bytes());
let remove_svg_onclick = app_dispatch.reduce_mut_callback(move |app| {
app.svgs.remove(i);
});
let footer = html!{
<Button
title="Remove"
style={ButtonStyle::Primary}
icon={
html_nested!(
<Icon name={IconName::Delete} />
)
}
onclick={remove_svg_onclick}
/>
};
html!{
<div class={classes!("column", "col-6", "col-xs-12")}>
<Card
title={svg.filename.clone()}
img={html_nested!(
<img class="img-responsive" src={format!("data:image/svg+xml;base64,{}", svg_base64)} alt={svg.filename.clone()} />
)}
footer={footer}
/>
</div>
<YewduxRoot>
<div class="container">
<div class={classes!("column")}>
<h1>
{ "svg2gcode" }
</h1>
<p>
{ env!("CARGO_PKG_DESCRIPTION") }
</p>
<SvgForm/>
<ButtonGroup>
<Button
title="Generate G-Code"
style={ButtonStyle::Primary}
loading={*generating}
icon={
html_nested! (
<Icon name={IconName::Download} />
)
}
})
}
disabled={generate_disabled}
onclick={generate_onclick}
/>
<HyperlinkButton
title="Settings"
style={ButtonStyle::Default}
icon={IconName::Edit}
href="#settings"
/>
</ButtonGroup>
<div class={classes!("card-container", "columns")}>
{
for app_store.svgs.iter().enumerate().map(|(i, svg)| {
let svg_base64 = base64::engine::general_purpose::STANDARD_NO_PAD.encode(svg.content.as_bytes());
let remove_svg_onclick = app_dispatch.reduce_mut_callback(move |app| {
app.svgs.remove(i);
});
let footer = html!{
<Button
title="Remove"
style={ButtonStyle::Primary}
icon={
html_nested!(
<Icon name={IconName::Delete} />
)
}
onclick={remove_svg_onclick}
/>
};
html!{
<div class={classes!("column", "col-6", "col-xs-12")}>
<Card
title={svg.filename.clone()}
img={html_nested!(
<img class="img-responsive" src={format!("data:image/svg+xml;base64,{}", svg_base64)} alt={svg.filename.clone()} />
)}
footer={footer}
/>
</div>
}
})
}
</div>
<SettingsForm/>
<ImportExportModal/>
</div>
<div class={classes!("text-right", "column")}>
<p>
{ "See the project " }
<a href={env!("CARGO_PKG_REPOSITORY")}>
{ "on GitHub" }
</a>
{" for support" }
</p>
</div>
<SettingsForm/>
<ImportExportModal/>
</div>
<div class={classes!("text-right", "column")}>
<p>
{ "See the project " }
<a href={env!("CARGO_PKG_REPOSITORY")}>
{ "on GitHub" }
</a>
{" for support" }
</p>
</div>
</div>
</YewduxRoot>
}
}

Expand Down
2 changes: 1 addition & 1 deletion web/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ where
props.default.as_ref(),
node_ref.cast::<HtmlInputElement>(),
) {
input_element.set_value(&default.to_string());
input_element.set_value(default.as_ref());
applied_default_value.set(true);
}

Expand Down

0 comments on commit 8ecce82

Please sign in to comment.