From f53170b16bedd10539176755898b3b09146631ad Mon Sep 17 00:00:00 2001 From: erius Date: Sat, 25 May 2024 02:30:16 +0300 Subject: [PATCH] Moved matrix yew components to file matrix.rs, minor refactoring --- web/src/main.rs | 97 ++------------------------------------ web/src/matrix.rs | 116 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 94 deletions(-) create mode 100644 web/src/matrix.rs diff --git a/web/src/main.rs b/web/src/main.rs index 31d1253..732cb49 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -1,98 +1,7 @@ -use math::matrix::Matrix; +mod matrix; + use yew::prelude::*; - -pub struct MatrixComponent { - matrix: Matrix -} - -pub enum MatrixMsg { - CellChange(Cell), - AddRow, - AddColumn, - RemoveRow, - RemoveColumn -} - -impl Component for MatrixComponent { - type Message = MatrixMsg; - type Properties = (); - - fn create(_ctx: &Context) -> Self { - Self { matrix: Matrix::new_zeroes(3, 3) } - } - - fn update(&mut self, _ctx: &Context, msg: Self::Message) -> bool { - match msg { - MatrixMsg::CellChange(cell) => self.matrix[cell.i][cell.j] = cell.value, - MatrixMsg::AddRow => self.matrix.append_row_zeroes(), - MatrixMsg::AddColumn => self.matrix.append_column_zeroes(), - MatrixMsg::RemoveRow => (), - MatrixMsg::RemoveColumn => () - } - return true; - } - - fn view(&self, ctx: &Context) -> Html { - let on_cell_change = ctx.link().callback(MatrixMsg::CellChange); - let on_click_add_row = ctx.link().callback(|_| MatrixMsg::AddRow); - let on_click_add_column = ctx.link().callback(|_| MatrixMsg::AddColumn); - html! { -
- { - self.matrix.iter_rows().enumerate().map(|(i, row)| html! { - { - row.iter().enumerate().map(|(j, e)| { - let cell = Cell { i, j, value: *e }; - html! { - - } - }).collect::() - } - - }).collect::() - } -
- -
- - -
- } - } -} - -pub struct MatrixCellComponent; - -#[derive(PartialEq, Properties)] -pub struct MatrixCellProps { - pub cell: Cell, - pub on_cell_change: Callback -} - -#[derive(PartialEq, Clone, Copy)] -pub struct Cell { - i: usize, j: usize, value: f64 -} - -impl Component for MatrixCellComponent { - type Message = (); - type Properties = MatrixCellProps; - - fn create(_ctx: &Context) -> Self { - Self - } - - fn view(&self, ctx: &Context) -> Html { - let cell = ctx.props().cell.clone(); - let oninput = ctx.props().on_cell_change - .reform(move |e: InputEvent| - Cell { i: cell.i, j: cell.j, value: e.as_f64().unwrap_or_default() } - ); - html! { - - } - } -} +use matrix::MatrixComponent; #[function_component] fn App() -> Html { diff --git a/web/src/matrix.rs b/web/src/matrix.rs new file mode 100644 index 0000000..4178382 --- /dev/null +++ b/web/src/matrix.rs @@ -0,0 +1,116 @@ +use math::matrix::Matrix; +use yew::prelude::*; + +pub enum MatrixMsg { + CellChange(Cell), + AddRow, + AddColumn, + RemoveRow, + RemoveColumn +} + +pub struct MatrixComponent { + matrix: Matrix +} + +impl Component for MatrixComponent { + type Message = MatrixMsg; + type Properties = (); + + fn create(_ctx: &Context) -> Self { + Self { matrix: Matrix::new_zeroes(3, 3) } + } + + fn update(&mut self, _ctx: &Context, msg: Self::Message) -> bool { + match msg { + MatrixMsg::CellChange(cell) => self.matrix[cell.i][cell.j] = cell.value, + MatrixMsg::AddRow => self.matrix.append_row_zeroes(), + MatrixMsg::AddColumn => self.matrix.append_column_zeroes(), + MatrixMsg::RemoveRow => { + if self.matrix.height() > 2 { + self.matrix.remove_last_row(); + } + }, + MatrixMsg::RemoveColumn => { + if self.matrix.width() > 2 { + self.matrix.remove_last_column(); + } + } + } + return true; + } + + fn view(&self, ctx: &Context) -> Html { + let on_size_change = ctx.link().callback(|msg| msg); + html! { +
+ { + self.matrix.iter_rows().enumerate().map(|(i, row)| html! { + { + row.iter().enumerate().map(|(j, e)| { + let cell = Cell { i, j, value: *e }; + let on_cell_change = ctx.link().callback(MatrixMsg::CellChange); + html! { + + } + }).collect::() + } + + }).collect::() + } +
+ +
+ +
+ } + } +} + +#[derive(PartialEq, Properties)] +pub struct MatrixCellProps { + pub cell: Cell, + pub on_cell_change: Callback +} + +#[derive(PartialEq, Clone, Copy)] +pub struct Cell { + i: usize, j: usize, value: f64 +} + +#[function_component] +pub fn MatrixCellComponent(props: &MatrixCellProps) -> Html { + let cell = props.cell.clone(); + let oninput = props.on_cell_change + .reform(move |e: InputEvent| + Cell { i: cell.i, j: cell.j, value: e.as_f64().unwrap_or_default() } + ); + html! { + + } +} + +#[derive(PartialEq, Properties)] +pub struct MatrixSizeControlsProps { + on_size_change: Callback +} + +#[function_component] +pub fn MatrixSizeControls(props: &MatrixSizeControlsProps) -> Html { + let on_click_add_row = props.on_size_change.reform(|_| MatrixMsg::AddRow); + let on_click_add_column = props.on_size_change.reform(|_| MatrixMsg::AddColumn); + let on_click_remove_row = props.on_size_change.reform(|_|MatrixMsg::RemoveRow); + let on_click_remove_column = props.on_size_change.reform(|_| MatrixMsg::RemoveColumn); + html! { +
+
+ + +
+
+ + +
+
+ } +}