zng-var 0.13.4

Part of the zng project.
Documentation
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Conditional var proxy.

use std::{
    sync::{
        Arc, Weak,
        atomic::{AtomicU32, AtomicUsize, Ordering},
    },
    time::Duration,
};

use crate::{
    AnyVar, VARS, Var,
    animation::{
        AnimationHandle, Transitionable,
        easing::{EasingStep, EasingTime},
    },
    contextual_var::{ContextInitFnImpl, any_contextual_var_impl},
    shared_var::MutexHooks,
};

use super::*;

///<span data-del-macro-root></span> Initializes a new conditional var.
///
/// A condition var updates when the first `true` condition changes or the mapped var for the current condition changes.
///
/// # Syntax
///
/// The macro expects a list of `condition-var => condition-value-var`, the list is separated by comma.
/// The last condition must be the `_` token that maps to the value for when none of the conditions are `true`.
///
/// The `condition-var` must be an expression that evaluates to a `Var<bool>` type. The `condition-value-var` must
/// by any type that implements `IntoVar`. All condition values must be of the same [`VarValue`] type.
///
/// # Examples
///
/// ```
/// # use zng_var::*;
/// # use zng_txt::ToTxt;
/// # macro_rules! Text { ($($tt:tt)*) => { () } }
/// let condition = var(true);
/// let when_false = var("condition: false".to_txt());
///
/// let t = Text!(when_var! {
///     condition.clone() => "condition: true".to_txt(),
///     _ => when_false.clone(),
/// });
/// ```
///
/// In the example if `condition` or `when_false` are modified the text updates.
///
/// # `cfg`
///
/// Every condition can be annotated with attributes, including `#[cfg(..)]`.
///
/// ```
/// # use zng_var::*;
/// # use zng_txt::*;
/// # macro_rules! Text { ($($tt:tt)*) => { () } }
/// # let condition0 = var(true);
/// # let condition1 = var(true);
/// let t = Text!(when_var! {
///     #[cfg(some_flag)]
///     condition0 => "is condition 0".to_txt(),
///     #[cfg(not(some_flag))]
///     condition1 => "is condition 1".to_txt(),
///     _ => "is default".to_txt(),
/// });
/// ```
///
/// In the example above only one of the conditions will be compiled, the generated variable is the same
/// type as if you had written a single condition.
///
/// # Capabilities
///
/// The when var is contextualized when needed, meaning if any input is [`CONTEXT`] at the moment the var is created it
/// is also contextual. The full output type of this macro is a `Var<T>`.
///
/// [`CONTEXT`]: crate::VarCapability::CONTEXT
#[macro_export]
macro_rules! when_var {
    ($($tt:tt)*) => {
        $crate::__when_var! {
            $crate
            $($tt)*
        }
    }
}

use zng_clone_move::clmv;
#[doc(hidden)]
pub use zng_var_proc_macros::when_var as __when_var;

/// Type erased [`when_var!`] manual builder.
///
/// See [`WhenVarBuilder`] for more details.
#[derive(Clone)]
pub struct AnyWhenVarBuilder {
    conditions: Vec<(Var<bool>, AnyVar)>,
    default: AnyVar,
}
impl AnyWhenVarBuilder {
    /// New with value variable used when no other conditions are `true`.
    pub fn new(default: AnyVar) -> Self {
        AnyWhenVarBuilder {
            conditions: Vec::with_capacity(2),
            default,
        }
    }

    /// Push a conditional value.
    ///
    /// When the `condition` is `true` and all previous pushed conditions
    /// are `false` the when variable represents the `value` variable.
    pub fn push(&mut self, condition: Var<bool>, value: AnyVar) -> &mut Self {
        self.conditions.push((condition, value));
        self
    }

    /// Replace the default value if `other` has default and extend the conditions with clones of `other`.
    pub fn replace_extend(&mut self, other: &Self) {
        self.default = other.default.clone();
        self.extend(other);
    }

