1use crate::{
2 AbsoluteLength, AlignContent, AlignItems, AlignSelf, BorderStyle, CursorStyle, DefiniteLength,
3 Display, Fill, FlexDirection, FlexWrap, Font, FontFeatures, FontStyle, FontWeight,
4 GridPlacement, GridTemplate, Hsla, JustifyContent, Length, SharedString, StrikethroughStyle,
5 StyleRefinement, TemplateColumnMinSize, TextAlign, TextOverflow, TextStyleRefinement,
6 UnderlineStyle, WhiteSpace, px, relative, rems,
7};
8pub use open_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 open_gpui_macros::derive_inspector_reflection
21)]
22pub trait Styled: Sized {
23 fn style(&mut self) -> &mut StyleRefinement;
25
26 open_gpui_macros::style_helpers!();
27 open_gpui_macros::visibility_style_methods!();
28 open_gpui_macros::margin_style_methods!();
29 open_gpui_macros::padding_style_methods!();
30 open_gpui_macros::position_style_methods!();
31 open_gpui_macros::overflow_style_methods!();
32 open_gpui_macros::cursor_style_methods!();
33 open_gpui_macros::border_style_methods!();
34 open_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 scrollbar_width(mut self, width: impl Into<AbsoluteLength>) -> Self {
69 self.style().scrollbar_width = Some(width.into());
70 self
71 }
72
73 fn whitespace_normal(mut self) -> Self {
76 self.text_style().white_space = Some(WhiteSpace::Normal);
77 self
78 }
79
80 fn whitespace_nowrap(mut self) -> Self {
83 self.text_style().white_space = Some(WhiteSpace::Nowrap);
84 self
85 }
86
87 fn text_ellipsis(mut self) -> Self {
90 self.text_style().text_overflow = Some(TextOverflow::Truncate(ELLIPSIS));
91 self
92 }
93
94 fn text_ellipsis_start(mut self) -> Self {
98 self.text_style().text_overflow = Some(TextOverflow::TruncateStart(ELLIPSIS));
99 self
100 }
101
102 fn text_overflow(mut self, overflow: TextOverflow) -> Self {
104 self.text_style().text_overflow = Some(overflow);
105 self
106 }
107
108 fn text_align(mut self, align: TextAlign) -> Self {
110 self.text_style().text_align = Some(align);
111 self
112 }
113
114 fn text_left(mut self) -> Self {
116 self.text_align(TextAlign::Left)
117 }
118
119 fn text_center(mut self) -> Self {
121 self.text_align(TextAlign::Center)
122 }
123
124 fn text_right(mut self) -> Self {
126 self.text_align(TextAlign::Right)
127 }
128
129 fn truncate(mut self) -> Self {
132 self.overflow_hidden().whitespace_nowrap().text_ellipsis()
133 }
134
135 fn line_clamp(mut self, lines: usize) -> Self {
138 let mut text_style = self.text_style();
139 text_style.line_clamp = Some(lines);
140 self.overflow_hidden()
141 }
142
143 fn flex_col(mut self) -> Self {
146 self.style().flex_direction = Some(FlexDirection::Column);
147 self
148 }
149
150 fn flex_col_reverse(mut self) -> Self {
153 self.style().flex_direction = Some(FlexDirection::ColumnReverse);
154 self
155 }
156
157 fn flex_row(mut self) -> Self {
160 self.style().flex_direction = Some(FlexDirection::Row);
161 self
162 }
163
164 fn flex_row_reverse(mut self) -> Self {
167 self.style().flex_direction = Some(FlexDirection::RowReverse);
168 self
169 }
170
171 fn flex_1(mut self) -> Self {
174 self.style().flex_grow = Some(1.);
175 self.style().flex_shrink = Some(1.);
176 self.style().flex_basis = Some(relative(0.).into());
177 self
178 }
179
180 fn flex_auto(mut self) -> Self {
183 self.style().flex_grow = Some(1.);
184 self.style().flex_shrink = Some(1.);
185 self.style().flex_basis = Some(Length::Auto);
186 self
187 }
188
189 fn flex_initial(mut self) -> Self {
192 self.style().flex_grow = Some(0.);
193 self.style().flex_shrink = Some(1.);
194 self.style().flex_basis = Some(Length::Auto);
195 self
196 }
197
198 fn flex_none(mut self) -> Self {
201 self.style().flex_grow = Some(0.);
202 self.style().flex_shrink = Some(0.);
203 self.style().flex_basis = Some(Length::Auto);
204 self
205 }
206
207 fn flex_basis(mut self, basis: impl Into<Length>) -> Self {
210 self.style().flex_basis = Some(basis.into());
211 self
212 }
213
214 fn flex_grow(mut self, grow: f32) -> Self {
217 self.style().flex_grow = Some(grow);
218 self
219 }
220
221 fn flex_grow_0(mut self) -> Self {
224 self.style().flex_grow = Some(0.);
225 self
226 }
227
228 fn flex_grow_1(mut self) -> Self {
231 self.style().flex_grow = Some(1.);
232 self
233 }
234
235 fn flex_shrink(mut self, shrink: f32) -> Self {
238 self.style().flex_shrink = Some(shrink);
239 self
240 }
241
242 fn flex_shrink_0(mut self) -> Self {
245 self.style().flex_shrink = Some(0.);
246 self
247 }
248
249 fn flex_shrink_1(mut self) -> Self {
252 self.style().flex_shrink = Some(1.);
253 self
254 }
255
256 fn flex_wrap(mut self) -> Self {
259 self.style().flex_wrap = Some(FlexWrap::Wrap);
260 self
261 }
262
263 fn flex_wrap_reverse(mut self) -> Self {
266 self.style().flex_wrap = Some(FlexWrap::WrapReverse);
267 self
268 }
269
270 fn flex_nowrap(mut self) -> Self {
273 self.style().flex_wrap = Some(FlexWrap::NoWrap);
274 self
275 }
276
277 fn items_start(mut self) -> Self {
280 self.style().align_items = Some(AlignItems::FlexStart);
281 self
282 }
283
284 fn items_end(mut self) -> Self {
287 self.style().align_items = Some(AlignItems::FlexEnd);
288 self
289 }
290
291 fn items_center(mut self) -> Self {
294 self.style().align_items = Some(AlignItems::Center);
295 self
296 }
297
298 fn items_baseline(mut self) -> Self {
301 self.style().align_items = Some(AlignItems::Baseline);
302 self
303 }
304
305 fn items_stretch(mut self) -> Self {
308 self.style().align_items = Some(AlignItems::Stretch);
309 self
310 }
311
312 fn self_start(mut self) -> Self {
315 self.style().align_self = Some(AlignSelf::Start);
316 self
317 }
318
319 fn self_end(mut self) -> Self {
322 self.style().align_self = Some(AlignSelf::End);
323 self
324 }
325
326 fn self_flex_start(mut self) -> Self {
329 self.style().align_self = Some(AlignSelf::FlexStart);
330 self
331 }
332
333 fn self_flex_end(mut self) -> Self {
336 self.style().align_self = Some(AlignSelf::FlexEnd);
337 self
338 }
339
340 fn self_center(mut self) -> Self {
343 self.style().align_self = Some(AlignSelf::Center);
344 self
345 }
346
347 fn self_baseline(mut self) -> Self {
350 self.style().align_self = Some(AlignSelf::Baseline);
351 self
352 }
353
354 fn self_stretch(mut self) -> Self {
357 self.style().align_self = Some(AlignSelf::Stretch);
358 self
359 }
360
361 fn justify_start(mut self) -> Self {
364 self.style().justify_content = Some(JustifyContent::Start);
365 self
366 }
367
368 fn justify_end(mut self) -> Self {
371 self.style().justify_content = Some(JustifyContent::End);
372 self
373 }
374
375 fn justify_center(mut self) -> Self {
378 self.style().justify_content = Some(JustifyContent::Center);
379 self
380 }
381
382 fn justify_between(mut self) -> Self {
386 self.style().justify_content = Some(JustifyContent::SpaceBetween);
387 self
388 }
389
390 fn justify_around(mut self) -> Self {
394 self.style().justify_content = Some(JustifyContent::SpaceAround);
395 self
396 }
397
398 fn justify_evenly(mut self) -> Self {
404 self.style().justify_content = Some(JustifyContent::SpaceEvenly);
405 self
406 }
407
408 fn content_normal(mut self) -> Self {
411 self.style().align_content = None;
412 self
413 }
414
415 fn content_center(mut self) -> Self {
418 self.style().align_content = Some(AlignContent::Center);
419 self
420 }
421
422 fn content_start(mut self) -> Self {
425 self.style().align_content = Some(AlignContent::FlexStart);
426 self
427 }
428
429 fn content_end(mut self) -> Self {
432 self.style().align_content = Some(AlignContent::FlexEnd);
433 self
434 }
435
436 fn content_between(mut self) -> Self {
440 self.style().align_content = Some(AlignContent::SpaceBetween);
441 self
442 }
443
444 fn content_around(mut self) -> Self {
448 self.style().align_content = Some(AlignContent::SpaceAround);
449 self
450 }
451
452 fn content_evenly(mut self) -> Self {
456 self.style().align_content = Some(AlignContent::SpaceEvenly);
457 self
458 }
459
460 fn content_stretch(mut self) -> Self {
463 self.style().align_content = Some(AlignContent::Stretch);
464 self
465 }
466
467 fn aspect_ratio(mut self, ratio: f32) -> Self {
470 self.style().aspect_ratio = Some(ratio);
471 self
472 }
473
474 fn aspect_square(mut self) -> Self {
477 self.style().aspect_ratio = Some(1.0);
478 self
479 }
480
481 fn bg<F>(mut self, fill: F) -> Self
483 where
484 F: Into<Fill>,
485 Self: Sized,
486 {
487 self.style().background = Some(fill.into());
488 self
489 }
490
491 fn border_dashed(mut self) -> Self {
493 self.style().border_style = Some(BorderStyle::Dashed);
494 self
495 }
496
497 fn text_style(&mut self) -> &mut TextStyleRefinement {
499 let style: &mut StyleRefinement = self.style();
500 &mut style.text
501 }
502
503 fn text_color(mut self, color: impl Into<Hsla>) -> Self {
507 self.text_style().color = Some(color.into());
508 self
509 }
510
511 fn font_weight(mut self, weight: FontWeight) -> Self {
515 self.text_style().font_weight = Some(weight);
516 self
517 }
518
519 fn text_bg(mut self, bg: impl Into<Hsla>) -> Self {
523 self.text_style().background_color = Some(bg.into());
524 self
525 }
526
527 fn text_size(mut self, size: impl Into<AbsoluteLength>) -> Self {
531 self.text_style().font_size = Some(size.into());
532 self
533 }
534
535 fn text_xs(mut self) -> Self {
538 self.text_style().font_size = Some(rems(0.75).into());
539 self
540 }
541
542 fn text_sm(mut self) -> Self {
545 self.text_style().font_size = Some(rems(0.875).into());
546 self
547 }
548
549 fn text_base(mut self) -> Self {
552 self.text_style().font_size = Some(rems(1.0).into());
553 self
554 }
555
556 fn text_lg(mut self) -> Self {
559 self.text_style().font_size = Some(rems(1.125).into());
560 self
561 }
562
563 fn text_xl(mut self) -> Self {
566 self.text_style().font_size = Some(rems(1.25).into());
567 self
568 }
569
570 fn text_2xl(mut self) -> Self {
573 self.text_style().font_size = Some(rems(1.5).into());
574 self
575 }
576
577 fn text_3xl(mut self) -> Self {
580 self.text_style().font_size = Some(rems(1.875).into());
581 self
582 }
583
584 fn italic(mut self) -> Self {
587 self.text_style().font_style = Some(FontStyle::Italic);
588 self
589 }
590
591 fn not_italic(mut self) -> Self {
594 self.text_style().font_style = Some(FontStyle::Normal);
595 self
596 }
597
598 fn underline(mut self) -> Self {
601 let style = self.text_style();
602 style.underline = Some(UnderlineStyle {
603 thickness: px(1.),
604 ..Default::default()
605 });
606 self
607 }
608
609 fn line_through(mut self) -> Self {
612 let style = self.text_style();
613 style.strikethrough = Some(StrikethroughStyle {
614 thickness: px(1.),
615 ..Default::default()
616 });
617 self
618 }
619
620 fn text_decoration_none(mut self) -> Self {
624 self.text_style().underline = None;
625 self
626 }
627
628 fn text_decoration_color(mut self, color: impl Into<Hsla>) -> Self {
630 let style = self.text_style();
631 let underline = style.underline.get_or_insert_with(Default::default);
632 underline.color = Some(color.into());
633 self
634 }
635
636 fn text_decoration_solid(mut self) -> Self {
639 let style = self.text_style();
640 let underline = style.underline.get_or_insert_with(Default::default);
641 underline.wavy = false;
642 self
643 }
644
645 fn text_decoration_wavy(mut self) -> Self {
648 let style = self.text_style();
649 let underline = style.underline.get_or_insert_with(Default::default);
650 underline.wavy = true;
651 self
652 }
653
654 fn text_decoration_0(mut self) -> Self {
657 let style = self.text_style();
658 let underline = style.underline.get_or_insert_with(Default::default);
659 underline.thickness = px(0.);
660 self
661 }
662
663 fn text_decoration_1(mut self) -> Self {
666 let style = self.text_style();
667 let underline = style.underline.get_or_insert_with(Default::default);
668 underline.thickness = px(1.);
669 self
670 }
671
672 fn text_decoration_2(mut self) -> Self {
675 let style = self.text_style();
676 let underline = style.underline.get_or_insert_with(Default::default);
677 underline.thickness = px(2.);
678 self
679 }
680
681 fn text_decoration_4(mut self) -> Self {
684 let style = self.text_style();
685 let underline = style.underline.get_or_insert_with(Default::default);
686 underline.thickness = px(4.);
687 self
688 }
689
690 fn text_decoration_8(mut self) -> Self {
693 let style = self.text_style();
694 let underline = style.underline.get_or_insert_with(Default::default);
695 underline.thickness = px(8.);
696 self
697 }
698
699 fn font_family(mut self, family_name: impl Into<SharedString>) -> Self {
701 self.text_style().font_family = Some(family_name.into());
702 self
703 }
704
705 fn font_features(mut self, features: FontFeatures) -> Self {
707 self.text_style().font_features = Some(features);
708 self
709 }
710
711 fn font(mut self, font: Font) -> Self {
713 let Font {
714 family,
715 features,
716 fallbacks,
717 weight,
718 style,
719 } = font;
720
721 let text_style = self.text_style();
722 text_style.font_family = Some(family);
723 text_style.font_features = Some(features);
724 text_style.font_weight = Some(weight);
725 text_style.font_style = Some(style);
726 text_style.font_fallbacks = fallbacks;
727
728 self
729 }
730
731 fn line_height(mut self, line_height: impl Into<DefiniteLength>) -> Self {
733 self.text_style().line_height = Some(line_height.into());
734 self
735 }
736
737 fn opacity(mut self, opacity: f32) -> Self {
739 self.style().opacity = Some(opacity);
740 self
741 }
742
743 fn grid_cols(mut self, cols: u16) -> Self {
745 self.style().grid_cols = Some(GridTemplate {
746 repeat: cols,
747 min_size: TemplateColumnMinSize::Zero,
748 });
749 self
750 }
751
752 fn grid_cols_min_content(mut self, cols: u16) -> Self {
755 self.style().grid_cols = Some(GridTemplate {
756 repeat: cols,
757 min_size: TemplateColumnMinSize::MinContent,
758 });
759 self
760 }
761
762 fn grid_cols_max_content(mut self, cols: u16) -> Self {
764 self.style().grid_cols = Some(GridTemplate {
765 repeat: cols,
766 min_size: TemplateColumnMinSize::MaxContent,
767 });
768 self
769 }
770
771 fn grid_rows(mut self, rows: u16) -> Self {
773 self.style().grid_rows = Some(GridTemplate {
774 repeat: rows,
775 min_size: TemplateColumnMinSize::Zero,
776 });
777 self
778 }
779
780 fn col_start(mut self, start: i16) -> Self {
782 let grid_location = self.style().grid_location_mut();
783 grid_location.column.start = GridPlacement::Line(start);
784 self
785 }
786
787 fn col_start_auto(mut self) -> Self {
789 let grid_location = self.style().grid_location_mut();
790 grid_location.column.start = GridPlacement::Auto;
791 self
792 }
793
794 fn col_end(mut self, end: i16) -> Self {
796 let grid_location = self.style().grid_location_mut();
797 grid_location.column.end = GridPlacement::Line(end);
798 self
799 }
800
801 fn col_end_auto(mut self) -> Self {
803 let grid_location = self.style().grid_location_mut();
804 grid_location.column.end = GridPlacement::Auto;
805 self
806 }
807
808 fn col_span(mut self, span: u16) -> Self {
810 let grid_location = self.style().grid_location_mut();
811 grid_location.column = GridPlacement::Span(span)..GridPlacement::Span(span);
812 self
813 }
814
815 fn col_span_full(mut self) -> Self {
817 let grid_location = self.style().grid_location_mut();
818 grid_location.column = GridPlacement::Line(1)..GridPlacement::Line(-1);
819 self
820 }
821
822 fn row_start(mut self, start: i16) -> Self {
824 let grid_location = self.style().grid_location_mut();
825 grid_location.row.start = GridPlacement::Line(start);
826 self
827 }
828
829 fn row_start_auto(mut self) -> Self {
831 let grid_location = self.style().grid_location_mut();
832 grid_location.row.start = GridPlacement::Auto;
833 self
834 }
835
836 fn row_end(mut self, end: i16) -> Self {
838 let grid_location = self.style().grid_location_mut();
839 grid_location.row.end = GridPlacement::Line(end);
840 self
841 }
842
843 fn row_end_auto(mut self) -> Self {
845 let grid_location = self.style().grid_location_mut();
846 grid_location.row.end = GridPlacement::Auto;
847 self
848 }
849
850 fn row_span(mut self, span: u16) -> Self {
852 let grid_location = self.style().grid_location_mut();
853 grid_location.row = GridPlacement::Span(span)..GridPlacement::Span(span);
854 self
855 }
856
857 fn row_span_full(mut self) -> Self {
859 let grid_location = self.style().grid_location_mut();
860 grid_location.row = GridPlacement::Line(1)..GridPlacement::Line(-1);
861 self
862 }
863
864 #[cfg(debug_assertions)]
866 fn debug(mut self) -> Self {
867 self.style().debug = Some(true);
868 self
869 }
870
871 #[cfg(debug_assertions)]
873 fn debug_below(mut self) -> Self {
874 self.style().debug_below = Some(true);
875 self
876 }
877}