1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#![doc(html_favicon_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo-icon.png")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo.png")]
//!
//! Color filter properties, [`opacity`](fn@opacity), [`filter`](fn@filter) and more.
//!
//! # Crate
//!
#![doc = include_str!(concat!("../", std::env!("CARGO_PKG_README")))]
#![warn(unused_extern_crates)]
#![warn(missing_docs)]

use zng_color::filter::{ColorMatrix, Filter};
use zng_wgt::prelude::*;

/// Color filter, or combination of filters.
///
/// This property allows setting multiple filters at once, there is also a property for every
/// filter for easier value updating.
///
/// # Performance
///
/// The performance for setting specific filter properties versus this one is the same, except for [`opacity`]
/// which can be animated using only frame updates instead of generating a new frame every change.
///
/// [`opacity`]: fn@opacity
#[property(CONTEXT, default(Filter::default()))]
pub fn filter(child: impl UiNode, filter: impl IntoVar<Filter>) -> impl UiNode {
    filter_any(child, filter, false)
}

/// Backdrop filter, or combination of filters.
///
/// This property allows setting multiple filters at once, there is also a property for every
/// filter for easier value updating.
///
/// The filters are applied to everything rendered behind the widget.
///
/// # Performance
///
/// The performance for setting specific filter properties versus this one is the same.
///
/// [`opacity`]: fn@opacity
#[property(CONTEXT, default(Filter::default()))]
pub fn backdrop_filter(child: impl UiNode, filter: impl IntoVar<Filter>) -> impl UiNode {
    backdrop_filter_any(child, filter)
}

/// Color filter, or combination of filters targeting the widget's descendants and not the widget itself.
///
/// This property allows setting multiple filters at once, there is also a property for every
/// filter for easier value updating.
///
/// # Performance
///
/// The performance for setting specific filter properties versus this one is the same, except for [`child_opacity`]
/// which can be animated using only frame updates instead of generating a new frame every change.
///
/// [`child_opacity`]: fn@child_opacity
#[property(CHILD_CONTEXT, default(Filter::default()))]
pub fn child_filter(child: impl UiNode, filter: impl IntoVar<Filter>) -> impl UiNode {
    filter_any(child, filter, true)
}

/// Inverts the colors of the widget.
///
/// Zero does not invert, one fully inverts.
///
/// This property is a shorthand way of setting [`filter`] to [`Filter::new_invert`] using variable mapping.
///
/// [`filter`]: fn@filter
/// [`Filter::new_invert`]: zng_color::filter::Filter::new_invert
#[property(CONTEXT, default(false))]
pub fn invert_color(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    filter_render(child, amount.into_var().map(|&a| Filter::new_invert(a)), false)
}

/// Inverts the colors of everything behind the widget.
///
/// Zero does not invert, one fully inverts.
///
/// This property is a shorthand way of setting [`backdrop_filter`] to [`Filter::new_invert`] using variable mapping.
///
/// [`backdrop_filter`]: fn@backdrop_filter
/// [`Filter::new_invert`]: zng_color::filter::Filter::new_invert
#[property(CONTEXT, default(false))]
pub fn backdrop_invert(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    backdrop_filter_render(child, amount.into_var().map(|&a| Filter::new_invert(a)))
}

/// Blur the widget.
///
/// This property is a shorthand way of setting [`filter`] to [`Filter::new_blur`] using variable mapping.
///
/// [`filter`]: fn@filter
/// [`Filter::new_blur`]: zng_color::filter::Filter::new_blur
#[property(CONTEXT, default(0))]
pub fn blur(child: impl UiNode, radius: impl IntoVar<Length>) -> impl UiNode {
    filter_layout(child, radius.into_var().map(|r| Filter::new_blur(r.clone())), false)
}

/// Blur the everything behind the widget.
///
/// This property is a shorthand way of setting [`backdrop_filter`] to [`Filter::new_blur`] using variable mapping.
///
/// [`backdrop_filter`]: fn@backdrop_filter
/// [`Filter::new_blur`]: zng_color::filter::Filter::new_blur
#[property(CONTEXT, default(0))]
pub fn backdrop_blur(child: impl UiNode, radius: impl IntoVar<Length>) -> impl UiNode {
    backdrop_filter_layout(child, radius.into_var().map(|r| Filter::new_blur(r.clone())))
}

