1use vizia_style::{ColorStop, CornerRadius};
2
3use super::internal;
4use crate::prelude::*;
5
6pub trait StyleModifiers: internal::Modifiable {
8 fn id(mut self, id: impl Into<String>) -> Self {
27 let id = id.into();
29 let entity = self.entity();
30 let cx = self.context();
31 cx.style.set_id(&cx.tree, entity, id.clone());
32
33 cx.entity_identifiers.insert(id, entity);
34
35 self
36 }
37
38 fn class(mut self, name: &str) -> Self {
55 let entity = self.entity();
56 let cx = self.context();
57 cx.style.set_class(&cx.tree, entity, name, true);
58
59 self
60 }
61
62 fn toggle_class(mut self, name: &str, applied: impl Res<bool>) -> Self {
64 let name = name.to_owned();
65 let entity = self.entity();
66 let current = self.current();
67 self.context().with_current(current, |cx| {
68 applied.set_or_bind(cx, move |cx, applied| {
69 let applied = applied.get_value(cx);
70 cx.style.set_class(&cx.tree, entity, &name, applied);
71 });
72 });
73
74 self
75 }
76
77 fn checked<U: Into<bool>>(mut self, state: impl Res<U>) -> Self {
82 let entity = self.entity();
83 let current = self.current();
84 if let Some(abilities) = self.context().style.abilities.get_mut(entity) {
86 abilities.set(Abilities::CHECKABLE, true);
87 }
88
89 self.context().with_current(current, move |cx| {
90 state.set_or_bind(cx, move |cx, val| {
91 let val = val.get_value(cx).into();
92 if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(entity) {
93 pseudo_classes.set(PseudoClassFlags::CHECKED, val);
94 }
95 cx.needs_restyle(entity);
96 cx.style.needs_access_update(entity);
97 });
98 });
99
100 self
101 }
102
103 fn focused<U: Into<bool>>(mut self, state: impl Res<U>) -> Self {
108 let entity = self.entity();
109 let current = self.current();
110
111 self.context().with_current(current, |cx| {
112 state.set_or_bind(cx, move |cx, val| {
113 let val = val.get_value(cx).into();
114
115 if val {
116 cx.with_current(entity, |cx| cx.focus());
117 }
119
120 cx.needs_restyle(entity);
121 });
122 });
123
124 self
125 }
126
127 fn focused_with_visibility<U: Into<bool>>(
129 mut self,
130 focus: impl Res<U> + Copy + 'static,
131 visibility: impl Res<U> + Copy + 'static,
132 ) -> Self {
133 let entity = self.entity();
134 let current = self.current();
135
136 self.context().with_current(current, move |cx| {
137 focus.set_or_bind(cx, move |cx, f| {
138 visibility.set_or_bind(cx, move |cx, v| {
139 let focus = f.get_value(cx).into();
140 let visibility = v.get_value(cx).into();
141 if focus {
142 cx.with_current(entity, |cx| cx.focus_with_visibility(visibility));
144 cx.needs_restyle(entity);
145 }
146 });
147 });
148 });
149
150 self
151 }
152
153 fn read_only<U: Into<bool>>(mut self, state: impl Res<U>) -> Self {
155 let entity = self.entity();
156 let current = self.current();
157 self.context().with_current(current, |cx| {
158 state.set_or_bind(cx, move |cx, val| {
159 let val = val.get_value(cx).into();
160 if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(entity) {
161 pseudo_classes.set(PseudoClassFlags::READ_ONLY, val);
162 }
163
164 cx.needs_restyle(entity);
165 });
166 });
167
168 self
169 }
170
171 fn read_write<U: Into<bool>>(mut self, state: impl Res<U>) -> Self {
173 let entity = self.entity();
174 let current = self.current();
175 self.context().with_current(current, |cx| {
176 state.set_or_bind(cx, move |cx, val| {
177 let val = val.get_value(cx).into();
178 if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(entity) {
179 pseudo_classes.set(PseudoClassFlags::READ_WRITE, val);
180 }
181
182 cx.needs_restyle(entity);
183 });
184 });
185
186 self
187 }
188
189 fn focus_within<U: Into<bool>>(mut self, state: impl Res<U>) -> Self {
191 let entity = self.entity();
192 let current = self.current();
193 self.context().with_current(current, |cx| {
194 state.set_or_bind(cx, move |cx, val| {
195 let val = val.get_value(cx).into();
196 if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(entity) {
197 pseudo_classes.set(PseudoClassFlags::FOCUS_WITHIN, val);
198 }
199
200 cx.needs_restyle(entity);
201 });
202 });
203
204 self
205 }
206
207 fn placeholder_shown<U: Into<bool>>(mut self, state: impl Res<U>) -> Self {
209 let entity = self.entity();
210 let current = self.current();
211 self.context().with_current(current, |cx| {
212 state.set_or_bind(cx, move |cx, val| {
213 let val = val.get_value(cx).into();
214 if let Some(pseudo_classes) = cx.style.pseudo_classes.get_mut(entity) {
215 pseudo_classes.set(PseudoClassFlags::PLACEHOLDER_SHOWN, val);
216 }
217
218 cx.needs_restyle(entity);
219 });
220 });
221
222 self
223 }
224
225 modifier!(
226 disabled,
230 bool,
231 SystemFlags::RESTYLE | SystemFlags::REACCESS
232 );
233
234 modifier!(
235 display,
239 Display,
240 SystemFlags::RELAYOUT | SystemFlags::REDRAW | SystemFlags::REFLOW
241 );
242
243 modifier!(
244 visibility,
248 Visibility,
249 SystemFlags::REDRAW
250 );
251
252 modifier!(
253 opacity,
257 Opacity,
258 SystemFlags::REDRAW
259 );
260
261 fn z_index<U: Into<i32>>(mut self, value: impl Res<U>) -> Self {
266 let entity = self.entity();
267 let cx = self.context();
269 let value = value.get_value(cx).into();
270 cx.style.z_index.insert(entity, value);
271 cx.needs_redraw(entity);
272 self
275 }
276
277 modifier!(
278 ignore_clipping,
282 bool,
283 SystemFlags::RECLIP | SystemFlags::REDRAW
284 );
285
286 fn clip_path<U: Into<ClipPath>>(mut self, value: impl Res<U>) -> Self {
288 let entity = self.entity();
289 let current = self.current();
290 self.context().with_current(current, |cx| {
291 value.set_or_bind(cx, move |cx, v| {
292 let value = v.get_value(cx).into();
293 cx.style.clip_path.insert(entity, value);
294 cx.needs_reclip(entity);
295 cx.needs_redraw(entity);
296 });
297 });
298
299 self
300 }
301
302 fn overflow<U: Into<Overflow>>(mut self, value: impl Res<U>) -> Self {
304 let entity = self.entity();
305 let current = self.current();
306 self.context().with_current(current, |cx| {
307 value.set_or_bind(cx, move |cx, v| {
308 let value = v.get_value(cx).into();
309 cx.style.overflowx.insert(entity, value);
310 cx.style.overflowy.insert(entity, value);
311 cx.needs_reclip(entity);
312 cx.needs_redraw(entity);
313 });
314 });
315
316 self
317 }
318
319 modifier!(
320 overflowx,
324 Overflow,
325 SystemFlags::RECLIP | SystemFlags::REDRAW
326 );
327
328 modifier!(
329 overflowy,
333 Overflow,
334 SystemFlags::RECLIP | SystemFlags::REDRAW
335 );
336
337 fn filter<U: Into<Filter>>(mut self, value: impl Res<U>) -> Self {
339 let entity = self.entity();
340 let current = self.current();
341 self.context().with_current(current, |cx| {
342 value.set_or_bind(cx, move |cx, v| {
343 let value = v.get_value(cx).into();
344 cx.style.filter.insert(entity, value);
345 cx.style.filter_entities.insert(entity);
346
347 cx.needs_redraw(entity);
348 });
349 });
350
351 self
352 }
353
354 fn backdrop_filter<U: Into<Filter>>(mut self, value: impl Res<U>) -> Self {
356 let entity = self.entity();
357 let current = self.current();
358 self.context().with_current(current, |cx| {
359 value.set_or_bind(cx, move |cx, v| {
360 let value = v.get_value(cx).into();
361 cx.style.backdrop_filter.insert(entity, value);
362 cx.style.filter_entities.insert(entity);
363
364 cx.needs_redraw(entity);
365 });
366 });
367
368 self
369 }
370
371 fn shadow<U: Into<Shadow>>(mut self, value: impl Res<U>) -> Self {
373 let entity = self.entity();
374 let current = self.current();
375 self.context().with_current(current, |cx| {
376 value.set_or_bind(cx, move |cx, v| {
377 let value = v.get_value(cx).into();
378 if let Some(shadows) = cx.style.shadow.get_inline_mut(entity) {
379 shadows.push(value);
380 } else {
381 cx.style.shadow.insert(entity, vec![value]);
382 }
383
384 cx.needs_redraw(entity);
385 });
386 });
387
388 self
389 }
390
391 fn shadows<U: Into<Vec<Shadow>>>(mut self, value: impl Res<U>) -> Self {
393 let entity = self.entity();
394 let current = self.current();
395 self.context().with_current(current, |cx| {
396 value.set_or_bind(cx, move |cx, v| {
397 let value = v.get_value(cx).into();
398
399 cx.style.shadow.insert(entity, value);
400
401 cx.needs_redraw(entity);
402 });
403 });
404
405 self
406 }
407
408 fn background_gradient<U: Into<Gradient>>(mut self, value: impl Res<U>) -> Self {
410 let entity = self.entity();
411 let current = self.current();
412 self.context().with_current(current, |cx| {
413 value.set_or_bind(cx, move |cx, v| {
414 let value = v.get_value(cx).into();
415 if let Some(background_images) = cx.style.background_image.get_inline_mut(entity) {
416 background_images.push(ImageOrGradient::Gradient(value));
417 } else {
418 cx.style
419 .background_image
420 .insert(entity, vec![ImageOrGradient::Gradient(value)]);
421 cx.style.background_image_entities.insert(entity);
422 }
423
424 cx.needs_redraw(entity);
425 });
426 });
427
428 self
429 }
430
431 fn background_color<U: Into<Color>>(mut self, value: impl Res<U>) -> Self {
434 let entity = self.entity();
435 value.set_or_bind(self.context(), move |cx, v| {
436 cx.style.background_color.insert(entity, v.get_value(cx).into());
437 cx.style.system_flags |= SystemFlags::REDRAW;
438 cx.set_system_flags(entity, SystemFlags::REDRAW);
439
440 if cx.style.text_span.get(entity).copied().unwrap_or_default() {
443 cx.style.needs_text_update(entity);
444 }
445 });
446
447 self
448 }
449
450 modifier!(
451 background_position,
453 Vec<Position>,
454 SystemFlags::REDRAW
455 );
456
457 modifier!(
458 background_size,
460 Vec<BackgroundSize>,
461 SystemFlags::REDRAW
462 );
463
464 modifier!(
465 background_repeat,
467 Vec<BackgroundRepeat>,
468 SystemFlags::REDRAW
469 );
470
471 fn background_image<'i, U: Into<BackgroundImage<'i>>>(mut self, value: impl Res<U>) -> Self {
473 let entity = self.entity();
474 let current = self.current();
475 self.context().with_current(current, |cx| {
476 value.set_or_bind(cx, move |cx, val| {
477 let image = val.get_value(cx).into();
478 let image = match image {
479 BackgroundImage::Gradient(gradient) => {
480 Some(ImageOrGradient::Gradient(*gradient))
481 }
482 BackgroundImage::Url(url) => Some(ImageOrGradient::Image(url.url.to_string())),
483
484 _ => None,
485 };
486
487 if let Some(image) = image {
488 cx.style.background_image.insert(entity, vec![image]);
489 cx.style.background_image_entities.insert(entity);
490 }
491
492 cx.needs_redraw(entity);
493 });
494 });
495
496 self
497 }
498
499 fn border_width<U: Into<LengthOrPercentage>>(mut self, value: impl Res<U>) -> Self {
503 let entity = self.entity();
504 value.set_or_bind(self.context(), move |cx, v| {
505 let w: LengthOrPercentage = v.get_value(cx).into();
506 cx.style.border_top_width.insert(entity, w.clone());
507 cx.style.border_right_width.insert(entity, w.clone());
508 cx.style.border_bottom_width.insert(entity, w.clone());
509 cx.style.border_left_width.insert(entity, w);
510 cx.cache.path.remove(entity);
511 cx.style.system_flags |= SystemFlags::RELAYOUT | SystemFlags::REDRAW;
512 cx.set_system_flags(entity, SystemFlags::RELAYOUT | SystemFlags::REDRAW);
513 });
514 self
515 }
516
517 fn border_top_width<U: Into<LengthOrPercentage>>(mut self, value: impl Res<U>) -> Self {
519 let entity = self.entity();
520 value.set_or_bind(self.context(), move |cx, v| {
521 cx.style.border_top_width.insert(entity, v.get_value(cx).into());
522 cx.cache.path.remove(entity);
523 cx.style.system_flags |= SystemFlags::RELAYOUT | SystemFlags::REDRAW;
524 cx.set_system_flags(entity, SystemFlags::RELAYOUT | SystemFlags::REDRAW);
525 });
526 self
527 }
528
529 fn border_right_width<U: Into<LengthOrPercentage>>(mut self, value: impl Res<U>) -> Self {
531 let entity = self.entity();
532 value.set_or_bind(self.context(), move |cx, v| {
533 cx.style.border_right_width.insert(entity, v.get_value(cx).into());
534 cx.cache.path.remove(entity);
535 cx.style.system_flags |= SystemFlags::RELAYOUT | SystemFlags::REDRAW;
536 cx.set_system_flags(entity, SystemFlags::RELAYOUT | SystemFlags::REDRAW);
537 });
538 self
539 }
540
541 fn border_bottom_width<U: Into<LengthOrPercentage>>(mut self, value: impl Res<U>) -> Self {
543 let entity = self.entity();
544 value.set_or_bind(self.context(), move |cx, v| {
545 cx.style.border_bottom_width.insert(entity, v.get_value(cx).into());
546 cx.cache.path.remove(entity);
547 cx.style.system_flags |= SystemFlags::RELAYOUT | SystemFlags::REDRAW;
548 cx.set_system_flags(entity, SystemFlags::RELAYOUT | SystemFlags::REDRAW);
549 });
550 self
551 }
552
553 fn border_left_width<U: Into<LengthOrPercentage>>(mut self, value: impl Res<U>) -> Self {
555 let entity = self.entity();
556 value.set_or_bind(self.context(), move |cx, v| {
557 cx.style.border_left_width.insert(entity, v.get_value(cx).into());
558 cx.cache.path.remove(entity);
559 cx.style.system_flags |= SystemFlags::RELAYOUT | SystemFlags::REDRAW;
560 cx.set_system_flags(entity, SystemFlags::RELAYOUT | SystemFlags::REDRAW);
561 });
562 self
563 }
564
565 fn border_color(mut self, value: impl Res<Color>) -> Self {
567 let entity = self.entity();
568 value.set_or_bind(self.context(), move |cx, v| {
569 let c = v.get_value(cx);
570 cx.style.border_top_color.insert(entity, c);
571 cx.style.border_right_color.insert(entity, c);
572 cx.style.border_bottom_color.insert(entity, c);
573 cx.style.border_left_color.insert(entity, c);
574 cx.needs_redraw(entity);
575 });
576 self
577 }
578
579 modifier!(
580 border_top_color,
582 Color,
583 SystemFlags::REDRAW
584 );
585
586 modifier!(
587 border_right_color,
589 Color,
590 SystemFlags::REDRAW
591 );
592
593 modifier!(
594 border_bottom_color,
596 Color,
597 SystemFlags::REDRAW
598 );
599
600 modifier!(
601 border_left_color,
603 Color,
604 SystemFlags::REDRAW
605 );
606
607 fn border_style(mut self, value: impl Res<BorderStyleKeyword>) -> Self {
609 let entity = self.entity();
610 value.set_or_bind(self.context(), move |cx, v| {
611 let s = v.get_value(cx);
612 cx.style.border_top_style.insert(entity, s);
613 cx.style.border_right_style.insert(entity, s);
614 cx.style.border_bottom_style.insert(entity, s);
615 cx.style.border_left_style.insert(entity, s);
616 cx.needs_redraw(entity);
617 });
618 self
619 }
620
621 modifier!(
622 border_top_style,
624 BorderStyleKeyword,
625 SystemFlags::REDRAW
626 );
627
628 modifier!(
629 border_right_style,
631 BorderStyleKeyword,
632 SystemFlags::REDRAW
633 );
634
635 modifier!(
636 border_bottom_style,
638 BorderStyleKeyword,
639 SystemFlags::REDRAW
640 );
641
642 modifier!(
643 border_left_style,
645 BorderStyleKeyword,
646 SystemFlags::REDRAW
647 );
648
649 modifier!(
650 corner_top_left_radius,
652 LengthOrPercentage,
653 SystemFlags::REDRAW
654 );
655
656 modifier!(
657 corner_top_right_radius,
659 LengthOrPercentage,
660 SystemFlags::REDRAW
661 );
662
663 modifier!(
664 corner_bottom_left_radius,
666 LengthOrPercentage,
667 SystemFlags::REDRAW
668 );
669
670 modifier!(
671 corner_bottom_right_radius,
673 LengthOrPercentage,
674 SystemFlags::REDRAW
675 );
676
677 fn corner_radius<U: std::fmt::Debug + Into<CornerRadius>>(
679 mut self,
680 value: impl Res<U>,
681 ) -> Self {
682 let entity = self.entity();
683 let current = self.current();
684 self.context().with_current(current, |cx| {
685 value.set_or_bind(cx, move |cx, v| {
686 let value = v.get_value(cx).into();
687 cx.style.corner_top_left_radius.insert(entity, value.top_left);
688 cx.style.corner_top_right_radius.insert(entity, value.top_right);
689 cx.style.corner_bottom_left_radius.insert(entity, value.bottom_left);
690 cx.style.corner_bottom_right_radius.insert(entity, value.bottom_right);
691
692 cx.needs_redraw(entity);
693 });
694 });
695
696 self
697 }
698
699 modifier!(
701 outline_width,
703 LengthOrPercentage,
704 SystemFlags::REDRAW
705 );
706
707 modifier!(
708 outline_color,
710 Color,
711 SystemFlags::REDRAW
712 );
713
714 modifier!(
715 outline_offset,
717 LengthOrPercentage,
718 SystemFlags::REDRAW
719 );
720
721 modifier!(
722 fill,
724 Color,
725 SystemFlags::REDRAW
726 );
727
728 modifier!(
730 cursor,
732 CursorIcon,
733 SystemFlags::empty()
734 );
735
736 fn pointer_events<U: Into<PointerEvents>>(mut self, value: impl Res<U>) -> Self {
738 let entity = self.entity();
739 let current = self.current();
740 self.context().with_current(current, |cx| {
741 value.set_or_bind(cx, move |cx, v| {
742 let value = v.get_value(cx).into();
743 cx.style.pointer_events.insert(entity, value);
744 });
745 });
746
747 self
748 }
749
750 fn transform<U: Into<Vec<Transform>>>(mut self, value: impl Res<U>) -> Self {
752 let entity = self.entity();
753 let current = self.current();
754 self.context().with_current(current, |cx| {
755 value.set_or_bind(cx, move |cx, v| {
756 let value = v.get_value(cx).into();
757 cx.style.transform.insert(entity, value);
758 cx.needs_retransform(entity);
759 cx.needs_redraw(entity);
760 });
761 });
762
763 self
764 }
765
766 fn transform_origin<U: Into<Position>>(mut self, value: impl Res<U>) -> Self {
768 let entity = self.entity();
769 let current = self.current();
770 self.context().with_current(current, |cx| {
771 value.set_or_bind(cx, move |cx, v| {
772 let value = v.get_value(cx).into();
773 let x = value.x.to_length_or_percentage();
774 let y = value.y.to_length_or_percentage();
775 cx.style.transform_origin.insert(entity, Translate { x, y });
776 cx.needs_retransform(entity);
777 cx.needs_redraw(entity);
778 });
779 });
780
781 self
782 }
783
784 modifier!(
786 translate,
790 Translate,
791 SystemFlags::RETRANSFORM | SystemFlags::REDRAW
792 );
793
794 modifier!(
796 rotate,
800 Angle,
801 SystemFlags::RETRANSFORM | SystemFlags::REDRAW
802 );
803
804 modifier!(
806 scale,
810 Scale,
811 SystemFlags::RETRANSFORM | SystemFlags::REDRAW
812 );
813}
814
815impl<V: View> StyleModifiers for Handle<'_, V> {}
816
817#[derive(Debug, Clone)]
819pub struct LinearGradientBuilder {
820 direction: LineDirection,
821 stops: Vec<ColorStop<LengthOrPercentage>>,
822}
823
824impl Default for LinearGradientBuilder {
825 fn default() -> Self {
826 Self::new()
827 }
828}
829
830impl LinearGradientBuilder {
831 pub fn new() -> Self {
833 LinearGradientBuilder { direction: LineDirection::default(), stops: Vec::new() }
834 }
835
836 pub fn with_direction(direction: impl Into<LineDirection>) -> Self {
838 LinearGradientBuilder { direction: direction.into(), stops: Vec::new() }
839 }
840
841 fn build(self) -> Gradient {
842 Gradient::Linear(LinearGradient { direction: self.direction, stops: self.stops })
843 }
844
845 pub fn add_stop(mut self, stop: impl Into<ColorStop<LengthOrPercentage>>) -> Self {
847 self.stops.push(stop.into());
848
849 self
850 }
851}
852
853impl From<LinearGradientBuilder> for Gradient {
854 fn from(value: LinearGradientBuilder) -> Self {
855 value.build()
856 }
857}
858
859#[derive(Debug, Clone)]
861pub struct ShadowBuilder {
862 shadow: Shadow,
863}
864
865impl Default for ShadowBuilder {
866 fn default() -> Self {
867 Self::new()
868 }
869}
870
871impl ShadowBuilder {
872 pub fn new() -> Self {
874 Self { shadow: Shadow::default() }
875 }
876
877 fn build(self) -> Shadow {
878 self.shadow
879 }
880
881 pub fn x_offset(mut self, offset: impl Into<Length>) -> Self {
883 self.shadow.x_offset = offset.into();
884
885 self
886 }
887
888 pub fn y_offset(mut self, offset: impl Into<Length>) -> Self {
890 self.shadow.y_offset = offset.into();
891
892 self
893 }
894
895 pub fn blur(mut self, radius: Length) -> Self {
897 self.shadow.blur_radius = Some(radius);
898
899 self
900 }
901
902 pub fn spread(mut self, radius: Length) -> Self {
904 self.shadow.spread_radius = Some(radius);
905
906 self
907 }
908
909 pub fn color(mut self, color: Color) -> Self {
911 self.shadow.color = Some(color);
912
913 self
914 }
915
916 pub fn inset(mut self) -> Self {
918 self.shadow.inset = true;
919
920 self
921 }
922}
923
924impl From<ShadowBuilder> for Shadow {
925 fn from(value: ShadowBuilder) -> Self {
926 value.build()
927 }
928}