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: Styled<Style = Style> {
  7    gpui3_macros::style_helpers!();
  8
  9    fn h(mut self, height: Length) -> Self
 10    where
 11        Self: Sized,
 12    {
 13        self.declared_style().size.height = Some(height);
 14        self
 15    }
 16
 17    /// size_{n}: Sets width & height to {n}
 18    ///
 19    /// Example:
 20    /// size_1: Sets width & height to 1
 21    fn size(mut self, size: Length) -> Self
 22    where
 23        Self: Sized,
 24    {
 25        self.declared_style().size.height = Some(size);
 26        self.declared_style().size.width = Some(size);
 27        self
 28    }
 29
 30    fn full(mut self) -> Self
 31    where
 32        Self: Sized,
 33    {
 34        self.declared_style().size.width = Some(relative(1.).into());
 35        self.declared_style().size.height = Some(relative(1.).into());
 36        self
 37    }
 38
 39    fn relative(mut self) -> Self
 40    where
 41        Self: Sized,
 42    {
 43        self.declared_style().position = Some(Position::Relative);
 44        self
 45    }
 46
 47    fn absolute(mut self) -> Self
 48    where
 49        Self: Sized,
 50    {
 51        self.declared_style().position = Some(Position::Absolute);
 52        self
 53    }
 54
 55    fn block(mut self) -> Self
 56    where
 57        Self: Sized,
 58    {
 59        self.declared_style().display = Some(Display::Block);
 60        self
 61    }
 62
 63    fn flex(mut self) -> Self
 64    where
 65        Self: Sized,
 66    {
 67        self.declared_style().display = Some(Display::Flex);
 68        self
 69    }
 70
 71    fn flex_col(mut self) -> Self
 72    where
 73        Self: Sized,
 74    {
 75        self.declared_style().flex_direction = Some(FlexDirection::Column);
 76        self
 77    }
 78
 79    fn flex_row(mut self) -> Self
 80    where
 81        Self: Sized,
 82    {
 83        self.declared_style().flex_direction = Some(FlexDirection::Row);
 84        self
 85    }
 86
 87    fn flex_1(mut self) -> Self
 88    where
 89        Self: Sized,
 90    {
 91        self.declared_style().flex_grow = Some(1.);
 92        self.declared_style().flex_shrink = Some(1.);
 93        self.declared_style().flex_basis = Some(relative(0.).into());
 94        self
 95    }
 96
 97    fn flex_auto(mut self) -> Self
 98    where
 99        Self: Sized,
100    {
101        self.declared_style().flex_grow = Some(1.);
102        self.declared_style().flex_shrink = Some(1.);
103        self.declared_style().flex_basis = Some(Length::Auto);
104        self
105    }
106
107    fn flex_initial(mut self) -> Self
108    where
109        Self: Sized,
110    {
111        self.declared_style().flex_grow = Some(0.);
112        self.declared_style().flex_shrink = Some(1.);
113        self.declared_style().flex_basis = Some(Length::Auto);
114        self
115    }
116
117    fn flex_none(mut self) -> Self
118    where
119        Self: Sized,
120    {
121        self.declared_style().flex_grow = Some(0.);
122        self.declared_style().flex_shrink = Some(0.);
123        self
124    }
125
126    fn grow(mut self) -> Self
127    where
128        Self: Sized,
129    {
130        self.declared_style().flex_grow = Some(1.);
131        self
132    }
133
134    fn items_start(mut self) -> Self
135    where
136        Self: Sized,
137    {
138        self.declared_style().align_items = Some(AlignItems::FlexStart);
139        self
140    }
141
142    fn items_end(mut self) -> Self
143    where
144        Self: Sized,
145    {
146        self.declared_style().align_items = Some(AlignItems::FlexEnd);
147        self
148    }
149
150    fn items_center(mut self) -> Self
151    where
152        Self: Sized,
153    {
154        self.declared_style().align_items = Some(AlignItems::Center);
155        self
156    }
157
158    fn justify_between(mut self) -> Self
159    where
160        Self: Sized,
161    {
162        self.declared_style().justify_content = Some(JustifyContent::SpaceBetween);
163        self
164    }
165
166    fn justify_center(mut self) -> Self
167    where
168        Self: Sized,
169    {
170        self.declared_style().justify_content = Some(JustifyContent::Center);
171        self
172    }
173
174    fn justify_start(mut self) -> Self
175    where
176        Self: Sized,
177    {
178        self.declared_style().justify_content = Some(JustifyContent::Start);
179        self
180    }
181
182    fn justify_end(mut self) -> Self
183    where
184        Self: Sized,
185    {
186        self.declared_style().justify_content = Some(JustifyContent::End);
187        self
188    }
189
190    fn justify_around(mut self) -> Self
191    where
192        Self: Sized,
193    {
194        self.declared_style().justify_content = Some(JustifyContent::SpaceAround);
195        self
196    }
197
198    fn fill<F>(mut self, fill: F) -> Self
199    where
200        F: Into<Fill>,
201        Self: Sized,
202    {
203        self.declared_style().fill = Some(fill.into());
204        self
205    }
206
207    fn border_color<C>(mut self, border_color: C) -> Self
208    where
209        C: Into<Hsla>,
210        Self: Sized,
211    {
212        self.declared_style().border_color = Some(border_color.into());
213        self
214    }
215
216    fn text_style(&mut self) -> &mut Option<TextStyleRefinement> {
217        let style: &mut StyleRefinement = self.declared_style();
218        &mut style.text
219    }
220
221    fn text_color(mut self, color: impl Into<Hsla>) -> Self
222    where
223        Self: Sized,
224    {
225        self.text_style().get_or_insert_with(Default::default).color = Some(color.into());
226        self
227    }
228
229    fn text_xs(mut self) -> Self
230    where
231        Self: Sized,
232    {
233        self.text_style()
234            .get_or_insert_with(Default::default)
235            .font_size = Some(rems(0.75));
236        self
237    }
238
239    fn text_sm(mut self) -> Self
240    where
241        Self: Sized,
242    {
243        self.text_style()
244            .get_or_insert_with(Default::default)
245            .font_size = Some(rems(0.875));
246        self
247    }
248
249    fn text_base(mut self) -> Self
250    where
251        Self: Sized,
252    {
253        self.text_style()
254            .get_or_insert_with(Default::default)
255            .font_size = Some(rems(1.0));
256        self
257    }
258
259    fn text_lg(mut self) -> Self
260    where
261        Self: Sized,
262    {
263        self.text_style()
264            .get_or_insert_with(Default::default)
265            .font_size = Some(rems(1.125));
266        self
267    }
268
269    fn text_xl(mut self) -> Self
270    where
271        Self: Sized,
272    {
273        self.text_style()
274            .get_or_insert_with(Default::default)
275            .font_size = Some(rems(1.25));
276        self
277    }
278
279    fn text_2xl(mut self) -> Self
280    where
281        Self: Sized,
282    {
283        self.text_style()
284            .get_or_insert_with(Default::default)
285            .font_size = Some(rems(1.5));
286        self
287    }
288
289    fn text_3xl(mut self) -> Self
290    where
291        Self: Sized,
292    {
293        self.text_style()
294            .get_or_insert_with(Default::default)
295            .font_size = Some(rems(1.875));
296        self
297    }
298
299    fn font(mut self, family_name: impl Into<SharedString>) -> Self
300    where
301        Self: Sized,
302    {
303        self.text_style()
304            .get_or_insert_with(Default::default)
305            .font_family = Some(family_name.into());
306        self
307    }
308}