makepad_render/
styling.rs1use crate::cx::*;
2use std::any::TypeId;
3
4impl Cx{
5 pub fn begin_style(&mut self, style_id:StyleId){
6 if let Some(index) = self.style_map.get(&style_id){
8 self.style_stack.push(*index);
9 }
10 else{
11 let index = self.styles.len();
12 self.style_map.insert(style_id, index);
13 self.styles.push(CxStyle::default());
14 self.style_stack.push(index);
15 }
16 }
17
18 pub fn end_style(&mut self){
19 self.style_stack.pop().expect("end_style pop failed");
20 }
21
22 pub fn get_mut_style_top(&mut self)->&mut CxStyle{
23 if let Some(last) = self.style_stack.last(){
24 &mut self.styles[*last]
25 }
26 else{
27 &mut self.style_base
28 }
29 }
30
31}
32
33#[derive(PartialEq, Copy, Clone, Hash, Eq)]
37pub struct FloatId(pub TypeId);
38
39impl FloatId {
40 pub fn set(&self, cx: &mut Cx, value: f32) {
41 cx.get_mut_style_top().floats.insert(*self, value);
42 }
43
44 pub fn get(&self, cx: &Cx) -> f32 {
45 for style_id in &cx.style_stack{
46 if let Some(value) = cx.styles[*style_id].floats.get(self){
47 return *value
48 }
49 }
50 *cx.style_base.floats.get(&*self).expect("Cannot find FloatId")
51 }
52}
53
54impl Into<FloatId> for UniqueId {
55 fn into(self) -> FloatId {FloatId(self.0)}
56}
57
58
59
60#[derive(PartialEq, Copy, Clone, Hash, Eq)]
64pub struct ColorId(pub TypeId);
65
66impl ColorId {
67 pub fn set(&self, cx: &mut Cx, value: Color) {
68 cx.get_mut_style_top().colors.insert(*self, value);
69 }
70
71 pub fn get(&self, cx: &Cx) -> Color {
72 for style_id in &cx.style_stack{
73 if let Some(value) = cx.styles[*style_id].colors.get(self){
74 return *value
75 }
76 }
77 *cx.style_base.colors.get(&*self).expect("Cannot find ColorId")
78 }
79}
80
81impl Into<ColorId> for UniqueId {
82 fn into(self) -> ColorId {ColorId(self.0)}
83}
84
85
86#[derive(PartialEq, Copy, Clone, Hash, Eq)]
91pub struct TextStyleId(pub TypeId);
92
93impl TextStyleId {
94 pub fn set(&self, cx: &mut Cx, value: TextStyle) {
95 cx.get_mut_style_top().text_styles.insert(*self, value);
96 }
97
98 pub fn get(&self, cx: &Cx) -> TextStyle {
99 for style_id in &cx.style_stack{
100 if let Some(value) = cx.styles[*style_id].text_styles.get(self){
101 return *value
102 }
103 }
104 *cx.style_base.text_styles.get(&*self).expect("Cannot find TextStyleId")
105 }
106}
107
108impl Into<TextStyleId> for UniqueId {
109 fn into(self) -> TextStyleId {TextStyleId(self.0)}
110}
111
112
113#[derive(PartialEq, Copy, Clone, Hash, Eq)]
116pub struct LayoutId(pub TypeId);
117
118impl LayoutId {
119 pub fn set(&self, cx: &mut Cx, value: Layout) {
120 cx.get_mut_style_top().layouts.insert(*self, value);
121 }
122
123 pub fn get(&self, cx: &Cx) -> Layout {
124 for style_id in &cx.style_stack{
125 if let Some(value) = cx.styles[*style_id].layouts.get(self){
126 return *value
127 }
128 }
129 *cx.style_base.layouts.get(&*self).expect("Cannot find LayoutId")
130 }
131}
132
133impl Into<LayoutId> for UniqueId {
134 fn into(self) -> LayoutId {LayoutId(self.0)}
135}
136
137
138#[derive(PartialEq, Copy, Clone, Hash, Eq)]
142pub struct WalkId(pub TypeId);
143
144impl WalkId {
145 pub fn set(&self, cx: &mut Cx, value: Walk) {
146 cx.get_mut_style_top().walks.insert(*self, value);
147 }
148
149 pub fn get(&self, cx: &Cx) -> Walk {
150 for style_id in &cx.style_stack{
151 if let Some(value) = cx.styles[*style_id].walks.get(self){
152 return *value
153 }
154 }
155 *cx.style_base.walks.get(&*self).expect("Cannot find WalkId")
156 }
157}
158
159
160impl Into<WalkId> for UniqueId {
161 fn into(self) -> WalkId {WalkId(self.0)}
162}
163
164
165
166#[derive(PartialEq, Copy, Clone, Hash, Eq)]
170pub struct AnimId(pub TypeId);
171
172impl AnimId {
173 pub fn set(&self, cx: &mut Cx, value: Anim) {
174 cx.get_mut_style_top().anims.insert(*self, value);
175 }
176
177 pub fn get(&self, cx: &Cx) -> Anim {
178 for style_id in &cx.style_stack{
179 if let Some(value) = cx.styles[*style_id].anims.get(self){
180 return value.clone()
181 }
182 }
183 cx.style_base.anims.get(&*self).expect("Cannot find AnimId").clone()
184 }
185}
186
187impl Into<AnimId> for UniqueId {
188 fn into(self) -> AnimId {AnimId(self.0)}
189}
190
191
192#[derive(PartialEq, Copy, Clone, Hash, Eq)]
195pub struct ShaderId(pub TypeId);
196
197impl ShaderId {
198 pub fn set(&self, cx: &mut Cx, sg: ShaderGen) {
199 let shader = cx.add_shader(sg, &format!("{:?}", self.0));
200 cx.get_mut_style_top().shaders.insert(*self, shader);
201 }
202
203 pub fn get(&self, cx: &Cx) -> Shader {
204 for style_id in &cx.style_stack{
205 if let Some(value) = cx.styles[*style_id].shaders.get(self){
206 return *value
207 }
208 }
209 *cx.style_base.shaders.get(&*self).expect("Cannot find AnimId")
210 }
211}
212
213
214impl Into<ShaderId> for UniqueId {
215 fn into(self) -> ShaderId {ShaderId(self.0)}
216}
217
218
219#[derive(PartialEq, Copy, Clone, Hash, Eq)]
223pub struct StyleId(pub TypeId);
224
225impl Into<StyleId> for UniqueId {
226 fn into(self) -> StyleId {StyleId(self.0)}
227}
228
229
230impl StyleId {
231 pub fn base() -> StyleId {uid!()}
232}