ferroui-controls 0.2.0

The controls of FerroUI: the application model, top levels, the control set and automation peers
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
//! The reference test suite has no tests for the toggle switch; these are
//! tests of this port for the knob position, the knob drag and the content
//! parts. The template has the parts of the reference simple theme: a canvas
//! (the switch knob) around a grid (the moving knobs), and the two content
//! presenters.

use crate::presenters::ContentPresenter;
use crate::templates::{FuncControlTemplate, FuncTemplateNameScopeExtensions, IControlTemplate};
use crate::test_support::{boxed_str, string_of, test_scope, TestRoot};
use crate::{Canvas, ContentControl, Control, Grid, Panel, ToggleSwitch};
use ferroui_base::animation::{DoubleTransition, ITransition, Transitions};
use ferroui_base::data::TemplateBinding;
use ferroui_base::input::{
    InputElement, IPointer, KeyModifiers, MouseButton, Pointer, PointerEventArgs, PointerPointProperties,
    PointerPressedEventArgs, PointerReleasedEventArgs, PointerType,
};
use crate::mouse_test_helper::MouseTestHelper;
use ferroui_base::layout::HorizontalAlignment;
use ferroui_base::media::{Geometry, GeometryHitTestResult};
use ferroui_base::rendering::IHitTester;
use ferroui_base::threading::Dispatcher;
use ferroui_base::{Point, Ref, StyledElement, Visual};
use std::cell::Cell;
use std::rc::Rc;

fn create_template() -> Rc<dyn IControlTemplate> {
    FuncControlTemplate::new(|_, ns| {
        let root = Grid::new();

        let off_content_presenter = ContentPresenter::new();
        off_content_presenter.set_name(Some("PART_OffContentPresenter".to_string()));
        off_content_presenter.bind_binding(
            ContentPresenter::content_property().as_property(),
            &TemplateBinding::new(ToggleSwitch::off_content_property().as_property()),
        );
        root.children().add(off_content_presenter.register_in_name_scope(&**ns));

        let on_content_presenter = ContentPresenter::new();
        on_content_presenter.set_name(Some("PART_OnContentPresenter".to_string()));
        on_content_presenter.bind_binding(
            ContentPresenter::content_property().as_property(),
            &TemplateBinding::new(ToggleSwitch::on_content_property().as_property()),
        );
        root.children().add(on_content_presenter.register_in_name_scope(&**ns));

        let content_presenter = ContentPresenter::new();
        content_presenter.set_name(Some("PART_ContentPresenter".to_string()));
        content_presenter.bind_binding(
            ContentPresenter::content_property().as_property(),
            &TemplateBinding::new(ContentControl::content_property().as_property()),
        );
        root.children().add(content_presenter.register_in_name_scope(&**ns));

        let switch_knob = Canvas::new();
        switch_knob.set_name(Some("PART_SwitchKnob".to_string()));
        switch_knob.set_horizontal_alignment(HorizontalAlignment::Left);
        switch_knob.set_width(20.0);
        switch_knob.set_height(20.0);

        let moving_knobs = Grid::new();
        moving_knobs.set_name(Some("PART_MovingKnobs".to_string()));
        moving_knobs.set_width(20.0);
        moving_knobs.set_height(20.0);
        switch_knob.children().add(moving_knobs.register_in_name_scope(&**ns));
        root.children().add(switch_knob.register_in_name_scope(&**ns));

        root.upcast()
    })
}

struct Target {
    switch: Ref<ToggleSwitch>,
    knobs: Ref<Panel>,
    root: Ref<TestRoot>,
    pointer: Rc<dyn IPointer>,
}

fn create_target(is_checked: Option<bool>) -> Target {
    let switch = ToggleSwitch::new();
    switch.set_is_checked(is_checked);
    switch.set_template(Some(create_template()));

    let root = TestRoot::with_child(switch.clone());
    root.execute_initial_layout_pass();

    let knobs = switch
        .get_template_descendants()
        .into_iter()
        .filter_map(|x| x.cast::<Panel>())
        .find(|x| x.name().as_deref() == Some("PART_MovingKnobs"))
        .expect("no moving knobs");
    let pointer: Rc<dyn IPointer> = Pointer::new(Pointer::get_next_free_id(), PointerType::Mouse, true);
    Target { switch, knobs, root, pointer }
}

