From 06439585ebbe034e694858ada44467635f29f77c Mon Sep 17 00:00:00 2001 From: doryan Date: Wed, 14 Aug 2024 18:51:44 +0400 Subject: [PATCH] feat(input): add macro for generation build_*() method code --- src/view/components/input.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/view/components/input.rs b/src/view/components/input.rs index 01935fd..a855c53 100644 --- a/src/view/components/input.rs +++ b/src/view/components/input.rs @@ -5,6 +5,41 @@ use gtk::{prelude::*, *}; pub type InputLabel = String; +macro_rules! build_for { + ( $(($comp:ty,$name:ident)),* ) => { + $( + pub fn $name(self, input_height: Option) -> Input<$comp> { + let input_component = Box::new(Orientation::Vertical, 0); + + let input_label = Label::builder() + .halign(self.align.horizontal) + .valign(self.align.vertical) + .set_margin(self.margins) + .label(self.label) + .build(); + + let mut input_builder = <$comp>::builder() + .set_margin(MarginData::EqualsMargin(6)); + + if let Some(height) = input_height { + input_builder = input_builder.height_request(height); + } + + let input = input_builder.build(); + + input_component.append(&input_label); + input_component.append(&input); + + Input { + component: input_component, + input: input.clone(), + input_label: input_label.clone(), + } + } + )* + }; +} + #[derive(Clone, Debug)] pub struct Input { component: Box, @@ -101,4 +136,5 @@ impl InputBuilder { input_label: text_view_label.clone(), } } + build_for!((TextView, build), (Entry, build_entry)); }