From 2701be91e33cb6fd4a3e5c9a6c710482db0b0d6c Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 18 Sep 2023 20:47:40 -0600 Subject: [PATCH] Add negative style helpers --- crates/gpui2_macros/src/styleable_helpers.rs | 45 ++++++++++++++----- crates/storybook/src/ui/component/facepile.rs | 8 ++-- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/crates/gpui2_macros/src/styleable_helpers.rs b/crates/gpui2_macros/src/styleable_helpers.rs index 2bc9c738e9..238e64ed14 100644 --- a/crates/gpui2_macros/src/styleable_helpers.rs +++ b/crates/gpui2_macros/src/styleable_helpers.rs @@ -44,7 +44,19 @@ fn generate_methods() -> Vec { prefix, suffix, &fields, - length_tokens, + &length_tokens, + false, + doc_string, + )); + } + + if suffix != "auto" { + methods.push(generate_predefined_setter( + prefix, + suffix, + &fields, + &length_tokens, + true, doc_string, )); } @@ -63,7 +75,8 @@ fn generate_methods() -> Vec { prefix, suffix, &fields, - radius_tokens, + &radius_tokens, + false, doc_string, )); } @@ -75,7 +88,8 @@ fn generate_methods() -> Vec { prefix, suffix, &fields, - width_tokens, + &width_tokens, + false, doc_string, )); } @@ -84,23 +98,30 @@ fn generate_methods() -> Vec { } fn generate_predefined_setter( - prefix: &'static str, - suffix: &'static str, + name: &'static str, + length: &'static str, fields: &Vec, - length_tokens: TokenStream2, + length_tokens: &TokenStream2, + negate: bool, doc_string: &'static str, ) -> TokenStream2 { - let method_name = if suffix.is_empty() { - format_ident!("{}", prefix) + let (negation_prefix, negation_token) = if negate { + ("neg_", quote! { - }) } else { - format_ident!("{}_{}", prefix, suffix) + ("", quote! {}) + }; + + let method_name = if length.is_empty() { + format_ident!("{}{}", negation_prefix, name) + } else { + format_ident!("{}{}_{}", negation_prefix, name, length) }; let field_assignments = fields .iter() .map(|field_tokens| { quote! { - style.#field_tokens = Some(gpui2::geometry::#length_tokens.into()); + style.#field_tokens = Some((#negation_token gpui2::geometry::#length_tokens).into()); } }) .collect::>(); @@ -114,6 +135,10 @@ fn generate_predefined_setter( } }; + if negate { + dbg!(method.to_string()); + } + method } diff --git a/crates/storybook/src/ui/component/facepile.rs b/crates/storybook/src/ui/component/facepile.rs index 38dbc55fb0..73ab231c07 100644 --- a/crates/storybook/src/ui/component/facepile.rs +++ b/crates/storybook/src/ui/component/facepile.rs @@ -1,6 +1,4 @@ -use crate::theme::theme; -use crate::ui::Avatar; -use gpui2::geometry::rems; +use crate::{theme::theme, ui::Avatar}; use gpui2::style::StyleHelpers; use gpui2::{elements::div, IntoElement}; use gpui2::{Element, ParentElement, ViewContext}; @@ -19,9 +17,9 @@ impl Facepile { let theme = theme(cx); let player_count = self.players.len(); let player_list = self.players.iter().enumerate().map(|(ix, player)| { - let before_last = ix < player_count - 1; + let isnt_last = ix < player_count - 1; div() - .when(before_last, |div| div.mr(-rems(0.5))) + .when(isnt_last, |div| div.neg_mr_1()) .child(player.clone()) }); div().p_1().flex().items_center().children(player_list)