Browse Source

Code improvements

master
Stephen 2 weeks ago
parent
commit
a7a46a91d9
5 changed files with 83 additions and 58 deletions
  1. +0
    -3
      src/components.rs
  2. +27
    -21
      src/components/available.rs
  3. +9
    -0
      src/components/mod.rs
  4. +11
    -4
      src/components/wood.rs
  5. +36
    -30
      src/lib.rs

+ 0
- 3
src/components.rs View File

@ -1,3 +0,0 @@
pub mod available;
pub mod cutlist;
pub mod wood;

+ 27
- 21
src/components/available.rs View File

@ -1,10 +1,16 @@
use crate::components::wood::WoodComponent;
use crate::components::WoodComponent;
use crate::components::WoodMsg;
use yew::prelude::*;
use yew::Properties;
#[derive(Clone)]
pub enum CallbackMsg {
WoodChange(WoodMsg),
CostChange(u32),
}
pub enum Msg {
NameChange(String),
LengthChange(f64),
WoodChange(WoodMsg),
CostChange(String),
}
@ -12,11 +18,9 @@ pub enum Msg {
pub struct AvailableProperties {
pub metric: bool,
pub name: String, // TODO we should auto-generate the names based on the given measurements (?)
pub name_callback: Callback<String>,
pub length: f64, // mm
pub length_callback: Callback<f64>,
pub cost: u32, // cents
pub cost_callback: Callback<u32>,
pub length: f64, // mm
pub cost: u32, // cents
pub callback: Callback<CallbackMsg>,
}
pub struct AvailableComponent {
@ -28,6 +32,13 @@ impl AvailableComponent {
fn cost_string(&self) -> String {
format!("{:.2}", self.props.cost as f64 / 100.0)
}
fn wood_change(&mut self, msg: WoodMsg) {
match msg {
WoodMsg::NameChange(s) => self.props.name = s,
WoodMsg::LengthChange(v) => self.props.length = v,
};
}
}
impl Component for AvailableComponent {
@ -42,22 +53,18 @@ impl Component for AvailableComponent {
let mut change = false;
match msg {
Msg::NameChange(s) => {
self.props.name = s.clone();
self.props.name_callback.emit(s);
change = true;
}
Msg::LengthChange(v) => {
self.props.length = v;
self.props.length_callback.emit(self.props.length);
Msg::WoodChange(x) => {
self.wood_change(x.clone());
self.props.callback.emit(CallbackMsg::WoodChange(x));
change = true;
}
Msg::CostChange(s) => {
if let Ok(cost) = s.parse::<f64>() {
self.props.cost = (cost * 100.0).round() as u32;
self.props.cost_callback.emit(self.props.cost);
self.props
.callback
.emit(CallbackMsg::CostChange(self.props.cost));
}
}
}
@ -80,9 +87,8 @@ impl Component for AvailableComponent {
<WoodComponent
metric=self.props.metric
name=self.props.name.clone()
name_callback=self.link.callback(Msg::NameChange)
length=self.props.length
length_callback=self.link.callback(Msg::LengthChange)/>
callback=self.link.callback(Msg::WoodChange)
length=self.props.length />
// price
<tr>
<td>


+ 9
- 0
src/components/mod.rs View File

@ -0,0 +1,9 @@
pub mod available;
pub mod cutlist;
pub mod wood;
pub use available::AvailableComponent;
pub use available::CallbackMsg as AvailableMsg;
pub use cutlist::CutlistComponent;
pub use wood::CallbackMsg as WoodMsg;
pub use wood::WoodComponent;

+ 11
- 4
src/components/wood.rs View File

@ -1,6 +1,12 @@
use yew::prelude::*;
use yew::Properties;
#[derive(Clone)]
pub enum CallbackMsg {
NameChange(String),
LengthChange(f64),
}
pub enum Msg {
NameChange(String),
FeetChange(String),
@ -12,9 +18,8 @@ pub enum Msg {
pub struct WoodProperties {
pub metric: bool,
pub length: f64, // mm
pub length_callback: Callback<f64>,
pub name: String,
pub name_callback: Callback<String>,
pub callback: Callback<CallbackMsg>,
}
pub struct WoodComponent {
@ -54,7 +59,7 @@ impl Component for WoodComponent {
match msg {
Msg::NameChange(s) => {
self.props.name = s.clone();
self.props.name_callback.emit(s);
self.props.callback.emit(CallbackMsg::NameChange(s));
change = true;
}
@ -81,7 +86,9 @@ impl Component for WoodComponent {
}
if change {
self.props.length_callback.emit(self.props.length);
self.props
.callback
.emit(CallbackMsg::LengthChange(self.props.length));
}
change


+ 36
- 30
src/lib.rs View File

@ -8,9 +8,10 @@ use yew::prelude::*;
use yew::services::DialogService;
mod components;
use components::available::AvailableComponent;
use components::cutlist::CutlistComponent;
use components::wood::WoodComponent;
use components::AvailableMsg;
use components::CutlistComponent;
use components::WoodMsg;
use components::{AvailableComponent, WoodComponent};
mod calculator;
use calculator::{find_best_cuts, CutRow, Req1D, Wood1D};
@ -71,14 +72,11 @@ impl RequiredRow {
enum Msg {
AddAvailable,
ChangeAvailableName(usize, String),
ChangeAvailableLength(usize, f64),
ChangeAvailableCost(usize, u32),
ChangeAvailable(usize, AvailableMsg),
DeleteAvailable(usize),
AddRequired,
ChangeRequiredName(usize, String),
ChangeRequiredLength(usize, f64),
ChangeRequired(usize, WoodMsg),
ChangeRequiredQuantity(usize, String),
DeleteRequired(usize),
@ -127,6 +125,30 @@ impl Model {
"\""
}
}
fn change_available(&mut self, id: usize, msg: AvailableMsg) {
match msg {
AvailableMsg::WoodChange(wood) => match wood {
WoodMsg::NameChange(name) => self.available[id].name = name,
WoodMsg::LengthChange(len) => self.available[id].length = len,
},
AvailableMsg::CostChange(cost) => {
self.available[id].cost = cost;
}
};
}
fn change_required(&mut self, id: usize, msg: WoodMsg) {
match msg {
WoodMsg::NameChange(name) => {
self.required[id].name = name;
}
WoodMsg::LengthChange(len) => {
self.required[id].length = len;
}
}
}
}
impl Component for Model {
@ -149,17 +171,8 @@ impl Component for Model {
match msg {
Msg::AddAvailable => self.available.push(AvailableRow::new()),
Msg::ChangeAvailableName(id, name) => {
self.available[id].name = name;
}
Msg::ChangeAvailableLength(id, len) => {
self.available[id].length = len;
}
Msg::ChangeAvailableCost(id, cost) => {
self.available[id].cost = cost;
change = false;
Msg::ChangeAvailable(id, avail_msg) => {
self.change_available(id, avail_msg);
}
Msg::DeleteAvailable(id) => {
@ -170,12 +183,8 @@ impl Component for Model {
self.required.push(RequiredRow::new());
}
Msg::ChangeRequiredName(id, name) => {
self.required[id].name = name;
}
Msg::ChangeRequiredLength(id, len) => {
self.required[id].length = len;
Msg::ChangeRequired(id, wood_msg) => {
self.change_required(id, wood_msg);
}
Msg::ChangeRequiredQuantity(id, s) => {
@ -318,11 +327,9 @@ impl Component for Model {
<AvailableComponent
metric=self.metric
length=wood.length
length_callback=self.link.callback(move |v| Msg::ChangeAvailableLength(idx, v))
callback=self.link.callback(move |v| Msg::ChangeAvailable(idx, v))
cost=wood.cost
cost_callback=self.link.callback(move |v| Msg::ChangeAvailableCost(idx, v))
name=wood.name.clone()
name_callback=self.link.callback(move |v| Msg::ChangeAvailableName(idx, v))
/>
<tr>
<td>
@ -355,9 +362,8 @@ impl Component for Model {
<WoodComponent
metric=self.metric
length=wood.length
length_callback=self.link.callback(move |v| Msg::ChangeRequiredLength(idx, v))
name=wood.name.clone()
name_callback=self.link.callback(move |v| Msg::ChangeRequiredName(idx, v))
callback=self.link.callback(move |v| Msg::ChangeRequired(idx, v))
/>
<tr>
<td>


Loading…
Cancel
Save