ferroui-base 0.3.0

The base library of FerroUI: the property system, layout, input, styling, media, text, animation, data binding and the compositor
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
//! Tests of setters whose value is a binding, ported from the upstream
//! `SetterTests` and `StyleTests` (the tests that need binding descriptions;
//! the others are in `styling/style_tests.rs`).
//!
//! The control library is a separate crate, so the tests use minimal styled
//! element classes in place of the controls used by the upstream tests.

use crate::utilities::CultureInfo;
use super::*;
use crate::data::converters::IValueConverter;
use crate::data::core::{Maybe, Value, ValueType};
use crate::data::model::{Event, INotifyPropertyChanged, Model};
use crate::data::{
    BindingError, BindingMode, BindingPriority, ReflectionBinding, RelativeSource, RelativeSourceMode,
    TemplateBinding,
};
use crate::media::{Brushes, IBrush};
use crate::styling::test_support::{record_values, remove_child, set_child, try_attach, Class1, TestRoot};
use crate::styling::{Selectors, Setter, Style};
use crate::{
    ferro_class, ferro_impl_classes, ferro_model, ferro_property, instantiate, ClassBindingManager, DirectProperty,
    FerroObjectImpl, FerroProperty, Ref, StyledElement, StyledElementImpl, StyledProperty,
};

/// An element with a tag.
#[repr(C)]
pub struct Decorator {
    base: StyledElement,
}

ferro_class!(Decorator: StyledElement);
ferro_impl_classes!(Decorator: FerroObjectImpl, StyledElementImpl);

impl Decorator {
    ferro_property!(pub fn tag_property() -> StyledProperty<Option<BoxedValue>> {
        FerroProperty::register::<Decorator, _>("Tag", None)
    });

    pub fn construct() -> Self {
        Self { base: StyledElement::construct() }
    }

    pub fn new() -> Ref<Self> {
        Self::tag_property();
        instantiate(Self::construct())
    }

    pub fn tag(&self) -> Option<BoxedValue> {
        self.get_value(Self::tag_property())
    }
}

/// A decorator with a background.
#[repr(C)]
pub struct Border {
    base: Decorator,
}

ferro_class!(Border: Decorator);
ferro_impl_classes!(Border: FerroObjectImpl, StyledElementImpl);

impl Border {
    ferro_property!(pub fn background_property() -> StyledProperty<Option<Rc<dyn IBrush>>> {
        FerroProperty::register::<Border, _>("Background", None)
    });

    pub fn new() -> Ref<Self> {
        Decorator::tag_property();
        Self::background_property();
        instantiate(Self { base: Decorator::construct() })
    }

    pub fn background(&self) -> Option<Rc<dyn IBrush>> {
        self.get_value(Self::background_property())
    }

    pub fn set_background(&self, value: Option<Rc<dyn IBrush>>) {
        self.set_value(Self::background_property(), value)
    }
}

#[repr(C)]
pub struct DirectPropertyClass {
    base: StyledElement,
    foo: RefCell<Option<String>>,
}

ferro_class!(DirectPropertyClass: StyledElement);
ferro_impl_classes!(DirectPropertyClass: FerroObjectImpl, StyledElementImpl);

impl DirectPropertyClass {
    ferro_property!(pub fn foo_property() -> DirectProperty<DirectPropertyClass, Option<String>> {
        FerroProperty::register_direct::<DirectPropertyClass, _>(
            "Foo",
            |o| o.foo(),
            Some(|o, v| o.set_foo(v)),
            None,
        )
    });

    pub fn new() -> Ref<Self> {
        Self::foo_property();
        instantiate(Self { base: StyledElement::construct(), foo: RefCell::new(None) })
    }

    pub fn foo(&self) -> Option<String> {
        self.foo.borrow().clone()
    }

    pub fn set_foo(&self, value: Option<String>) {
        self.set_and_raise(Self::foo_property(), &self.foo, value);
    }
}

/// A view model whose properties do not raise change notifications.
pub struct Data {
    foo: RefCell<Option<String>>,
    bar: RefCell<Option<Rc<dyn IBrush>>>,
    property_changed: Event<str>,
}

