| 1 | use std::{cell::RefCell, rc::Rc}; |
| 2 | |
| 3 | use gpui::{Context, IntoElement, Render, Styled as _, TestAppContext, Window, div, px}; |
| 4 | use gpui_base::ElementExt as _; |
| 5 | |
| 6 | struct PrepaintHarness { |
| 7 | captured: Rc<RefCell<Option<gpui::Bounds<gpui::Pixels>>>>, |
| 8 | } |
| 9 | |
| 10 | impl Render for PrepaintHarness { |
| 11 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 12 | let captured = self.captured.clone(); |
| 13 | div() |
| 14 | .size(px(80.)) |
| 15 | .on_prepaint(move |bounds, _, _| *captured.borrow_mut() = Some(bounds)) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | #[gpui::test] |
| 20 | fn prepaint_callback_observes_the_parent_bounds(cx: &mut TestAppContext) { |
| 21 | let captured = Rc::new(RefCell::new(None)); |
| 22 | let result = captured.clone(); |
| 23 | let (_, cx) = cx.add_window_view(move |_, _| PrepaintHarness { captured }); |
| 24 | |
| 25 | cx.update(|window, cx| window.draw(cx).clear(cx)); |
| 26 | |
| 27 | let bounds = result.borrow().expect("prepaint callback should run"); |
| 28 | assert_eq!(bounds.size.width, px(80.)); |
| 29 | assert_eq!(bounds.size.height, px(80.)); |
| 30 | } |