impl Target {
    fn root_visual(&self) -> Ref<Visual> {
        self.root.clone().upcast()
    }

    fn left(&self) -> f64 {
        Canvas::get_left(&self.knobs)
    }

    fn press(&self, x: f64) {
        self.knobs.raise_event(&PointerPressedEventArgs::new(
            self.knobs.clone(),
            self.pointer.clone(),
            &self.root_visual(),
            Point::new(x, 5.0),
            0,
            PointerPointProperties::default(),
            KeyModifiers::NONE,
            1,
        ));
    }

    fn move_to(&self, x: f64) {
        self.knobs.raise_event(&PointerEventArgs::new(
            Some(InputElement::pointer_moved_event()),
            self.knobs.clone(),
            self.pointer.clone(),
            Some(&self.root_visual()),
            Point::new(x, 5.0),
            0,
            PointerPointProperties::default(),
            KeyModifiers::NONE,
        ));
    }

    /// Releases the pointer; returns whether the switch handled the event.
    fn release(&self, x: f64) -> bool {
        let e = PointerReleasedEventArgs::new(
            self.knobs.clone(),
            self.pointer.clone(),
            &self.root_visual(),
            Point::new(x, 5.0),
            0,
            PointerPointProperties::default(),
            KeyModifiers::NONE,
            MouseButton::Left,
        );
        self.knobs.raise_event(&e);
        e.handled()
    }
}

#[test]
fn knob_position_follows_is_checked() {
    let _scope = test_scope();
    let target = create_target(Some(false));

    assert_eq!(0.0, target.left());

    target.switch.set_is_checked(Some(true));
    assert_eq!(20.0, target.left());

    target.switch.set_is_checked(Some(false));
    assert_eq!(0.0, target.left());

    // An indeterminate switch keeps the position of the knob.
    target.switch.set_is_checked(Some(true));
    target.switch.set_is_checked(None);
    assert_eq!(20.0, target.left());
}

#[test]
fn knob_position_is_set_when_the_template_is_applied() {
    let _scope = test_scope();
    let target = create_target(Some(true));

    // The knob has no bounds when the template is applied; the change of the
    // bounds of the switch moves the knob once it is laid out.
    target.switch.set_width(100.0);
    target.root.layout_manager().execute_layout_pass();

    assert_eq!(20.0, target.left());
}

#[test]
fn content_presenters_are_registered_and_contents_are_logical_children() {
    let _scope = test_scope();
    let target = create_target(Some(false));

    let off_presenter = target.switch.off_content_presenter().expect("no off content presenter");
    let on_presenter = target.switch.on_content_presenter().expect("no on content presenter");
    assert_eq!(Some("PART_OffContentPresenter".to_string()), off_presenter.name());
    assert_eq!(Some("PART_OnContentPresenter".to_string()), on_presenter.name());
    assert_eq!(Some("Off".to_string()), target.switch.off_content().as_ref().and_then(string_of));
    assert_eq!(Some("On".to_string()), target.switch.on_content().as_ref().and_then(string_of));

    let logical_children = StyledElement::logical_children(&target.switch);
    let off = Control::new();
    let on = Control::new();
    target.switch.set_off_content(Some(Control::boxed(off.clone())));
    target.switch.set_on_content(Some(Control::boxed(on.clone())));
    assert!(logical_children.contains(&off.clone().upcast::<StyledElement>()));
    assert!(logical_children.contains(&on.clone().upcast::<StyledElement>()));

    target.switch.set_off_content(boxed_str("No"));
    target.switch.set_on_content(None);
    assert!(!logical_children.contains(&off.upcast::<StyledElement>()));
    assert!(!logical_children.contains(&on.upcast::<StyledElement>()));
}

#[test]
fn dragging_the_knob_past_the_middle_checks_the_switch() {
    let _scope = test_scope();
    let target = create_target(Some(false));

    target.press(5.0);
    assert!(!target.switch.classes().contains(":dragging"));

    // A move of three pixels or less is not a drag.
    target.move_to(8.0);
    assert!(!target.switch.classes().contains(":dragging"));
    assert_eq!(0.0, target.left());

    target.move_to(17.0);
    assert!(target.switch.classes().contains(":dragging"));
    assert_eq!(12.0, target.left());

    // The knob stays inside the switch knob.
    target.move_to(100.0);
    assert_eq!(20.0, target.left());
    target.move_to(-100.0);
    assert_eq!(0.0, target.left());

    target.move_to(17.0);
    assert!(target.release(17.0));

    assert!(!target.switch.classes().contains(":dragging"));
    assert_eq!(Some(true), target.switch.is_checked());
    assert_eq!(20.0, target.left());
}

