@@ -187,6 +187,59 @@ impl WindowResizeConstraints {
187
187
/// }
188
188
/// }
189
189
/// ```
190
+ /// To test code that uses `Window`s, one can test it with varying `Window` parameters by
191
+ /// creating `WindowResizeConstraints` or `WindowDescriptor` structures.
192
+ /// values by setting
193
+ ///
194
+ /// ```
195
+ /// # use bevy_utils::default;
196
+ /// # use bevy_window::{Window, WindowCommand, WindowDescriptor, WindowId, WindowResizeConstraints};
197
+ /// # fn compute_window_area(w: &Window) -> f32 {
198
+ /// # w.width() * w.height()
199
+ /// # }
200
+ /// # fn grow_window_to_text_size(_window: &mut Window, _text: &str) {}
201
+ /// # fn set_new_title(window: &mut Window, text: String) { window.set_title(text); }
202
+ /// # fn a_window_resize_test() {
203
+ /// let resize_constraints = WindowResizeConstraints {
204
+ /// min_width: 400.0,
205
+ /// min_height: 300.0,
206
+ /// max_width: 1280.0,
207
+ /// max_height: 1024.0,
208
+ /// };
209
+ /// let window_descriptor = WindowDescriptor {
210
+ /// width: 800.0,
211
+ /// height: 600.0,
212
+ /// resizable: true,
213
+ /// resize_constraints,
214
+ /// ..default()
215
+ /// };
216
+ /// let mut window = Window::new(
217
+ /// WindowId::new(),
218
+ /// &window_descriptor,
219
+ /// 100, // physical_width
220
+ /// 100, // physical_height
221
+ /// 1.0, // scale_factor
222
+ /// None, None);
223
+ ///
224
+ /// let area = compute_window_area(&window);
225
+ /// assert_eq!(area, 100.0 * 100.0);
226
+ ///
227
+ /// grow_window_to_text_size(&mut window, "very long text that does not wrap");
228
+ /// assert_eq!(window.physical_width(), window.requested_width() as u32);
229
+ /// grow_window_to_text_size(&mut window, "very long text that does wrap, creating a maximum width window");
230
+ /// assert_eq!(window.physical_width(), window.requested_width() as u32);
231
+ ///
232
+ /// set_new_title(&mut window, "new title".to_string());
233
+ /// let mut found_command = false;
234
+ /// for command in window.drain_commands() {
235
+ /// if command == (WindowCommand::SetTitle{ title: "new title".to_string() }) {
236
+ /// found_command = true;
237
+ /// break;
238
+ /// }
239
+ /// }
240
+ /// assert_eq!(found_command, true);
241
+ /// }
242
+ /// ```
190
243
#[ derive( Debug ) ]
191
244
pub struct Window {
192
245
id : WindowId ,
@@ -206,7 +259,7 @@ pub struct Window {
206
259
cursor_visible : bool ,
207
260
cursor_locked : bool ,
208
261
physical_cursor_position : Option < DVec2 > ,
209
- raw_window_handle : RawWindowHandleWrapper ,
262
+ raw_window_handle : Option < RawWindowHandleWrapper > ,
210
263
focused : bool ,
211
264
mode : WindowMode ,
212
265
canvas : Option < String > ,
@@ -217,7 +270,7 @@ pub struct Window {
217
270
///
218
271
/// Bevy apps don't interact with this `enum` directly. Instead, they should use the methods on [`Window`].
219
272
/// This `enum` is meant for authors of windowing plugins. See the documentation on [`crate::WindowPlugin`] for more information.
220
- #[ derive( Debug ) ]
273
+ #[ derive( Debug , PartialEq ) ]
221
274
#[ cfg_attr( feature = "serialize" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
222
275
pub enum WindowCommand {
223
276
/// Set the window's [`WindowMode`].
@@ -315,7 +368,7 @@ impl Window {
315
368
physical_height : u32 ,
316
369
scale_factor : f64 ,
317
370
position : Option < IVec2 > ,
318
- raw_window_handle : RawWindowHandle ,
371
+ raw_window_handle : Option < RawWindowHandle > ,
319
372
) -> Self {
320
373
Window {
321
374
id,
@@ -335,7 +388,7 @@ impl Window {
335
388
cursor_locked : window_descriptor. cursor_locked ,
336
389
cursor_icon : CursorIcon :: Default ,
337
390
physical_cursor_position : None ,
338
- raw_window_handle : RawWindowHandleWrapper :: new ( raw_window_handle ) ,
391
+ raw_window_handle : raw_window_handle . map ( RawWindowHandleWrapper :: new) ,
339
392
focused : true ,
340
393
mode : window_descriptor. mode ,
341
394
canvas : window_descriptor. canvas . clone ( ) ,
@@ -719,9 +772,11 @@ impl Window {
719
772
pub fn is_focused ( & self ) -> bool {
720
773
self . focused
721
774
}
722
- /// Get the [`RawWindowHandleWrapper`] corresponding to this window
723
- pub fn raw_window_handle ( & self ) -> RawWindowHandleWrapper {
724
- self . raw_window_handle . clone ( )
775
+ /// Get the [`RawWindowHandleWrapper`] corresponding to this window if set.
776
+ ///
777
+ /// During normal use, this can be safely unwrapped; the value should only be [`None`] when synthetically constructed for tests.
778
+ pub fn raw_window_handle ( & self ) -> Option < RawWindowHandleWrapper > {
779
+ self . raw_window_handle . as_ref ( ) . cloned ( )
725
780
}
726
781
727
782
/// The "html canvas" element selector.
0 commit comments