the refactor i've wanted most

SourcePlate and DestinationPlate are now the same
why did i not do this originally omg
This commit is contained in:
Emilia Allison 2024-02-10 21:57:30 -05:00
parent 0625854895
commit 338a7b98c7
Signed by: emilia
GPG Key ID: 05D5D1107E5100A1
6 changed files with 142 additions and 291 deletions

View File

@ -1,216 +0,0 @@
#![allow(non_snake_case)]
use std::collections::HashMap;
use yew::prelude::*;
use yewdux::prelude::*;
use crate::components::states::{CurrentTransfer, MainState};
use crate::data::plate_instances::PlateInstance;
use crate::data::transfer::Transfer;
use crate::data::transfer_region::Region;
// Color Palette for the Source Plates, can be changed here
use crate::components::plates::util::Palettes;
const PALETTE: super::util::ColorPalette = Palettes::RAINBOW;
use super::super::transfer_menu::{num_to_letters, RegionDisplay};
#[derive(Properties, PartialEq)]
pub struct DestinationPlateProps {
pub source_plate: PlateInstance,
pub destination_plate: PlateInstance,
pub cell_height: f64,
}
#[function_component]
pub fn DestinationPlate(props: &DestinationPlateProps) -> Html {
let (main_state, _) = use_store::<MainState>();
let (ct_state, ct_dispatch) = use_store::<CurrentTransfer>();
let m_start_handle: UseStateHandle<Option<(u8, u8)>> = use_state_eq(|| None);
let m_end_handle: UseStateHandle<Option<(u8, u8)>> = use_state_eq(|| None);
let m_stat_handle: UseStateHandle<bool> = use_state_eq(|| false);
if !(*m_stat_handle) {
let (pt1, pt2) = match ct_state.transfer.transfer_region.dest_region {
Region::Point((x, y)) => ((x, y), (x, y)),
Region::Rect(c1, c2) => (c1, c2),
Region::Custom(_) => ((0,0), (0,0)),
};
m_start_handle.set(Some(pt1));
m_end_handle.set(Some(pt2));
}
let destination_wells = ct_state.transfer.transfer_region.get_destination_wells();
let ordered_ids: Vec<uuid::Uuid> = {
let mut ids: Vec<uuid::Uuid> = main_state.transfers.clone().iter()
.map(|x| x.id)
.collect();
ids.sort_unstable();
ids
};
let mouse_callback = {
let m_start_handle = m_start_handle.clone();
let m_end_handle = m_end_handle.clone();
let m_stat_handle = m_stat_handle.clone();
Callback::from(move |(i, j, t)| match t {
MouseEventType::Mousedown => {
m_start_handle.set(Some((i, j)));
m_end_handle.set(Some((i, j)));
m_stat_handle.set(true);
}
MouseEventType::Mouseenter => {
if *m_stat_handle {
m_end_handle.set(Some((i, j)));
}
}
})
};
let transfer_map = {
let ts = main_state
.transfers
.iter()
.filter(|t| t.dest_id == props.destination_plate.get_uuid());
let mut tooltip_map: HashMap<(u8, u8), Vec<&Transfer>> = HashMap::new();
for t in ts {
let dws = t.transfer_region.get_destination_wells();
for dw in dws {
if let Some(val) = tooltip_map.get_mut(&dw) {
val.push(t);
} else {
tooltip_map.insert(dw, vec![t]);
}
}
}
tooltip_map
};
let mouseup_callback = {
let m_start_handle = m_start_handle.clone();
let m_end_handle = m_end_handle.clone();
Callback::from(move |_: MouseEvent| {
m_stat_handle.set(false);
if let Some(ul) = *m_start_handle {
if let Some(br) = *m_end_handle {
if let Ok(rd) = RegionDisplay::try_from((ul.0, ul.1, br.0, br.1)) {
ct_dispatch.reduce_mut(|state| {
state.transfer.transfer_region.dest_region = Region::from(&rd);
});
}
}
}
})
};
let mouseleave_callback = Callback::clone(&mouseup_callback);
let screenshot_callback = Callback::from(|_| {
let _ = js_sys::eval("copy_screenshot_dest()");
});
let column_header = {
let headers = (1..=props.destination_plate.plate.size().1)
.map(|j| {
html! {<th>{format!("{:0>2}", j)}</th>}
})
.collect::<Html>();
html! {<tr><th />{ headers }</tr>}
};
let rows = (1..=props.destination_plate.plate.size().0)
.map(|i| {
let row_header = html! {<th>{num_to_letters(i)}</th>};
let row = (1..=props.destination_plate.plate.size().1).map(|j| {
html! {
<DestPlateCell i={i} j={j}
selected={super::source_plate::in_rect(*m_start_handle.clone(), *m_end_handle.clone(), (i,j))}
mouse={mouse_callback.clone()}
in_transfer={destination_wells.contains(&(i,j)) && main_state.preferences.in_transfer_hashes}
color={transfer_map.get(&(i,j))
.and_then(|t| t.last())
.map(|t| PALETTE.get_ordered(t.get_uuid(), &ordered_ids))
}
cell_height={props.cell_height}
title={transfer_map.get(&(i,j)).map(|transfers| format!("Used by: {}", transfers.iter().map(|t| t.name.clone())
.collect::<Vec<_>>().join(", ")))}
/>
}
}).collect::<Html>();
html! {
<tr>
{ row_header }{ row }
</tr>
}
})
.collect::<Html>();
html! {
<div ondblclick={screenshot_callback}
class={classes!{"dest_plate",
"W".to_owned()+&props.source_plate.plate.plate_format.to_string()}}>
<table
onmouseup={move |e| {
mouseup_callback.emit(e);
}}
onmouseleave={move |e| {
mouseleave_callback.emit(e);
}}>
{ column_header }{ rows }
</table>
</div>
}
}
#[derive(Debug)]
pub enum MouseEventType {
Mousedown,
Mouseenter,
}
#[derive(Properties, PartialEq)]
pub struct DestPlateCellProps {
pub i: u8,
pub j: u8,
pub selected: bool,
pub mouse: Callback<(u8, u8, MouseEventType)>,
pub in_transfer: Option<bool>,
color: Option<[f64; 3]>,
cell_height: f64,
title: Option<String>,
}
#[function_component]
fn DestPlateCell(props: &DestPlateCellProps) -> Html {
let selected_class = match props.selected {
true => Some("current_select"),
false => None,
};
let in_transfer_class = match props.in_transfer {
Some(true) => Some("in_transfer"),
_ => None,
};
let color = props.color.unwrap_or([255.0, 255.0, 255.0]);
let mouse = Callback::clone(&props.mouse);
let mouse2 = Callback::clone(&props.mouse);
let (i, j) = (props.i, props.j);
html! {
<td class={classes!("plate_cell", selected_class, in_transfer_class)}
style={format!("height: {}px;", props.cell_height)}
onmousedown={move |_| {
mouse.emit((i,j, MouseEventType::Mousedown))
}}
onmouseenter={move |_| {
mouse2.emit((i,j, MouseEventType::Mouseenter))
}}>
<div class="plate_cell_inner"
style={format!("background: rgba({},{},{},1);", color[0], color[1], color[2])}
title={if let Some(text) = &props.title {
text.clone()
} else { "".to_string() }}/>
</td>
}
}