/// Sepia tone the widget.
///
/// zero is the original colors, one is the full desaturated brown look.
///
/// This property is a shorthand way of setting [`filter`] to [`Filter::new_sepia`] using variable mapping.
///
/// [`filter`]: fn@filter
/// [`Filter::new_sepia`]: zng_color::filter::Filter::new_sepia
#[property(CONTEXT, default(false))]
pub fn sepia(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    filter_render(child, amount.into_var().map(|&a| Filter::new_sepia(a)), false)
}

/// Sepia tone everything behind the widget.
///
/// zero is the original colors, one is the full desaturated brown look.
///
/// This property is a shorthand way of setting [`backdrop_filter`] to [`Filter::new_sepia`] using variable mapping.
///
/// [`backdrop_filter`]: fn@backdrop_filter
/// [`Filter::new_sepia`]: zng_color::filter::Filter::new_sepia
#[property(CONTEXT, default(false))]
pub fn backdrop_sepia(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    backdrop_filter_render(child, amount.into_var().map(|&a| Filter::new_sepia(a)))
}

/// Grayscale tone the widget.
///
/// Zero is the original colors, one if the full grayscale.
///
/// This property is a shorthand way of setting [`filter`] to [`Filter::new_grayscale`] using variable mapping.
///
/// [`filter`]: fn@filter
/// [`Filter::new_grayscale`]: zng_color::filter::Filter::new_grayscale
#[property(CONTEXT, default(false))]
pub fn grayscale(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    filter_render(child, amount.into_var().map(|&a| Filter::new_grayscale(a)), false)
}

/// Grayscale tone everything behind the widget.
///
/// Zero is the original colors, one if the full grayscale.
///
/// This property is a shorthand way of setting [`backdrop_filter`] to [`Filter::new_grayscale`] using variable mapping.
///
/// [`backdrop_filter`]: fn@backdrop_filter
/// [`Filter::new_grayscale`]: zng_color::filter::Filter::new_grayscale
#[property(CONTEXT, default(false))]
pub fn backdrop_grayscale(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    backdrop_filter_render(child, amount.into_var().map(|&a| Filter::new_grayscale(a)))
}

/// Drop-shadow effect for the widget.
///
/// The shadow is *pixel accurate*.
///
/// This property is a shorthand way of setting [`filter`] to [`Filter::new_drop_shadow`] using variable merging.
///
/// [`filter`]: fn@filter
/// [`Filter::new_drop_shadow`]: zng_color::filter::Filter::new_drop_shadow
#[property(CONTEXT, default((0, 0), 0, colors::BLACK.transparent()))]
pub fn drop_shadow(
    child: impl UiNode,
    offset: impl IntoVar<Point>,
    blur_radius: impl IntoVar<Length>,
    color: impl IntoVar<Rgba>,
) -> impl UiNode {
    filter_layout(
        child,
        merge_var!(offset.into_var(), blur_radius.into_var(), color.into_var(), |o, r, &c| {
            Filter::new_drop_shadow(o.clone(), r.clone(), c)
        }),
        false,
    )
}

/// Adjust the widget colors brightness.
///
/// Zero removes all brightness, one is the original brightness.
///
/// This property is a shorthand way of setting [`filter`] to [`Filter::new_brightness`] using variable mapping.
///
/// [`filter`]: fn@filter
/// [`Filter::new_brightness`]: zng_color::filter::Filter::new_brightness
#[property(CONTEXT, default(1.0))]
pub fn brightness(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    filter_render(child, amount.into_var().map(|&a| Filter::new_brightness(a)), false)
}

/// Adjust color brightness of everything behind the widget.
///
/// Zero removes all brightness, one is the original brightness.
///
/// This property is a shorthand way of setting [`backdrop_filter`] to [`Filter::new_brightness`] using variable mapping.
///
/// [`backdrop_filter`]: fn@backdrop_filter
/// [`Filter::new_brightness`]: zng_color::filter::Filter::new_brightness
#[property(CONTEXT, default(1.0))]
pub fn backdrop_brightness(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    backdrop_filter_render(child, amount.into_var().map(|&a| Filter::new_brightness(a)))
}

