Skip to main content

vizia_core/modifiers/
style.rs

1use vizia_style::{ColorStop, CornerRadius};
2
3use super::internal;
4use crate::prelude::*;
5
6/// Modifiers for changing the style properties of a view.
7pub trait StyleModifiers: internal::Modifiable {
8    // Selectors
9
10    /// Sets the ID name of the view.
11    ///
12    /// A view can have only one ID name and it must be unique.
13    /// The ID name can be referenced by a CSS selector.
14    /// # Example
15    /// ```
16    /// # use vizia_core::prelude::*;
17    /// # let cx = &mut Context::default();
18    /// Element::new(cx).id("foo");
19    /// ```
20    /// css
21    /// ```css
22    /// #foo {
23    ///     background-color: red;
24    /// }
25    ///```
26    fn id(mut self, id: impl Into<String>) -> Self {
27        // TODO - What should happen if the id already exists?
28        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    /// Adds a class name to the view.
39    ///
40    /// A view can have multiple classes.
41    /// The class name can be referenced by a CSS selector.
42    /// # Example
43    /// ```
44    /// # use vizia_core::prelude::*;
45    /// # let cx = &mut Context::default();
46    /// Element::new(cx).class("foo");
47    /// ```
48    /// css
49    /// ```css
50    /// .foo {
51    ///     background-color: red;
52    /// }
53    ///```
54    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    /// Sets whether a view should have the given class name.
63    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    // PseudoClassFlags
78    // TODO: Should these have their own modifiers trait?
79
80    /// Sets the checked state of the view.
81    fn checked<U: Into<bool>>(mut self, state: impl Res<U>) -> Self {
82        let entity = self.entity();
83        let current = self.current();
84        // Setting a checked state should make it checkable
85        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    /// Sets the focused state of the view.
104    ///
105    /// Since only one view can have keyboard focus at a time, subsequent calls to this
106    /// function on other views will cause those views to gain focus and this view to lose it.
107    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                    // cx.focus_with_visibility(true);
118                }
119
120                cx.needs_restyle(entity);
121            });
122        });
123
124        self
125    }
126
127    /// Sets the focused state of the view as well as the focus visibility.
128    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.focus();
143                        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    /// Sets whether the view should be in a read-only state.
154    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    /// Sets whether the view should be in a read-write state.
172    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    /// Sets whether any descendant of the view has keyboard focus.
190    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    /// Sets whether the view is showing a placeholder.
208    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        /// Sets the view to be disabled.
227        ///
228        /// This property is inherited by the descendants of the view.
229        disabled,
230        bool,
231        SystemFlags::RESTYLE | SystemFlags::REACCESS
232    );
233
234    modifier!(
235        /// Sets whether the view should be positioned and rendered.
236        ///
237        /// A display value of `Display::None` causes the view to be ignored by both layout and rendering.
238        display,
239        Display,
240        SystemFlags::RELAYOUT | SystemFlags::REDRAW | SystemFlags::REFLOW
241    );
242
243    modifier!(
244        /// Sets whether the view should be rendered.
245        ///
246        /// The layout system will still compute the size and position of an invisible (hidden) view.
247        visibility,
248        Visibility,
249        SystemFlags::REDRAW
250    );
251
252    modifier!(
253        /// Sets the opacity of the view.
254        ///
255        /// Exects a value between 0.0 (transparent) and 1.0 (opaque).
256        opacity,
257        Opacity,
258        SystemFlags::REDRAW
259    );
260
261    /// Sets the z-index of the view.
262    ///
263    /// Views with a higher z-index will be rendered on top of those with a lower z-order.
264    /// Views with the same z-index are rendered in tree order.
265    fn z_index<U: Into<i32>>(mut self, value: impl Res<U>) -> Self {
266        let entity = self.entity();
267        // value.set_or_bind(self.context(), |cx, v| {
268        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        // });
273
274        self
275    }
276
277    modifier!(
278        /// Sets whether the view should ignore ancestor clipping paths when drawing.
279        ///
280        /// This is useful for overlays such as popovers that need to escape overflow clipping.
281        ignore_clipping,
282        bool,
283        SystemFlags::RECLIP | SystemFlags::REDRAW
284    );
285
286    /// Sets the clip path for the the view.
287    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    /// Sets the overflow behavior of the view in the horizontal and vertical directions simultaneously.
303    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        /// Sets the overflow behavior of the view in the horizontal direction.
321        ///
322        /// The overflow behavior determines whether child views can render outside the bounds of their parent.
323        overflowx,
324        Overflow,
325        SystemFlags::RECLIP | SystemFlags::REDRAW
326    );
327
328    modifier!(
329        /// Sets the overflow behavior of the view in the vertical direction.
330        ///
331        /// The overflow behavior determines whether child views can render outside the bounds of their parent.
332        overflowy,
333        Overflow,
334        SystemFlags::RECLIP | SystemFlags::REDRAW
335    );
336
337    /// Sets the filter for the view.
338    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    /// Sets the backdrop filter for the view.
355    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    /// Add a shadow to the view.
372    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    /// Set the shadows of the view.
392    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    /// Set the background gradient of the view.
409    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    // Background Properties
432    /// Sets the background color of the view.
433    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            // A span's background is painted by the paragraph it's shaped into. Any other view
441            // draws its own, and reflowing it would rebuild every paragraph below it.
442            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        /// Sets the background position of the view.
452        background_position,
453        Vec<Position>,
454        SystemFlags::REDRAW
455    );
456
457    modifier!(
458        /// Sets the background size of the view.
459        background_size,
460        Vec<BackgroundSize>,
461        SystemFlags::REDRAW
462    );
463
464    modifier!(
465        /// Sets the background repeat mode of the view.
466        background_repeat,
467        Vec<BackgroundRepeat>,
468        SystemFlags::REDRAW
469    );
470
471    /// Set the background image of the view.
472    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    // Border Properties
500
501    /// Sets the border width for all four sides of the view.
502    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    /// Sets the top border width of the view.
518    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    /// Sets the right border width of the view.
530    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    /// Sets the bottom border width of the view.
542    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    /// Sets the left border width of the view.
554    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    /// Sets the border color for all four sides of the view.
566    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        /// Sets the top border color of the view.
581        border_top_color,
582        Color,
583        SystemFlags::REDRAW
584    );
585
586    modifier!(
587        /// Sets the right border color of the view.
588        border_right_color,
589        Color,
590        SystemFlags::REDRAW
591    );
592
593    modifier!(
594        /// Sets the bottom border color of the view.
595        border_bottom_color,
596        Color,
597        SystemFlags::REDRAW
598    );
599
600    modifier!(
601        /// Sets the left border color of the view.
602        border_left_color,
603        Color,
604        SystemFlags::REDRAW
605    );
606
607    /// Sets the border style for all four sides of the view.
608    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        /// Sets the top border style of the view.
623        border_top_style,
624        BorderStyleKeyword,
625        SystemFlags::REDRAW
626    );
627
628    modifier!(
629        /// Sets the right border style of the view.
630        border_right_style,
631        BorderStyleKeyword,
632        SystemFlags::REDRAW
633    );
634
635    modifier!(
636        /// Sets the bottom border style of the view.
637        border_bottom_style,
638        BorderStyleKeyword,
639        SystemFlags::REDRAW
640    );
641
642    modifier!(
643        /// Sets the left border style of the view.
644        border_left_style,
645        BorderStyleKeyword,
646        SystemFlags::REDRAW
647    );
648
649    modifier!(
650        /// Sets the corner radius for the top-left corner of the view.
651        corner_top_left_radius,
652        LengthOrPercentage,
653        SystemFlags::REDRAW
654    );
655
656    modifier!(
657        /// Sets the corner radius for the top-right corner of the view.
658        corner_top_right_radius,
659        LengthOrPercentage,
660        SystemFlags::REDRAW
661    );
662
663    modifier!(
664        /// Sets the corner radius for the bottom-left corner of the view.
665        corner_bottom_left_radius,
666        LengthOrPercentage,
667        SystemFlags::REDRAW
668    );
669
670    modifier!(
671        /// Sets the corner radius for the bottom-right corner of the view.
672        corner_bottom_right_radius,
673        LengthOrPercentage,
674        SystemFlags::REDRAW
675    );
676
677    /// Sets the corner radius for all four corners of the view.
678    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    // Outline Properties
700    modifier!(
701        /// Sets the outline width of the view.
702        outline_width,
703        LengthOrPercentage,
704        SystemFlags::REDRAW
705    );
706
707    modifier!(
708        /// Sets the outline color of the view.
709        outline_color,
710        Color,
711        SystemFlags::REDRAW
712    );
713
714    modifier!(
715        /// Sets the outline offset of the view.
716        outline_offset,
717        LengthOrPercentage,
718        SystemFlags::REDRAW
719    );
720
721    modifier!(
722        /// Overrides the fill color of SVG images.
723        fill,
724        Color,
725        SystemFlags::REDRAW
726    );
727
728    // Cursor Icon
729    modifier!(
730        /// Sets the mouse cursor used when the view is hovered.
731        cursor,
732        CursorIcon,
733        SystemFlags::empty()
734    );
735
736    /// Sets whether the view can be become the target of pointer events.
737    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    /// Sets the transform of the view with a list of transform functions.
751    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    /// Sets the transform origin of the the view.
767    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    // Translate
785    modifier!(
786        /// Sets the translation offset of the view.
787        ///
788        /// Translation applies to the rendered view and does not affect layout.
789        translate,
790        Translate,
791        SystemFlags::RETRANSFORM | SystemFlags::REDRAW
792    );
793
794    // Rotate
795    modifier!(
796        /// Sets the angle of rotation for the view.
797        ///
798        /// Rotation applies to the rendered view and does not affect layout.
799        rotate,
800        Angle,
801        SystemFlags::RETRANSFORM | SystemFlags::REDRAW
802    );
803
804    // Scale
805    modifier!(
806        /// Sets the scale of the view.
807        ///
808        /// Scale applies to the rendered view and does not affect layout.
809        scale,
810        Scale,
811        SystemFlags::RETRANSFORM | SystemFlags::REDRAW
812    );
813}
814
815impl<V: View> StyleModifiers for Handle<'_, V> {}
816
817/// A builder for constructing linear gradients.
818#[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    /// Creates a new [LinearGradientBuilder].
832    pub fn new() -> Self {
833        LinearGradientBuilder { direction: LineDirection::default(), stops: Vec::new() }
834    }
835
836    /// Set the direction of the linear gradient.
837    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    /// Add a color stop to the linear gradient.
846    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/// A builder for constructing a shadow.
860#[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    /// Creates a new [ShadowBuilder].
873    pub fn new() -> Self {
874        Self { shadow: Shadow::default() }
875    }
876
877    fn build(self) -> Shadow {
878        self.shadow
879    }
880
881    /// Sets the horizontal offset of the shadow.
882    pub fn x_offset(mut self, offset: impl Into<Length>) -> Self {
883        self.shadow.x_offset = offset.into();
884
885        self
886    }
887
888    /// Set the vertical offset of the shadow.
889    pub fn y_offset(mut self, offset: impl Into<Length>) -> Self {
890        self.shadow.y_offset = offset.into();
891
892        self
893    }
894
895    /// Sets the blur radius of the shadow.
896    pub fn blur(mut self, radius: Length) -> Self {
897        self.shadow.blur_radius = Some(radius);
898
899        self
900    }
901
902    /// Sets the spread amount of the shadow.
903    pub fn spread(mut self, radius: Length) -> Self {
904        self.shadow.spread_radius = Some(radius);
905
906        self
907    }
908
909    /// Sets the color of the shadow.
910    pub fn color(mut self, color: Color) -> Self {
911        self.shadow.color = Some(color);
912
913        self
914    }
915
916    /// Sets whether the shadow should be inset.
917    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}