use crate::{
icons::Icon,
theme::color,
tokens::{ColorRole, REM_PX},
};
use gpui_kit::{App, IntoElement, ParentElement, RenderOnce, Styled, Window, div, rems};
pub const DEFAULT_SIZE: f32 = 80.;
pub fn icon_size(size: f32) -> f32 {
(size * 0.5).round()
}
#[derive(IntoElement)]
pub struct Pictogram {
icon: Icon,
size: f32,
}
impl Pictogram {
pub fn new(icon: impl Into<Icon>) -> Self {
Self {
icon: icon.into(),
size: DEFAULT_SIZE,
}
}
pub fn size(mut self, size: f32) -> Self {
self.size = size;
self
}
}
impl RenderOnce for Pictogram {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let side = rems(self.size / REM_PX);
div()
.flex()
.flex_none()
.items_center()
.justify_center()
.size(side)
.bg(color(ColorRole::LayerAccent01))
.child(
self.icon
.size_length(rems(icon_size(self.size) / REM_PX))
.color(color(ColorRole::IconSecondary)),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn icon_is_half_the_square_rounded() {
assert_eq!(icon_size(DEFAULT_SIZE), 40.);
assert_eq!(icon_size(64.), 32.);
assert_eq!(icon_size(120.), 60.);
assert_eq!(icon_size(45.), 23.);
}
}