use crate::{
density::{self, Density},
theme::color,
tokens::{self, ColorRole, TokenLength, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
AnyElement, App, IntoElement, ParentElement, RenderOnce, Styled, Window, div,
prelude::FluentBuilder as _,
};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RowGeometry {
pub min_height: TokenLength,
pub padding_end: TokenLength,
pub padding_start: TokenLength,
}
impl RowGeometry {
pub const fn of(density: Density) -> Self {
Self {
min_height: density.row_lg(),
padding_end: tokens::SPACING_02,
padding_start: tokens::SPACING_05,
}
}
}
#[derive(IntoElement, Default)]
pub struct PatternRow {
children: Vec<AnyElement>,
density: Option<Density>,
}
impl PatternRow {
pub fn new() -> Self {
Self::default()
}
pub fn density(mut self, density: Density) -> Self {
self.density = Some(density);
self
}
}
impl ParentElement for PatternRow {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for PatternRow {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let g = RowGeometry::of(self.density.unwrap_or_else(density::current));
div()
.flex()
.flex_row()
.items_center()
.justify_between()
.min_h(g.min_height)
.pl(g.padding_start)
.pr(g.padding_end)
.border_b_1()
.border_color(color(ColorRole::BorderSubtle01))
.type_style(TypeStyle::BodyCompact01)
.text_color(color(ColorRole::TextPrimary))
.when(!self.children.is_empty(), |this| {
this.children(self.children)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn row_is_density_row_lg_with_asymmetric_padding() {
let g = RowGeometry::of(Density::Compact);
assert_eq!(g.min_height.px_value(), 36.);
assert_eq!(
(g.padding_start.px_value(), g.padding_end.px_value()),
(16., 4.)
);
}
}