    /// Extend the conditions with clones of `other`.
    pub fn extend(&mut self, other: &Self) {
        for (c, v) in other.conditions.iter() {
            self.conditions.push((c.clone(), v.clone()));
        }
    }

    /// Build the when var.
    ///
    /// The `value_type` is the when var output value type.
    pub fn build(self, value_type: TypeId) -> AnyVar {
        when_var(self, value_type)
    }

    /// Convert to typed builder.
    ///
    /// Note that the type is not checked.
    pub fn into_typed<O: VarValue>(self) -> WhenVarBuilder<O> {
        WhenVarBuilder {
            builder: self,
            _t: PhantomData,
        }
    }

    /// If the `var` was built by [`build`] clones the internal conditions, values and default variables into a new builder.
    ///
    /// [`build`]: Self::build
    pub fn try_from_built(var: &AnyVar) -> Option<Self> {
        match &var.0 {
            DynAnyVar::When(built) => Some(Self {
                conditions: built.0.conditions.to_vec(),
                default: built.0.default.clone(),
            }),
            DynAnyVar::Contextual(built) => {
                let init = built.0.init.lock();
                let init: &dyn Any = &**init;
                init.downcast_ref::<Self>().cloned()
            }
            _ => None,
        }
    }

    fn is_contextual(&self) -> bool {
        self.default.capabilities().is_contextual()
            || self
                .conditions
                .iter()
                .any(|(c, v)| c.capabilities().is_contextual() || v.capabilities().is_contextual())
    }

    fn current_context(&self) -> Self {
        AnyWhenVarBuilder {
            conditions: self
                .conditions
                .iter()
                .map(|(c, v)| (c.current_context(), v.current_context()))
                .collect(),
            default: self.default.current_context(),
        }
    }
}
impl AnyWhenVarBuilder {
    /// Returns the number of conditions set.
    pub fn condition_count(&self) -> usize {
        self.conditions.len()
    }
}

/// Manual [`when_var!`] builder.
#[derive(Clone)]
pub struct WhenVarBuilder<O: VarValue> {
    builder: AnyWhenVarBuilder,
    _t: PhantomData<O>,
}
impl<O: VarValue> WhenVarBuilder<O> {
    /// New with value variable used when no other conditions are `true`.
    pub fn new(default: impl IntoVar<O>) -> Self {
        Self {
            builder: AnyWhenVarBuilder::new(default.into_var().into()),
            _t: PhantomData,
        }
    }

    /// Push a conditional value.
    ///
    /// When the `condition` is `true` and all previous pushed conditions
    /// are `false` the when variable represents the `value` variable.
    pub fn push(&mut self, condition: impl IntoVar<bool>, value: impl IntoVar<O>) -> &mut Self {
        self.builder.conditions.push((condition.into_var(), value.into_var().into()));
        self
    }

    /// Build the when var.
    pub fn build(self) -> Var<O> {
        Var::new_any(self.builder.build(TypeId::of::<O>()))
    }

    /// Reference the type erased when builder.
    pub fn as_any(&mut self) -> &mut AnyWhenVarBuilder {
        &mut self.builder
    }

    /// If the `var` was built by [`build`] clones the internal conditions, values and default variables into a new builder.
    ///
    /// [`build`]: Self::build
    pub fn try_from_built(var: &Var<O>) -> Option<Self> {
        // this is used by #[easing(_)] in PropertyAttribute to modify widget properties

        let builder = AnyWhenVarBuilder::try_from_built(var)?;
        Some(Self { builder, _t: PhantomData })
    }
}