impl Data {
    fn new(foo: Option<&str>, bar: Option<Rc<dyn IBrush>>) -> Rc<Self> {
        Model::new_model(Self {
            foo: RefCell::new(foo.map(s)),
            bar: RefCell::new(bar),
            property_changed: Event::new(),
        })
    }

    fn foo(&self) -> Option<String> {
        self.foo.borrow().clone()
    }

    fn bar(&self) -> Option<Rc<dyn IBrush>> {
        self.bar.borrow().clone()
    }

    fn property_changed_subscription_count(&self) -> usize {
        self.property_changed.handler_count()
    }
}

impl INotifyPropertyChanged for Data {
    fn property_changed(&self) -> &Event<str> {
        &self.property_changed
    }
}

ferro_model!(Data, |b| b
    .notify_property_changed()
    .property::<Maybe<String>>("Foo", |o| o.foo(), |o, v| {
        o.foo.replace(v);
    })
    .property::<Maybe<Rc<dyn IBrush>>>("Bar", |o| o.bar(), |o, v| {
        o.bar.replace(v);
    }));

pub struct FooBar {
    foo: String,
    bar: String,
}

ferro_model!(FooBar, |b| b
    .read_only::<Value<String>>("Foo", |o| o.foo.clone())
    .read_only::<Value<String>>("Bar", |o| o.bar.clone()));

struct TestConverter;

impl IValueConverter for TestConverter {
    fn convert(
        &self,
        value: Option<&BoxedValue>,
        _target_type: ValueType,
        _parameter: Option<&BoxedValue>,
        _culture: &CultureInfo,
    ) -> Result<Option<BoxedValue>, BindingError> {
        let value = value.and_then(|v| v.downcast_ref::<String>().cloned()).unwrap_or_default();
        Ok(Some(boxed(value + "bar")))
    }

    fn convert_back(
        &self,
        _value: Option<&BoxedValue>,
        _target_type: ValueType,
        _parameter: Option<&BoxedValue>,
        _culture: &CultureInfo,
    ) -> Result<Option<BoxedValue>, BindingError> {
        panic!("The method or operation is not implemented.");
    }
}

fn red() -> Option<Rc<dyn IBrush>> {
    Some(Brushes::red())
}

fn green() -> Option<Rc<dyn IBrush>> {
    Some(Brushes::green())
}

fn blue() -> Option<Rc<dyn IBrush>> {
    Some(Brushes::blue())
}

fn binding_style<T: crate::ObjectType + crate::Upcast<StyledElement>>(
    class: Option<&str>,
    property: &'static FerroProperty,
    binding: Rc<ReflectionBinding>,
) -> Ref<Style> {
    let selector = Selectors::of_type::<T>();
    let selector = match class {
        Some(class) => selector.class(class),
        None => selector,
    };
    Style::with_setters(selector, [Setter::new_binding_base(property, binding)])
}

fn green_style() -> Ref<Style> {
    Style::with_setters(
        Selectors::of_type::<Border>().class("foo"),
        [Setter::new(Border::background_property(), green())],
    )
}

// --- upstream `SetterTests` ---------------------------------------------------

#[test]
fn does_not_call_converter_convert_back_on_one_way_binding() {
    let control = Decorator::new();
    control.set_name(Some(s("foo")));
    control.classes().add("foo");

    let binding = ReflectionBinding::new("Name").with_mode(BindingMode::OneWay);
    binding.set_converter(Some(Rc::new(TestConverter)));
    binding.set_relative_source(Some(RelativeSource::new(RelativeSourceMode::SelfMode)));

    let style = binding_style::<Decorator>(Some("foo"), Decorator::tag_property().as_property(), binding);

    try_attach(&style, &control, None);

    assert_eq!(control.tag().and_then(|v| v.downcast_ref::<String>().cloned()).as_deref(), Some("foobar"));

    // Issue #1218 caused the converter's `convert_back` to be called here.
    control.classes().remove("foo");
    assert!(control.tag().is_none());
}

