From 143035b1edbdae267088d2d3a0afc55c07b6f2f6 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Mon, 15 Jul 2024 15:51:29 -0400 Subject: [PATCH] gpui_macros: Extract `border_style_methods` macro (#14514) This PR extracts a separate `border_style_methods` macro so that it can be used independently from `style_helpers!`. Release Notes: - N/A --- crates/gpui/src/styled.rs | 16 ++---- crates/gpui_macros/src/gpui_macros.rs | 6 +++ crates/gpui_macros/src/styles.rs | 74 ++++++++++++++++++--------- 3 files changed, 59 insertions(+), 37 deletions(-) diff --git a/crates/gpui/src/styled.rs b/crates/gpui/src/styled.rs index 37102467a6..ff8c86a1d6 100644 --- a/crates/gpui/src/styled.rs +++ b/crates/gpui/src/styled.rs @@ -5,8 +5,9 @@ use crate::{ SharedString, StyleRefinement, WhiteSpace, }; pub use gpui_macros::{ - box_shadow_style_methods, cursor_style_methods, margin_style_methods, overflow_style_methods, - padding_style_methods, position_style_methods, visibility_style_methods, + border_style_methods, box_shadow_style_methods, cursor_style_methods, margin_style_methods, + overflow_style_methods, padding_style_methods, position_style_methods, + visibility_style_methods, }; use taffy::style::{AlignContent, Display}; @@ -23,6 +24,7 @@ pub trait Styled: Sized { gpui_macros::position_style_methods!(); gpui_macros::overflow_style_methods!(); gpui_macros::cursor_style_methods!(); + gpui_macros::border_style_methods!(); gpui_macros::box_shadow_style_methods!(); /// Sets the display type of the element to `block`. @@ -303,16 +305,6 @@ pub trait Styled: Sized { self } - /// Sets the border color of the element. - fn border_color(mut self, border_color: C) -> Self - where - C: Into, - Self: Sized, - { - self.style().border_color = Some(border_color.into()); - self - } - /// Get the text style that has been configured on this element. fn text_style(&mut self) -> &mut Option { let style: &mut StyleRefinement = self.style(); diff --git a/crates/gpui_macros/src/gpui_macros.rs b/crates/gpui_macros/src/gpui_macros.rs index 940214a071..b45624b895 100644 --- a/crates/gpui_macros/src/gpui_macros.rs +++ b/crates/gpui_macros/src/gpui_macros.rs @@ -70,6 +70,12 @@ pub fn cursor_style_methods(input: TokenStream) -> TokenStream { styles::cursor_style_methods(input) } +/// Generates methods for border styles. +#[proc_macro] +pub fn border_style_methods(input: TokenStream) -> TokenStream { + styles::border_style_methods(input) +} + /// Generates methods for box shadow styles. #[proc_macro] pub fn box_shadow_style_methods(input: TokenStream) -> TokenStream { diff --git a/crates/gpui_macros/src/styles.rs b/crates/gpui_macros/src/styles.rs index f399c62683..afbc0e1f26 100644 --- a/crates/gpui_macros/src/styles.rs +++ b/crates/gpui_macros/src/styles.rs @@ -317,6 +317,55 @@ pub fn cursor_style_methods(input: TokenStream) -> TokenStream { output.into() } +pub fn border_style_methods(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as StyleableMacroInput); + let visibility = input.method_visibility; + + let mut methods = Vec::new(); + + for border_style_prefix in border_prefixes() { + methods.push(generate_custom_value_setter( + visibility.clone(), + border_style_prefix.prefix, + quote! { AbsoluteLength }, + &border_style_prefix.fields, + border_style_prefix.doc_string_prefix, + )); + + for border_style_suffix in border_suffixes() { + methods.push(generate_predefined_setter( + visibility.clone(), + border_style_prefix.prefix, + border_style_suffix.suffix, + &border_style_prefix.fields, + &border_style_suffix.width_tokens, + false, + &format!( + "{prefix}\n\n{suffix}", + prefix = border_style_prefix.doc_string_prefix, + suffix = border_style_suffix.doc_string_suffix, + ), + )); + } + } + + let output = quote! { + /// Sets the border color of the element. + #visibility fn border_color(mut self, border_color: C) -> Self + where + C: Into, + Self: Sized, + { + self.style().border_color = Some(border_color.into()); + self + } + + #(#methods)* + }; + + output.into() +} + pub fn box_shadow_style_methods(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as StyleableMacroInput); let visibility = input.method_visibility; @@ -564,31 +613,6 @@ fn generate_methods() -> Vec { } } - for border_style_prefix in border_prefixes() { - methods.push(generate_custom_value_setter( - visibility.clone(), - border_style_prefix.prefix, - quote! { AbsoluteLength }, - &border_style_prefix.fields, - border_style_prefix.doc_string_prefix, - )); - - for border_style_suffix in border_suffixes() { - methods.push(generate_predefined_setter( - visibility.clone(), - border_style_prefix.prefix, - border_style_suffix.suffix, - &border_style_prefix.fields, - &border_style_suffix.width_tokens, - false, - &format!( - "{prefix}\n\n{suffix}", - prefix = border_style_prefix.doc_string_prefix, - suffix = border_style_suffix.doc_string_suffix, - ), - )); - } - } methods }