use derive_setters::Setters;
use tessera_ui::{
Color, ComputedData, Dp, LayoutInput, LayoutOutput, LayoutSpec, MeasurementError, Px,
RenderInput, tessera,
};
use crate::pipelines::checkmark::command::CheckmarkCommand;
#[derive(Clone, Setters)]
pub struct CheckmarkArgs {
pub color: Color,
pub stroke_width: f32,
pub progress: f32,
pub padding: [f32; 2],
pub size: Dp,
}
impl std::fmt::Debug for CheckmarkArgs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CheckmarkArgs")
.field("color", &self.color)
.field("stroke_width", &self.stroke_width)
.field("progress", &self.progress)
.field("padding", &self.padding)
.field("size", &self.size)
.finish()
}
}
impl Default for CheckmarkArgs {
fn default() -> Self {
Self {
color: Color::new(0.0, 0.6, 0.0, 1.0),
stroke_width: 5.0,
progress: 1.0,
padding: [2.0, 2.0],
size: Dp(20.0),
}
}
}
#[derive(Clone, PartialEq)]
struct CheckmarkLayout {
size: Px,
color: Color,
stroke_width: f32,
progress: f32,
padding: [f32; 2],
}
impl LayoutSpec for CheckmarkLayout {
fn measure(
&self,
_input: &LayoutInput<'_>,
_output: &mut LayoutOutput<'_>,
) -> Result<ComputedData, MeasurementError> {
Ok(ComputedData {
width: self.size,
height: self.size,
})
}
fn record(&self, input: &RenderInput<'_>) {
let command = CheckmarkCommand::new()
.with_color(self.color)
.with_stroke_width(self.stroke_width)
.with_progress(self.progress)
.with_padding(self.padding[0], self.padding[1]);
input.metadata_mut().push_draw_command(command);
}
}
#[tessera]
pub fn checkmark(args: impl Into<CheckmarkArgs>) {
let args: CheckmarkArgs = args.into();
let size_px = args.size.to_px();
layout(CheckmarkLayout {
size: Px::new(size_px.to_f32() as i32),
color: args.color,
stroke_width: args.stroke_width,
progress: args.progress,
padding: args.padding,
});
}