#[test]
fn dragging_the_knob_back_before_the_middle_unchecks_the_switch() {
    let _scope = test_scope();
    let target = create_target(Some(true));
    assert_eq!(20.0, target.left());

    target.press(25.0);
    target.move_to(10.0);
    assert_eq!(5.0, target.left());
    assert!(target.release(10.0));

    assert_eq!(Some(false), target.switch.is_checked());
    assert_eq!(0.0, target.left());
}

#[test]
fn releasing_the_knob_on_the_side_it_came_from_restores_its_position() {
    let _scope = test_scope();
    let target = create_target(Some(false));

    target.press(5.0);
    target.move_to(10.0);
    assert_eq!(5.0, target.left());
    assert!(target.release(10.0));

    assert_eq!(Some(false), target.switch.is_checked());
    assert_eq!(0.0, target.left());
}

#[test]
fn releasing_without_a_drag_is_not_handled() {
    let _scope = test_scope();
    let target = create_target(Some(false));

    target.press(5.0);
    target.move_to(6.0);
    assert!(!target.release(6.0));

    assert_eq!(Some(false), target.switch.is_checked());

    // Moves without a press do nothing.
    target.move_to(50.0);
    assert!(!target.switch.classes().contains(":dragging"));
    assert_eq!(0.0, target.left());
}

#[test]
fn knob_transitions_are_applied_when_loaded_and_suspended_while_dragging() {
    let _scope = test_scope();
    let target = create_target(Some(false));
    Dispatcher::ui_thread().run_jobs(None);
    assert!(target.knobs.transitions().is_none());

    let transition = DoubleTransition::new();
    transition.set_property(Some(Canvas::left_property().as_property()));
    let transition: Rc<dyn ITransition> = transition.into();
    let transitions = Transitions::from_items([transition]);
    target.switch.set_knob_transitions(Some(transitions.clone()));
    assert!(target.knobs.transitions().is_some_and(|x| x.ptr_eq(&transitions)));

    target.press(5.0);
    target.move_to(17.0);
    assert!(target.knobs.transitions().is_none());

    target.release(17.0);
    assert!(target.knobs.transitions().is_some_and(|x| x.ptr_eq(&transitions)));
}

// ---------------------------------------------------------------------------
// The switch as a toggle button: the events of a pressed left button reach
// the button behind the knob, which toggles on a click unless the release
// ended a drag of the knob.
// ---------------------------------------------------------------------------

/// A hit tester that finds the root of the search when it contains the
/// point.
struct BoundsHitTester;

impl IHitTester for BoundsHitTester {
    fn hit_test(&self, p: Point, root: &Visual, _filter: Option<&dyn Fn(&Visual) -> bool>) -> Vec<Ref<Visual>> {
        if root.bounds().contains(p) {
            vec![root.to_ref()]
        } else {
            Vec::new()
        }
    }

    fn hit_test_geometry(
        &self,
        _geometry: &Geometry,
        _root: &Visual,
        _filter: Option<&dyn Fn(&Visual) -> bool>,
    ) -> Vec<GeometryHitTestResult> {
        Vec::new()
    }

    fn hit_test_first(&self, p: Point, root: &Visual, filter: Option<&dyn Fn(&Visual) -> bool>) -> Option<Ref<Visual>> {
        self.hit_test(p, root, filter).into_iter().next()
    }

    fn hit_test_first_geometry(
        &self,
        _geometry: &Geometry,
        _root: &Visual,
        _filter: Option<&dyn Fn(&Visual) -> bool>,
    ) -> Option<GeometryHitTestResult> {
        None
    }
}

/// A target whose root finds the switch under the pointer, and the mouse
/// that presses its left button on the knob.
fn create_clickable_target(is_checked: Option<bool>) -> (Target, MouseTestHelper, Rc<Cell<i32>>) {
    let target = create_target(is_checked);
    target.root.set_hit_tester(Some(Rc::new(BoundsHitTester)));
    let clicks = Rc::new(Cell::new(0));
    target.switch.click({
        let clicks = clicks.clone();
        move |_, _| clicks.set(clicks.get() + 1)
    });
    (target, MouseTestHelper::new(), clicks)
}

