1use crate::{
2 AnyWindowHandle, AtlasKey, AtlasTextureId, AtlasTile, Bounds, GlobalPixels, Pixels,
3 PlatformAtlas, PlatformDisplay, PlatformInput, PlatformInputHandler, PlatformWindow, Point,
4 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) -> 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 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
106 }
107}
108
109impl PlatformWindow for TestWindow {
110 fn bounds(&self) -> Bounds<GlobalPixels> {
111 self.0.lock().bounds
112 }
113
114 fn content_size(&self) -> Size<Pixels> {
115 self.bounds().size.into()
116 }
117
118 fn scale_factor(&self) -> f32 {
119 2.0
120 }
121
122 fn titlebar_height(&self) -> Pixels {
123 unimplemented!()
124 }
125
126 fn appearance(&self) -> WindowAppearance {
127 WindowAppearance::Light
128 }
129
130 fn display(&self) -> std::rc::Rc<dyn crate::PlatformDisplay> {
131 self.0.lock().display.clone()
132 }
133
134 fn mouse_position(&self) -> Point<Pixels> {
135 Point::default()
136 }
137
138 fn modifiers(&self) -> crate::Modifiers {
139 crate::Modifiers::default()
140 }
141
142 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
143 self
144 }
145
146 fn set_input_handler(&mut self, input_handler: PlatformInputHandler) {
147 self.0.lock().input_handler = Some(input_handler);
148 }
149
150 fn take_input_handler(&mut self) -> Option<PlatformInputHandler> {
151 self.0.lock().input_handler.take()
152 }
153
154 fn prompt(
155 &self,
156 _level: crate::PromptLevel,
157 _msg: &str,
158 _detail: Option<&str>,
159 _answers: &[&str],
160 ) -> Option<futures::channel::oneshot::Receiver<usize>> {
161 Some(
162 self.0
163 .lock()
164 .platform
165 .upgrade()
166 .expect("platform dropped")
167 .prompt(),
168 )
169 }
170
171 fn activate(&self) {
172 self.0
173 .lock()
174 .platform
175 .upgrade()
176 .unwrap()
177 .set_active_window(Some(self.clone()))
178 }
179
180 fn set_title(&mut self, title: &str) {
181 self.0.lock().title = Some(title.to_owned());
182 }
183
184 fn set_edited(&mut self, edited: bool) {
185 self.0.lock().edited = edited;
186 }
187
188 fn show_character_palette(&self) {
189 unimplemented!()
190 }
191
192 fn minimize(&self) {
193 unimplemented!()
194 }
195
196 fn zoom(&self) {
197 unimplemented!()
198 }
199
200 fn toggle_full_screen(&self) {
201 let mut lock = self.0.lock();
202 lock.is_fullscreen = !lock.is_fullscreen;
203 }
204
205 fn is_full_screen(&self) -> bool {
206 self.0.lock().is_fullscreen
207 }
208
209 fn on_request_frame(&self, _callback: Box<dyn FnMut()>) {}
210
211 fn on_input(&self, callback: Box<dyn FnMut(crate::PlatformInput) -> bool>) {
212 self.0.lock().input_callback = Some(callback)
213 }
214
215 fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) {
216 self.0.lock().active_status_change_callback = Some(callback)
217 }
218
219 fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) {
220 self.0.lock().resize_callback = Some(callback)
221 }
222
223 fn on_fullscreen(&self, _callback: Box<dyn FnMut(bool)>) {
224 unimplemented!()
225 }
226
227 fn on_moved(&self, callback: Box<dyn FnMut()>) {
228 self.0.lock().moved_callback = Some(callback)
229 }
230
231 fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) {
232 self.0.lock().should_close_handler = Some(callback);
233 }
234
235 fn on_close(&self, _callback: Box<dyn FnOnce()>) {}
236
237 fn on_appearance_changed(&self, _callback: Box<dyn FnMut()>) {}
238
239 fn is_topmost_for_position(&self, _position: crate::Point<Pixels>) -> bool {
240 unimplemented!()
241 }
242
243 fn draw(&self, _scene: &crate::Scene) {}
244
245 fn sprite_atlas(&self) -> sync::Arc<dyn crate::PlatformAtlas> {
246 self.0.lock().sprite_atlas.clone()
247 }
248
249 fn as_test(&mut self) -> Option<&mut TestWindow> {
250 Some(self)
251 }
252}
253
254pub(crate) struct TestAtlasState {
255 next_id: u32,
256 tiles: HashMap<AtlasKey, AtlasTile>,
257}
258
259pub(crate) struct TestAtlas(Mutex<TestAtlasState>);
260
261impl TestAtlas {
262 pub fn new() -> Self {
263 TestAtlas(Mutex::new(TestAtlasState {
264 next_id: 0,
265 tiles: HashMap::default(),
266 }))
267 }
268}
269
270impl PlatformAtlas for TestAtlas {
271 fn get_or_insert_with<'a>(
272 &self,
273 key: &crate::AtlasKey,
274 build: &mut dyn FnMut() -> anyhow::Result<(
275 Size<crate::DevicePixels>,
276 std::borrow::Cow<'a, [u8]>,
277 )>,
278 ) -> anyhow::Result<crate::AtlasTile> {
279 let mut state = self.0.lock();
280 if let Some(tile) = state.tiles.get(key) {
281 return Ok(tile.clone());
282 }
283
284 state.next_id += 1;
285 let texture_id = state.next_id;
286 state.next_id += 1;
287 let tile_id = state.next_id;
288
289 drop(state);
290 let (size, _) = build()?;
291 let mut state = self.0.lock();
292
293 state.tiles.insert(
294 key.clone(),
295 crate::AtlasTile {
296 texture_id: AtlasTextureId {
297 index: texture_id,
298 kind: crate::AtlasTextureKind::Path,
299 },
300 tile_id: TileId(tile_id),
301 padding: 0,
302 bounds: crate::Bounds {
303 origin: Point::default(),
304 size,
305 },
306 },
307 );
308
309 Ok(state.tiles[key].clone())
310 }
311}