1use crate::{
2 self as gpui, AbsoluteLength, AlignContent, AlignItems, BorderStyle, CursorStyle,
3 DefiniteLength, Display, Fill, FlexDirection, FlexWrap, Font, FontFeatures, FontStyle,
4 FontWeight, GridPlacement, Hsla, JustifyContent, Length, SharedString, StrikethroughStyle,
5 StyleRefinement, TextAlign, TextOverflow, TextStyleRefinement, UnderlineStyle, WhiteSpace, px,
6 relative, rems,
7};
8pub use gpui_macros::{
9 border_style_methods, box_shadow_style_methods, cursor_style_methods, margin_style_methods,
10 overflow_style_methods, padding_style_methods, position_style_methods,
11 visibility_style_methods,
12};
13const ELLIPSIS: SharedString = SharedString::new_static("…");
14
15#[cfg_attr(
19 all(any(feature = "inspector", debug_assertions), not(rust_analyzer)),
20 gpui_macros::derive_inspector_reflection
21)]
22pub trait Styled: Sized {
23 fn style(&mut self) -> &mut StyleRefinement;
25
26 gpui_macros::style_helpers!();
27 gpui_macros::visibility_style_methods!();
28 gpui_macros::margin_style_methods!();
29 gpui_macros::padding_style_methods!();
30 gpui_macros::position_style_methods!();
31 gpui_macros::overflow_style_methods!();
32 gpui_macros::cursor_style_methods!();
33 gpui_macros::border_style_methods!();
34 gpui_macros::box_shadow_style_methods!();
35
36 fn block(mut self) -> Self {
39 self.style().display = Some(Display::Block);
40 self
41 }
42
43 fn flex(mut self) -> Self {
46 self.style().display = Some(Display::Flex);
47 self
48 }
49
50 fn grid(mut self) -> Self {
53 self.style().display = Some(Display::Grid);
54 self
55 }
56
57 fn hidden(mut self) -> Self {
60 self.style().display = Some(Display::None);
61 self
62 }
63
64 fn whitespace_normal(mut self) -> Self {
67 self.text_style().white_space = Some(WhiteSpace::Normal);
68 self
69 }
70
71 fn whitespace_nowrap(mut self) -> Self {
74 self.text_style().white_space = Some(WhiteSpace::Nowrap);
75 self
76 }
77
78 fn text_ellipsis(mut self) -> Self {
81 self.text_style().text_overflow = Some(TextOverflow::Truncate(ELLIPSIS));
82 self
83 }
84
85 fn text_ellipsis_start(mut self) -> Self {
89 self.text_style().text_overflow = Some(TextOverflow::TruncateStart(ELLIPSIS));
90 self
91 }
92
93 fn text_overflow(mut self, overflow: TextOverflow) -> Self {
95 self.text_style().text_overflow = Some(overflow);
96 self
97 }
98
99 fn text_align(mut self, align: TextAlign) -> Self {
101 self.text_style().text_align = Some(align);
102 self
103 }
104
105 fn text_left(mut self) -> Self {
107 self.text_align(TextAlign::Left)
108 }
109
110 fn text_center(mut self) -> Self {
112 self.text_align(TextAlign::Center)
113 }
114
115 fn text_right(mut self) -> Self {
117 self.text_align(TextAlign::Right)
118 }
119
120 fn truncate(mut self) -> Self {
123 self.overflow_hidden().whitespace_nowrap().text_ellipsis()
124 }
125
126 fn line_clamp(mut self, lines: usize) -> Self {
129 let mut text_style = self.text_style();
130 text_style.line_clamp = Some(lines);
131 self.overflow_hidden()
132 }
133
134 fn flex_col(mut self) -> Self {
137 self.style().flex_direction = Some(FlexDirection::Column);
138 self
139 }
140
141 fn flex_col_reverse(mut self) -> Self {
144 self.style().flex_direction = Some(FlexDirection::ColumnReverse);
145 self
146 }
147
148 fn flex_row(mut self) -> Self {
151 self.style().flex_direction = Some(FlexDirection::Row);
152 self
153 }
154
155 fn flex_row_reverse(mut self) -> Self {
158 self.style().flex_direction = Some(FlexDirection::RowReverse);
159 self
160 }
161
162 fn flex_1(mut self) -> Self {
165 self.style().flex_grow = Some(1.);
166 self.style().flex_shrink = Some(1.);
167 self.style().flex_basis = Some(relative(0.).into());
168 self
169 }
170
171 fn flex_auto(mut self) -> Self {
174 self.style().flex_grow = Some(1.);
175 self.style().flex_shrink = Some(1.);
176 self.style().flex_basis = Some(Length::Auto);
177 self
178 }
179
180 fn flex_initial(mut self) -> Self {
183 self.style().flex_grow = Some(0.);
184 self.style().flex_shrink = Some(1.);
185 self.style().flex_basis = Some(Length::Auto);
186 self
187 }
188
189 fn flex_none(mut self) -> Self {
192 self.style().flex_grow = Some(0.);
193 self.style().flex_shrink = Some(0.);
194 self
195 }
196
197 fn flex_basis(mut self, basis: impl Into<Length>) -> Self {
200 self.style().flex_basis = Some(basis.into());
201 self
202 }
203
204 fn flex_grow(mut self) -> Self {
207 self.style().flex_grow = Some(1.);
208 self
209 }
210
211 fn flex_shrink(mut self) -> Self {
214 self.style().flex_shrink = Some(1.);
215 self
216 }
217
218 fn flex_shrink_0(mut self) -> Self {
221 self.style().flex_shrink = Some(0.);
222 self
223 }
224
225 fn flex_wrap(mut self) -> Self {
228 self.style().flex_wrap = Some(FlexWrap::Wrap);
229 self
230 }
231
232 fn flex_wrap_reverse(mut self) -> Self {
235 self.style().flex_wrap = Some(FlexWrap::WrapReverse);
236 self
237 }
238
239 fn flex_nowrap(mut self) -> Self {
242 self.style().flex_wrap = Some(FlexWrap::NoWrap);
243 self
244 }
245
246 fn items_start(mut self) -> Self {
249 self.style().align_items = Some(AlignItems::FlexStart);
250 self
251 }
252
253 fn items_end(mut self) -> Self {
256 self.style().align_items = Some(AlignItems::FlexEnd);
257 self
258 }
259
260 fn items_center(mut self) -> Self {
263 self.style().align_items = Some(AlignItems::Center);
264 self
265 }
266
267 fn items_baseline(mut self) -> Self {
270 self.style().align_items = Some(AlignItems::Baseline);
271 self
272 }
273
274 fn justify_start(mut self) -> Self {
277 self.style().justify_content = Some(JustifyContent::Start);
278 self
279 }
280
281 fn justify_end(mut self) -> Self {
284 self.style().justify_content = Some(JustifyContent::End);
285 self
286 }
287
288 fn justify_center(mut self) -> Self {
291 self.style().justify_content = Some(JustifyContent::Center);
292 self
293 }
294
295 fn justify_between(mut self) -> Self {
299 self.style().justify_content = Some(JustifyContent::SpaceBetween);
300 self
301 }
302
303 fn justify_around(mut self) -> Self {
307 self.style().justify_content = Some(JustifyContent::SpaceAround);
308 self
309 }
310
311 fn justify_evenly(mut self) -> Self {
317 self.style().justify_content = Some(JustifyContent::SpaceEvenly);
318 self
319 }
320
321 fn content_normal(mut self) -> Self {
324 self.style().align_content = None;
325 self
326 }
327
328 fn content_center(mut self) -> Self {
331 self.style().align_content = Some(AlignContent::Center);
332 self
333 }
334
335 fn content_start(mut self) -> Self {
338 self.style().align_content = Some(AlignContent::FlexStart);
339 self
340 }
341
342 fn content_end(mut self) -> Self {
345 self.style().align_content = Some(AlignContent::FlexEnd);
346 self
347 }
348
349 fn content_between(mut self) -> Self {
353 self.style().align_content = Some(AlignContent::SpaceBetween);
354 self
355 }
356
357 fn content_around(mut self) -> Self {
361 self.style().align_content = Some(AlignContent::SpaceAround);
362 self
363 }
364
365 fn content_evenly(mut self) -> Self {
369 self.style().align_content = Some(AlignContent::SpaceEvenly);
370 self
371 }
372
373 fn content_stretch(mut self) -> Self {
376 self.style().align_content = Some(AlignContent::Stretch);
377 self
378 }
379
380 fn bg<F>(mut self, fill: F) -> Self
382 where
383 F: Into<Fill>,
384 Self: Sized,
385 {
386 self.style().background = Some(fill.into());
387 self
388 }
389
390 fn border_dashed(mut self) -> Self {
392 self.style().border_style = Some(BorderStyle::Dashed);
393 self
394 }
395
396 fn text_style(&mut self) -> &mut TextStyleRefinement {
398 let style: &mut StyleRefinement = self.style();
399 &mut style.text
400 }
401
402 fn text_color(mut self, color: impl Into<Hsla>) -> Self {
406 self.text_style().color = Some(color.into());
407 self
408 }
409
410 fn font_weight(mut self, weight: FontWeight) -> Self {
414 self.text_style().font_weight = Some(weight);
415 self
416 }
417
418 fn text_bg(mut self, bg: impl Into<Hsla>) -> Self {
422 self.text_style().background_color = Some(bg.into());
423 self
424 }
425
426 fn text_size(mut self, size: impl Into<AbsoluteLength>) -> Self {
430 self.text_style().font_size = Some(size.into());
431 self
432 }
433
434 fn text_xs(mut self) -> Self {
437 self.text_style().font_size = Some(rems(0.75).into());
438 self
439 }
440
441 fn text_sm(mut self) -> Self {
444 self.text_style().font_size = Some(rems(0.875).into());
445 self
446 }
447
448 fn text_base(mut self) -> Self {
451 self.text_style().font_size = Some(rems(1.0).into());
452 self
453 }
454
455 fn text_lg(mut self) -> Self {
458 self.text_style().font_size = Some(rems(1.125).into());
459 self
460 }
461
462 fn text_xl(mut self) -> Self {
465 self.text_style().font_size = Some(rems(1.25).into());
466 self
467 }
468
469 fn text_2xl(mut self) -> Self {
472 self.text_style().font_size = Some(rems(1.5).into());
473 self
474 }
475
476 fn text_3xl(mut self) -> Self {
479 self.text_style().font_size = Some(rems(1.875).into());
480 self
481 }
482
483 fn italic(mut self) -> Self {
486 self.text_style().font_style = Some(FontStyle::Italic);
487 self
488 }
489
490 fn not_italic(mut self) -> Self {
493 self.text_style().font_style = Some(FontStyle::Normal);
494 self
495 }
496
497 fn underline(mut self) -> Self {
500 let style = self.text_style();
501 style.underline = Some(UnderlineStyle {
502 thickness: px(1.),
503 ..Default::default()
504 });
505 self
506 }
507
508 fn line_through(mut self) -> Self {
511 let style = self.text_style();
512 style.strikethrough = Some(StrikethroughStyle {
513 thickness: px(1.),
514 ..Default::default()
515 });
516 self
517 }
518
519 fn text_decoration_none(mut self) -> Self {
523 self.text_style().underline = None;
524 self
525 }
526
527 fn text_decoration_color(mut self, color: impl Into<Hsla>) -> Self {
529 let style = self.text_style();
530 let underline = style.underline.get_or_insert_with(Default::default);
531 underline.color = Some(color.into());
532 self
533 }
534
535 fn text_decoration_solid(mut self) -> Self {
538 let style = self.text_style();
539 let underline = style.underline.get_or_insert_with(Default::default);
540 underline.wavy = false;
541 self
542 }
543
544 fn text_decoration_wavy(mut self) -> Self {
547 let style = self.text_style();
548 let underline = style.underline.get_or_insert_with(Default::default);
549 underline.wavy = true;
550 self
551 }
552
553 fn text_decoration_0(mut self) -> Self {
556 let style = self.text_style();
557 let underline = style.underline.get_or_insert_with(Default::default);
558 underline.thickness = px(0.);
559 self
560 }
561
562 fn text_decoration_1(mut self) -> Self {
565 let style = self.text_style();
566 let underline = style.underline.get_or_insert_with(Default::default);
567 underline.thickness = px(1.);
568 self
569 }
570
571 fn text_decoration_2(mut self) -> Self {
574 let style = self.text_style();
575 let underline = style.underline.get_or_insert_with(Default::default);
576 underline.thickness = px(2.);
577 self
578 }
579
580 fn text_decoration_4(mut self) -> Self {
583 let style = self.text_style();
584 let underline = style.underline.get_or_insert_with(Default::default);
585 underline.thickness = px(4.);
586 self
587 }
588
589 fn text_decoration_8(mut self) -> Self {
592 let style = self.text_style();
593 let underline = style.underline.get_or_insert_with(Default::default);
594 underline.thickness = px(8.);
595 self
596 }
597
598 fn font_family(mut self, family_name: impl Into<SharedString>) -> Self {
600 self.text_style().font_family = Some(family_name.into());
601 self
602 }
603
604 fn font_features(mut self, features: FontFeatures) -> Self {
606 self.text_style().font_features = Some(features);
607 self
608 }
609
610 fn font(mut self, font: Font) -> Self {
612 let Font {
613 family,
614 features,
615 fallbacks,
616 weight,
617 style,
618 } = font;
619
620 let text_style = self.text_style();
621 text_style.font_family = Some(family);
622 text_style.font_features = Some(features);
623 text_style.font_weight = Some(weight);
624 text_style.font_style = Some(style);
625 text_style.font_fallbacks = fallbacks;
626
627 self
628 }
629
630 fn line_height(mut self, line_height: impl Into<DefiniteLength>) -> Self {
632 self.text_style().line_height = Some(line_height.into());
633 self
634 }
635
636 fn opacity(mut self, opacity: f32) -> Self {
638 self.style().opacity = Some(opacity);
639 self
640 }
641
642 fn grid_cols(mut self, cols: u16) -> Self {
644 self.style().grid_cols = Some(cols);
645 self
646 }
647
648 fn grid_cols_min_content(mut self, cols: u16) -> Self {
651 self.style().grid_cols_min_content = Some(cols);
652 self
653 }
654
655 fn grid_rows(mut self, rows: u16) -> Self {
657 self.style().grid_rows = Some(rows);
658 self
659 }
660
661 fn col_start(mut self, start: i16) -> Self {
663 let grid_location = self.style().grid_location_mut();
664 grid_location.column.start = GridPlacement::Line(start);
665 self
666 }
667
668 fn col_start_auto(mut self) -> Self {
670 let grid_location = self.style().grid_location_mut();
671 grid_location.column.start = GridPlacement::Auto;
672 self
673 }
674
675 fn col_end(mut self, end: i16) -> Self {
677 let grid_location = self.style().grid_location_mut();
678 grid_location.column.end = GridPlacement::Line(end);
679 self
680 }
681
682 fn col_end_auto(mut self) -> Self {
684 let grid_location = self.style().grid_location_mut();
685 grid_location.column.end = GridPlacement::Auto;
686 self
687 }
688
689 fn col_span(mut self, span: u16) -> Self {
691 let grid_location = self.style().grid_location_mut();
692 grid_location.column = GridPlacement::Span(span)..GridPlacement::Span(span);
693 self
694 }
695
696 fn col_span_full(mut self) -> Self {
698 let grid_location = self.style().grid_location_mut();
699 grid_location.column = GridPlacement::Line(1)..GridPlacement::Line(-1);
700 self
701 }
702
703 fn row_start(mut self, start: i16) -> Self {
705 let grid_location = self.style().grid_location_mut();
706 grid_location.row.start = GridPlacement::Line(start);
707 self
708 }
709
710 fn row_start_auto(mut self) -> Self {
712 let grid_location = self.style().grid_location_mut();
713 grid_location.row.start = GridPlacement::Auto;
714 self
715 }
716
717 fn row_end(mut self, end: i16) -> Self {
719 let grid_location = self.style().grid_location_mut();
720 grid_location.row.end = GridPlacement::Line(end);
721 self
722 }
723
724 fn row_end_auto(mut self) -> Self {
726 let grid_location = self.style().grid_location_mut();
727 grid_location.row.end = GridPlacement::Auto;
728 self
729 }
730
731 fn row_span(mut self, span: u16) -> Self {
733 let grid_location = self.style().grid_location_mut();
734 grid_location.row = GridPlacement::Span(span)..GridPlacement::Span(span);
735 self
736 }
737
738 fn row_span_full(mut self) -> Self {
740 let grid_location = self.style().grid_location_mut();
741 grid_location.row = GridPlacement::Line(1)..GridPlacement::Line(-1);
742 self
743 }
744
745 #[cfg(debug_assertions)]
747 fn debug(mut self) -> Self {
748 self.style().debug = Some(true);
749 self
750 }
751
752 #[cfg(debug_assertions)]
754 fn debug_below(mut self) -> Self {
755 self.style().debug_below = Some(true);
756 self
757 }
758}