#[test]
fn clicking_the_knob_without_dragging_toggles_the_switch() {
    let _scope = test_scope();
    let (target, mouse, clicks) = create_clickable_target(Some(false));

    mouse.down_at(&target.knobs, MouseButton::Left, Point::new(5.0, 5.0), 1);
    // A move within the drag threshold keeps it a click.
    mouse.move_(&target.knobs, Point::new(7.0, 5.0));
    mouse.up_at(&target.knobs, MouseButton::Left, Point::new(7.0, 5.0));

    assert_eq!(1, clicks.get());
    assert!(!target.switch.classes().contains(":dragging"));
    assert_eq!(Some(true), target.switch.is_checked());
    assert_eq!(20.0, target.left());

    mouse.down_at(&target.knobs, MouseButton::Left, Point::new(5.0, 5.0), 1);
    mouse.up_at(&target.knobs, MouseButton::Left, Point::new(5.0, 5.0));

    assert_eq!(2, clicks.get());
    assert_eq!(Some(false), target.switch.is_checked());
    assert_eq!(0.0, target.left());
}

#[test]
fn releasing_a_dragged_knob_does_not_click_the_switch() {
    let _scope = test_scope();
    let (target, mouse, clicks) = create_clickable_target(Some(false));

    // Past the middle: checked by the drag, and not toggled back by a click.
    mouse.down_at(&target.knobs, MouseButton::Left, Point::new(5.0, 5.0), 1);
    mouse.move_(&target.knobs, Point::new(17.0, 5.0));
    assert!(target.switch.classes().contains(":dragging"));
    mouse.up_at(&target.knobs, MouseButton::Left, Point::new(17.0, 5.0));

    assert_eq!(0, clicks.get());
    assert!(!target.switch.classes().contains(":dragging"));
    assert_eq!(Some(true), target.switch.is_checked());
    assert_eq!(20.0, target.left());

    // Released on the side it came from: still checked, and no click.
    mouse.down_at(&target.knobs, MouseButton::Left, Point::new(25.0, 5.0), 1);
    mouse.move_(&target.knobs, Point::new(20.0, 5.0));
    assert_eq!(15.0, target.left());
    mouse.up_at(&target.knobs, MouseButton::Left, Point::new(20.0, 5.0));

    assert_eq!(0, clicks.get());
    assert_eq!(Some(true), target.switch.is_checked());
    assert_eq!(20.0, target.left());

    // The next press starts over: without a drag it is a click again. (The
    // switch is as wide as its knob: the pointer is released inside it.)
    mouse.down_at(&target.knobs, MouseButton::Left, Point::new(5.0, 5.0), 1);
    mouse.up_at(&target.knobs, MouseButton::Left, Point::new(5.0, 5.0));

    assert_eq!(1, clicks.get());
    assert_eq!(Some(false), target.switch.is_checked());
}

#[test]
fn dragging_the_knob_of_an_indeterminate_switch_unchecks_it() {
    let _scope = test_scope();
    let target = create_target(None);

    // The knob of an indeterminate switch has no position.
    assert!(target.left().is_nan());

    target.press(5.0);
    target.move_to(17.0);
    assert!(target.switch.classes().contains(":dragging"));
    // The position stays unset: it is computed from the unset one.
    assert!(target.left().is_nan());

    assert!(target.release(17.0));

    // No position is not past the middle.
    assert!(!target.switch.classes().contains(":dragging"));
    assert_eq!(Some(false), target.switch.is_checked());
    assert_eq!(0.0, target.left());
}

#[test]
fn a_press_after_a_drag_starts_from_the_current_knob_position() {
    let _scope = test_scope();
    let target = create_target(Some(false));

    target.press(5.0);
    target.move_to(17.0);
    assert!(target.release(17.0));
    assert_eq!(Some(true), target.switch.is_checked());

    // The second drag starts at the checked position and is not a drag
    // until the pointer moves more than three pixels again.
    target.press(25.0);
    target.move_to(22.0);
    assert!(!target.switch.classes().contains(":dragging"));
    assert_eq!(20.0, target.left());
    target.move_to(21.0);
    assert!(target.switch.classes().contains(":dragging"));
    assert_eq!(16.0, target.left());
    assert!(target.release(21.0));

    assert_eq!(Some(true), target.switch.is_checked());
    assert_eq!(20.0, target.left());
}