1use crate::{
2 px, AnyWindowHandle, AtlasKey, AtlasTextureId, AtlasTile, Bounds, Pixels, PlatformAtlas,
3 PlatformDisplay, PlatformInput, PlatformInputHandler, PlatformWindow, Point, Size,
4 TestPlatform, TileId, WindowAppearance, WindowBounds, WindowOptions,
5};
6use collections::HashMap;
7use parking_lot::Mutex;
8use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
9use std::{
10 rc::{Rc, Weak},
11 sync::{self, Arc},
12};
13
14pub(crate) struct TestWindowState {
15 pub(crate) bounds: WindowBounds,
16 pub(crate) handle: AnyWindowHandle,
17 display: Rc<dyn PlatformDisplay>,
18 pub(crate) title: Option<String>,
19 pub(crate) edited: bool,
20 platform: Weak<TestPlatform>,
21 sprite_atlas: Arc<dyn PlatformAtlas>,
22 pub(crate) should_close_handler: Option<Box<dyn FnMut() -> bool>>,
23 input_callback: Option<Box<dyn FnMut(PlatformInput) -> bool>>,
24 active_status_change_callback: Option<Box<dyn FnMut(bool)>>,
25 resize_callback: Option<Box<dyn FnMut(Size<Pixels>, f32)>>,
26 moved_callback: Option<Box<dyn FnMut()>>,
27 input_handler: Option<PlatformInputHandler>,
28}
29
30#[derive(Clone)]
31pub(crate) struct TestWindow(pub(crate) Arc<Mutex<TestWindowState>>);
32
33impl HasWindowHandle for TestWindow {
34 fn window_handle(
35 &self,
36 ) -> Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
37 unimplemented!("Test Windows are not backed by a real platform window")
38 }
39}
40
41impl HasDisplayHandle for TestWindow {
42 fn display_handle(
43 &self,
44 ) -> Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
45 unimplemented!("Test Windows are not backed by a real platform window")
46 }
47}
48
49impl TestWindow {
50 pub fn new(
51 options: WindowOptions,
52 handle: AnyWindowHandle,
53 platform: Weak<TestPlatform>,
54 display: Rc<dyn PlatformDisplay>,
55 ) -> Self {
56 Self(Arc::new(Mutex::new(TestWindowState {
57 bounds: options.bounds,
58 display,
59 platform,
60 handle,
61 sprite_atlas: Arc::new(TestAtlas::new()),
62 title: Default::default(),
63 edited: false,
64 should_close_handler: None,
65 input_callback: None,
66 active_status_change_callback: None,
67 resize_callback: None,
68 moved_callback: None,
69 input_handler: None,
70 })))
71 }
72
73 pub fn simulate_resize(&mut self, size: Size<Pixels>) {
74 let scale_factor = self.scale_factor();
75 let mut lock = self.0.lock();
76 let Some(mut callback) = lock.resize_callback.take() else {
77 return;
78 };
79 match &mut lock.bounds {
80 WindowBounds::Fullscreen | WindowBounds::Maximized => {
81 lock.bounds = WindowBounds::Fixed(Bounds {
82 origin: Point::default(),
83 size: size.map(|pixels| f64::from(pixels).into()),
84 });
85 }
86 WindowBounds::Fixed(bounds) => {
87 bounds.size = size.map(|pixels| f64::from(pixels).into());
88 }
89 }
90 drop(lock);
91 callback(size, scale_factor);
92 self.0.lock().resize_callback = Some(callback);
93 }
94
95 pub(crate) fn simulate_active_status_change(&self, active: bool) {
96 let mut lock = self.0.lock();
97 let Some(mut callback) = lock.active_status_change_callback.take() else {
98 return;
99 };
100 drop(lock);
101 callback(active);
102 self.0.lock().active_status_change_callback = Some(callback);
103 }
104
105 pub fn simulate_input(&mut self, event: PlatformInput) -> bool {
106 let mut lock = self.0.lock();
107 let Some(mut callback) = lock.input_callback.take() else {
108 return false;
109 };
110 drop(lock);
111 let result = callback(event);
112 self.0.lock().input_callback = Some(callback);
113 result
114 }
115}
116
117impl PlatformWindow for TestWindow {
118 fn bounds(&self) -> WindowBounds {
119 self.0.lock().bounds
120 }
121
122 fn content_size(&self) -> Size<Pixels> {
123 let bounds = match self.bounds() {
124 WindowBounds::Fixed(bounds) => bounds,
125 WindowBounds::Maximized | WindowBounds::Fullscreen => self.display().bounds(),
126 };
127 bounds.size.map(|p| px(p.0))
128 }
129
130 fn scale_factor(&self) -> f32 {
131 2.0
132 }
133
134 fn titlebar_height(&self) -> Pixels {
135 unimplemented!()
136 }
137
138 fn appearance(&self) -> WindowAppearance {
139 WindowAppearance::Light
140 }
141
142 fn display(&self) -> std::rc::Rc<dyn crate::PlatformDisplay> {
143 self.0.lock().display.clone()
144 }
145
146 fn mouse_position(&self) -> Point<Pixels> {
147 Point::default()
148 }
149
150 fn modifiers(&self) -> crate::Modifiers {
151 crate::Modifiers::default()
152 }
153
154 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
155 self
156 }
157
158 fn set_input_handler(&mut self, input_handler: PlatformInputHandler) {
159 self.0.lock().input_handler = Some(input_handler);
160 }
161
162 fn take_input_handler(&mut self) -> Option<PlatformInputHandler> {
163 self.0.lock().input_handler.take()
164 }
165
166 fn prompt(
167 &self,
168 _level: crate::PromptLevel,
169 _msg: &str,
170 _detail: Option<&str>,
171 _answers: &[&str],
172 ) -> futures::channel::oneshot::Receiver<usize> {
173 self.0
174 .lock()
175 .platform
176 .upgrade()
177 .expect("platform dropped")
178 .prompt()
179 }
180
181 fn activate(&self) {
182 self.0
183 .lock()
184 .platform
185 .upgrade()
186 .unwrap()
187 .set_active_window(Some(self.clone()))
188 }
189
190 fn set_title(&mut self, title: &str) {
191 self.0.lock().title = Some(title.to_owned());
192 }
193
194 fn set_edited(&mut self, edited: bool) {
195 self.0.lock().edited = edited;
196 }
197
198 fn show_character_palette(&self) {
199 unimplemented!()
200 }
201
202 fn minimize(&self) {
203 unimplemented!()
204 }
205
206 fn zoom(&self) {
207 unimplemented!()
208 }
209
210 fn toggle_full_screen(&self) {
211 unimplemented!()
212 }
213
214 fn on_request_frame(&self, _callback: Box<dyn FnMut()>) {}
215
216 fn on_input(&self, callback: Box<dyn FnMut(crate::PlatformInput) -> bool>) {
217 self.0.lock().input_callback = Some(callback)
218 }
219
220 fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) {
221 self.0.lock().active_status_change_callback = Some(callback)
222 }
223
224 fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) {
225 self.0.lock().resize_callback = Some(callback)
226 }
227
228 fn on_fullscreen(&self, _callback: Box<dyn FnMut(bool)>) {
229 unimplemented!()
230 }
231
232 fn on_moved(&self, callback: Box<dyn FnMut()>) {
233 self.0.lock().moved_callback = Some(callback)
234 }
235
236 fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) {
237 self.0.lock().should_close_handler = Some(callback);
238 }
239
240 fn on_close(&self, _callback: Box<dyn FnOnce()>) {}
241
242 fn on_appearance_changed(&self, _callback: Box<dyn FnMut()>) {}
243
244 fn is_topmost_for_position(&self, _position: crate::Point<Pixels>) -> bool {
245 unimplemented!()
246 }
247
248 fn draw(&self, _scene: &crate::Scene) {}
249
250 fn sprite_atlas(&self) -> sync::Arc<dyn crate::PlatformAtlas> {
251 self.0.lock().sprite_atlas.clone()
252 }
253
254 fn as_test(&mut self) -> Option<&mut TestWindow> {
255 Some(self)
256 }
257}
258
259pub(crate) struct TestAtlasState {
260 next_id: u32,
261 tiles: HashMap<AtlasKey, AtlasTile>,
262}
263
264pub(crate) struct TestAtlas(Mutex<TestAtlasState>);
265
266impl TestAtlas {
267 pub fn new() -> Self {
268 TestAtlas(Mutex::new(TestAtlasState {
269 next_id: 0,
270 tiles: HashMap::default(),
271 }))
272 }
273}
274
275impl PlatformAtlas for TestAtlas {
276 fn get_or_insert_with<'a>(
277 &self,
278 key: &crate::AtlasKey,
279 build: &mut dyn FnMut() -> anyhow::Result<(
280 Size<crate::DevicePixels>,
281 std::borrow::Cow<'a, [u8]>,
282 )>,
283 ) -> anyhow::Result<crate::AtlasTile> {
284 let mut state = self.0.lock();
285 if let Some(tile) = state.tiles.get(key) {
286 return Ok(tile.clone());
287 }
288
289 state.next_id += 1;
290 let texture_id = state.next_id;
291 state.next_id += 1;
292 let tile_id = state.next_id;
293
294 drop(state);
295 let (size, _) = build()?;
296 let mut state = self.0.lock();
297
298 state.tiles.insert(
299 key.clone(),
300 crate::AtlasTile {
301 texture_id: AtlasTextureId {
302 index: texture_id,
303 kind: crate::AtlasTextureKind::Path,
304 },
305 tile_id: TileId(tile_id),
306 padding: 0,
307 bounds: crate::Bounds {
308 origin: Point::default(),
309 size,
310 },
311 },
312 );
313
314 Ok(state.tiles[key].clone())
315 }
316}