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