fn when_var(builder: AnyWhenVarBuilder, value_type: TypeId) -> AnyVar {
    if builder.is_contextual() {
        return any_contextual_var_impl(smallbox!(builder), value_type);
    }
    when_var_tail(builder)
}
impl ContextInitFnImpl for AnyWhenVarBuilder {
    fn init(&mut self) -> AnyVar {
        let builder = self.current_context();
        when_var_tail(builder)
    }
}
fn when_var_tail(mut builder: AnyWhenVarBuilder) -> AnyVar {
    builder.conditions.retain(|(c, _)| !c.capabilities().is_const() || c.get());
    if builder.conditions.is_empty() {
        return builder.default;
    }

    let all_equal = builder.default.capabilities().is_const()
        && builder.default.with(|default| {
            builder
                .conditions
                .iter()
                .all(|(_, v)| v.capabilities().is_const() && v.with(|v| v == default))
        });
    if all_equal {
        // this happens usually due to a multi input property where only one input
        // really changes over when blocks.
        return builder.default;
    }

    AnyVar(DynAnyVar::When(when_var_tail_impl(builder)))
}
fn when_var_tail_impl(builder: AnyWhenVarBuilder) -> WhenVar {
    let data = Arc::new(WhenVarData {
        active_condition: AtomicUsize::new(builder.conditions.iter().position(|(c, _)| c.get()).unwrap_or(usize::MAX)),
        conditions: builder.conditions.into_boxed_slice(),
        default: builder.default,
        hooks: MutexHooks::default(),
        last_active_change: AtomicU32::new(VarUpdateId::never().0),
    });

    for (i, (c, v)) in data.conditions.iter().enumerate() {
        let weak = Arc::downgrade(&data);
        c.hook(clmv!(weak, |args| {
            if let Some(data) = weak.upgrade() {
                let mut changed = false;
                let mut active = data.active_condition.load(Ordering::Relaxed);
                if active == i {
                    if !*args.value() {
                        // deactivated active
                        active = data.conditions.iter().position(|(c, _)| c.get()).unwrap_or(usize::MAX);
                        changed = true;
                    }
                } else if active > i && *args.value() {
                    // activated higher priority
                    changed = true;
                    active = i;
                }

                if changed {
                    data.active_condition.store(active, Ordering::Relaxed);
                    data.last_active_change.store(VARS.update_id().0, Ordering::Relaxed);

                    let active = if active < data.conditions.len() {
                        &data.conditions[active].1
                    } else {
                        &data.default
                    };

                    active.0.with(&mut |v| {
                        data.hooks.notify(&AnyVarHookArgs {
                            var_instance_tag: VarInstanceTag(Arc::as_ptr(&data) as _),
                            value: v,
                            update: args.update,
                            tags: args.tags,
                        });
                    });
                }

                true
            } else {
                false
            }
        }))
        .perm();

        v.hook(move |args| {
            if let Some(data) = weak.upgrade() {
                if data.active_condition.load(Ordering::Relaxed) == i {
                    data.hooks.notify(args);
                }
                true
            } else {
                false
            }
        })
        .perm();
    }
    let weak = Arc::downgrade(&data);
    data.default
        .hook(move |args| {
            if let Some(data) = weak.upgrade() {
                if data.active_condition.load(Ordering::Relaxed) >= data.conditions.len() {
                    data.hooks.notify(args);
                }
                true
            } else {
                false
            }
        })
        .perm();

    WhenVar(data)
}

struct WhenVarData {
    conditions: Box<[(Var<bool>, AnyVar)]>,
    default: AnyVar,
    active_condition: AtomicUsize,
    hooks: MutexHooks,
    // Atomic<VarUpdateId>
    last_active_change: AtomicU32,
}
pub(crate) struct WhenVar(Arc<WhenVarData>);
impl WhenVar {
    fn active(&self) -> &AnyVar {
        let i = self.0.active_condition.load(Ordering::Relaxed);
        if i < self.0.conditions.len() {
            &self.0.conditions[i].1
        } else {
            &self.0.default
        }
    }
}
impl fmt::Debug for WhenVar {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut b = f.debug_struct("MergeVar");
        b.field("var_instance_tag()", &self.var_instance_tag());
        b.field("inputs", &self.0.conditions);
        b.field("default", &self.0.default);
        let n = self.0.active_condition.load(Ordering::Relaxed);
        b.field("active_condition", if n < self.0.conditions.len() { &n } else { &None::<usize> });
        b.field(
            "last_active_change",
            &VarUpdateId(self.0.last_active_change.load(Ordering::Relaxed)),
        );
        b.field("hooks", &self.0.hooks);
        b.finish()
    }
}
impl VarImpl for WhenVar {
    fn clone_dyn(&self) -> DynAnyVar {
        DynAnyVar::When(Self(self.0.clone()))
    }

