use derive_where::derive_where;
use topcoat::{context::Cx, view::*};
use super::{Column, ColumnBase, ColumnWidth};
use crate::{Lens, values::FormScalar};
#[derive_where(Clone, Debug)]
pub struct ImageColumn<M, T> {
lens: Lens<M, T>,
base: ColumnBase,
size: u16,
circular: bool,
remote: bool,
}
impl<M, T> ImageColumn<M, T>
where
M: toasty::schema::Model,
{
pub fn new(lens: Lens<M, T>) -> Self
where
T: toasty::schema::Field<Inner = String>,
{
Self {
base: ColumnBase::new(lens.path(), ColumnWidth::Auto),
lens,
size: 40,
circular: false,
remote: false,
}
}
pub fn size(mut self, pixels: u16) -> Self {
self.size = pixels;
self
}
pub fn circular(mut self) -> Self {
self.circular = true;
self
}
pub fn remote(mut self) -> Self {
self.remote = true;
self
}
base_builders!();
}
impl<M, T> Column<M> for ImageColumn<M, T>
where
M: toasty::schema::Model + Send + Sync + 'static,
T: FormScalar + Send + Sync + 'static,
{
base_column_methods!(binding);
fn text(&self, _cx: &Cx, row: &M) -> String {
self.lens.read(row).to_form()
}
fn cell<'a>(&self, cx: &'a Cx, row: &M) -> BoxView<'a> {
let src = self.text(cx, row).trim().to_string();
let shown = if self.remote {
crate::schema::is_linkable(&src)
} else {
crate::schema::is_rooted(&src)
};
if !shown {
return crate::schema::value_cell(cx, &src);
}
let src = crate::schema::file_href(cx, &src);
let class = if self.circular {
"shrink-0 rounded-full object-cover ring-1 ring-foreground/10"
} else {
"shrink-0 rounded-md object-cover ring-1 ring-foreground/10"
};
let size = self.size.to_string();
view! {
cx =>
<img
src=(src)
alt=""
loading="lazy"
referrerpolicy="no-referrer"
width=(size.clone())
height=(size)
class=(class)
>
}
.boxed()
}
}