1use crate::{
4 self as rgpui, AbsoluteLength, AlignContent, AlignItems, AlignSelf, BlendMode, BorderStyle,
5 CursorStyle, DefiniteLength, Display, Fill, FlexDirection, FlexWrap, Font, FontFeatures,
6 FontStyle, FontWeight, GridPlacement, GridTemplate, GridTemplateMinSize, Hsla, JustifyContent,
7 Length, SharedString, StrikethroughStyle, StyleRefinement, TextAlign, TextOverflow,
8 TextStyleRefinement, UnderlineStyle, WhiteSpace, point, px, relative, rems,
9};
10pub use rgpui_macros::{
11 border_style_methods, box_shadow_style_methods, cursor_style_methods, margin_style_methods,
12 overflow_style_methods, padding_style_methods, position_style_methods,
13 visibility_style_methods,
14};
15const ELLIPSIS: SharedString = SharedString::new_static("…");
16
17#[cfg_attr(
21 all(any(feature = "inspector", debug_assertions), not(rust_analyzer)),
22 rgpui_macros::derive_inspector_reflection
23)]
24pub trait Styled: Sized {
25 fn style(&mut self) -> &mut StyleRefinement;
27
28 rgpui_macros::style_helpers!();
29 rgpui_macros::visibility_style_methods!();
30 rgpui_macros::margin_style_methods!();
31 rgpui_macros::padding_style_methods!();
32 rgpui_macros::position_style_methods!();
33 rgpui_macros::overflow_style_methods!();
34 rgpui_macros::cursor_style_methods!();
35 rgpui_macros::border_style_methods!();
36 rgpui_macros::box_shadow_style_methods!();
37
38 fn block(mut self) -> Self {
41 self.style().display = Some(Display::Block);
42 self
43 }
44
45 fn flex(mut self) -> Self {
48 self.style().display = Some(Display::Flex);
49 self
50 }
51
52 fn grid(mut self) -> Self {
55 self.style().display = Some(Display::Grid);
56 self
57 }
58
59 fn hidden(mut self) -> Self {
62 self.style().display = Some(Display::None);
63 self
64 }
65
66 fn scrollbar_width(mut self, width: impl Into<AbsoluteLength>) -> Self {
71 self.style().scrollbar_width = Some(width.into());
72 self
73 }
74
75 fn whitespace_normal(mut self) -> Self {
78 self.text_style().white_space = Some(WhiteSpace::Normal);
79 self
80 }
81
82 fn whitespace_nowrap(mut self) -> Self {
85 self.text_style().white_space = Some(WhiteSpace::Nowrap);
86 self
87 }
88
89 fn text_ellipsis(mut self) -> Self {
92 self.text_style().text_overflow = Some(TextOverflow::Truncate(ELLIPSIS));
93 self
94 }
95
96 fn text_ellipsis_start(mut self) -> Self {
100 self.text_style().text_overflow = Some(TextOverflow::TruncateStart(ELLIPSIS));
101 self
102 }
103
104 fn text_ellipsis_middle(mut self) -> Self {
108 self.text_style().text_overflow = Some(TextOverflow::TruncateMiddle(ELLIPSIS));
109 self
110 }
111
112 fn text_overflow(mut self, overflow: TextOverflow) -> Self {
114 self.text_style().text_overflow = Some(overflow);
115 self
116 }
117
118 fn text_align(mut self, align: TextAlign) -> Self {
120 self.text_style().text_align = Some(align);
121 self
122 }
123
124 fn text_left(mut self) -> Self {
126 self.text_align(TextAlign::Left)
127 }
128
129 fn text_center(mut self) -> Self {
131 self.text_align(TextAlign::Center)
132 }
133
134 fn text_right(mut self) -> Self {
136 self.text_align(TextAlign::Right)
137 }
138
139 fn truncate(mut self) -> Self {
142 self.overflow_hidden().whitespace_nowrap().text_ellipsis()
143 }
144
145 fn line_clamp(mut self, lines: usize) -> Self {
148 let mut text_style = self.text_style();
149 text_style.line_clamp = Some(lines);
150 self.overflow_hidden()
151 }
152
153 fn flex_col(mut self) -> Self {
156 self.style().flex_direction = Some(FlexDirection::Column);
157 self
158 }
159
160 fn flex_col_reverse(mut self) -> Self {
163 self.style().flex_direction = Some(FlexDirection::ColumnReverse);
164 self
165 }
166
167 fn flex_row(mut self) -> Self {
170 self.style().flex_direction = Some(FlexDirection::Row);
171 self
172 }
173
174 fn flex_row_reverse(mut self) -> Self {
177 self.style().flex_direction = Some(FlexDirection::RowReverse);
178 self
179 }
180
181 fn flex_1(mut self) -> Self {
184 self.style().flex_grow = Some(1.);
185 self.style().flex_shrink = Some(1.);
186 self.style().flex_basis = Some(relative(0.).into());
187 self
188 }
189
190 fn flex_auto(mut self) -> Self {
193 self.style().flex_grow = Some(1.);
194 self.style().flex_shrink = Some(1.);
195 self.style().flex_basis = Some(Length::Auto);
196 self
197 }
198
199 fn flex_initial(mut self) -> Self {
202 self.style().flex_grow = Some(0.);
203 self.style().flex_shrink = Some(1.);
204 self.style().flex_basis = Some(Length::Auto);
205 self
206 }
207
208 fn flex_none(mut self) -> Self {
211 self.style().flex_grow = Some(0.);
212 self.style().flex_shrink = Some(0.);
213 self.style().flex_basis = Some(Length::Auto);
214 self
215 }
216
217 fn flex_basis(mut self, basis: impl Into<Length>) -> Self {
220 self.style().flex_basis = Some(basis.into());
221 self
222 }
223
224 fn flex_grow(mut self, grow: f32) -> Self {
227 self.style().flex_grow = Some(grow);
228 self
229 }
230
231 fn flex_grow_0(mut self) -> Self {
234 self.style().flex_grow = Some(0.);
235 self
236 }
237
238 fn flex_grow_1(mut self) -> Self {
241 self.style().flex_grow = Some(1.);
242 self
243 }
244
245 fn flex_shrink(mut self) -> Self {
248 self.style().flex_shrink = Some(1.);
249 self
250 }
251
252 fn flex_shrink_0(mut self) -> Self {
255 self.style().flex_shrink = Some(0.);
256 self
257 }
258
259 fn flex_shrink_1(mut self) -> Self {
262 self.style().flex_shrink = Some(1.);
263 self
264 }
265
266 fn flex_wrap(mut self) -> Self {
269 self.style().flex_wrap = Some(FlexWrap::Wrap);
270 self
271 }
272
273 fn flex_wrap_reverse(mut self) -> Self {
276 self.style().flex_wrap = Some(FlexWrap::WrapReverse);
277 self
278 }
279
280 fn flex_nowrap(mut self) -> Self {
283 self.style().flex_wrap = Some(FlexWrap::NoWrap);
284 self
285 }
286
287 fn items_start(mut self) -> Self {
290 self.style().align_items = Some(AlignItems::FlexStart);
291 self
292 }
293
294 fn items_end(mut self) -> Self {
297 self.style().align_items = Some(AlignItems::FlexEnd);
298 self
299 }
300
301 fn items_center(mut self) -> Self {
304 self.style().align_items = Some(AlignItems::Center);
305 self
306 }
307
308 fn items_baseline(mut self) -> Self {
311 self.style().align_items = Some(AlignItems::Baseline);
312 self
313 }
314
315 fn items_stretch(mut self) -> Self {
318 self.style().align_items = Some(AlignItems::Stretch);
319 self
320 }
321
322 fn self_start(mut self) -> Self {
325 self.style().align_self = Some(AlignSelf::Start);
326 self
327 }
328
329 fn self_end(mut self) -> Self {
332 self.style().align_self = Some(AlignSelf::End);
333 self
334 }
335
336 fn self_flex_start(mut self) -> Self {
339 self.style().align_self = Some(AlignSelf::FlexStart);
340 self
341 }
342
343 fn self_flex_end(mut self) -> Self {
346 self.style().align_self = Some(AlignSelf::FlexEnd);
347 self
348 }
349
350 fn self_center(mut self) -> Self {
353 self.style().align_self = Some(AlignSelf::Center);
354 self
355 }
356
357 fn self_baseline(mut self) -> Self {
360 self.style().align_self = Some(AlignSelf::Baseline);
361 self
362 }
363
364 fn self_stretch(mut self) -> Self {
367 self.style().align_self = Some(AlignSelf::Stretch);
368 self
369 }
370
371 fn justify_start(mut self) -> Self {
374 self.style().justify_content = Some(JustifyContent::Start);
375 self
376 }
377
378 fn justify_end(mut self) -> Self {
381 self.style().justify_content = Some(JustifyContent::End);
382 self
383 }
384
385 fn justify_center(mut self) -> Self {
388 self.style().justify_content = Some(JustifyContent::Center);
389 self
390 }
391
392 fn justify_between(mut self) -> Self {
395 self.style().justify_content = Some(JustifyContent::SpaceBetween);
396 self
397 }
398
399 fn justify_around(mut self) -> Self {
402 self.style().justify_content = Some(JustifyContent::SpaceAround);
403 self
404 }
405
406 fn justify_evenly(mut self) -> Self {
410 self.style().justify_content = Some(JustifyContent::SpaceEvenly);
411 self
412 }
413
414 fn content_normal(mut self) -> Self {
417 self.style().align_content = None;
418 self
419 }
420
421 fn content_center(mut self) -> Self {
424 self.style().align_content = Some(AlignContent::Center);
425 self
426 }
427
428 fn content_start(mut self) -> Self {
431 self.style().align_content = Some(AlignContent::FlexStart);
432 self
433 }
434
435 fn content_end(mut self) -> Self {
438 self.style().align_content = Some(AlignContent::FlexEnd);
439 self
440 }
441
442 fn content_between(mut self) -> Self {
445 self.style().align_content = Some(AlignContent::SpaceBetween);
446 self
447 }
448
449 fn content_around(mut self) -> Self {
452 self.style().align_content = Some(AlignContent::SpaceAround);
453 self
454 }
455
456 fn content_evenly(mut self) -> Self {
459 self.style().align_content = Some(AlignContent::SpaceEvenly);
460 self
461 }
462
463 fn content_stretch(mut self) -> Self {
466 self.style().align_content = Some(AlignContent::Stretch);
467 self
468 }
469
470 fn aspect_ratio(mut self, ratio: f32) -> Self {
473 self.style().aspect_ratio = Some(ratio);
474 self
475 }
476
477 fn aspect_square(mut self) -> Self {
480 self.style().aspect_ratio = Some(1.0);
481 self
482 }
483
484 fn bg<F>(mut self, fill: F) -> Self
486 where
487 F: Into<Fill>,
488 Self: Sized,
489 {
490 self.style().background = Some(fill.into());
491 self
492 }
493
494 fn border_dashed(mut self) -> Self {
496 self.style().border_style = Some(BorderStyle::Dashed);
497 self
498 }
499
500 fn continuous_corners(mut self) -> Self {
503 self.style().continuous_corners = Some(true);
504 self
505 }
506
507 fn text_style(&mut self) -> &mut TextStyleRefinement {
509 let style: &mut StyleRefinement = self.style();
510 &mut style.text
511 }
512
513 fn text_color(mut self, color: impl Into<Hsla>) -> Self {
517 self.text_style().color = Some(color.into());
518 self
519 }
520
521 fn font_weight(mut self, weight: FontWeight) -> Self {
525 self.text_style().font_weight = Some(weight);
526 self
527 }
528
529 fn text_bg(mut self, bg: impl Into<Hsla>) -> Self {
533 self.text_style().background_color = Some(bg.into());
534 self
535 }
536
537 fn text_size(mut self, size: impl Into<AbsoluteLength>) -> Self {
541 self.text_style().font_size = Some(size.into());
542 self
543 }
544
545 fn text_xs(mut self) -> Self {
548 self.text_style().font_size = Some(rems(0.75).into());
549 self
550 }
551
552 fn text_sm(mut self) -> Self {
555 self.text_style().font_size = Some(rems(0.875).into());
556 self
557 }
558
559 fn text_base(mut self) -> Self {
562 self.text_style().font_size = Some(rems(1.0).into());
563 self
564 }
565
566 fn text_lg(mut self) -> Self {
569 self.text_style().font_size = Some(rems(1.125).into());
570 self
571 }
572
573 fn text_xl(mut self) -> Self {
576 self.text_style().font_size = Some(rems(1.25).into());
577 self
578 }
579
580 fn text_2xl(mut self) -> Self {
583 self.text_style().font_size = Some(rems(1.5).into());
584 self
585 }
586
587 fn text_3xl(mut self) -> Self {
590 self.text_style().font_size = Some(rems(1.875).into());
591 self
592 }
593
594 fn italic(mut self) -> Self {
597 self.text_style().font_style = Some(FontStyle::Italic);
598 self
599 }
600
601 fn not_italic(mut self) -> Self {
604 self.text_style().font_style = Some(FontStyle::Normal);
605 self
606 }
607
608 fn underline(mut self) -> Self {
611 let style = self.text_style();
612 style.underline = Some(UnderlineStyle {
613 thickness: px(1.),
614 ..Default::default()
615 });
616 self
617 }
618
619 fn line_through(mut self) -> Self {
622 let style = self.text_style();
623 style.strikethrough = Some(StrikethroughStyle {
624 thickness: px(1.),
625 ..Default::default()
626 });
627 self
628 }
629
630 fn text_decoration_none(mut self) -> Self {
634 self.text_style().underline = None;
635 self
636 }
637
638 fn text_decoration_color(mut self, color: impl Into<Hsla>) -> Self {
640 let style = self.text_style();
641 let underline = style.underline.get_or_insert_with(Default::default);
642 underline.color = Some(color.into());
643 self
644 }
645
646 fn text_decoration_solid(mut self) -> Self {
649 let style = self.text_style();
650 let underline = style.underline.get_or_insert_with(Default::default);
651 underline.wavy = false;
652 self
653 }
654
655 fn text_decoration_wavy(mut self) -> Self {
658 let style = self.text_style();
659 let underline = style.underline.get_or_insert_with(Default::default);
660 underline.wavy = true;
661 self
662 }
663
664 fn text_decoration_0(mut self) -> Self {
667 let style = self.text_style();
668 let underline = style.underline.get_or_insert_with(Default::default);
669 underline.thickness = px(0.);
670 self
671 }
672
673 fn text_decoration_1(mut self) -> Self {
676 let style = self.text_style();
677 let underline = style.underline.get_or_insert_with(Default::default);
678 underline.thickness = px(1.);
679 self
680 }
681
682 fn text_decoration_2(mut self) -> Self {
685 let style = self.text_style();
686 let underline = style.underline.get_or_insert_with(Default::default);
687 underline.thickness = px(2.);
688 self
689 }
690
691 fn text_decoration_4(mut self) -> Self {
694 let style = self.text_style();
695 let underline = style.underline.get_or_insert_with(Default::default);
696 underline.thickness = px(4.);
697 self
698 }
699
700 fn text_decoration_8(mut self) -> Self {
703 let style = self.text_style();
704 let underline = style.underline.get_or_insert_with(Default::default);
705 underline.thickness = px(8.);
706 self
707 }
708
709 fn font_family(mut self, family_name: impl Into<SharedString>) -> Self {
711 self.text_style().font_family = Some(family_name.into());
712 self
713 }
714
715 fn font_features(mut self, features: FontFeatures) -> Self {
717 self.text_style().font_features = Some(features);
718 self
719 }
720
721 fn font(mut self, font: Font) -> Self {
723 let Font {
724 family,
725 features,
726 fallbacks,
727 weight,
728 style,
729 } = font;
730
731 let text_style = self.text_style();
732 text_style.font_family = Some(family);
733 text_style.font_features = Some(features);
734 text_style.font_weight = Some(weight);
735 text_style.font_style = Some(style);
736 text_style.font_fallbacks = fallbacks;
737
738 self
739 }
740
741 fn line_height(mut self, line_height: impl Into<DefiniteLength>) -> Self {
743 self.text_style().line_height = Some(line_height.into());
744 self
745 }
746
747 fn opacity(mut self, opacity: f32) -> Self {
749 self.style().opacity = Some(opacity);
750 self
751 }
752
753 fn blend_mode(mut self, mode: BlendMode) -> Self {
755 self.style().blend_mode = Some(mode);
756 self
757 }
758
759 fn rotate(mut self, angle_degrees: f32) -> Self {
761 self.style().rotate = Some(angle_degrees.to_radians());
762 self
763 }
764
765 fn scale(mut self, factor: f32) -> Self {
767 self.style().scale = Some(point(factor, factor));
768 self
769 }
770
771 fn scale_xy(mut self, x: f32, y: f32) -> Self {
773 self.style().scale = Some(point(x, y));
774 self
775 }
776
777 fn transform_origin(mut self, x: f32, y: f32) -> Self {
780 self.style().transform_origin = Some(point(x, y));
781 self
782 }
783
784 fn grid_cols(mut self, cols: u16) -> Self {
786 self.style().grid_cols = Some(GridTemplate {
787 repeat: cols,
788 min_size: GridTemplateMinSize::Zero,
789 });
790 self
791 }
792
793 fn grid_cols_min_content(mut self, cols: u16) -> Self {
796 self.style().grid_cols = Some(GridTemplate {
797 repeat: cols,
798 min_size: GridTemplateMinSize::MinContent,
799 });
800 self
801 }
802
803 fn grid_cols_max_content(mut self, cols: u16) -> Self {
805 self.style().grid_cols = Some(GridTemplate {
806 repeat: cols,
807 min_size: GridTemplateMinSize::MaxContent,
808 });
809 self
810 }
811
812 fn grid_rows(mut self, rows: u16) -> Self {
814 self.style().grid_rows = Some(GridTemplate {
815 repeat: rows,
816 min_size: GridTemplateMinSize::Zero,
817 });
818 self
819 }
820
821 fn grid_rows_min_content(mut self, rows: u16) -> Self {
824 self.style().grid_rows = Some(GridTemplate {
825 repeat: rows,
826 min_size: GridTemplateMinSize::MinContent,
827 });
828 self
829 }
830
831 fn grid_rows_max_content(mut self, rows: u16) -> Self {
833 self.style().grid_rows = Some(GridTemplate {
834 repeat: rows,
835 min_size: GridTemplateMinSize::MaxContent,
836 });
837 self
838 }
839
840 fn col_start(mut self, start: i16) -> Self {
842 let grid_location = self.style().grid_location_mut();
843 grid_location.column.start = GridPlacement::Line(start);
844 self
845 }
846
847 fn col_start_auto(mut self) -> Self {
849 let grid_location = self.style().grid_location_mut();
850 grid_location.column.start = GridPlacement::Auto;
851 self
852 }
853
854 fn col_end(mut self, end: i16) -> Self {
856 let grid_location = self.style().grid_location_mut();
857 grid_location.column.end = GridPlacement::Line(end);
858 self
859 }
860
861 fn col_end_auto(mut self) -> Self {
863 let grid_location = self.style().grid_location_mut();
864 grid_location.column.end = GridPlacement::Auto;
865 self
866 }
867
868 fn col_span(mut self, span: u16) -> Self {
870 let grid_location = self.style().grid_location_mut();
871 grid_location.column = GridPlacement::Span(span)..GridPlacement::Span(span);
872 self
873 }
874
875 fn col_span_full(mut self) -> Self {
877 let grid_location = self.style().grid_location_mut();
878 grid_location.column = GridPlacement::Line(1)..GridPlacement::Line(-1);
879 self
880 }
881
882 fn row_start(mut self, start: i16) -> Self {
884 let grid_location = self.style().grid_location_mut();
885 grid_location.row.start = GridPlacement::Line(start);
886 self
887 }
888
889 fn row_start_auto(mut self) -> Self {
891 let grid_location = self.style().grid_location_mut();
892 grid_location.row.start = GridPlacement::Auto;
893 self
894 }
895
896 fn row_end(mut self, end: i16) -> Self {
898 let grid_location = self.style().grid_location_mut();
899 grid_location.row.end = GridPlacement::Line(end);
900 self
901 }
902
903 fn row_end_auto(mut self) -> Self {
905 let grid_location = self.style().grid_location_mut();
906 grid_location.row.end = GridPlacement::Auto;
907 self
908 }
909
910 fn row_span(mut self, span: u16) -> Self {
912 let grid_location = self.style().grid_location_mut();
913 grid_location.row = GridPlacement::Span(span)..GridPlacement::Span(span);
914 self
915 }
916
917 fn row_span_full(mut self) -> Self {
919 let grid_location = self.style().grid_location_mut();
920 grid_location.row = GridPlacement::Line(1)..GridPlacement::Line(-1);
921 self
922 }
923
924 #[cfg(debug_assertions)]
926 fn debug(mut self) -> Self {
927 self.style().debug = Some(true);
928 self
929 }
930
931 #[cfg(debug_assertions)]
933 fn debug_below(mut self) -> Self {
934 self.style().debug_below = Some(true);
935 self
936 }
937}