vizia_core/modifiers/
mod.rs

1//! Methods on views for changing their properties or for adding actions.
2//!
3//! # Examples
4//! Modifiers can be used to apply inline [style](StyleModifiers) and [layout](LayoutModifiers) properties to a view:
5//! ```no_run
6//! # use vizia_core::prelude::*;
7//! # let cx = &mut Context::default();
8//! # use vizia_winit::application::Application;
9//! # Application::new(|cx|{
10//! // Sets the background color of the label to red.
11//! Label::new(cx, "Hello World")
12//!     .background_color(Color::red());
13//! # }).run();
14//! ```
15//!
16//! ```no_run
17//! # use vizia_core::prelude::*;
18//! # let cx = &mut Context::default();
19//! # use vizia_winit::application::Application;
20//! # Application::new(|cx|{
21//! // Sets the width of the label to be 100 pixels.
22//! Label::new(cx, "Hello World")
23//!     .width(Pixels(100.0));
24//! # }).run();
25//! ```
26//!
27//! Modifiers can also be used to add [actions](ActionModifiers) to a view:
28//! ```no_run
29//! # use vizia_core::prelude::*;
30//! # let cx = &mut Context::default();
31//! # use vizia_winit::application::Application;
32//! # Application::new(|cx|{
33//! // Closes the window when the label is pressed.
34//! Label::new(cx, "Hello World")
35//!     .on_press(|cx| cx.emit(WindowEvent::WindowClose));
36//! # }).run();
37//! ```
38
39// Macro used within modifier traits to set style properties.
40macro_rules! modifier {
41    (
42        $(#[$meta:meta])*
43        $name:ident, $t:ty, $flags:expr
44    ) => {
45        $(#[$meta])*
46        #[allow(unused_variables)]
47        fn $name<U: Into<$t>>(mut self, value: impl Res<U>) -> Self {
48            let entity = self.entity();
49            let current = self.current();
50            value.set_or_bind(self.context(), current, move |cx, v| {
51                cx.style.$name.insert(entity, v.get(cx).into());
52
53                cx.style.system_flags |= $flags;
54                cx.set_system_flags(entity, $flags);
55            });
56
57            self
58        }
59    };
60}
61
62// Inside private module to hide implementation details.
63mod internal {
64    use crate::prelude::{Context, Entity, Handle};
65
66    // Allows a modifier trait to access to context and entity from `self`.
67    pub trait Modifiable: Sized {
68        fn context(&mut self) -> &mut Context;
69        fn entity(&self) -> Entity;
70        fn current(&self) -> Entity;
71    }
72
73    impl<V> Modifiable for Handle<'_, V> {
74        fn context(&mut self) -> &mut Context {
75            self.cx
76        }
77
78        fn entity(&self) -> Entity {
79            self.entity
80        }
81
82        fn current(&self) -> Entity {
83            self.current
84        }
85    }
86}
87
88mod accessibility;
89pub use accessibility::*;
90
91mod actions;
92pub use actions::*;
93
94mod layout;
95pub use layout::*;
96
97mod style;
98pub use style::*;
99
100mod text;
101pub use text::*;
102
103mod abilities;
104pub use abilities::*;