styled.rs

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