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()
68 .get_or_insert_with(Default::default)
69 .white_space = Some(WhiteSpace::Normal);
70 self
71 }
72
73 fn whitespace_nowrap(mut self) -> Self {
76 self.text_style()
77 .get_or_insert_with(Default::default)
78 .white_space = Some(WhiteSpace::Nowrap);
79 self
80 }
81
82 fn text_ellipsis(mut self) -> Self {
85 self.text_style()
86 .get_or_insert_with(Default::default)
87 .text_overflow = Some(TextOverflow::Truncate(ELLIPSIS));
88 self
89 }
90
91 fn text_overflow(mut self, overflow: TextOverflow) -> Self {
93 self.text_style()
94 .get_or_insert_with(Default::default)
95 .text_overflow = Some(overflow);
96 self
97 }
98
99 fn text_align(mut self, align: TextAlign) -> Self {
101 self.text_style()
102 .get_or_insert_with(Default::default)
103 .text_align = Some(align);
104 self
105 }
106
107 fn text_left(mut self) -> Self {
109 self.text_align(TextAlign::Left)
110 }
111
112 fn text_center(mut self) -> Self {
114 self.text_align(TextAlign::Center)
115 }
116
117 fn text_right(mut self) -> Self {
119 self.text_align(TextAlign::Right)
120 }
121
122 fn truncate(mut self) -> Self {
125 self.overflow_hidden().whitespace_nowrap().text_ellipsis()
126 }
127
128 fn line_clamp(mut self, lines: usize) -> Self {
131 let mut text_style = self.text_style().get_or_insert_with(Default::default);
132 text_style.line_clamp = Some(lines);
133 self.overflow_hidden()
134 }
135
136 fn flex_col(mut self) -> Self {
139 self.style().flex_direction = Some(FlexDirection::Column);
140 self
141 }
142
143 fn flex_col_reverse(mut self) -> Self {
146 self.style().flex_direction = Some(FlexDirection::ColumnReverse);
147 self
148 }
149
150 fn flex_row(mut self) -> Self {
153 self.style().flex_direction = Some(FlexDirection::Row);
154 self
155 }
156
157 fn flex_row_reverse(mut self) -> Self {
160 self.style().flex_direction = Some(FlexDirection::RowReverse);
161 self
162 }
163
164 fn flex_1(mut self) -> Self {
167 self.style().flex_grow = Some(1.);
168 self.style().flex_shrink = Some(1.);
169 self.style().flex_basis = Some(relative(0.).into());
170 self
171 }
172
173 fn flex_auto(mut self) -> Self {
176 self.style().flex_grow = Some(1.);
177 self.style().flex_shrink = Some(1.);
178 self.style().flex_basis = Some(Length::Auto);
179 self
180 }
181
182 fn flex_initial(mut self) -> Self {
185 self.style().flex_grow = Some(0.);
186 self.style().flex_shrink = Some(1.);
187 self.style().flex_basis = Some(Length::Auto);
188 self
189 }
190
191 fn flex_none(mut self) -> Self {
194 self.style().flex_grow = Some(0.);
195 self.style().flex_shrink = Some(0.);
196 self
197 }
198
199 fn flex_basis(mut self, basis: impl Into<Length>) -> Self {
202 self.style().flex_basis = Some(basis.into());
203 self
204 }
205
206 fn flex_grow(mut self) -> Self {
209 self.style().flex_grow = Some(1.);
210 self
211 }
212
213 fn flex_shrink(mut self) -> Self {
216 self.style().flex_shrink = Some(1.);
217 self
218 }
219
220 fn flex_shrink_0(mut self) -> Self {
223 self.style().flex_shrink = Some(0.);
224 self
225 }
226
227 fn flex_wrap(mut self) -> Self {
230 self.style().flex_wrap = Some(FlexWrap::Wrap);
231 self
232 }
233
234 fn flex_wrap_reverse(mut self) -> Self {
237 self.style().flex_wrap = Some(FlexWrap::WrapReverse);
238 self
239 }
240
241 fn flex_nowrap(mut self) -> Self {
244 self.style().flex_wrap = Some(FlexWrap::NoWrap);
245 self
246 }
247
248 fn items_start(mut self) -> Self {
251 self.style().align_items = Some(AlignItems::FlexStart);
252 self
253 }
254
255 fn items_end(mut self) -> Self {
258 self.style().align_items = Some(AlignItems::FlexEnd);
259 self
260 }
261
262 fn items_center(mut self) -> Self {
265 self.style().align_items = Some(AlignItems::Center);
266 self
267 }
268
269 fn items_baseline(mut self) -> Self {
272 self.style().align_items = Some(AlignItems::Baseline);
273 self
274 }
275
276 fn justify_start(mut self) -> Self {
279 self.style().justify_content = Some(JustifyContent::Start);
280 self
281 }
282
283 fn justify_end(mut self) -> Self {
286 self.style().justify_content = Some(JustifyContent::End);
287 self
288 }
289
290 fn justify_center(mut self) -> Self {
293 self.style().justify_content = Some(JustifyContent::Center);
294 self
295 }
296
297 fn justify_between(mut self) -> Self {
301 self.style().justify_content = Some(JustifyContent::SpaceBetween);
302 self
303 }
304
305 fn justify_around(mut self) -> Self {
309 self.style().justify_content = Some(JustifyContent::SpaceAround);
310 self
311 }
312
313 fn justify_evenly(mut self) -> Self {
319 self.style().justify_content = Some(JustifyContent::SpaceEvenly);
320 self
321 }
322
323 fn content_normal(mut self) -> Self {
326 self.style().align_content = None;
327 self
328 }
329
330 fn content_center(mut self) -> Self {
333 self.style().align_content = Some(AlignContent::Center);
334 self
335 }
336
337 fn content_start(mut self) -> Self {
340 self.style().align_content = Some(AlignContent::FlexStart);
341 self
342 }
343
344 fn content_end(mut self) -> Self {
347 self.style().align_content = Some(AlignContent::FlexEnd);
348 self
349 }
350
351 fn content_between(mut self) -> Self {
355 self.style().align_content = Some(AlignContent::SpaceBetween);
356 self
357 }
358
359 fn content_around(mut self) -> Self {
363 self.style().align_content = Some(AlignContent::SpaceAround);
364 self
365 }
366
367 fn content_evenly(mut self) -> Self {
371 self.style().align_content = Some(AlignContent::SpaceEvenly);
372 self
373 }
374
375 fn content_stretch(mut self) -> Self {
378 self.style().align_content = Some(AlignContent::Stretch);
379 self
380 }
381
382 fn bg<F>(mut self, fill: F) -> Self
384 where
385 F: Into<Fill>,
386 Self: Sized,
387 {
388 self.style().background = Some(fill.into());
389 self
390 }
391
392 fn border_dashed(mut self) -> Self {
394 self.style().border_style = Some(BorderStyle::Dashed);
395 self
396 }
397
398 fn text_style(&mut self) -> &mut Option<TextStyleRefinement> {
400 let style: &mut StyleRefinement = self.style();
401 &mut style.text
402 }
403
404 fn text_color(mut self, color: impl Into<Hsla>) -> Self {
408 self.text_style().get_or_insert_with(Default::default).color = Some(color.into());
409 self
410 }
411
412 fn font_weight(mut self, weight: FontWeight) -> Self {
416 self.text_style()
417 .get_or_insert_with(Default::default)
418 .font_weight = Some(weight);
419 self
420 }
421
422 fn text_bg(mut self, bg: impl Into<Hsla>) -> Self {
426 self.text_style()
427 .get_or_insert_with(Default::default)
428 .background_color = Some(bg.into());
429 self
430 }
431
432 fn text_size(mut self, size: impl Into<AbsoluteLength>) -> Self {
436 self.text_style()
437 .get_or_insert_with(Default::default)
438 .font_size = Some(size.into());
439 self
440 }
441
442 fn text_xs(mut self) -> Self {
445 self.text_style()
446 .get_or_insert_with(Default::default)
447 .font_size = Some(rems(0.75).into());
448 self
449 }
450
451 fn text_sm(mut self) -> Self {
454 self.text_style()
455 .get_or_insert_with(Default::default)
456 .font_size = Some(rems(0.875).into());
457 self
458 }
459
460 fn text_base(mut self) -> Self {
463 self.text_style()
464 .get_or_insert_with(Default::default)
465 .font_size = Some(rems(1.0).into());
466 self
467 }
468
469 fn text_lg(mut self) -> Self {
472 self.text_style()
473 .get_or_insert_with(Default::default)
474 .font_size = Some(rems(1.125).into());
475 self
476 }
477
478 fn text_xl(mut self) -> Self {
481 self.text_style()
482 .get_or_insert_with(Default::default)
483 .font_size = Some(rems(1.25).into());
484 self
485 }
486
487 fn text_2xl(mut self) -> Self {
490 self.text_style()
491 .get_or_insert_with(Default::default)
492 .font_size = Some(rems(1.5).into());
493 self
494 }
495
496 fn text_3xl(mut self) -> Self {
499 self.text_style()
500 .get_or_insert_with(Default::default)
501 .font_size = Some(rems(1.875).into());
502 self
503 }
504
505 fn italic(mut self) -> Self {
508 self.text_style()
509 .get_or_insert_with(Default::default)
510 .font_style = Some(FontStyle::Italic);
511 self
512 }
513
514 fn not_italic(mut self) -> Self {
517 self.text_style()
518 .get_or_insert_with(Default::default)
519 .font_style = Some(FontStyle::Normal);
520 self
521 }
522
523 fn underline(mut self) -> Self {
526 let style = self.text_style().get_or_insert_with(Default::default);
527 style.underline = Some(UnderlineStyle {
528 thickness: px(1.),
529 ..Default::default()
530 });
531 self
532 }
533
534 fn line_through(mut self) -> Self {
537 let style = self.text_style().get_or_insert_with(Default::default);
538 style.strikethrough = Some(StrikethroughStyle {
539 thickness: px(1.),
540 ..Default::default()
541 });
542 self
543 }
544
545 fn text_decoration_none(mut self) -> Self {
549 self.text_style()
550 .get_or_insert_with(Default::default)
551 .underline = None;
552 self
553 }
554
555 fn text_decoration_color(mut self, color: impl Into<Hsla>) -> Self {
557 let style = self.text_style().get_or_insert_with(Default::default);
558 let underline = style.underline.get_or_insert_with(Default::default);
559 underline.color = Some(color.into());
560 self
561 }
562
563 fn text_decoration_solid(mut self) -> Self {
566 let style = self.text_style().get_or_insert_with(Default::default);
567 let underline = style.underline.get_or_insert_with(Default::default);
568 underline.wavy = false;
569 self
570 }
571
572 fn text_decoration_wavy(mut self) -> Self {
575 let style = self.text_style().get_or_insert_with(Default::default);
576 let underline = style.underline.get_or_insert_with(Default::default);
577 underline.wavy = true;
578 self
579 }
580
581 fn text_decoration_0(mut self) -> Self {
584 let style = self.text_style().get_or_insert_with(Default::default);
585 let underline = style.underline.get_or_insert_with(Default::default);
586 underline.thickness = px(0.);
587 self
588 }
589
590 fn text_decoration_1(mut self) -> Self {
593 let style = self.text_style().get_or_insert_with(Default::default);
594 let underline = style.underline.get_or_insert_with(Default::default);
595 underline.thickness = px(1.);
596 self
597 }
598
599 fn text_decoration_2(mut self) -> Self {
602 let style = self.text_style().get_or_insert_with(Default::default);
603 let underline = style.underline.get_or_insert_with(Default::default);
604 underline.thickness = px(2.);
605 self
606 }
607
608 fn text_decoration_4(mut self) -> Self {
611 let style = self.text_style().get_or_insert_with(Default::default);
612 let underline = style.underline.get_or_insert_with(Default::default);
613 underline.thickness = px(4.);
614 self
615 }
616
617 fn text_decoration_8(mut self) -> Self {
620 let style = self.text_style().get_or_insert_with(Default::default);
621 let underline = style.underline.get_or_insert_with(Default::default);
622 underline.thickness = px(8.);
623 self
624 }
625
626 fn font_family(mut self, family_name: impl Into<SharedString>) -> Self {
628 self.text_style()
629 .get_or_insert_with(Default::default)
630 .font_family = Some(family_name.into());
631 self
632 }
633
634 fn font_features(mut self, features: FontFeatures) -> Self {
636 self.text_style()
637 .get_or_insert_with(Default::default)
638 .font_features = Some(features);
639 self
640 }
641
642 fn font(mut self, font: Font) -> Self {
644 let Font {
645 family,
646 features,
647 fallbacks,
648 weight,
649 style,
650 } = font;
651
652 let text_style = self.text_style().get_or_insert_with(Default::default);
653 text_style.font_family = Some(family);
654 text_style.font_features = Some(features);
655 text_style.font_weight = Some(weight);
656 text_style.font_style = Some(style);
657 text_style.font_fallbacks = fallbacks;
658
659 self
660 }
661
662 fn line_height(mut self, line_height: impl Into<DefiniteLength>) -> Self {
664 self.text_style()
665 .get_or_insert_with(Default::default)
666 .line_height = Some(line_height.into());
667 self
668 }
669
670 fn opacity(mut self, opacity: f32) -> Self {
672 self.style().opacity = Some(opacity);
673 self
674 }
675
676 fn grid_cols(mut self, cols: u16) -> Self {
678 self.style().grid_cols = Some(cols);
679 self
680 }
681
682 fn grid_rows(mut self, rows: u16) -> Self {
684 self.style().grid_rows = Some(rows);
685 self
686 }
687
688 fn col_start(mut self, start: i16) -> Self {
690 let grid_location = self.style().grid_location_mut();
691 grid_location.column.start = GridPlacement::Line(start);
692 self
693 }
694
695 fn col_start_auto(mut self) -> Self {
697 let grid_location = self.style().grid_location_mut();
698 grid_location.column.start = GridPlacement::Auto;
699 self
700 }
701
702 fn col_end(mut self, end: i16) -> Self {
704 let grid_location = self.style().grid_location_mut();
705 grid_location.column.end = GridPlacement::Line(end);
706 self
707 }
708
709 fn col_end_auto(mut self) -> Self {
711 let grid_location = self.style().grid_location_mut();
712 grid_location.column.end = GridPlacement::Auto;
713 self
714 }
715
716 fn col_span(mut self, span: u16) -> Self {
718 let grid_location = self.style().grid_location_mut();
719 grid_location.column = GridPlacement::Span(span)..GridPlacement::Span(span);
720 self
721 }
722
723 fn col_span_full(mut self) -> Self {
725 let grid_location = self.style().grid_location_mut();
726 grid_location.column = GridPlacement::Line(1)..GridPlacement::Line(-1);
727 self
728 }
729
730 fn row_start(mut self, start: i16) -> Self {
732 let grid_location = self.style().grid_location_mut();
733 grid_location.row.start = GridPlacement::Line(start);
734 self
735 }
736
737 fn row_start_auto(mut self) -> Self {
739 let grid_location = self.style().grid_location_mut();
740 grid_location.row.start = GridPlacement::Auto;
741 self
742 }
743
744 fn row_end(mut self, end: i16) -> Self {
746 let grid_location = self.style().grid_location_mut();
747 grid_location.row.end = GridPlacement::Line(end);
748 self
749 }
750
751 fn row_end_auto(mut self) -> Self {
753 let grid_location = self.style().grid_location_mut();
754 grid_location.row.end = GridPlacement::Auto;
755 self
756 }
757
758 fn row_span(mut self, span: u16) -> Self {
760 let grid_location = self.style().grid_location_mut();
761 grid_location.row = GridPlacement::Span(span)..GridPlacement::Span(span);
762 self
763 }
764
765 fn row_span_full(mut self) -> Self {
767 let grid_location = self.style().grid_location_mut();
768 grid_location.row = GridPlacement::Line(1)..GridPlacement::Line(-1);
769 self
770 }
771
772 #[cfg(debug_assertions)]
774 fn debug(mut self) -> Self {
775 self.style().debug = Some(true);
776 self
777 }
778
779 #[cfg(debug_assertions)]
781 fn debug_below(mut self) -> Self {
782 self.style().debug_below = Some(true);
783 self
784 }
785}