/// Adjust the widget colors contrast.
///
/// Zero removes all contrast, one is the original contrast.
///
/// This property is a shorthand way of setting [`filter`] to [`Filter::new_contrast`] using variable mapping.
///
/// [`filter`]: fn@filter
/// [`Filter::new_contrast`]: zng_color::filter::Filter::new_contrast
#[property(CONTEXT, default(1.0))]
pub fn contrast(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    filter_render(child, amount.into_var().map(|&a| Filter::new_contrast(a)), false)
}

/// Adjust the color contrast of everything behind the widget.
///
/// Zero removes all contrast, one is the original contrast.
///
/// This property is a shorthand way of setting [`backdrop_filter`] to [`Filter::new_contrast`] using variable mapping.
///
/// [`backdrop_filter`]: fn@backdrop_filter
/// [`Filter::new_contrast`]: zng_color::filter::Filter::new_contrast
#[property(CONTEXT, default(1.0))]
pub fn backdrop_contrast(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    backdrop_filter_render(child, amount.into_var().map(|&a| Filter::new_contrast(a)))
}

/// Adjust the widget colors saturation.
///
/// Zero fully desaturates, one is the original saturation.
///
/// This property is a shorthand way of setting [`filter`] to [`Filter::new_saturate`] using variable mapping.
///
/// [`filter`]: fn@filter
/// [`Filter::new_saturate`]: zng_color::filter::Filter::new_saturate
#[property(CONTEXT, default(1.0))]
pub fn saturate(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    filter_render(child, amount.into_var().map(|&a| Filter::new_saturate(a)), false)
}

/// Adjust color saturation of everything behind the widget.
///
/// Zero fully desaturates, one is the original saturation.
///
/// This property is a shorthand way of setting [`backdrop_filter`] to [`Filter::new_saturate`] using variable mapping.
///
/// [`backdrop_filter`]: fn@backdrop_filter
/// [`Filter::new_saturate`]: zng_color::filter::Filter::new_saturate
#[property(CONTEXT, default(1.0))]
pub fn backdrop_saturate(child: impl UiNode, amount: impl IntoVar<Factor>) -> impl UiNode {
    backdrop_filter_render(child, amount.into_var().map(|&a| Filter::new_saturate(a)))
}

/// Hue shift the widget colors.
///
/// Adds `angle` to the [`hue`] of the widget colors.
///
/// This property is a shorthand way of setting [`filter`] to [`Filter::new_hue_rotate`] using variable mapping.
///
/// [`filter`]: fn@filter
/// [`hue`]: Hsla::hue
/// [`Filter::new_hue_rotate`]: zng_color::filter::Filter::new_hue_rotate
#[property(CONTEXT, default(0.deg()))]
pub fn hue_rotate(child: impl UiNode, angle: impl IntoVar<AngleDegree>) -> impl UiNode {
    filter_render(child, angle.into_var().map(|&a| Filter::new_hue_rotate(a)), false)
}

/// Hue shift the colors behind the widget.
///
/// Adds `angle` to the [`hue`] of the widget colors.
///
/// This property is a shorthand way of setting [`backdrop_filter`] to [`Filter::new_hue_rotate`] using variable mapping.
///
/// [`backdrop_filter`]: fn@backdrop_filter
/// [`hue`]: Hsla::hue
/// [`Filter::new_hue_rotate`]: zng_color::filter::Filter::new_hue_rotate
#[property(CONTEXT, default(0.deg()))]
pub fn backdrop_hue_rotate(child: impl UiNode, angle: impl IntoVar<AngleDegree>) -> impl UiNode {
    backdrop_filter_render(child, angle.into_var().map(|&a| Filter::new_hue_rotate(a)))
}

/// Custom color filter.
///
/// The color matrix is in the format of SVG color matrix, [0..5] is the first matrix row.
#[property(CONTEXT, default(ColorMatrix::identity()))]
pub fn color_matrix(child: impl UiNode, matrix: impl IntoVar<ColorMatrix>) -> impl UiNode {
    filter_render(child, matrix.into_var().map(|&m| Filter::new_color_matrix(m)), false)
}

/// Custom backdrop filter.
///
/// The color matrix is in the format of SVG color matrix, [0..5] is the first matrix row.
#[property(CONTEXT, default(ColorMatrix::identity()))]
pub fn backdrop_color_matrix(child: impl UiNode, matrix: impl IntoVar<ColorMatrix>) -> impl UiNode {
    backdrop_filter_render(child, matrix.into_var().map(|&m| Filter::new_color_matrix(m)))
}