#[test]
fn setter_should_apply_binding_without_activator_with_style_priority() {
    let control = Border::new();
    control.set_data_context(Some(boxed(s("foo"))));

    let style =
        binding_style::<Border>(None, Decorator::tag_property().as_property(), ReflectionBinding::empty());

    let raised = Counter::new();
    let r = raised.clone();
    control.property_changed(move |e| {
        assert_eq!(e.property(), Decorator::tag_property().as_property());
        assert_eq!(e.priority(), BindingPriority::Style);
        r.increment();
    });

    try_attach(&style, &control, None);

    assert_eq!(raised.get(), 1);
}

#[test]
fn setter_should_apply_binding_with_activator_with_style_trigger_priority() {
    let control = Border::new();
    control.classes().add("foo");
    control.set_data_context(Some(boxed(s("foo"))));

    let style =
        binding_style::<Border>(Some("foo"), Decorator::tag_property().as_property(), ReflectionBinding::empty());

    let raised = Counter::new();
    let r = raised.clone();
    control.property_changed(move |e| {
        assert_eq!(e.property(), Decorator::tag_property().as_property());
        assert_eq!(e.priority(), BindingPriority::StyleTrigger);
        r.increment();
    });

    try_attach(&style, &control, None);

    assert_eq!(raised.get(), 1);
}

#[test]
fn direct_property_setter_with_two_way_binding_should_update_source() {
    let data = Data::new(Some("foo"), None);
    let control = DirectPropertyClass::new();
    control.set_data_context(Some(data.clone()));

    let style = binding_style::<DirectPropertyClass>(
        None,
        DirectPropertyClass::foo_property().as_property(),
        ReflectionBinding::new("Foo").with_mode(BindingMode::TwoWay),
    );

    try_attach(&style, &control, None);
    assert_eq!(control.foo().as_deref(), Some("foo"));

    control.set_foo(Some(s("bar")));
    assert_eq!(data.foo().as_deref(), Some("bar"));
}

#[test]
fn styled_property_setter_with_two_way_binding_should_update_source() {
    let data = Data::new(None, red());
    let control = Border::new();
    control.set_data_context(Some(data.clone()));

    let style = binding_style::<Border>(
        None,
        Border::background_property().as_property(),
        ReflectionBinding::new("Bar").with_mode(BindingMode::TwoWay),
    );

    try_attach(&style, &control, None);
    assert_eq!(control.background(), red());

    control.set_background(green());
    assert_eq!(data.bar(), green());
}

#[test]
fn non_active_styled_property_binding_should_be_unsubscribed() {
    let data = Data::new(None, red());
    let control = Border::new();
    control.set_data_context(Some(data.clone()));

    let style1 =
        binding_style::<Border>(None, Border::background_property().as_property(), ReflectionBinding::new("Bar"));
    let style2 = green_style();

    try_attach(&style1, &control, None);
    try_attach(&style2, &control, None);

    // `style1` is initially active.
    assert_eq!(control.background(), red());
    assert_eq!(data.property_changed_subscription_count(), 1);

    // Activate `style2`.
    control.classes().add("foo");
    assert_eq!(control.background(), green());

    // The binding from `style1` is now inactive and so should be
    // unsubscribed.
    assert_eq!(data.property_changed_subscription_count(), 0);
}

#[test]
fn non_active_styled_property_setter_with_two_way_binding_should_not_update_source() {
    let data = Data::new(None, red());
    let control = Border::new();
    control.set_data_context(Some(data.clone()));

    let style1 = binding_style::<Border>(
        None,
        Border::background_property().as_property(),
        ReflectionBinding::new("Bar").with_mode(BindingMode::TwoWay),
    );
    let style2 = green_style();

    try_attach(&style1, &control, None);
    try_attach(&style2, &control, None);

    // `style1` is initially active.
    assert_eq!(control.background(), red());

    // Activate `style2`.
    control.classes().add("foo");
    assert_eq!(control.background(), green());

    // The two-way binding from `style1` is now inactive and so should not
    // write back to the data context.
    assert_eq!(data.bar(), red());
}

