Fix shared element state across component instances

This commit is contained in:
2026-11-09 14:04:25 +07:00
parent f022f08947
commit 73f7dc31d4
20 changed files with 489 additions and 136 deletions
+129
View File
@@ -153,3 +153,132 @@ fn arrow_keys_step_the_value_while_focused() {
assert_eq!(*changes.borrow(), vec![0.0, 10.0, 20.0, 10.0]);
}
struct TwoSliders {
changes_a: Rc<RefCell<Vec<f32>>>,
changes_b: Rc<RefCell<Vec<f32>>>,
value_a: f32,
value_b: f32,
}
impl Render for TwoSliders {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let (changes_a, changes_b) = (self.changes_a.clone(), self.changes_b.clone());
div()
.size_full()
.child(
div().absolute().top_0().left_0().w(px(200.)).child(
Slider::new("slider-a", self.value_a)
.min(0.0)
.max(100.0)
.step(10.0)
.on_change({
let view = cx.entity();
let changes_a = changes_a.clone();
move |next, _, cx| {
changes_a.borrow_mut().push(next);
view.update(cx, |harness, cx| {
harness.value_a = next;
cx.notify();
});
}
}),
),
)
.child(
div().absolute().top(px(40.)).left_0().w(px(200.)).child(
Slider::new("slider-b", self.value_b)
.min(0.0)
.max(100.0)
.step(10.0)
.on_change({
let view = cx.entity();
move |next, _, cx| {
changes_b.borrow_mut().push(next);
view.update(cx, |harness, cx| {
harness.value_b = next;
cx.notify();
});
}
}),
),
)
}
}
#[allow(clippy::type_complexity)]
fn two_slider_window() -> (
VisualTestContext,
Rc<RefCell<Vec<f32>>>,
Rc<RefCell<Vec<f32>>>,
) {
let mut cx = TestAppContext::single();
cx.update(Theme::init);
let changes_a = Rc::new(RefCell::new(Vec::new()));
let changes_b = Rc::new(RefCell::new(Vec::new()));
let (changes_a_for_window, changes_b_for_window) = (changes_a.clone(), changes_b.clone());
let window = cx.add_window(move |_, _| TwoSliders {
changes_a: changes_a_for_window,
changes_b: changes_b_for_window,
value_a: 0.0,
value_b: 0.0,
});
let window = window.into();
cx.update_window(window, |_, window, cx| {
window.draw(cx).clear(cx);
})
.unwrap();
(
VisualTestContext::from_window(window, &cx),
changes_a,
changes_b,
)
}
/// Dragging one slider must never drive a sibling slider: the drag flag is
/// per instance, not shared keyed state.
#[test]
fn two_sliders_drag_independently() {
let (mut cx, changes_a, changes_b) = two_slider_window();
// Slider A occupies y 0..16, slider B starts at y 40.
cx.simulate_mouse_down(
point(px(100.), px(10.)),
MouseButton::Left,
Modifiers::default(),
);
cx.simulate_mouse_move(
point(px(120.), px(10.)),
MouseButton::Left,
Modifiers::default(),
);
cx.simulate_mouse_up(
point(px(120.), px(10.)),
MouseButton::Left,
Modifiers::default(),
);
assert_eq!(*changes_a.borrow(), vec![50.0, 60.0]);
assert!(changes_b.borrow().is_empty(), "A's drag leaked into B");
cx.simulate_mouse_down(
point(px(100.), px(50.)),
MouseButton::Left,
Modifiers::default(),
);
cx.simulate_mouse_move(
point(px(40.), px(50.)),
MouseButton::Left,
Modifiers::default(),
);
cx.simulate_mouse_up(
point(px(40.), px(50.)),
MouseButton::Left,
Modifiers::default(),
);
assert_eq!(*changes_b.borrow(), vec![50.0, 20.0]);
assert_eq!(
*changes_a.borrow(),
vec![50.0, 60.0],
"B's drag leaked into A"
);
}