/// Opacity/transparency of the widget.
///
/// This property provides the same visual result as setting [`filter`] to [`Filter::new_opacity`],
/// **but** updating the opacity is faster in this property.
///
/// [`filter`]: fn@filter
/// [`Filter::new_opacity`]: zng_color::filter::Filter::new_opacity
#[property(CONTEXT, default(1.0))]
pub fn opacity(child: impl UiNode, alpha: impl IntoVar<Factor>) -> impl UiNode {
    opacity_impl(child, alpha, false)
}

/// Opacity/transparency of the widget's child.
///
/// This property provides the same visual result as setting [`child_filter`] to [`Filter::new_opacity`],
/// **but** updating the opacity is faster in this property.
///
/// [`child_filter`]: fn@child_filter
/// [`Filter::new_opacity`]: zng_color::filter::Filter::new_opacity
#[property(CHILD_CONTEXT, default(1.0))]
pub fn child_opacity(child: impl UiNode, alpha: impl IntoVar<Factor>) -> impl UiNode {
    opacity_impl(child, alpha, true)
}

/// impl any filter, may need layout or not.
fn filter_any(child: impl UiNode, filter: impl IntoVar<Filter>, target_child: bool) -> impl UiNode {
    let filter = filter.into_var();
    let mut render_filter = None;
    match_node(child, move |child, op| match op {
        UiNodeOp::Init => {
            WIDGET.sub_var(&filter);
            render_filter = filter.with(Filter::try_render);
        }
        UiNodeOp::Update { .. } => {
            filter.with_new(|f| {
                if let Some(f) = f.try_render() {
                    render_filter = Some(f);
                    WIDGET.render();
                } else {
                    render_filter = None;
                    WIDGET.layout();
                }
            });
        }
        UiNodeOp::Layout { .. } => {
            filter.with(|f| {
                if f.needs_layout() {
                    let f = Some(f.layout());
                    if render_filter != f {
                        render_filter = f;
                        WIDGET.render();
                    }
                }
            });
        }
        UiNodeOp::Render { frame } => {
            if target_child {
                frame.push_filter(MixBlendMode::Normal.into(), render_filter.as_ref().unwrap(), |frame| {
                    child.render(frame)
                });
            } else {
                frame.push_inner_filter(render_filter.clone().unwrap(), |frame| child.render(frame));
            }
        }
        _ => {}
    })
}

/// impl any backdrop filter, may need layout or not.
fn backdrop_filter_any(child: impl UiNode, filter: impl IntoVar<Filter>) -> impl UiNode {
    let filter = filter.into_var();
    let mut render_filter = None;
    match_node(child, move |child, op| match op {
        UiNodeOp::Init => {
            WIDGET.sub_var(&filter);
            render_filter = filter.with(Filter::try_render);
        }
        UiNodeOp::Update { .. } => {
            filter.with_new(|f| {
                if let Some(f) = f.try_render() {
                    render_filter = Some(f);
                    WIDGET.render();
                } else {
                    render_filter = None;
                    WIDGET.layout();
                }
            });
        }
        UiNodeOp::Layout { .. } => {
            filter.with(|f| {
                if f.needs_layout() {
                    let f = Some(f.layout());
                    if render_filter != f {
                        render_filter = f;
                        WIDGET.render();
                    }
                }
            });
        }
        UiNodeOp::Render { frame } => {
            frame.push_inner_backdrop_filter(render_filter.clone().unwrap(), |frame| child.render(frame));
        }
        _ => {}
    })
}

/// impl filters that need layout.
fn filter_layout(child: impl UiNode, filter: impl IntoVar<Filter>, target_child: bool) -> impl UiNode {
    let filter = filter.into_var();

    let mut render_filter = None;
    match_node(child, move |child, op| match op {
        UiNodeOp::Init => {
            WIDGET.sub_var_layout(&filter);
        }
        UiNodeOp::Layout { .. } => {
            filter.with(|f| {
                if f.needs_layout() {
                    let f = Some(f.layout());
                    if render_filter != f {
                        render_filter = f;
                        WIDGET.render();
                    }
                }
            });
        }
        UiNodeOp::Render { frame } => {
            if target_child {
                frame.push_filter(MixBlendMode::Normal.into(), render_filter.as_ref().unwrap(), |frame| {
                    child.render(frame)
                });
            } else {
                frame.push_inner_filter(render_filter.clone().unwrap(), |frame| child.render(frame));
            }
        }
        _ => {}
    })
}

