window.rs

  1use crate::{
  2    AnyWindowHandle, AtlasKey, AtlasTextureId, AtlasTile, Bounds, DevicePixels,
  3    DispatchEventResult, GpuSpecs, Pixels, PlatformAtlas, PlatformDisplay,
  4    PlatformHeadlessRenderer, PlatformInput, PlatformInputHandler, PlatformWindow, Point,
  5    PromptButton, RequestFrameOptions, Scene, Size, TestPlatform, TileId, WindowAppearance,
  6    WindowBackgroundAppearance, WindowBounds, WindowControlArea, WindowParams,
  7};
  8use collections::HashMap;
  9use image::RgbaImage;
 10use parking_lot::Mutex;
 11use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
 12use std::{
 13    rc::{Rc, Weak},
 14    sync::{self, Arc},
 15};
 16
 17pub(crate) struct TestWindowState {
 18    pub(crate) bounds: Bounds<Pixels>,
 19    pub(crate) handle: AnyWindowHandle,
 20    display: Rc<dyn PlatformDisplay>,
 21    pub(crate) title: Option<String>,
 22    pub(crate) edited: bool,
 23    platform: Weak<TestPlatform>,
 24    // TODO: Replace with `Rc`
 25    sprite_atlas: Arc<dyn PlatformAtlas>,
 26    renderer: Option<Box<dyn PlatformHeadlessRenderer>>,
 27    pub(crate) should_close_handler: Option<Box<dyn FnMut() -> bool>>,
 28    hit_test_window_control_callback: Option<Box<dyn FnMut() -> Option<WindowControlArea>>>,
 29    input_callback: Option<Box<dyn FnMut(PlatformInput) -> DispatchEventResult>>,
 30    active_status_change_callback: Option<Box<dyn FnMut(bool)>>,
 31    hover_status_change_callback: Option<Box<dyn FnMut(bool)>>,
 32    resize_callback: Option<Box<dyn FnMut(Size<Pixels>, f32)>>,
 33    moved_callback: Option<Box<dyn FnMut()>>,
 34    input_handler: Option<PlatformInputHandler>,
 35    is_fullscreen: bool,
 36}
 37
 38#[derive(Clone)]
 39pub struct TestWindow(pub(crate) Rc<Mutex<TestWindowState>>);
 40
 41impl HasWindowHandle for TestWindow {
 42    fn window_handle(
 43        &self,
 44    ) -> Result<raw_window_handle::WindowHandle<'_>, raw_window_handle::HandleError> {
 45        unimplemented!("Test Windows are not backed by a real platform window")
 46    }
 47}
 48
 49impl HasDisplayHandle for TestWindow {
 50    fn display_handle(
 51        &self,
 52    ) -> Result<raw_window_handle::DisplayHandle<'_>, raw_window_handle::HandleError> {
 53        unimplemented!("Test Windows are not backed by a real platform window")
 54    }
 55}
 56
 57impl TestWindow {
 58    pub(crate) fn new(
 59        handle: AnyWindowHandle,
 60        params: WindowParams,
 61        platform: Weak<TestPlatform>,
 62        display: Rc<dyn PlatformDisplay>,
 63        renderer: Option<Box<dyn PlatformHeadlessRenderer>>,
 64    ) -> Self {
 65        let sprite_atlas: Arc<dyn PlatformAtlas> = match &renderer {
 66            Some(r) => r.sprite_atlas(),
 67            None => Arc::new(TestAtlas::new()),
 68        };
 69        Self(Rc::new(Mutex::new(TestWindowState {
 70            bounds: params.bounds,
 71            display,
 72            platform,
 73            handle,
 74            sprite_atlas,
 75            renderer,
 76            title: Default::default(),
 77            edited: false,
 78            should_close_handler: None,
 79            hit_test_window_control_callback: None,
 80            input_callback: None,
 81            active_status_change_callback: None,
 82            hover_status_change_callback: None,
 83            resize_callback: None,
 84            moved_callback: None,
 85            input_handler: None,
 86            is_fullscreen: false,
 87        })))
 88    }
 89
 90    pub fn simulate_resize(&mut self, size: Size<Pixels>) {
 91        let scale_factor = self.scale_factor();
 92        let mut lock = self.0.lock();
 93        // Always update bounds, even if no callback is registered
 94        lock.bounds.size = size;
 95        let Some(mut callback) = lock.resize_callback.take() else {
 96            return;
 97        };
 98        drop(lock);
 99        callback(size, scale_factor);
100        self.0.lock().resize_callback = Some(callback);
101    }
102
103    pub(crate) fn simulate_active_status_change(&self, active: bool) {
104        let mut lock = self.0.lock();
105        let Some(mut callback) = lock.active_status_change_callback.take() else {
106            return;
107        };
108        drop(lock);
109        callback(active);
110        self.0.lock().active_status_change_callback = Some(callback);
111    }
112
113    pub fn simulate_input(&mut self, event: PlatformInput) -> bool {
114        let mut lock = self.0.lock();
115        let Some(mut callback) = lock.input_callback.take() else {
116            return false;
117        };
118        drop(lock);
119        let result = callback(event);
120        self.0.lock().input_callback = Some(callback);
121        !result.propagate
122    }
123}
124
125impl PlatformWindow for TestWindow {
126    fn bounds(&self) -> Bounds<Pixels> {
127        self.0.lock().bounds
128    }
129
130    fn window_bounds(&self) -> WindowBounds {
131        WindowBounds::Windowed(self.bounds())
132    }
133
134    fn is_maximized(&self) -> bool {
135        false
136    }
137
138    fn content_size(&self) -> Size<Pixels> {
139        self.bounds().size
140    }
141
142    fn resize(&mut self, size: Size<Pixels>) {
143        let mut lock = self.0.lock();
144        lock.bounds.size = size;
145    }
146
147    fn scale_factor(&self) -> f32 {
148        2.0
149    }
150
151    fn appearance(&self) -> WindowAppearance {
152        WindowAppearance::Light
153    }
154
155    fn display(&self) -> Option<std::rc::Rc<dyn crate::PlatformDisplay>> {
156        Some(self.0.lock().display.clone())
157    }
158
159    fn mouse_position(&self) -> Point<Pixels> {
160        Point::default()
161    }
162
163    fn modifiers(&self) -> crate::Modifiers {
164        crate::Modifiers::default()
165    }
166
167    fn capslock(&self) -> crate::Capslock {
168        crate::Capslock::default()
169    }
170
171    fn set_input_handler(&mut self, input_handler: PlatformInputHandler) {
172        self.0.lock().input_handler = Some(input_handler);
173    }
174
175    fn take_input_handler(&mut self) -> Option<PlatformInputHandler> {
176        self.0.lock().input_handler.take()
177    }
178
179    fn prompt(
180        &self,
181        _level: crate::PromptLevel,
182        msg: &str,
183        detail: Option<&str>,
184        answers: &[PromptButton],
185    ) -> Option<futures::channel::oneshot::Receiver<usize>> {
186        Some(
187            self.0
188                .lock()
189                .platform
190                .upgrade()
191                .expect("platform dropped")
192                .prompt(msg, detail, answers),
193        )
194    }
195
196    fn activate(&self) {
197        self.0
198            .lock()
199            .platform
200            .upgrade()
201            .unwrap()
202            .set_active_window(Some(self.clone()))
203    }
204
205    fn is_active(&self) -> bool {
206        false
207    }
208
209    fn is_hovered(&self) -> bool {
210        false
211    }
212
213    fn background_appearance(&self) -> WindowBackgroundAppearance {
214        WindowBackgroundAppearance::Opaque
215    }
216
217    fn is_subpixel_rendering_supported(&self) -> bool {
218        false
219    }
220
221    fn set_title(&mut self, title: &str) {
222        self.0.lock().title = Some(title.to_owned());
223    }
224
225    fn set_app_id(&mut self, _app_id: &str) {}
226
227    fn set_background_appearance(&self, _background: WindowBackgroundAppearance) {}
228
229    fn set_edited(&mut self, edited: bool) {
230        self.0.lock().edited = edited;
231    }
232
233    fn show_character_palette(&self) {
234        unimplemented!()
235    }
236
237    fn minimize(&self) {
238        unimplemented!()
239    }
240
241    fn zoom(&self) {
242        unimplemented!()
243    }
244
245    fn toggle_fullscreen(&self) {
246        let mut lock = self.0.lock();
247        lock.is_fullscreen = !lock.is_fullscreen;
248    }
249
250    fn is_fullscreen(&self) -> bool {
251        self.0.lock().is_fullscreen
252    }
253
254    fn on_request_frame(&self, _callback: Box<dyn FnMut(RequestFrameOptions)>) {}
255
256    fn on_input(&self, callback: Box<dyn FnMut(crate::PlatformInput) -> DispatchEventResult>) {
257        self.0.lock().input_callback = Some(callback)
258    }
259
260    fn on_active_status_change(&self, callback: Box<dyn FnMut(bool)>) {
261        self.0.lock().active_status_change_callback = Some(callback)
262    }
263
264    fn on_hover_status_change(&self, callback: Box<dyn FnMut(bool)>) {
265        self.0.lock().hover_status_change_callback = Some(callback)
266    }
267
268    fn on_resize(&self, callback: Box<dyn FnMut(Size<Pixels>, f32)>) {
269        self.0.lock().resize_callback = Some(callback)
270    }
271
272    fn on_moved(&self, callback: Box<dyn FnMut()>) {
273        self.0.lock().moved_callback = Some(callback)
274    }
275
276    fn on_should_close(&self, callback: Box<dyn FnMut() -> bool>) {
277        self.0.lock().should_close_handler = Some(callback);
278    }
279
280    fn on_close(&self, _callback: Box<dyn FnOnce()>) {}
281
282    fn on_hit_test_window_control(&self, callback: Box<dyn FnMut() -> Option<WindowControlArea>>) {
283        self.0.lock().hit_test_window_control_callback = Some(callback);
284    }
285
286    fn on_appearance_changed(&self, _callback: Box<dyn FnMut()>) {}
287
288    fn draw(&self, _scene: &Scene) {}
289
290    fn sprite_atlas(&self) -> sync::Arc<dyn crate::PlatformAtlas> {
291        self.0.lock().sprite_atlas.clone()
292    }
293
294    #[cfg(any(test, feature = "test-support"))]
295    fn render_to_image(&self, scene: &Scene) -> anyhow::Result<RgbaImage> {
296        let mut state = self.0.lock();
297        let size = state.bounds.size;
298        if let Some(renderer) = &mut state.renderer {
299            let scale_factor = 2.0;
300            let device_size: Size<DevicePixels> = size.to_device_pixels(scale_factor);
301            renderer.render_scene_to_image(scene, device_size)
302        } else {
303            anyhow::bail!("render_to_image not available: no HeadlessRenderer configured")
304        }
305    }
306
307    fn as_test(&mut self) -> Option<&mut TestWindow> {
308        Some(self)
309    }
310
311    #[cfg(target_os = "windows")]
312    fn get_raw_handle(&self) -> windows::Win32::Foundation::HWND {
313        unimplemented!()
314    }
315
316    fn show_window_menu(&self, _position: Point<Pixels>) {
317        unimplemented!()
318    }
319
320    fn start_window_move(&self) {
321        unimplemented!()
322    }
323
324    fn update_ime_position(&self, _bounds: Bounds<Pixels>) {}
325
326    fn gpu_specs(&self) -> Option<GpuSpecs> {
327        None
328    }
329}
330
331pub(crate) struct TestAtlasState {
332    next_id: u32,
333    tiles: HashMap<AtlasKey, AtlasTile>,
334}
335
336pub(crate) struct TestAtlas(Mutex<TestAtlasState>);
337
338impl TestAtlas {
339    pub fn new() -> Self {
340        TestAtlas(Mutex::new(TestAtlasState {
341            next_id: 0,
342            tiles: HashMap::default(),
343        }))
344    }
345}
346
347impl PlatformAtlas for TestAtlas {
348    fn get_or_insert_with<'a>(
349        &self,
350        key: &crate::AtlasKey,
351        build: &mut dyn FnMut() -> anyhow::Result<
352            Option<(Size<crate::DevicePixels>, std::borrow::Cow<'a, [u8]>)>,
353        >,
354    ) -> anyhow::Result<Option<crate::AtlasTile>> {
355        let mut state = self.0.lock();
356        if let Some(tile) = state.tiles.get(key) {
357            return Ok(Some(tile.clone()));
358        }
359        drop(state);
360
361        let Some((size, _)) = build()? else {
362            return Ok(None);
363        };
364
365        let mut state = self.0.lock();
366        state.next_id += 1;
367        let texture_id = state.next_id;
368        state.next_id += 1;
369        let tile_id = state.next_id;
370
371        state.tiles.insert(
372            key.clone(),
373            crate::AtlasTile {
374                texture_id: AtlasTextureId {
375                    index: texture_id,
376                    kind: crate::AtlasTextureKind::Monochrome,
377                },
378                tile_id: TileId(tile_id),
379                padding: 0,
380                bounds: crate::Bounds {
381                    origin: Point::default(),
382                    size,
383                },
384            },
385        );
386
387        Ok(Some(state.tiles[key].clone()))
388    }
389
390    fn remove(&self, key: &AtlasKey) {
391        let mut state = self.0.lock();
392        state.tiles.remove(key);
393    }
394}