1use crate::prelude::*;
2
3#[macro_export]
4macro_rules! impl_res_simple {
6 ($t:ty) => {
7 impl ResGet<$t> for $t {
8 fn get_ref<'a>(&self, _: &impl DataContext) -> Option<LensValue<$t>> {
9 Some(LensValue::Borrowed(self))
10 }
11
12 fn get(&self, _: &impl DataContext) -> $t {
13 *self
14 }
15 }
16
17 impl Res<$t> for $t {}
18 };
19}
20
21#[macro_export]
22macro_rules! impl_res_clone {
24 ($t:ty) => {
25 impl ResGet<$t> for $t {
26 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, $t>> {
27 Some(LensValue::Borrowed(self))
28 }
29
30 fn get(&self, _: &impl DataContext) -> $t {
31 self.clone()
32 }
33 }
34
35 impl Res<$t> for $t {}
36 };
37}
38
39pub trait ResGet<T> {
41 fn get_ref<'a>(&'a self, cx: &'a impl DataContext) -> Option<LensValue<'a, T>>;
43 fn get(&self, _: &impl DataContext) -> T;
45}
46
47pub trait Res<T>: ResGet<T> {
53 fn set_or_bind<F>(self, cx: &mut Context, entity: Entity, closure: F)
54 where
55 Self: Sized,
56 F: 'static + Fn(&mut Context, Self),
57 {
58 cx.with_current(entity, |cx| {
59 (closure)(cx, self);
60 });
61 }
62}
63
64impl<L> ResGet<L::Target> for L
65where
66 L: Lens<Target: Clone>,
67{
68 fn get_ref<'a>(&'a self, cx: &'a impl DataContext) -> Option<LensValue<'a, L::Target>> {
69 self.view(
70 cx.data()
71 .unwrap_or_else(|| panic!("Failed to get data from context for lens: {:?}", self)),
72 )
73 }
74
75 fn get(&self, cx: &impl DataContext) -> L::Target {
76 self.get_ref(cx).unwrap().into_owned()
77 }
78}
79
80impl<L> Res<L::Target> for L
81where
82 L: Lens<Target: Data>,
83{
84 fn set_or_bind<F>(self, cx: &mut Context, entity: Entity, closure: F)
85 where
86 F: 'static + Fn(&mut Context, Self),
87 {
88 Binding::new(cx, self, move |cx, val| {
90 cx.with_current(entity, |cx| {
91 (closure)(cx, val);
92 });
93 });
94 }
97}
98
99impl_res_simple!(i8);
100impl_res_simple!(i16);
101impl_res_simple!(i32);
102impl_res_simple!(i64);
103impl_res_simple!(i128);
104impl_res_simple!(isize);
105impl_res_simple!(u8);
106impl_res_simple!(u16);
107impl_res_simple!(u32);
108impl_res_simple!(u64);
109impl_res_simple!(u128);
110impl_res_simple!(usize);
111impl_res_simple!(char);
112impl_res_simple!(bool);
113impl_res_simple!(f32);
114impl_res_simple!(f64);
115impl_res_simple!(CursorIcon);
116impl_res_simple!(Overflow);
117impl_res_simple!(LengthValue);
118impl_res_simple!(FontWeight);
119impl_res_simple!(FontWeightKeyword);
120impl_res_simple!(FontSlant);
121impl_res_simple!(CornerShape);
122impl_res_simple!(Angle);
123impl_res_simple!(TextAlign);
124impl_res_simple!(TextOverflow);
125impl_res_simple!(LineClamp);
126impl_res_clone!(Shadow);
127impl_res_clone!(LinearGradientBuilder);
128impl_res_clone!(ShadowBuilder);
129impl_res_simple!(FontVariation);
130impl_res_clone!(Filter);
131impl_res_simple!(Opacity);
132impl_res_simple!(FontWidth);
133impl_res_clone!(Translate);
134impl_res_clone!(Scale);
135impl_res_clone!(Position);
136impl_res_simple!(PointerEvents);
137impl_res_simple!(ButtonVariant);
138impl_res_simple!(AvatarVariant);
139impl_res_clone!(FamilyOwned);
140impl_res_simple!(TextDecorationLine);
141impl_res_clone!(TextStroke);
142impl_res_clone!(TextStrokeStyle);
143impl_res_simple!(Alignment);
144impl_res_simple!(WindowPosition);
145impl_res_simple!(Anchor);
146impl_res_simple!(AnchorTarget);
147
148impl<'i> ResGet<FontFamily<'i>> for FontFamily<'i> {
149 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
150 Some(LensValue::Borrowed(self))
151 }
152
153 fn get(&self, _: &impl DataContext) -> Self {
154 self.clone()
155 }
156}
157
158impl<'i> Res<FontFamily<'i>> for FontFamily<'i> {}
159
160impl<'i> ResGet<BackgroundImage<'i>> for BackgroundImage<'i> {
161 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
162 Some(LensValue::Borrowed(self))
163 }
164
165 fn get(&self, _: &impl DataContext) -> Self {
166 self.clone()
167 }
168}
169
170impl<'i> Res<BackgroundImage<'i>> for BackgroundImage<'i> {}
171
172impl<'s> ResGet<&'s str> for &'s str {
173 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
174 Some(LensValue::Borrowed(self))
175 }
176
177 fn get(&self, _: &impl DataContext) -> &'s str {
178 self
179 }
180}
181
182impl<'s> Res<&'s str> for &'s str {}
183
184impl<'s> ResGet<&'s String> for &'s String {
185 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
186 Some(LensValue::Borrowed(self))
187 }
188
189 fn get(&self, _: &impl DataContext) -> Self {
190 self
191 }
192}
193
194impl<'s> Res<&'s String> for &'s String {}
195
196impl ResGet<String> for String {
197 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
198 Some(LensValue::Borrowed(self))
199 }
200
201 fn get(&self, _: &impl DataContext) -> Self {
202 self.clone()
203 }
204}
205
206impl Res<String> for String {}
207
208impl ResGet<Transform> for Transform {
209 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
210 Some(LensValue::Borrowed(self))
211 }
212
213 fn get(&self, _: &impl DataContext) -> Transform {
214 self.clone()
215 }
216}
217
218impl Res<Transform> for Transform {}
219
220impl ResGet<Color> for Color {
221 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
222 Some(LensValue::Borrowed(self))
223 }
224
225 fn get(&self, _: &impl DataContext) -> Color {
226 *self
227 }
228}
229
230impl Res<Color> for Color {}
231
232impl ResGet<LinearGradient> for LinearGradient {
233 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
234 Some(LensValue::Borrowed(self))
235 }
236
237 fn get(&self, _: &impl DataContext) -> LinearGradient {
238 self.clone()
239 }
240}
241
242impl Res<LinearGradient> for LinearGradient {}
243
244impl ResGet<Units> for Units {
245 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
246 Some(LensValue::Borrowed(self))
247 }
248
249 fn get(&self, _: &impl DataContext) -> Units {
250 *self
251 }
252}
253
254impl Res<Units> for Units {}
255
256impl ResGet<Visibility> for Visibility {
257 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
258 Some(LensValue::Borrowed(self))
259 }
260
261 fn get(&self, _: &impl DataContext) -> Visibility {
262 *self
263 }
264}
265
266impl Res<Visibility> for Visibility {}
267
268impl ResGet<Display> for Display {
269 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
270 Some(LensValue::Borrowed(self))
271 }
272
273 fn get(&self, _: &impl DataContext) -> Display {
274 *self
275 }
276}
277
278impl Res<Display> for Display {}
279
280impl ResGet<LayoutType> for LayoutType {
281 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
282 Some(LensValue::Borrowed(self))
283 }
284
285 fn get(&self, _: &impl DataContext) -> LayoutType {
286 *self
287 }
288}
289impl Res<LayoutType> for LayoutType {}
290
291impl ResGet<PositionType> for PositionType {
292 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
293 Some(LensValue::Borrowed(self))
294 }
295
296 fn get(&self, _: &impl DataContext) -> PositionType {
297 *self
298 }
299}
300
301impl Res<PositionType> for PositionType {}
302
303impl<T: Clone + ResGet<T>> ResGet<Option<T>> for Option<T> {
304 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
305 Some(LensValue::Borrowed(self))
306 }
307
308 fn get(&self, _: &impl DataContext) -> Option<T> {
309 self.clone()
310 }
311}
312
313impl<T: Clone + ResGet<T>> Res<Option<T>> for Option<T> {}
314
315impl ResGet<Length> for Length {
316 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
317 Some(LensValue::Borrowed(self))
318 }
319
320 fn get(&self, _: &impl DataContext) -> Self {
321 self.clone()
322 }
323}
324
325impl Res<Length> for Length {}
326
327impl ResGet<LengthOrPercentage> for LengthOrPercentage {
328 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
329 Some(LensValue::Borrowed(self))
330 }
331
332 fn get(&self, _: &impl DataContext) -> Self {
333 self.clone()
334 }
335}
336
337impl Res<LengthOrPercentage> for LengthOrPercentage {}
338
339impl ResGet<RGBA> for RGBA {
340 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
341 Some(LensValue::Borrowed(self))
342 }
343
344 fn get(&self, _: &impl DataContext) -> Self {
345 *self
346 }
347}
348
349impl Res<RGBA> for RGBA {}
350
351impl<T: Clone + ResGet<T>> ResGet<Vec<T>> for Vec<T> {
352 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
353 Some(LensValue::Borrowed(self))
354 }
355
356 fn get(&self, _: &impl DataContext) -> Vec<T> {
357 self.clone()
358 }
359}
360
361impl<T: Clone + ResGet<T>> Res<Vec<T>> for Vec<T> {}
362
363impl<T: Clone + ResGet<T>, const N: usize> ResGet<[T; N]> for [T; N] {
364 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
365 Some(LensValue::Borrowed(self))
366 }
367
368 fn get(&self, _: &impl DataContext) -> Self {
369 self.clone()
370 }
371}
372
373impl<T: Clone + ResGet<T>, const N: usize> Res<[T; N]> for [T; N] {}
374
375impl<T1: Clone, T2: Clone> ResGet<(T1, T2)> for (T1, T2) {
376 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
377 Some(LensValue::Borrowed(self))
378 }
379
380 fn get(&self, _cx: &impl DataContext) -> (T1, T2) {
381 self.clone()
382 }
383}
384
385impl<T1: Clone, T2: Clone> Res<(T1, T2)> for (T1, T2) {}
386
387impl<T1: Clone, T2: Clone, T3: Clone> ResGet<(T1, T2, T3)> for (T1, T2, T3) {
388 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
389 Some(LensValue::Borrowed(self))
390 }
391
392 fn get(&self, _cx: &impl DataContext) -> (T1, T2, T3) {
393 self.clone()
394 }
395}
396
397impl<T1: Clone, T2: Clone, T3: Clone> Res<(T1, T2, T3)> for (T1, T2, T3) {}
398
399impl<T1: Clone, T2: Clone, T3: Clone, T4: Clone> ResGet<(T1, T2, T3, T4)> for (T1, T2, T3, T4) {
400 fn get_ref<'a>(&'a self, _: &'a impl DataContext) -> Option<LensValue<'a, Self>> {
401 Some(LensValue::Borrowed(self))
402 }
403
404 fn get(&self, _cx: &impl DataContext) -> (T1, T2, T3, T4) {
405 self.clone()
406 }
407}
408
409impl<T1: Clone, T2: Clone, T3: Clone, T4: Clone> Res<(T1, T2, T3, T4)> for (T1, T2, T3, T4) {}