/// impl backdrop filters that need layout.
fn backdrop_filter_layout(child: impl UiNode, filter: impl IntoVar<Filter>) -> impl UiNode {
    let filter = filter.into_var();

    let mut render_filter = None;
    match_node(child, move |child, op| match op {
        UiNodeOp::Init => {
            WIDGET.sub_var_layout(&filter);
        }
        UiNodeOp::Layout { .. } => {
            filter.with(|f| {
                if f.needs_layout() {
                    let f = Some(f.layout());
                    if render_filter != f {
                        render_filter = f;
                        WIDGET.render();
                    }
                }
            });
        }
        UiNodeOp::Render { frame } => {
            frame.push_inner_backdrop_filter(render_filter.clone().unwrap(), |frame| child.render(frame));
        }
        _ => {}
    })
}

/// impl filters that only need render.
fn filter_render(child: impl UiNode, filter: impl IntoVar<Filter>, target_child: bool) -> impl UiNode {
    let filter = filter.into_var().map(|f| f.try_render().unwrap());
    match_node(child, move |child, op| match op {
        UiNodeOp::Init => {
            WIDGET.sub_var_render(&filter);
        }
        UiNodeOp::Render { frame } => {
            if target_child {
                filter.with(|f| {
                    frame.push_filter(MixBlendMode::Normal.into(), f, |frame| child.render(frame));
                });
            } else {
                frame.push_inner_filter(filter.get(), |frame| child.render(frame));
            }
        }
        _ => {}
    })
}

/// impl backdrop filter that only need render.
fn backdrop_filter_render(child: impl UiNode, filter: impl IntoVar<Filter>) -> impl UiNode {
    let filter = filter.into_var().map(|f| f.try_render().unwrap());
    match_node(child, move |child, op| match op {
        UiNodeOp::Init => {
            WIDGET.sub_var_render(&filter);
        }
        UiNodeOp::Render { frame } => {
            frame.push_inner_backdrop_filter(filter.get(), |frame| child.render(frame));
        }
        _ => {}
    })
}

fn opacity_impl(child: impl UiNode, alpha: impl IntoVar<Factor>, target_child: bool) -> impl UiNode {
    let frame_key = FrameValueKey::new_unique();
    let alpha = alpha.into_var();

    match_node(child, move |child, op| match op {
        UiNodeOp::Init => {
            WIDGET.sub_var_render_update(&alpha);
        }
        UiNodeOp::Render { frame } => {
            let opacity = frame_key.bind_var(&alpha, |f| f.0);
            if target_child {
                frame.push_opacity(opacity, |frame| child.render(frame));
            } else {
                frame.push_inner_opacity(opacity, |frame| child.render(frame));
            }
        }
        UiNodeOp::RenderUpdate { update } => {
            update.update_f32_opt(frame_key.update_var(&alpha, |f| f.0));
            child.render_update(update);
        }
        _ => {}
    })
}

/// Sets how the widget blends with the parent widget.
#[property(CONTEXT, default(MixBlendMode::default()))]
pub fn mix_blend(child: impl UiNode, mode: impl IntoVar<MixBlendMode>) -> impl UiNode {
    let mode = mode.into_var();
    match_node(child, move |c, op| match op {
        UiNodeOp::Init => {
            WIDGET.sub_var_render(&mode);
        }
        UiNodeOp::Render { frame } => {
            frame.push_inner_blend(mode.get().into(), |frame| c.render(frame));
        }
        _ => {}
    })
}

/// Sets how the widget's child content blends with the widget.
#[property(CHILD_CONTEXT, default(MixBlendMode::default()))]
pub fn child_mix_blend(child: impl UiNode, mode: impl IntoVar<MixBlendMode>) -> impl UiNode {
    let mode = mode.into_var();
    match_node(child, move |c, op| match op {
        UiNodeOp::Init => {
            WIDGET.sub_var_render(&mode);
        }
        UiNodeOp::Render { frame } => {
            frame.push_filter(mode.get().into(), &vec![], |frame| c.render(frame));
        }
        _ => {}
    })
}