1use crate::{
2 self as wgpui, 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 wgpui_derive::{
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 wgpui_derive::derive_inspector_reflection
21)]
22pub trait Styled: Sized {
23 fn style(&mut self) -> &mut StyleRefinement;
25
26 wgpui_derive::style_helpers!();
27 wgpui_derive::visibility_style_methods!();
28 wgpui_derive::margin_style_methods!();
29 wgpui_derive::padding_style_methods!();
30 wgpui_derive::position_style_methods!();
31 wgpui_derive::overflow_style_methods!();
32 wgpui_derive::cursor_style_methods!();
33 wgpui_derive::border_style_methods!();
34 wgpui_derive::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 aspect_ratio(mut self, ratio: f32) -> Self {
208 self.style().aspect_ratio = Some(ratio);
209 self
210 }
211
212 fn self_start(mut self) -> Self {
214 self.style().align_self = Some(AlignItems::Start);
215 self
216 }
217
218 fn self_end(mut self) -> Self {
220 self.style().align_self = Some(AlignItems::End);
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_1(mut self) -> Self {
233 self.flex_grow(1.)
234 }
235
236 fn flex_shrink(mut self, shrink: f32) -> Self {
239 self.style().flex_shrink = Some(shrink);
240 self
241 }
242
243 fn flex_grow_0(self) -> Self {
245 self.flex_grow(0.)
246 }
247
248 fn flex_shrink_1(mut self) -> Self {
250 self.flex_shrink(1.)
251 }
252
253 fn flex_shrink_0(mut self) -> Self {
256 self.flex_shrink(0.)
257 }
258
259 fn flex_wrap(mut self) -> Self {
262 self.style().flex_wrap = Some(FlexWrap::Wrap);
263 self
264 }
265
266 fn flex_wrap_reverse(mut self) -> Self {
269 self.style().flex_wrap = Some(FlexWrap::WrapReverse);
270 self
271 }
272
273 fn flex_nowrap(mut self) -> Self {
276 self.style().flex_wrap = Some(FlexWrap::NoWrap);
277 self
278 }
279
280 fn items_start(mut self) -> Self {
283 self.style().align_items = Some(AlignItems::FlexStart);
284 self
285 }
286
287 fn items_end(mut self) -> Self {
290 self.style().align_items = Some(AlignItems::FlexEnd);
291 self
292 }
293
294 fn items_stretch(mut self) -> Self {
296 self.style().align_items = Some(crate::AlignItems::Stretch);
297 self
298 }
299
300 fn items_center(mut self) -> Self {
303 self.style().align_items = Some(AlignItems::Center);
304 self
305 }
306
307 fn items_baseline(mut self) -> Self {
310 self.style().align_items = Some(AlignItems::Baseline);
311 self
312 }
313
314 fn justify_start(mut self) -> Self {
317 self.style().justify_content = Some(JustifyContent::Start);
318 self
319 }
320
321 fn justify_end(mut self) -> Self {
324 self.style().justify_content = Some(JustifyContent::End);
325 self
326 }
327
328 fn justify_center(mut self) -> Self {
331 self.style().justify_content = Some(JustifyContent::Center);
332 self
333 }
334
335 fn justify_between(mut self) -> Self {
339 self.style().justify_content = Some(JustifyContent::SpaceBetween);
340 self
341 }
342
343 fn justify_around(mut self) -> Self {
347 self.style().justify_content = Some(JustifyContent::SpaceAround);
348 self
349 }
350
351 fn justify_evenly(mut self) -> Self {
357 self.style().justify_content = Some(JustifyContent::SpaceEvenly);
358 self
359 }
360
361 fn content_normal(mut self) -> Self {
364 self.style().align_content = None;
365 self
366 }
367
368 fn content_center(mut self) -> Self {
371 self.style().align_content = Some(AlignContent::Center);
372 self
373 }
374
375 fn content_start(mut self) -> Self {
378 self.style().align_content = Some(AlignContent::FlexStart);
379 self
380 }
381
382 fn content_end(mut self) -> Self {
385 self.style().align_content = Some(AlignContent::FlexEnd);
386 self
387 }
388
389 fn content_between(mut self) -> Self {
393 self.style().align_content = Some(AlignContent::SpaceBetween);
394 self
395 }
396
397 fn content_around(mut self) -> Self {
401 self.style().align_content = Some(AlignContent::SpaceAround);
402 self
403 }
404
405 fn content_evenly(mut self) -> Self {
409 self.style().align_content = Some(AlignContent::SpaceEvenly);
410 self
411 }
412
413 fn content_stretch(mut self) -> Self {
416 self.style().align_content = Some(AlignContent::Stretch);
417 self
418 }
419
420 fn bg<F>(mut self, fill: F) -> Self
422 where
423 F: Into<Fill>,
424 Self: Sized,
425 {
426 self.style().background = Some(fill.into());
427 self
428 }
429
430 fn border_dashed(mut self) -> Self {
432 self.style().border_style = Some(BorderStyle::Dashed);
433 self
434 }
435
436 fn text_style(&mut self) -> &mut Option<TextStyleRefinement> {
438 let style: &mut StyleRefinement = self.style();
439 &mut style.text
440 }
441
442 fn text_color(mut self, color: impl Into<Hsla>) -> Self {
446 self.text_style().get_or_insert_with(Default::default).color = Some(color.into());
447 self
448 }
449
450 fn font_weight(mut self, weight: FontWeight) -> Self {
454 self.text_style()
455 .get_or_insert_with(Default::default)
456 .font_weight = Some(weight);
457 self
458 }
459
460 fn text_bg(mut self, bg: impl Into<Hsla>) -> Self {
464 self.text_style()
465 .get_or_insert_with(Default::default)
466 .background_color = Some(bg.into());
467 self
468 }
469
470 fn text_size(mut self, size: impl Into<AbsoluteLength>) -> Self {
474 self.text_style()
475 .get_or_insert_with(Default::default)
476 .font_size = Some(size.into());
477 self
478 }
479
480 fn text_xs(mut self) -> Self {
483 self.text_style()
484 .get_or_insert_with(Default::default)
485 .font_size = Some(rems(0.75).into());
486 self
487 }
488
489 fn text_sm(mut self) -> Self {
492 self.text_style()
493 .get_or_insert_with(Default::default)
494 .font_size = Some(rems(0.875).into());
495 self
496 }
497
498 fn text_base(mut self) -> Self {
501 self.text_style()
502 .get_or_insert_with(Default::default)
503 .font_size = Some(rems(1.0).into());
504 self
505 }
506
507 fn text_lg(mut self) -> Self {
510 self.text_style()
511 .get_or_insert_with(Default::default)
512 .font_size = Some(rems(1.125).into());
513 self
514 }
515
516 fn text_xl(mut self) -> Self {
519 self.text_style()
520 .get_or_insert_with(Default::default)
521 .font_size = Some(rems(1.25).into());
522 self
523 }
524
525 fn text_2xl(mut self) -> Self {
528 self.text_style()
529 .get_or_insert_with(Default::default)
530 .font_size = Some(rems(1.5).into());
531 self
532 }
533
534 fn text_3xl(mut self) -> Self {
537 self.text_style()
538 .get_or_insert_with(Default::default)
539 .font_size = Some(rems(1.875).into());
540 self
541 }
542
543 fn italic(mut self) -> Self {
546 self.text_style()
547 .get_or_insert_with(Default::default)
548 .font_style = Some(FontStyle::Italic);
549 self
550 }
551
552 fn not_italic(mut self) -> Self {
555 self.text_style()
556 .get_or_insert_with(Default::default)
557 .font_style = Some(FontStyle::Normal);
558 self
559 }
560
561 fn underline(mut self) -> Self {
564 let style = self.text_style().get_or_insert_with(Default::default);
565 style.underline = Some(UnderlineStyle {
566 thickness: px(1.),
567 ..Default::default()
568 });
569 self
570 }
571
572 fn line_through(mut self) -> Self {
575 let style = self.text_style().get_or_insert_with(Default::default);
576 style.strikethrough = Some(StrikethroughStyle {
577 thickness: px(1.),
578 ..Default::default()
579 });
580 self
581 }
582
583 fn text_decoration_none(mut self) -> Self {
587 self.text_style()
588 .get_or_insert_with(Default::default)
589 .underline = None;
590 self
591 }
592
593 fn text_decoration_color(mut self, color: impl Into<Hsla>) -> Self {
595 let style = self.text_style().get_or_insert_with(Default::default);
596 let underline = style.underline.get_or_insert_with(Default::default);
597 underline.color = Some(color.into());
598 self
599 }
600
601 fn text_decoration_solid(mut self) -> Self {
604 let style = self.text_style().get_or_insert_with(Default::default);
605 let underline = style.underline.get_or_insert_with(Default::default);
606 underline.wavy = false;
607 self
608 }
609
610 fn text_decoration_wavy(mut self) -> Self {
613 let style = self.text_style().get_or_insert_with(Default::default);
614 let underline = style.underline.get_or_insert_with(Default::default);
615 underline.wavy = true;
616 self
617 }
618
619 fn text_decoration_0(mut self) -> Self {
622 let style = self.text_style().get_or_insert_with(Default::default);
623 let underline = style.underline.get_or_insert_with(Default::default);
624 underline.thickness = px(0.);
625 self
626 }
627
628 fn text_decoration_1(mut self) -> Self {
631 let style = self.text_style().get_or_insert_with(Default::default);
632 let underline = style.underline.get_or_insert_with(Default::default);
633 underline.thickness = px(1.);
634 self
635 }
636
637 fn text_decoration_2(mut self) -> Self {
640 let style = self.text_style().get_or_insert_with(Default::default);
641 let underline = style.underline.get_or_insert_with(Default::default);
642 underline.thickness = px(2.);
643 self
644 }
645
646 fn text_decoration_4(mut self) -> Self {
649 let style = self.text_style().get_or_insert_with(Default::default);
650 let underline = style.underline.get_or_insert_with(Default::default);
651 underline.thickness = px(4.);
652 self
653 }
654
655 fn text_decoration_8(mut self) -> Self {
658 let style = self.text_style().get_or_insert_with(Default::default);
659 let underline = style.underline.get_or_insert_with(Default::default);
660 underline.thickness = px(8.);
661 self
662 }
663
664 fn font_family(mut self, family_name: impl Into<SharedString>) -> Self {
666 self.text_style()
667 .get_or_insert_with(Default::default)
668 .font_family = Some(family_name.into());
669 self
670 }
671
672 fn font_features(mut self, features: FontFeatures) -> Self {
674 self.text_style()
675 .get_or_insert_with(Default::default)
676 .font_features = Some(features);
677 self
678 }
679
680 fn font(mut self, font: Font) -> Self {
682 let Font {
683 family,
684 features,
685 fallbacks,
686 weight,
687 style,
688 } = font;
689
690 let text_style = self.text_style().get_or_insert_with(Default::default);
691 text_style.font_family = Some(family);
692 text_style.font_features = Some(features);
693 text_style.font_weight = Some(weight);
694 text_style.font_style = Some(style);
695 text_style.font_fallbacks = fallbacks;
696
697 self
698 }
699
700 fn line_height(mut self, line_height: impl Into<DefiniteLength>) -> Self {
702 self.text_style()
703 .get_or_insert_with(Default::default)
704 .line_height = Some(line_height.into());
705 self
706 }
707
708 fn opacity(mut self, opacity: f32) -> Self {
710 self.style().opacity = Some(opacity);
711 self
712 }
713
714 fn grid_cols(mut self, cols: u16) -> Self {
716 self.style().grid_cols = Some(cols);
717 self
718 }
719
720 fn grid_rows(mut self, rows: u16) -> Self {
722 self.style().grid_rows = Some(rows);
723 self
724 }
725
726 fn col_start(mut self, start: i16) -> Self {
728 let grid_location = self.style().grid_location_mut();
729 grid_location.column.start = GridPlacement::Line(start);
730 self
731 }
732
733 fn col_start_auto(mut self) -> Self {
735 let grid_location = self.style().grid_location_mut();
736 grid_location.column.start = GridPlacement::Auto;
737 self
738 }
739
740 fn col_end(mut self, end: i16) -> Self {
742 let grid_location = self.style().grid_location_mut();
743 grid_location.column.end = GridPlacement::Line(end);
744 self
745 }
746
747 fn col_end_auto(mut self) -> Self {
749 let grid_location = self.style().grid_location_mut();
750 grid_location.column.end = GridPlacement::Auto;
751 self
752 }
753
754 fn col_span(mut self, span: u16) -> Self {
756 let grid_location = self.style().grid_location_mut();
757 grid_location.column = GridPlacement::Span(span)..GridPlacement::Span(span);
758 self
759 }
760
761 fn col_span_full(mut self) -> Self {
763 let grid_location = self.style().grid_location_mut();
764 grid_location.column = GridPlacement::Line(1)..GridPlacement::Line(-1);
765 self
766 }
767
768 fn row_start(mut self, start: i16) -> Self {
770 let grid_location = self.style().grid_location_mut();
771 grid_location.row.start = GridPlacement::Line(start);
772 self
773 }
774
775 fn row_start_auto(mut self) -> Self {
777 let grid_location = self.style().grid_location_mut();
778 grid_location.row.start = GridPlacement::Auto;
779 self
780 }
781
782 fn row_end(mut self, end: i16) -> Self {
784 let grid_location = self.style().grid_location_mut();
785 grid_location.row.end = GridPlacement::Line(end);
786 self
787 }
788
789 fn row_end_auto(mut self) -> Self {
791 let grid_location = self.style().grid_location_mut();
792 grid_location.row.end = GridPlacement::Auto;
793 self
794 }
795
796 fn row_span(mut self, span: u16) -> Self {
798 let grid_location = self.style().grid_location_mut();
799 grid_location.row = GridPlacement::Span(span)..GridPlacement::Span(span);
800 self
801 }
802
803 fn row_span_full(mut self) -> Self {
805 let grid_location = self.style().grid_location_mut();
806 grid_location.row = GridPlacement::Line(1)..GridPlacement::Line(-1);
807 self
808 }
809
810 #[cfg(debug_assertions)]
812 fn debug(mut self) -> Self {
813 self.style().debug = Some(true);
814 self
815 }
816
817 #[cfg(debug_assertions)]
819 fn debug_below(mut self) -> Self {
820 self.style().debug_below = Some(true);
821 self
822 }
823}