#[test]
fn styled_property_setter_with_two_way_binding_updates_source_when_made_active() {
    let data = Data::new(None, red());
    let control = Border::new();
    control.classes().add("foo");
    control.set_data_context(Some(data.clone()));

    let style1 = binding_style::<Border>(
        None,
        Border::background_property().as_property(),
        ReflectionBinding::new("Bar").with_mode(BindingMode::TwoWay),
    );
    let style2 = green_style();

    try_attach(&style1, &control, None);
    try_attach(&style2, &control, None);

    // `style2` is initially active.
    assert_eq!(control.background(), green());

    // Deactivate `style2`.
    control.classes().remove("foo");
    assert_eq!(control.background(), red());

    // The two-way binding from `style1` is now active and so should write
    // back to the data context.
    control.set_background(blue());
    assert_eq!(data.bar(), blue());
}

// --- upstream `StyleTests` ----------------------------------------------------

#[test]
fn style_with_class_selector_should_update_and_restore_value_with_template_binding() {
    let style = Style::with_setters(
        Selectors::of_type::<Class1>().class("foo"),
        [Setter::new(Class1::foo_property(), s("Foo"))],
    );

    let templated_parent = Class1::new();
    templated_parent.set_foo("unset-foo");
    let target = Class1::new();
    target.set_templated_parent(&templated_parent);
    // A template binding binds with template priority.
    target.bind_binding(Class1::foo_property(), &TemplateBinding::new(Class1::foo_property()));

    try_attach(&style, &target, None);
    assert_eq!(target.foo(), "unset-foo");
    target.classes().add("foo");
    assert_eq!(target.foo(), "Foo");
    target.classes().remove("foo");
    assert_eq!(target.foo(), "unset-foo");
}

fn root_with_two_binding_styles() -> Ref<TestRoot> {
    let root = TestRoot::new();
    root.styles().add(binding_style::<Class1>(
        None,
        Class1::foo_property().as_property(),
        ReflectionBinding::new("Foo"),
    ));
    root.styles().add(binding_style::<Class1>(
        None,
        Class1::foo_property().as_property(),
        ReflectionBinding::new("Bar"),
    ));
    root
}

#[test]
fn inactive_bindings_should_not_be_made_active_during_style_attach() {
    let root = root_with_two_binding_styles();

    let target = Class1::new();
    target.set_data_context(Some(Model::new_model(FooBar { foo: s("Foo"), bar: s("Bar") })));

    let values = record_values(&target, Class1::foo_property());
    set_child(&root, &target);

    assert_eq!(*values.borrow(), vec!["foodefault", "Bar"]);
}

#[test]
fn inactive_bindings_should_not_be_made_active_during_style_detach() {
    let root = root_with_two_binding_styles();

    let target = Class1::new();
    target.set_data_context(Some(Model::new_model(FooBar { foo: s("Foo"), bar: s("Bar") })));

    set_child(&root, &target);

    let values = record_values(&target, Class1::foo_property());
    remove_child(&root, &target);

    assert_eq!(*values.borrow(), vec!["Bar", "foodefault"]);
}

// --- class bindings in setters (specific to this port) ---------------------------

/// The upstream setter rejects a class binding property in a style with an
/// activator; there is no upstream test for it.
#[test]
fn cannot_set_class_binding_property_in_style_with_activator() {
    let control = Border::new();
    control.classes().add("foo");
    control.set_data_context(Some(boxed(true)));

    let style = binding_style::<Border>(
        Some("foo"),
        ClassBindingManager::get_class_property("bar"),
        ReflectionBinding::empty(),
    );

    assert_panics(|| {
        try_attach(&style, &control, None);
    });
}

#[test]
fn can_set_class_binding_property_in_style_without_activator() {
    let control = Border::new();
    control.set_data_context(Some(boxed(true)));

    let style =
        binding_style::<Border>(None, ClassBindingManager::get_class_property("bar"), ReflectionBinding::empty());

    try_attach(&style, &control, None);

    assert!(control.classes().contains("bar"));
}