style_helpers.rs

  1use crate::{
  2    self as gpui3, relative, rems, AlignItems, Display, Fill, FlexDirection, Hsla, JustifyContent,
  3    Length, Position, SharedString, Style, StyleRefinement, Styled, TextStyleRefinement,
  4};
  5
  6pub trait StyleHelpers: Sized + Styled<Style = Style> {
  7    gpui3_macros::style_helpers!();
  8
  9    fn h(mut self, height: Length) -> Self {
 10        self.declared_style().size.height = Some(height);
 11        self
 12    }
 13
 14    /// size_{n}: Sets width & height to {n}
 15    ///
 16    /// Example:
 17    /// size_1: Sets width & height to 1
 18    fn size(mut self, size: Length) -> Self {
 19        self.declared_style().size.height = Some(size);
 20        self.declared_style().size.width = Some(size);
 21        self
 22    }
 23
 24    fn full(mut self) -> Self {
 25        self.declared_style().size.width = Some(relative(1.).into());
 26        self.declared_style().size.height = Some(relative(1.).into());
 27        self
 28    }
 29
 30    fn relative(mut self) -> Self {
 31        self.declared_style().position = Some(Position::Relative);
 32        self
 33    }
 34
 35    fn absolute(mut self) -> Self {
 36        self.declared_style().position = Some(Position::Absolute);
 37        self
 38    }
 39
 40    fn block(mut self) -> Self {
 41        self.declared_style().display = Some(Display::Block);
 42        self
 43    }
 44
 45    fn flex(mut self) -> Self {
 46        self.declared_style().display = Some(Display::Flex);
 47        self
 48    }
 49
 50    fn flex_col(mut self) -> Self {
 51        self.declared_style().flex_direction = Some(FlexDirection::Column);
 52        self
 53    }
 54
 55    fn flex_row(mut self) -> Self {
 56        self.declared_style().flex_direction = Some(FlexDirection::Row);
 57        self
 58    }
 59
 60    fn flex_1(mut self) -> Self {
 61        self.declared_style().flex_grow = Some(1.);
 62        self.declared_style().flex_shrink = Some(1.);
 63        self.declared_style().flex_basis = Some(relative(0.).into());
 64        self
 65    }
 66
 67    fn flex_auto(mut self) -> Self {
 68        self.declared_style().flex_grow = Some(1.);
 69        self.declared_style().flex_shrink = Some(1.);
 70        self.declared_style().flex_basis = Some(Length::Auto);
 71        self
 72    }
 73
 74    fn flex_initial(mut self) -> Self {
 75        self.declared_style().flex_grow = Some(0.);
 76        self.declared_style().flex_shrink = Some(1.);
 77        self.declared_style().flex_basis = Some(Length::Auto);
 78        self
 79    }
 80
 81    fn flex_none(mut self) -> Self {
 82        self.declared_style().flex_grow = Some(0.);
 83        self.declared_style().flex_shrink = Some(0.);
 84        self
 85    }
 86
 87    fn grow(mut self) -> Self {
 88        self.declared_style().flex_grow = Some(1.);
 89        self
 90    }
 91
 92    fn items_start(mut self) -> Self {
 93        self.declared_style().align_items = Some(AlignItems::FlexStart);
 94        self
 95    }
 96
 97    fn items_end(mut self) -> Self {
 98        self.declared_style().align_items = Some(AlignItems::FlexEnd);
 99        self
100    }
101
102    fn items_center(mut self) -> Self {
103        self.declared_style().align_items = Some(AlignItems::Center);
104        self
105    }
106
107    fn justify_between(mut self) -> Self {
108        self.declared_style().justify_content = Some(JustifyContent::SpaceBetween);
109        self
110    }
111
112    fn justify_center(mut self) -> Self {
113        self.declared_style().justify_content = Some(JustifyContent::Center);
114        self
115    }
116
117    fn justify_start(mut self) -> Self {
118        self.declared_style().justify_content = Some(JustifyContent::Start);
119        self
120    }
121
122    fn justify_end(mut self) -> Self {
123        self.declared_style().justify_content = Some(JustifyContent::End);
124        self
125    }
126
127    fn justify_around(mut self) -> Self {
128        self.declared_style().justify_content = Some(JustifyContent::SpaceAround);
129        self
130    }
131
132    fn fill<F>(mut self, fill: F) -> Self
133    where
134        F: Into<Fill>,
135        Self: Sized,
136    {
137        self.declared_style().fill = Some(fill.into());
138        self
139    }
140
141    fn border_color<C>(mut self, border_color: C) -> Self
142    where
143        C: Into<Hsla>,
144        Self: Sized,
145    {
146        self.declared_style().border_color = Some(border_color.into());
147        self
148    }
149
150    fn text_style(&mut self) -> &mut Option<TextStyleRefinement> {
151        let style: &mut StyleRefinement = self.declared_style();
152        &mut style.text
153    }
154
155    fn text_color(mut self, color: impl Into<Hsla>) -> Self {
156        self.text_style().get_or_insert_with(Default::default).color = Some(color.into());
157        self
158    }
159
160    fn text_xs(mut self) -> Self {
161        self.text_style()
162            .get_or_insert_with(Default::default)
163            .font_size = Some(rems(0.75));
164        self
165    }
166
167    fn text_sm(mut self) -> Self {
168        self.text_style()
169            .get_or_insert_with(Default::default)
170            .font_size = Some(rems(0.875));
171        self
172    }
173
174    fn text_base(mut self) -> Self {
175        self.text_style()
176            .get_or_insert_with(Default::default)
177            .font_size = Some(rems(1.0));
178        self
179    }
180
181    fn text_lg(mut self) -> Self {
182        self.text_style()
183            .get_or_insert_with(Default::default)
184            .font_size = Some(rems(1.125));
185        self
186    }
187
188    fn text_xl(mut self) -> Self {
189        self.text_style()
190            .get_or_insert_with(Default::default)
191            .font_size = Some(rems(1.25));
192        self
193    }
194
195    fn text_2xl(mut self) -> Self {
196        self.text_style()
197            .get_or_insert_with(Default::default)
198            .font_size = Some(rems(1.5));
199        self
200    }
201
202    fn text_3xl(mut self) -> Self {
203        self.text_style()
204            .get_or_insert_with(Default::default)
205            .font_size = Some(rems(1.875));
206        self
207    }
208
209    fn font(mut self, family_name: impl Into<SharedString>) -> Self {
210        self.text_style()
211            .get_or_insert_with(Default::default)
212            .font_family = Some(family_name.into());
213        self
214    }
215}