window.rs

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