teksilo_widgets/styles/
recipe_badge_style.rs1use teksilo_core::build_context::BuildContext;
17use teksilo_core::color_prop::ColorProp;
18use teksilo_core::styles::{BadgeStyle, BadgeStyleConfig};
19use teksilo_core::widget_id::WidgetId;
20use teksilo_tokens::{CornerRadius, InputTokens, SurfaceRole};
21
22use crate::primitives::{Padding, RectWidget, ZStack};
23use teksilo_core::styles::density::spacing;
24
25pub const BADGE_PADDING_HORIZONTAL: f32 = 6.0;
27pub const BADGE_PADDING_VERTICAL: f32 = 1.0;
28pub const BADGE_CORNER_RADIUS: f32 = 9999.0;
31
32#[derive(Debug, Clone, Copy, PartialEq)]
35pub struct BadgeRecipe {
36 pub padding_horizontal: f32,
37 pub padding_vertical: f32,
38 pub corner_radius: f32,
39}
40
41impl BadgeRecipe {
42 pub fn for_tokens(tokens: &InputTokens) -> Self {
48 Self {
49 padding_horizontal: spacing(BADGE_PADDING_HORIZONTAL, tokens),
50 padding_vertical: spacing(BADGE_PADDING_VERTICAL, tokens),
51 corner_radius: BADGE_CORNER_RADIUS,
52 }
53 }
54}
55
56impl Default for BadgeRecipe {
57 fn default() -> Self {
58 Self::for_tokens(&InputTokens::default())
59 }
60}
61
62#[derive(Debug, Default, Clone, Copy)]
65pub struct RecipeBadgeStyle {
66 pub recipe: BadgeRecipe,
67}
68
69impl RecipeBadgeStyle {
70 pub fn new(recipe: BadgeRecipe) -> Self {
71 Self { recipe }
72 }
73
74 pub fn for_tokens(tokens: &InputTokens) -> Self {
81 Self {
82 recipe: BadgeRecipe::for_tokens(tokens),
83 }
84 }
85}
86
87impl BadgeStyle for RecipeBadgeStyle {
88 fn make_body(&self, cfg: &BadgeStyleConfig, ctx: &mut BuildContext) -> WidgetId {
89 let bg: ColorProp = cfg
90 .background_override
91 .clone()
92 .unwrap_or(ColorProp::SurfaceRole(SurfaceRole::AccentSubtle));
93 let bg_rect = ctx.add(
94 RectWidget::new()
95 .background(bg)
96 .corner_radius(CornerRadius::uniform(self.recipe.corner_radius)),
97 );
98 let padding_id = ctx.add(
99 Padding::symmetric(self.recipe.padding_vertical, self.recipe.padding_horizontal)
100 .child(cfg.content),
101 );
102 ctx.add(ZStack::new().child(bg_rect).child(padding_id))
103 }
104}