Skip to content

Visual Studio Code, rebuilt in Rust

Rust
2.7 KB
Raw Blame
use gpui::{Div, div, prelude::*, px};

/// iconlabel.css `.label-description` after a 13px name: `font-size: 0.9em`, `margin-left: 0.5em` of its own size.
/// It is an inline span, so the name's 13px strut sets its line box and baseline.
pub(super) fn label_description() -> Div {
    let mut description = div().ml(px(5.85)).text_size(px(11.7));
    description.text_style().strut_size = Some(px(13.).into());
    description
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_description_is_0_9em_and_sits_0_5em_of_itself_after_the_name() {
        let mut description = label_description();
        let style = description.style();
        assert_eq!(style.margin.left, Some(px(5.85).into()));
        assert_eq!(style.text.font_size, Some(px(11.7).into()));
    }

    #[derive(Default)]
    struct Row {
        description: gpui::TextLayout,
    }

    impl gpui::Render for Row {
        fn render(&mut self, _: &mut gpui::Window, _: &mut gpui::Context<Self>) -> impl gpui::IntoElement {
            let description = gpui::StyledText::new("b");
            self.description = description.layout().clone();
            div()
                .flex()
                .items_center()
                .h(px(22.))
                .text_size(px(13.))
                .child(div().debug_selector(|| "name".into()).w(px(10.3)).child("a"))
                .child(label_description().debug_selector(|| "description".into()).w(px(1.)).child(description))
        }
    }

    #[gpui::test]
    fn the_description_sits_at_the_exact_sum_of_name_and_margin(cx: &mut gpui::TestAppContext) {
        let (_, cx) = cx.add_window_view(|_, _| Row::default());
        cx.run_until_parked();
        // 10.3 + 5.85 = 16.15px snaps to 32 device px at 2x; rounding each length first gave 21 + 12 = 33.
        assert_eq!(f32::from(cx.debug_bounds("description").unwrap().origin.x), 16.);
    }

    #[gpui::test]
    fn the_description_s_line_box_is_the_name_s(cx: &mut gpui::TestAppContext) {
        let (_, cx) = cx.add_window_view(|_, _| Row::default());
        cx.run_until_parked();
        let (name, description) = (cx.debug_bounds("name").unwrap(), cx.debug_bounds("description").unwrap());
        assert_eq!((description.origin.y, description.size.height), (name.origin.y, name.size.height));
    }

    #[gpui::test]
    fn the_description_s_text_is_painted_at_its_unsnapped_x(cx: &mut gpui::TestAppContext) {
        let (row, cx) = cx.add_window_view(|_, _| Row::default());
        cx.run_until_parked();
        let x = row.read_with(cx, |row, _| f32::from(row.description.bounds().origin.x));
        // 10.3 + 5.85 in LayoutUnits of 1/64 device px at 2x, where the box snapped to 16.
        assert_eq!(x, 16.15625);
    }
}