    fn value_type(&self) -> TypeId {
        self.0.default.0.value_type()
    }

    #[cfg(feature = "type_names")]
    fn value_type_name(&self) -> &'static str {
        self.0.default.0.value_type_name()
    }

    fn strong_count(&self) -> usize {
        Arc::strong_count(&self.0)
    }

    fn var_eq(&self, other: &DynAnyVar) -> bool {
        match other {
            DynAnyVar::When(o) => Arc::ptr_eq(&self.0, &o.0),
            _ => false,
        }
    }

    fn var_instance_tag(&self) -> VarInstanceTag {
        VarInstanceTag(Arc::as_ptr(&self.0) as _)
    }

    fn downgrade(&self) -> DynWeakAnyVar {
        DynWeakAnyVar::When(WeakWhenVar(Arc::downgrade(&self.0)))
    }

    fn capabilities(&self) -> VarCapability {
        fn cap_changes(caps: VarCapability) -> VarCapability {
            let mut out = VarCapability::NEW;
            if caps.contains(VarCapability::MODIFY) || caps.contains(VarCapability::MODIFY_CHANGES) {
                out |= VarCapability::MODIFY_CHANGES;
            }
            // this is never true, already contextualized
            // if caps.contains(VarCapability::CONTEXT) || caps.contains(VarCapability::CONTEXT_CHANGES) {
            //     out |= VarCapability::CONTEXT_CHANGES;
            // }
            out
        }
        self.active().0.capabilities()
            | cap_changes(self.0.default.capabilities())
            | self
                .0
                .conditions
                .iter()
                .map(|(_, v)| cap_changes(v.capabilities()))
                .fold(VarCapability::empty(), |a, b| a | b)
    }

    fn with(&self, visitor: &mut dyn FnMut(&dyn AnyVarValue)) {
        self.active().0.with(visitor)
    }

    fn get(&self) -> BoxAnyVarValue {
        self.active().0.get()
    }

    fn set(&self, new_value: BoxAnyVarValue) -> bool {
        self.active().0.set(new_value)
    }

    fn update(&self) -> bool {
        self.active().0.update()
    }

    fn modify(&self, modify: SmallBox<dyn FnMut(&mut AnyVarModify) + Send + 'static, smallbox::space::S4>) -> bool {
        self.active().0.modify(modify)
    }

    fn hook(&self, on_new: SmallBox<dyn FnMut(&AnyVarHookArgs) -> bool + Send + 'static, smallbox::space::S4>) -> VarHandle {
        self.0.hooks.push(on_new)
    }

    fn last_update(&self) -> VarUpdateId {
        // can be active update, or any of the conditions that updated and caused this update.
        VarUpdateId(self.0.last_active_change.load(Ordering::Relaxed)).max(self.active().0.last_update())
    }

    fn modify_info(&self) -> ModifyInfo {
        self.active().0.modify_info()
    }

    fn modify_importance(&self) -> usize {
        self.active().0.modify_importance()
    }

    // the conditions could be animating too, but this was not handled in the previous impl either

    fn is_animating(&self) -> bool {
        self.active().0.is_animating()
    }

    fn hook_animation_stop(&self, handler: AnimationStopFn) -> VarHandle {
        self.active().0.hook_animation_stop(handler)
    }

    fn current_context(&self) -> DynAnyVar {
        self.clone_dyn()
    }
}

