style_helpers.rs

  1use crate::{
  2    self as gpui3, hsla, point, px, relative, rems, AlignItems, BoxShadow, Display, Fill,
  3    FlexDirection, Hsla, JustifyContent, Length, Position, SharedString, Style, StyleRefinement,
  4    Styled, TextStyleRefinement,
  5};
  6use smallvec::smallvec;
  7
  8pub trait StyleHelpers: Styled<Style = Style> {
  9    gpui3_macros::style_helpers!();
 10
 11    fn h(mut self, height: Length) -> Self
 12    where
 13        Self: Sized,
 14    {
 15        self.declared_style().size.height = Some(height);
 16        self
 17    }
 18
 19    /// size_{n}: Sets width & height to {n}
 20    ///
 21    /// Example:
 22    /// size_1: Sets width & height to 1
 23    fn size(mut self, size: Length) -> Self
 24    where
 25        Self: Sized,
 26    {
 27        self.declared_style().size.height = Some(size);
 28        self.declared_style().size.width = Some(size);
 29        self
 30    }
 31
 32    fn full(mut self) -> Self
 33    where
 34        Self: Sized,
 35    {
 36        self.declared_style().size.width = Some(relative(1.).into());
 37        self.declared_style().size.height = Some(relative(1.).into());
 38        self
 39    }
 40
 41    fn relative(mut self) -> Self
 42    where
 43        Self: Sized,
 44    {
 45        self.declared_style().position = Some(Position::Relative);
 46        self
 47    }
 48
 49    fn absolute(mut self) -> Self
 50    where
 51        Self: Sized,
 52    {
 53        self.declared_style().position = Some(Position::Absolute);
 54        self
 55    }
 56
 57    fn block(mut self) -> Self
 58    where
 59        Self: Sized,
 60    {
 61        self.declared_style().display = Some(Display::Block);
 62        self
 63    }
 64
 65    fn flex(mut self) -> Self
 66    where
 67        Self: Sized,
 68    {
 69        self.declared_style().display = Some(Display::Flex);
 70        self
 71    }
 72
 73    fn flex_col(mut self) -> Self
 74    where
 75        Self: Sized,
 76    {
 77        self.declared_style().flex_direction = Some(FlexDirection::Column);
 78        self
 79    }
 80
 81    fn flex_row(mut self) -> Self
 82    where
 83        Self: Sized,
 84    {
 85        self.declared_style().flex_direction = Some(FlexDirection::Row);
 86        self
 87    }
 88
 89    fn flex_1(mut self) -> Self
 90    where
 91        Self: Sized,
 92    {
 93        self.declared_style().flex_grow = Some(1.);
 94        self.declared_style().flex_shrink = Some(1.);
 95        self.declared_style().flex_basis = Some(relative(0.).into());
 96        self
 97    }
 98
 99    fn flex_auto(mut self) -> Self
100    where
101        Self: Sized,
102    {
103        self.declared_style().flex_grow = Some(1.);
104        self.declared_style().flex_shrink = Some(1.);
105        self.declared_style().flex_basis = Some(Length::Auto);
106        self
107    }
108
109    fn flex_initial(mut self) -> Self
110    where
111        Self: Sized,
112    {
113        self.declared_style().flex_grow = Some(0.);
114        self.declared_style().flex_shrink = Some(1.);
115        self.declared_style().flex_basis = Some(Length::Auto);
116        self
117    }
118
119    fn flex_none(mut self) -> Self
120    where
121        Self: Sized,
122    {
123        self.declared_style().flex_grow = Some(0.);
124        self.declared_style().flex_shrink = Some(0.);
125        self
126    }
127
128    fn grow(mut self) -> Self
129    where
130        Self: Sized,
131    {
132        self.declared_style().flex_grow = Some(1.);
133        self
134    }
135
136    fn items_start(mut self) -> Self
137    where
138        Self: Sized,
139    {
140        self.declared_style().align_items = Some(AlignItems::FlexStart);
141        self
142    }
143
144    fn items_end(mut self) -> Self
145    where
146        Self: Sized,
147    {
148        self.declared_style().align_items = Some(AlignItems::FlexEnd);
149        self
150    }
151
152    fn items_center(mut self) -> Self
153    where
154        Self: Sized,
155    {
156        self.declared_style().align_items = Some(AlignItems::Center);
157        self
158    }
159
160    fn justify_between(mut self) -> Self
161    where
162        Self: Sized,
163    {
164        self.declared_style().justify_content = Some(JustifyContent::SpaceBetween);
165        self
166    }
167
168    fn justify_center(mut self) -> Self
169    where
170        Self: Sized,
171    {
172        self.declared_style().justify_content = Some(JustifyContent::Center);
173        self
174    }
175
176    fn justify_start(mut self) -> Self
177    where
178        Self: Sized,
179    {
180        self.declared_style().justify_content = Some(JustifyContent::Start);
181        self
182    }
183
184    fn justify_end(mut self) -> Self
185    where
186        Self: Sized,
187    {
188        self.declared_style().justify_content = Some(JustifyContent::End);
189        self
190    }
191
192    fn justify_around(mut self) -> Self
193    where
194        Self: Sized,
195    {
196        self.declared_style().justify_content = Some(JustifyContent::SpaceAround);
197        self
198    }
199
200    fn fill<F>(mut self, fill: F) -> Self
201    where
202        F: Into<Fill>,
203        Self: Sized,
204    {
205        self.declared_style().fill = Some(fill.into());
206        self
207    }
208
209    fn border_color<C>(mut self, border_color: C) -> Self
210    where
211        C: Into<Hsla>,
212        Self: Sized,
213    {
214        self.declared_style().border_color = Some(border_color.into());
215        self
216    }
217
218    fn shadow(mut self) -> Self
219    where
220        Self: Sized,
221    {
222        self.declared_style().box_shadow = Some(smallvec![
223            BoxShadow {
224                color: hsla(0., 0., 0., 0.1),
225                offset: point(px(0.), px(1.)),
226                blur_radius: px(3.),
227                spread_radius: px(0.),
228            },
229            BoxShadow {
230                color: hsla(0., 0., 0., 0.1),
231                offset: point(px(0.), px(1.)),
232                blur_radius: px(2.),
233                spread_radius: px(-1.),
234            }
235        ]);
236        self
237    }
238
239    fn shadow_none(mut self) -> Self
240    where
241        Self: Sized,
242    {
243        self.declared_style().box_shadow = Some(Default::default());
244        self
245    }
246
247    fn shadow_sm(mut self) -> Self
248    where
249        Self: Sized,
250    {
251        self.declared_style().box_shadow = Some(smallvec![BoxShadow {
252            color: hsla(0., 0., 0., 0.05),
253            offset: point(px(0.), px(1.)),
254            blur_radius: px(2.),
255            spread_radius: px(0.),
256        }]);
257        self
258    }
259
260    fn shadow_md(mut self) -> Self
261    where
262        Self: Sized,
263    {
264        self.declared_style().box_shadow = Some(smallvec![
265            BoxShadow {
266                color: hsla(0.5, 0., 0., 1.0),
267                offset: point(px(0.), px(4.)),
268                blur_radius: px(6.),
269                spread_radius: px(-1.),
270            },
271            BoxShadow {
272                color: hsla(0., 0., 0., 0.1),
273                offset: point(px(0.), px(2.)),
274                blur_radius: px(4.),
275                spread_radius: px(-2.),
276            }
277        ]);
278        self
279    }
280
281    fn shadow_lg(mut self) -> Self
282    where
283        Self: Sized,
284    {
285        self.declared_style().box_shadow = Some(smallvec![
286            BoxShadow {
287                color: hsla(0., 0., 0., 0.1),
288                offset: point(px(0.), px(10.)),
289                blur_radius: px(15.),
290                spread_radius: px(-3.),
291            },
292            BoxShadow {
293                color: hsla(0., 0., 0., 0.1),
294                offset: point(px(0.), px(4.)),
295                blur_radius: px(6.),
296                spread_radius: px(-4.),
297            }
298        ]);
299        self
300    }
301
302    fn shadow_xl(mut self) -> Self
303    where
304        Self: Sized,
305    {
306        self.declared_style().box_shadow = Some(smallvec![
307            BoxShadow {
308                color: hsla(0., 0., 0., 0.1),
309                offset: point(px(0.), px(20.)),
310                blur_radius: px(25.),
311                spread_radius: px(-5.),
312            },
313            BoxShadow {
314                color: hsla(0., 0., 0., 0.1),
315                offset: point(px(0.), px(8.)),
316                blur_radius: px(10.),
317                spread_radius: px(-6.),
318            }
319        ]);
320        self
321    }
322
323    fn shadow_2xl(mut self) -> Self
324    where
325        Self: Sized,
326    {
327        self.declared_style().box_shadow = Some(smallvec![BoxShadow {
328            color: hsla(0., 0., 0., 0.25),
329            offset: point(px(0.), px(25.)),
330            blur_radius: px(50.),
331            spread_radius: px(-12.),
332        }]);
333        self
334    }
335
336    fn text_style(&mut self) -> &mut Option<TextStyleRefinement> {
337        let style: &mut StyleRefinement = self.declared_style();
338        &mut style.text
339    }
340
341    fn text_color(mut self, color: impl Into<Hsla>) -> Self
342    where
343        Self: Sized,
344    {
345        self.text_style().get_or_insert_with(Default::default).color = Some(color.into());
346        self
347    }
348
349    fn text_xs(mut self) -> Self
350    where
351        Self: Sized,
352    {
353        self.text_style()
354            .get_or_insert_with(Default::default)
355            .font_size = Some(rems(0.75));
356        self
357    }
358
359    fn text_sm(mut self) -> Self
360    where
361        Self: Sized,
362    {
363        self.text_style()
364            .get_or_insert_with(Default::default)
365            .font_size = Some(rems(0.875));
366        self
367    }
368
369    fn text_base(mut self) -> Self
370    where
371        Self: Sized,
372    {
373        self.text_style()
374            .get_or_insert_with(Default::default)
375            .font_size = Some(rems(1.0));
376        self
377    }
378
379    fn text_lg(mut self) -> Self
380    where
381        Self: Sized,
382    {
383        self.text_style()
384            .get_or_insert_with(Default::default)
385            .font_size = Some(rems(1.125));
386        self
387    }
388
389    fn text_xl(mut self) -> Self
390    where
391        Self: Sized,
392    {
393        self.text_style()
394            .get_or_insert_with(Default::default)
395            .font_size = Some(rems(1.25));
396        self
397    }
398
399    fn text_2xl(mut self) -> Self
400    where
401        Self: Sized,
402    {
403        self.text_style()
404            .get_or_insert_with(Default::default)
405            .font_size = Some(rems(1.5));
406        self
407    }
408
409    fn text_3xl(mut self) -> Self
410    where
411        Self: Sized,
412    {
413        self.text_style()
414            .get_or_insert_with(Default::default)
415            .font_size = Some(rems(1.875));
416        self
417    }
418
419    fn text_decoration_none(mut self) -> Self
420    where
421        Self: Sized,
422    {
423        self.text_style()
424            .get_or_insert_with(Default::default)
425            .underline = None;
426        self
427    }
428
429    fn text_decoration_color(mut self, color: impl Into<Hsla>) -> Self
430    where
431        Self: Sized,
432    {
433        let style = self.text_style().get_or_insert_with(Default::default);
434        let underline = style.underline.get_or_insert_with(Default::default);
435        underline.color = Some(color.into());
436        self
437    }
438
439    fn text_decoration_solid(mut self) -> Self
440    where
441        Self: Sized,
442    {
443        let style = self.text_style().get_or_insert_with(Default::default);
444        let underline = style.underline.get_or_insert_with(Default::default);
445        underline.wavy = false;
446        self
447    }
448
449    fn text_decoration_wavy(mut self) -> Self
450    where
451        Self: Sized,
452    {
453        let style = self.text_style().get_or_insert_with(Default::default);
454        let underline = style.underline.get_or_insert_with(Default::default);
455        underline.wavy = true;
456        self
457    }
458
459    fn text_decoration_0(mut self) -> Self
460    where
461        Self: Sized,
462    {
463        let style = self.text_style().get_or_insert_with(Default::default);
464        let underline = style.underline.get_or_insert_with(Default::default);
465        underline.thickness = px(0.);
466        self
467    }
468
469    fn text_decoration_1(mut self) -> Self
470    where
471        Self: Sized,
472    {
473        let style = self.text_style().get_or_insert_with(Default::default);
474        let underline = style.underline.get_or_insert_with(Default::default);
475        underline.thickness = px(1.);
476        self
477    }
478
479    fn text_decoration_2(mut self) -> Self
480    where
481        Self: Sized,
482    {
483        let style = self.text_style().get_or_insert_with(Default::default);
484        let underline = style.underline.get_or_insert_with(Default::default);
485        underline.thickness = px(2.);
486        self
487    }
488
489    fn text_decoration_4(mut self) -> Self
490    where
491        Self: Sized,
492    {
493        let style = self.text_style().get_or_insert_with(Default::default);
494        let underline = style.underline.get_or_insert_with(Default::default);
495        underline.thickness = px(4.);
496        self
497    }
498
499    fn text_decoration_8(mut self) -> Self
500    where
501        Self: Sized,
502    {
503        let style = self.text_style().get_or_insert_with(Default::default);
504        let underline = style.underline.get_or_insert_with(Default::default);
505        underline.thickness = px(8.);
506        self
507    }
508
509    fn font(mut self, family_name: impl Into<SharedString>) -> Self
510    where
511        Self: Sized,
512    {
513        self.text_style()
514            .get_or_insert_with(Default::default)
515            .font_family = Some(family_name.into());
516        self
517    }
518}