1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4
5import { Slider } from "std-widgets.slint";
6export component TestCase inherits Window {
7 width: 100px;
8 height: 100px;
9 slider := Slider {
10 minimum: 0;
11 maximum: 100;
12 value: 10;
13 width: 100%;
14 height: 100%;
15 }
16
17 forward-focus: slider;
18 out property <bool> slider-focused <=> slider.has_focus;
19 in-out property <float> value <=> slider.value;
20 in property <float> step <=> slider.step;
21 callback released <=> slider.released;
22 callback changed <=> slider.changed;
23}
24
25/*
26
27
28```rust
29use slint::platform::Key;
30use std::cell::RefCell;
31use std::rc::Rc;
32
33let instance = TestCase::new().unwrap();
34slint_testing::send_mouse_click(&instance, 5., 5.);
35instance.set_value(10.);
36
37let edits = Rc::new(RefCell::new(Vec::new()));
38let changes = Rc::new(RefCell::new(Vec::new()));
39
40instance.on_released({
41 let edits = edits.clone();
42 move |val| {
43 edits.borrow_mut().push(val);
44}});
45
46instance.on_changed({
47 let changes = changes.clone();
48 move |val| {
49 changes.borrow_mut().push(val);
50}});
51
52assert!(edits.borrow().is_empty());
53assert!(changes.borrow().is_empty());
54assert!(instance.get_slider_focused());
55
56slint_testing::send_keyboard_char(&instance, Key::RightArrow.into(), true);
57slint_testing::send_keyboard_char(&instance, Key::RightArrow.into(), true);
58slint_testing::send_keyboard_char(&instance, Key::RightArrow.into(), true);
59slint_testing::send_keyboard_char(&instance, Key::RightArrow.into(), true);
60slint_testing::send_keyboard_char(&instance, Key::RightArrow.into(), false);
61// Send bogus key to verify that it doesn't trigger released
62slint_testing::send_keyboard_char(&instance, 'a', true);
63slint_testing::send_keyboard_char(&instance, 'a', false);
64
65assert_eq!(edits.borrow().clone(), vec![14f32]);
66assert_eq!(changes.borrow().clone(), vec![11f32, 12f32, 13f32, 14f32]);
67
68edits.borrow_mut().clear();
69
70// Reset to center
71instance.set_value(50.);
72
73// Press center and then drag outside
74let mut position = slint::LogicalPosition::new(50., 50.);
75let button = slint::platform::PointerEventButton::Left;
76instance.window().dispatch_event(slint::platform::WindowEvent::PointerMoved { position });
77instance.window().dispatch_event(slint::platform::WindowEvent::PointerPressed { position, button });
78slint_testing::mock_elapsed_time(50);
79for x in (position.x as usize..200).step_by(10) {
80 position.x = x as _;
81 instance.window().dispatch_event(slint::platform::WindowEvent::PointerMoved { position });
82}
83instance.window().dispatch_event(slint::platform::WindowEvent::PointerReleased { position, button });
84assert_eq!(edits.borrow().clone(), vec![100f32]);
85
86edits.borrow_mut().clear();
87changes.borrow_mut().clear();
88
89instance.set_step(30.);
90slint_testing::send_keyboard_char(&instance, Key::LeftArrow.into(), true);
91slint_testing::send_keyboard_char(&instance, Key::LeftArrow.into(), true);
92slint_testing::send_keyboard_char(&instance, Key::LeftArrow.into(), true);
93slint_testing::send_keyboard_char(&instance, Key::LeftArrow.into(), true);
94slint_testing::send_keyboard_char(&instance, Key::LeftArrow.into(), false);
95
96assert_eq!(edits.borrow().clone(), vec![0f32]);
97assert_eq!(changes.borrow().clone(), vec![70f32, 40f32, 10f32, 0f32]);
98
99edits.borrow_mut().clear();
100changes.borrow_mut().clear();
101
102slint_testing::send_keyboard_char(&instance, Key::End.into(), true);
103slint_testing::send_keyboard_char(&instance, Key::End.into(), false);
104slint_testing::send_keyboard_char(&instance, Key::Home.into(), true);
105slint_testing::send_keyboard_char(&instance, Key::Home.into(), false);
106
107assert_eq!(edits.borrow().clone(), vec![100f32, 0f32]);
108assert_eq!(changes.borrow().clone(), vec![100f32, 0f32]);
109```
110
111*/
112