window.rs

  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    ) -> Option<futures::channel::oneshot::Receiver<usize>> {
173        Some(
174            self.0
175                .lock()
176                .platform
177                .upgrade()
178                .expect("platform dropped")
179                .prompt(),
180        )
181    }
182
183    fn activate(&self) {
184        self.0
185            .lock()
186            .platform
187            .upgrade()
188            .unwrap()
189            .set_active_window(Some(self.clone()))
190    }
191
192    fn set_title(&mut self, title: &str) {
193        self.0.lock().title = Some(title.to_owned());
194    }
195
196    fn set_edited(&mut self, edited: bool) {
197        self.0.lock().edited = edited;
198    }
199
200    fn show_character_palette(&self) {
201        unimplemented!()
202    }
203
204    fn minimize(&self) {
205        unimplemented!()
206    }
207
208    fn zoom(&self) {
209        unimplemented!()
210    }
211
212    fn toggle_full_screen(&self) {
213        unimplemented!()
214    }
215
216    fn on_request_frame(&self, _callback: Box<dyn FnMut()>) {}
217
218    fn on_input(&self, callback: Box<dyn FnMut(crate::PlatformInput) -> bool>) {
219        self.0.lock().input_callback = Some(callback)
220    }
221
222    fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) {
223        self.0.lock().active_status_change_callback = Some(callback)
224    }
225
226    fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) {
227        self.0.lock().resize_callback = Some(callback)
228    }
229
230    fn on_fullscreen(&self, _callback: Box<dyn FnMut(bool)>) {
231        unimplemented!()
232    }
233
234    fn on_moved(&self, callback: Box<dyn FnMut()>) {
235        self.0.lock().moved_callback = Some(callback)
236    }
237
238    fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) {
239        self.0.lock().should_close_handler = Some(callback);
240    }
241
242    fn on_close(&self, _callback: Box<dyn FnOnce()>) {}
243
244    fn on_appearance_changed(&self, _callback: Box<dyn FnMut()>) {}
245
246    fn is_topmost_for_position(&self, _position: crate::Point<Pixels>) -> bool {
247        unimplemented!()
248    }
249
250    fn draw(&self, _scene: &crate::Scene) {}
251
252    fn sprite_atlas(&self) -> sync::Arc<dyn crate::PlatformAtlas> {
253        self.0.lock().sprite_atlas.clone()
254    }
255
256    fn as_test(&mut self) -> Option<&mut TestWindow> {
257        Some(self)
258    }
259}
260
261pub(crate) struct TestAtlasState {
262    next_id: u32,
263    tiles: HashMap<AtlasKey, AtlasTile>,
264}
265
266pub(crate) struct TestAtlas(Mutex<TestAtlasState>);
267
268impl TestAtlas {
269    pub fn new() -> Self {
270        TestAtlas(Mutex::new(TestAtlasState {
271            next_id: 0,
272            tiles: HashMap::default(),
273        }))
274    }
275}
276
277impl PlatformAtlas for TestAtlas {
278    fn get_or_insert_with<'a>(
279        &self,
280        key: &crate::AtlasKey,
281        build: &mut dyn FnMut() -> anyhow::Result<(
282            Size<crate::DevicePixels>,
283            std::borrow::Cow<'a, [u8]>,
284        )>,
285    ) -> anyhow::Result<crate::AtlasTile> {
286        let mut state = self.0.lock();
287        if let Some(tile) = state.tiles.get(key) {
288            return Ok(tile.clone());
289        }
290
291        state.next_id += 1;
292        let texture_id = state.next_id;
293        state.next_id += 1;
294        let tile_id = state.next_id;
295
296        drop(state);
297        let (size, _) = build()?;
298        let mut state = self.0.lock();
299
300        state.tiles.insert(
301            key.clone(),
302            crate::AtlasTile {
303                texture_id: AtlasTextureId {
304                    index: texture_id,
305                    kind: crate::AtlasTextureKind::Path,
306                },
307                tile_id: TileId(tile_id),
308                padding: 0,
309                bounds: crate::Bounds {
310                    origin: Point::default(),
311                    size,
312                },
313            },
314        );
315
316        Ok(state.tiles[key].clone())
317    }
318}