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: Sized + Styled<Style = Style> {
  9    gpui3_macros::style_helpers!();
 10
 11    fn full(mut self) -> Self {
 12        self.declared_style().size.width = Some(relative(1.).into());
 13        self.declared_style().size.height = Some(relative(1.).into());
 14        self
 15    }
 16
 17    fn relative(mut self) -> Self {
 18        self.declared_style().position = Some(Position::Relative);
 19        self
 20    }
 21
 22    fn absolute(mut self) -> Self {
 23        self.declared_style().position = Some(Position::Absolute);
 24        self
 25    }
 26
 27    fn block(mut self) -> Self {
 28        self.declared_style().display = Some(Display::Block);
 29        self
 30    }
 31
 32    fn flex(mut self) -> Self {
 33        self.declared_style().display = Some(Display::Flex);
 34        self
 35    }
 36
 37    fn flex_col(mut self) -> Self {
 38        self.declared_style().flex_direction = Some(FlexDirection::Column);
 39        self
 40    }
 41
 42    fn flex_row(mut self) -> Self {
 43        self.declared_style().flex_direction = Some(FlexDirection::Row);
 44        self
 45    }
 46
 47    fn flex_1(mut self) -> Self {
 48        self.declared_style().flex_grow = Some(1.);
 49        self.declared_style().flex_shrink = Some(1.);
 50        self.declared_style().flex_basis = Some(relative(0.).into());
 51        self
 52    }
 53
 54    fn flex_auto(mut self) -> Self {
 55        self.declared_style().flex_grow = Some(1.);
 56        self.declared_style().flex_shrink = Some(1.);
 57        self.declared_style().flex_basis = Some(Length::Auto);
 58        self
 59    }
 60
 61    fn flex_initial(mut self) -> Self {
 62        self.declared_style().flex_grow = Some(0.);
 63        self.declared_style().flex_shrink = Some(1.);
 64        self.declared_style().flex_basis = Some(Length::Auto);
 65        self
 66    }
 67
 68    fn flex_none(mut self) -> Self {
 69        self.declared_style().flex_grow = Some(0.);
 70        self.declared_style().flex_shrink = Some(0.);
 71        self
 72    }
 73
 74    fn grow(mut self) -> Self {
 75        self.declared_style().flex_grow = Some(1.);
 76        self
 77    }
 78
 79    fn items_start(mut self) -> Self {
 80        self.declared_style().align_items = Some(AlignItems::FlexStart);
 81        self
 82    }
 83
 84    fn items_end(mut self) -> Self {
 85        self.declared_style().align_items = Some(AlignItems::FlexEnd);
 86        self
 87    }
 88
 89    fn items_center(mut self) -> Self {
 90        self.declared_style().align_items = Some(AlignItems::Center);
 91        self
 92    }
 93
 94    fn justify_between(mut self) -> Self {
 95        self.declared_style().justify_content = Some(JustifyContent::SpaceBetween);
 96        self
 97    }
 98
 99    fn justify_center(mut self) -> Self {
100        self.declared_style().justify_content = Some(JustifyContent::Center);
101        self
102    }
103
104    fn justify_start(mut self) -> Self {
105        self.declared_style().justify_content = Some(JustifyContent::Start);
106        self
107    }
108
109    fn justify_end(mut self) -> Self {
110        self.declared_style().justify_content = Some(JustifyContent::End);
111        self
112    }
113
114    fn justify_around(mut self) -> Self {
115        self.declared_style().justify_content = Some(JustifyContent::SpaceAround);
116        self
117    }
118
119    fn fill<F>(mut self, fill: F) -> Self
120    where
121        F: Into<Fill>,
122        Self: Sized,
123    {
124        self.declared_style().fill = Some(fill.into());
125        self
126    }
127
128    fn border_color<C>(mut self, border_color: C) -> Self
129    where
130        C: Into<Hsla>,
131        Self: Sized,
132    {
133        self.declared_style().border_color = Some(border_color.into());
134        self
135    }
136
137    fn shadow(mut self) -> Self {
138        self.declared_style().box_shadow = Some(smallvec![
139            BoxShadow {
140                color: hsla(0., 0., 0., 0.1),
141                offset: point(px(0.), px(1.)),
142                blur_radius: px(3.),
143                spread_radius: px(0.),
144            },
145            BoxShadow {
146                color: hsla(0., 0., 0., 0.1),
147                offset: point(px(0.), px(1.)),
148                blur_radius: px(2.),
149                spread_radius: px(-1.),
150            }
151        ]);
152        self
153    }
154
155    fn shadow_none(mut self) -> Self {
156        self.declared_style().box_shadow = Some(Default::default());
157        self
158    }
159
160    fn shadow_sm(mut self) -> Self {
161        self.declared_style().box_shadow = Some(smallvec![BoxShadow {
162            color: hsla(0., 0., 0., 0.05),
163            offset: point(px(0.), px(1.)),
164            blur_radius: px(2.),
165            spread_radius: px(0.),
166        }]);
167        self
168    }
169
170    fn shadow_md(mut self) -> Self {
171        self.declared_style().box_shadow = Some(smallvec![
172            BoxShadow {
173                color: hsla(0.5, 0., 0., 1.0),
174                offset: point(px(0.), px(4.)),
175                blur_radius: px(6.),
176                spread_radius: px(-1.),
177            },
178            BoxShadow {
179                color: hsla(0., 0., 0., 0.1),
180                offset: point(px(0.), px(2.)),
181                blur_radius: px(4.),
182                spread_radius: px(-2.),
183            }
184        ]);
185        self
186    }
187
188    fn shadow_lg(mut self) -> Self {
189        self.declared_style().box_shadow = Some(smallvec![
190            BoxShadow {
191                color: hsla(0., 0., 0., 0.1),
192                offset: point(px(0.), px(10.)),
193                blur_radius: px(15.),
194                spread_radius: px(-3.),
195            },
196            BoxShadow {
197                color: hsla(0., 0., 0., 0.1),
198                offset: point(px(0.), px(4.)),
199                blur_radius: px(6.),
200                spread_radius: px(-4.),
201            }
202        ]);
203        self
204    }
205
206    fn shadow_xl(mut self) -> Self {
207        self.declared_style().box_shadow = Some(smallvec![
208            BoxShadow {
209                color: hsla(0., 0., 0., 0.1),
210                offset: point(px(0.), px(20.)),
211                blur_radius: px(25.),
212                spread_radius: px(-5.),
213            },
214            BoxShadow {
215                color: hsla(0., 0., 0., 0.1),
216                offset: point(px(0.), px(8.)),
217                blur_radius: px(10.),
218                spread_radius: px(-6.),
219            }
220        ]);
221        self
222    }
223
224    fn shadow_2xl(mut self) -> Self {
225        self.declared_style().box_shadow = Some(smallvec![BoxShadow {
226            color: hsla(0., 0., 0., 0.25),
227            offset: point(px(0.), px(25.)),
228            blur_radius: px(50.),
229            spread_radius: px(-12.),
230        }]);
231        self
232    }
233
234    fn text_style(&mut self) -> &mut Option<TextStyleRefinement> {
235        let style: &mut StyleRefinement = self.declared_style();
236        &mut style.text
237    }
238
239    fn text_color(mut self, color: impl Into<Hsla>) -> Self {
240        self.text_style().get_or_insert_with(Default::default).color = Some(color.into());
241        self
242    }
243
244    fn text_xs(mut self) -> Self {
245        self.text_style()
246            .get_or_insert_with(Default::default)
247            .font_size = Some(rems(0.75));
248        self
249    }
250
251    fn text_sm(mut self) -> Self {
252        self.text_style()
253            .get_or_insert_with(Default::default)
254            .font_size = Some(rems(0.875));
255        self
256    }
257
258    fn text_base(mut self) -> Self {
259        self.text_style()
260            .get_or_insert_with(Default::default)
261            .font_size = Some(rems(1.0));
262        self
263    }
264
265    fn text_lg(mut self) -> Self {
266        self.text_style()
267            .get_or_insert_with(Default::default)
268            .font_size = Some(rems(1.125));
269        self
270    }
271
272    fn text_xl(mut self) -> Self {
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        self.text_style()
281            .get_or_insert_with(Default::default)
282            .font_size = Some(rems(1.5));
283        self
284    }
285
286    fn text_3xl(mut self) -> Self {
287        self.text_style()
288            .get_or_insert_with(Default::default)
289            .font_size = Some(rems(1.875));
290        self
291    }
292
293    fn text_decoration_none(mut self) -> Self {
294        self.text_style()
295            .get_or_insert_with(Default::default)
296            .underline = None;
297        self
298    }
299
300    fn text_decoration_color(mut self, color: impl Into<Hsla>) -> Self {
301        let style = self.text_style().get_or_insert_with(Default::default);
302        let underline = style.underline.get_or_insert_with(Default::default);
303        underline.color = Some(color.into());
304        self
305    }
306
307    fn text_decoration_solid(mut self) -> Self {
308        let style = self.text_style().get_or_insert_with(Default::default);
309        let underline = style.underline.get_or_insert_with(Default::default);
310        underline.wavy = false;
311        self
312    }
313
314    fn text_decoration_wavy(mut self) -> Self {
315        let style = self.text_style().get_or_insert_with(Default::default);
316        let underline = style.underline.get_or_insert_with(Default::default);
317        underline.wavy = true;
318        self
319    }
320
321    fn text_decoration_0(mut self) -> Self {
322        let style = self.text_style().get_or_insert_with(Default::default);
323        let underline = style.underline.get_or_insert_with(Default::default);
324        underline.thickness = px(0.);
325        self
326    }
327
328    fn text_decoration_1(mut self) -> Self {
329        let style = self.text_style().get_or_insert_with(Default::default);
330        let underline = style.underline.get_or_insert_with(Default::default);
331        underline.thickness = px(1.);
332        self
333    }
334
335    fn text_decoration_2(mut self) -> Self {
336        let style = self.text_style().get_or_insert_with(Default::default);
337        let underline = style.underline.get_or_insert_with(Default::default);
338        underline.thickness = px(2.);
339        self
340    }
341
342    fn text_decoration_4(mut self) -> Self {
343        let style = self.text_style().get_or_insert_with(Default::default);
344        let underline = style.underline.get_or_insert_with(Default::default);
345        underline.thickness = px(4.);
346        self
347    }
348
349    fn text_decoration_8(mut self) -> Self {
350        let style = self.text_style().get_or_insert_with(Default::default);
351        let underline = style.underline.get_or_insert_with(Default::default);
352        underline.thickness = px(8.);
353        self
354    }
355
356    fn font(mut self, family_name: impl Into<SharedString>) -> Self {
357        self.text_style()
358            .get_or_insert_with(Default::default)
359            .font_family = Some(family_name.into());
360        self
361    }
362}
363
364impl<E: Styled<Style = Style>> StyleHelpers for E {}