use teksilo_core::build_context::BuildContext;
use teksilo_core::color_prop::ColorProp;
use teksilo_core::styles::{ToastStyle, ToastStyleConfig};
use teksilo_core::widget_id::WidgetId;
use teksilo_tokens::{CornerRadius, VAlignment};
use crate::primitives::{Expand, HStack, Padding, RectWidget, ZStack};
pub const TOAST_PADDING_HORIZONTAL: f32 = 14.0;
pub const TOAST_PADDING_VERTICAL: f32 = 12.0;
pub const TOAST_CORNER_RADIUS: f32 = 8.0;
pub const TOAST_GLYPH_SIZE: f32 = 16.0;
pub const TOAST_CONTENT_GAP: f32 = 12.0;
pub const TOAST_TITLE_BODY_GAP: f32 = 2.0;
pub const TOAST_BODY_ACTIONS_GAP: f32 = 8.0;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ToastRecipe {
pub padding_horizontal: f32,
pub padding_vertical: f32,
pub corner_radius: f32,
pub glyph_size: f32,
pub content_gap: f32,
pub title_body_gap: f32,
pub body_actions_gap: f32,
}
impl Default for ToastRecipe {
fn default() -> Self {
Self {
padding_horizontal: TOAST_PADDING_HORIZONTAL,
padding_vertical: TOAST_PADDING_VERTICAL,
corner_radius: TOAST_CORNER_RADIUS,
glyph_size: TOAST_GLYPH_SIZE,
content_gap: TOAST_CONTENT_GAP,
title_body_gap: TOAST_TITLE_BODY_GAP,
body_actions_gap: TOAST_BODY_ACTIONS_GAP,
}
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct RecipeToastStyle {
pub recipe: ToastRecipe,
}
impl RecipeToastStyle {
pub fn new(recipe: ToastRecipe) -> Self {
Self { recipe }
}
}
impl ToastStyle for RecipeToastStyle {
fn make_body(&self, cfg: &ToastStyleConfig, ctx: &mut BuildContext) -> WidgetId {
let radius = CornerRadius::uniform(self.recipe.corner_radius);
let bg = ctx.add(
RectWidget::new()
.background(ColorProp::SurfaceRole(cfg.severity.surface()))
.corner_radius(radius),
);
let body = ctx.add(Expand::horizontal().child_id(cfg.content));
let mut row = HStack::new()
.spacing(self.recipe.content_gap)
.alignment(VAlignment::Top)
.add_child(cfg.leading_glyph)
.add_child(body);
if let Some(close_id) = cfg.trailing_close {
row = row.add_child(close_id);
}
let row_id = ctx.add(row);
let padded = ctx.add(
Padding::symmetric(self.recipe.padding_vertical, self.recipe.padding_horizontal)
.child_id(row_id),
);
ctx.add(ZStack::new().add_child(bg).add_child(padded))
}
}