pub(crate) struct WeakWhenVar(Weak<WhenVarData>);
impl fmt::Debug for WeakWhenVar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("WeakWhenVar").field(&self.0.as_ptr()).finish()
    }
}
impl WeakVarImpl for WeakWhenVar {
    fn clone_dyn(&self) -> DynWeakAnyVar {
        DynWeakAnyVar::When(Self(self.0.clone()))
    }

    fn strong_count(&self) -> usize {
        self.0.strong_count()
    }

    fn upgrade(&self) -> Option<DynAnyVar> {
        Some(DynAnyVar::When(WhenVar(self.0.upgrade()?)))
    }

    fn var_eq(&self, other: &DynWeakAnyVar) -> bool {
        match other {
            DynWeakAnyVar::When(o) => self.0.ptr_eq(&o.0),
            _ => false,
        }
    }
}

fn when_var_easing<O: VarValue + Transitionable>(builder: AnimatingWhenVarBuilder<O>) -> Var<O> {
    if builder.builder.is_contextual() {
        let any = any_contextual_var_impl(smallbox!(builder), TypeId::of::<O>());
        return Var::new_any(any);
    }
    when_var_easing_tail(builder)
}
struct AnimatingWhenVarBuilder<O: VarValue + Transitionable> {
    builder: AnyWhenVarBuilder,
    condition_easing: Vec<Option<EasingData>>,
    default_easing: EasingData,
    _t: PhantomData<fn() -> O>,
}
impl<O: VarValue + Transitionable> ContextInitFnImpl for AnimatingWhenVarBuilder<O> {
    fn init(&mut self) -> AnyVar {
        let builder = AnimatingWhenVarBuilder {
            builder: self.builder.current_context(),
            condition_easing: self.condition_easing.clone(),
            default_easing: self.default_easing.clone(),
            _t: self._t,
        };
        when_var_easing_tail(builder).any
    }
}
fn when_var_easing_tail<O: VarValue + Transitionable>(builder: AnimatingWhenVarBuilder<O>) -> Var<O> {
    let source = when_var_tail_impl(builder.builder);
    let weak_source = Arc::downgrade(&source.0);
    let output = var(source.get().downcast::<O>().unwrap());
    let weak_output = output.downgrade();
    let condition_easing = builder.condition_easing.into_boxed_slice();
    let default_easing = builder.default_easing;
    let mut _animation_handle = AnimationHandle::dummy();
    source
        .hook(smallbox!(move |args: &AnyVarHookArgs| {
            if let Some(output) = weak_output.upgrade() {
                let source = weak_source.upgrade().unwrap();
                for ((c, _), easing) in source.conditions.iter().zip(&condition_easing) {
                    if let Some((duration, func)) = easing
                        && c.get()
                    {
                        _animation_handle = output.ease(args.downcast_value::<O>().unwrap().clone(), *duration, clmv!(func, |t| func(t)));
                        return true;
                    }
                }
                // else default
                let (duration, func) = &default_easing;
                _animation_handle = output.ease(args.downcast_value::<O>().unwrap().clone(), *duration, clmv!(func, |t| func(t)));
                true
            } else {
                false
            }
        }))
        .perm();
    output.hold(source).perm();
    output
}

type EasingData = (Duration, Arc<dyn Fn(EasingTime) -> EasingStep + Send + Sync>);

impl<O: VarValue + Transitionable> WhenVarBuilder<O> {
    /// Build a variable similar to [`Var::easing`], but with different duration and easing functions for each condition.
    ///
    /// The `condition_easing` must contain one entry for each when condition, entries can be `None`, the easing used
    /// is the first entry that corresponds to a `true` condition, or falls back to the `default_easing`.
    pub fn build_easing(self, condition_easing: Vec<Option<EasingData>>, default_easing: EasingData) -> Var<O> {
        when_var_easing(AnimatingWhenVarBuilder {
            builder: self.builder,
            condition_easing,
            default_easing,
            _t: PhantomData,
        })
    }
}