View File

@ -1,4 +1,5 @@
pub mod destination_plate;
pub mod plate_container; pub mod plate_container;
pub mod source_plate;
mod util; mod util;
mod plate_callbacks;
mod plate_data;
mod plate;

View File

@ -5,7 +5,7 @@ use yew::prelude::*;
use yewdux::prelude::*; use yewdux::prelude::*;
use crate::components::states::{CurrentTransfer, MainState}; use crate::components::states::{CurrentTransfer, MainState};
use crate::data::plate_instances::PlateInstance; use crate::data::plate::PlateType;
use crate::data::transfer::Transfer; use crate::data::transfer::Transfer;
use crate::data::transfer_region::Region; use crate::data::transfer_region::Region;
@ -15,56 +15,60 @@ const PALETTE: super::util::ColorPalette = Palettes::RAINBOW;
use super::super::transfer_menu::{num_to_letters, RegionDisplay}; use super::super::transfer_menu::{num_to_letters, RegionDisplay};
#[derive(PartialEq, Properties)] use super::plate_data::*;
pub struct SourcePlateProps { use super::plate_callbacks;
pub source_plate: PlateInstance,
pub destination_plate: PlateInstance,
pub cell_height: f64,
}
#[function_component] #[function_component]
pub fn SourcePlate(props: &SourcePlateProps) -> Html { pub fn Plate(props: &PlateProps) -> Html {
let (main_state, _) = use_store::<MainState>(); let (main_state, _) = use_store::<MainState>();
let (ct_state, ct_dispatch) = use_store::<CurrentTransfer>(); let (ct_state, ct_dispatch) = use_store::<CurrentTransfer>();
let m_start_handle: UseStateHandle<Option<(u8, u8)>> = use_state_eq(|| None); let m_start_handle: MStartHandle = use_state_eq(|| None);
let m_end_handle: UseStateHandle<Option<(u8, u8)>> = use_state_eq(|| None); let m_end_handle: MEndHandle = use_state_eq(|| None);
let m_stat_handle: UseStateHandle<bool> = use_state_eq(|| false); let m_stat_handle: MStatHandle = use_state_eq(|| false);
if !(*m_stat_handle) { if !(*m_stat_handle) {
let (pt1, pt2) = match ct_state.transfer.transfer_region.source_region { let region = match props.ptype {
PlateType::Source => ct_state.transfer.transfer_region.source_region.clone(),
PlateType::Destination => ct_state.transfer.transfer_region.dest_region.clone(),
};
let (pt1, pt2) = match region {
Region::Point((x, y)) => ((x, y), (x, y)), Region::Point((x, y)) => ((x, y), (x, y)),
Region::Rect(c1, c2) => (c1, c2), Region::Rect(c1, c2) => (c1, c2),
Region::Custom(_) => ((0,0), (0,0)), Region::Custom(_) => ((0, 0), (0, 0)),
}; };
m_start_handle.set(Some(pt1)); m_start_handle.set(Some(pt1));
m_end_handle.set(Some(pt2)); m_end_handle.set(Some(pt2));
} }
let transfer_map = { let transfer_map = {
let ts = main_state let transfers = main_state.transfers.iter().filter(|t| match props.ptype {
.transfers PlateType::Source => t.source_id == props.source_plate.get_uuid(),
.iter() PlateType::Destination => t.dest_id == props.destination_plate.get_uuid(),
.filter(|t| t.source_id == props.source_plate.get_uuid()); });
let mut tooltip_map: HashMap<(u8, u8), Vec<&Transfer>> = HashMap::new(); let mut tooltip_map: HashMap<(u8, u8), Vec<&Transfer>> = HashMap::new();
for t in ts { for transfer in transfers {
let sws = t.transfer_region.get_source_wells(); let wells = match props.ptype {
for sw in sws { PlateType::Source => transfer.transfer_region.get_source_wells(),
if let Some(val) = tooltip_map.get_mut(&sw) { PlateType::Destination => transfer.transfer_region.get_destination_wells(),
val.push(t); };
for well in wells {
if let Some(val) = tooltip_map.get_mut(&well) {
val.push(transfer);
} else { } else {
tooltip_map.insert(sw, vec![t]); tooltip_map.insert(well, vec![transfer]);
} }
} }
} }
tooltip_map tooltip_map
}; };
let source_wells = ct_state.transfer.transfer_region.get_source_wells(); let wells = match props.ptype {
PlateType::Source => ct_state.transfer.transfer_region.get_source_wells(),
PlateType::Destination => ct_state.transfer.transfer_region.get_destination_wells(),
};
let ordered_ids: Vec<uuid::Uuid> = { let ordered_ids: Vec<uuid::Uuid> = {
let mut ids: Vec<uuid::Uuid> = main_state.transfers.clone().iter() let mut ids: Vec<uuid::Uuid> = main_state.transfers.clone().iter().map(|x| x.id).collect();
.map(|x| x.id)
.collect();
ids.sort_unstable(); ids.sort_unstable();
ids ids
}; };
@ -73,37 +77,13 @@ pub fn SourcePlate(props: &SourcePlateProps) -> Html {
let m_start_handle = m_start_handle.clone(); let m_start_handle = m_start_handle.clone();
let m_end_handle = m_end_handle.clone(); let m_end_handle = m_end_handle.clone();
let m_stat_handle = m_stat_handle.clone(); let m_stat_handle = m_stat_handle.clone();
plate_callbacks::mouse_callback(m_start_handle, m_end_handle, m_stat_handle)
Callback::from(move |(i, j, t)| match t {
MouseEventType::Mousedown => {
m_start_handle.set(Some((i, j)));
m_end_handle.set(Some((i, j)));
m_stat_handle.set(true);
}
MouseEventType::Mouseenter => {
if *m_stat_handle {
m_end_handle.set(Some((i, j)));
}
}
})
}; };
let mouseup_callback = { let mouseup_callback = {
let m_start_handle = m_start_handle.clone(); let m_start_handle = m_start_handle.clone();
let m_end_handle = m_end_handle.clone(); let m_end_handle = m_end_handle.clone();
plate_callbacks::mouseup_callback(m_start_handle, m_end_handle, m_stat_handle, ct_dispatch)
Callback::from(move |_: MouseEvent| {
m_stat_handle.set(false);
if let Some(ul) = *m_start_handle {
if let Some(br) = *m_end_handle {
if let Ok(rd) = RegionDisplay::try_from((ul.0, ul.1, br.0, br.1)) {
ct_dispatch.reduce_mut(|state| {
state.transfer.transfer_region.source_region = Region::from(&rd);
});
}
}
}
})
}; };
let mouseleave_callback = Callback::clone(&mouseup_callback); let mouseleave_callback = Callback::clone(&mouseup_callback);
@ -112,8 +92,20 @@ pub fn SourcePlate(props: &SourcePlateProps) -> Html {
let _ = js_sys::eval("copy_screenshot_src()"); let _ = js_sys::eval("copy_screenshot_src()");
}); });
let width = match props.ptype {
PlateType::Source => props.source_plate.plate.size().1,
PlateType::Destination => props.destination_plate.plate.size().1,
};
let height = match props.ptype {
PlateType::Source => props.source_plate.plate.size().0,
PlateType::Destination => props.destination_plate.plate.size().0,
};
let pformat = match props.ptype {
PlateType::Source => props.source_plate.plate.plate_format,
PlateType::Destination => props.destination_plate.plate.plate_format,
};
let column_header = { let column_header = {
let headers = (1..=props.source_plate.plate.size().1) let headers = (1..=width)
.map(|j| { .map(|j| {
html! {<th> html! {<th>
{format!("{:0>2}", j)} {format!("{:0>2}", j)}
@ -122,16 +114,17 @@ pub fn SourcePlate(props: &SourcePlateProps) -> Html {
.collect::<Html>(); .collect::<Html>();
html! {<tr><th />{ headers }</tr>} html! {<tr><th />{ headers }</tr>}
}; };
let rows = (1..=props.source_plate.plate.size().0) let rows =
(1..=height)
.map(|i| { .map(|i| {
let row_header = html! {<th>{num_to_letters(i)}</th>}; let row_header = html! {<th>{num_to_letters(i)}</th>};
let row = (1..=props.source_plate.plate.size().1) let row = (1..=width)
.map(|j| { .map(|j| {
html! { html! {
<SourcePlateCell i={i} j={j} <PlateCell i={i} j={j}
selected={in_rect(*m_start_handle.clone(), *m_end_handle.clone(), (i,j))} selected={in_rect(*m_start_handle.clone(), *m_end_handle.clone(), (i,j))}
mouse={mouse_callback.clone()} mouse={mouse_callback.clone()}
in_transfer={source_wells.contains(&(i,j)) && main_state.preferences.in_transfer_hashes} in_transfer={wells.contains(&(i,j)) && main_state.preferences.in_transfer_hashes}
color={transfer_map.get(&(i,j)) color={transfer_map.get(&(i,j))
.and_then(|t| t.last()) .and_then(|t| t.last())
.map(|t| PALETTE.get_ordered(t.get_uuid(), &ordered_ids)) .map(|t| PALETTE.get_ordered(t.get_uuid(), &ordered_ids))
@ -153,8 +146,11 @@ pub fn SourcePlate(props: &SourcePlateProps) -> Html {
html! { html! {
<div ondblclick={screenshot_callback} <div ondblclick={screenshot_callback}
class={classes!{"source_plate", class={classes!{match props.ptype {
"W".to_owned()+&props.source_plate.plate.plate_format.to_string()}}> PlateType::Source => "source_plate",
PlateType::Destination => "dest_plate",
},
"W".to_owned()+&pformat.to_string()}}>
<table <table
onmouseup={move |e| { onmouseup={move |e| {
mouseup_callback.emit(e); mouseup_callback.emit(e);
@ -170,7 +166,7 @@ pub fn SourcePlate(props: &SourcePlateProps) -> Html {
} }
#[derive(PartialEq, Properties)] #[derive(PartialEq, Properties)]
pub struct SourcePlateCellProps { pub struct PlateCellProps {
i: u8, i: u8,
j: u8, j: u8,
selected: bool, selected: bool,
@ -180,14 +176,9 @@ pub struct SourcePlateCellProps {
cell_height: f64, cell_height: f64,
title: Option<String>, title: Option<String>,
} }
#[derive(Debug)]
pub enum MouseEventType {
Mousedown,
Mouseenter,
}
#[function_component] #[function_component]
fn SourcePlateCell(props: &SourcePlateCellProps) -> Html { fn PlateCell(props: &PlateCellProps) -> Html {
let selected_class = match props.selected { let selected_class = match props.selected {
true => Some("current_select"), true => Some("current_select"),
false => None, false => None,

View File

@ -0,0 +1,50 @@
use yew::prelude::*;
use yewdux::prelude::*;
use crate::components::states::CurrentTransfer;
use crate::data::transfer_region::Region;
// Color Palette for the Source Plates, can be changed here
use super::super::transfer_menu::RegionDisplay;
use super::plate_data::*;
pub fn mouse_callback(
m_start_handle: MStartHandle,
m_end_handle: MEndHandle,
m_stat_handle: MStatHandle,
) -> Callback<(u8, u8, MouseEventType)> {
Callback::from(move |(i, j, t)| match t {
MouseEventType::Mousedown => {
m_start_handle.set(Some((i, j)));
m_end_handle.set(Some((i, j)));
m_stat_handle.set(true);
}
MouseEventType::Mouseenter => {
if *m_stat_handle {
m_end_handle.set(Some((i, j)));
}
}
})
}
pub fn mouseup_callback(
m_start_handle: MStartHandle,
m_end_handle: MEndHandle,
m_stat_handle: MStatHandle,
ct_dispatch: Dispatch<CurrentTransfer>,
) -> Callback<MouseEvent> {
Callback::from(move |_: MouseEvent| {
m_stat_handle.set(false);
if let Some(ul) = *m_start_handle {
if let Some(br) = *m_end_handle {
if let Ok(rd) = RegionDisplay::try_from((ul.0, ul.1, br.0, br.1)) {
ct_dispatch.reduce_mut(|state| {
state.transfer.transfer_region.source_region = Region::from(&rd);
});
}
}
}
})
}

View File

@ -3,10 +3,12 @@ use wasm_bindgen::prelude::Closure;
use wasm_bindgen::JsCast; use wasm_bindgen::JsCast;
use yew::prelude::*; use yew::prelude::*;
use crate::data::plate::PlateType;
use crate::data::plate_instances::PlateInstance; use crate::data::plate_instances::PlateInstance;
use super::destination_plate::DestinationPlate; // use super::destination_plate::DestinationPlate;
use super::source_plate::SourcePlate; // use super::source_plate::SourcePlate;
use super::plate::Plate;
#[derive(Properties, PartialEq)] #[derive(Properties, PartialEq)]
pub struct PlateContainerProps { pub struct PlateContainerProps {
@ -53,13 +55,13 @@ pub fn PlateContainer(props: &PlateContainerProps) -> Html {
if let Some(dpi) = props.destination_dims.clone() { if let Some(dpi) = props.destination_dims.clone() {
<div class="plate_container--source"> <div class="plate_container--source">
<h2>{spi.name.clone()}</h2> <h2>{spi.name.clone()}</h2>
<SourcePlate source_plate={spi.clone()} destination_plate={dpi.clone()} <Plate source_plate={spi.clone()} destination_plate={dpi.clone()}
cell_height={cell_height}/> cell_height={cell_height} ptype={PlateType::Source}/>
</div> </div>
<div class="plate_container--destination"> <div class="plate_container--destination">
<h2>{dpi.name.clone()}</h2> <h2>{dpi.name.clone()}</h2>
<DestinationPlate source_plate={spi.clone()} destination_plate={dpi.clone()} <Plate source_plate={spi.clone()} destination_plate={dpi.clone()}
cell_height={cell_height}/> cell_height={cell_height} ptype={PlateType::Destination}/>
</div> </div>
} else { } else {
<h2>{"No Destination Plate Selected"}</h2> <h2>{"No Destination Plate Selected"}</h2>

View File

@ -0,0 +1,23 @@
use serde::{Serialize, Deserialize};
use yew::prelude::*;
use crate::data::plate_instances::PlateInstance;
use crate::data::plate::PlateType;
#[derive(PartialEq, Properties)]
pub struct PlateProps {
pub source_plate: PlateInstance,
pub destination_plate: PlateInstance,
pub cell_height: f64,
pub ptype: PlateType,
}
pub type MStartHandle = UseStateHandle<Option<(u8, u8)>>;
pub type MEndHandle = UseStateHandle<Option<(u8, u8)>>;
pub type MStatHandle = UseStateHandle<bool>;
#[derive(Debug)]
pub enum MouseEventType {
Mousedown,
Mouseenter,
}