nannou 0.20.0

A Creative Coding Framework for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//! The nannou Window API.
//!
//! Create a new window via `app.new_window()`. This produces a [**Builder**](Builder) which can be
//! used to build a window and obtain its [`Entity`].

use std::{fmt, path::PathBuf};

use crate::context::App;
use crate::prelude::{MonitorSelection, render::NannouCamera};
use crate::{geom::Point2, glam::Vec2, prelude::WindowResizeConstraints};
use bevy::{
    camera::Hdr,
    camera::RenderTarget,
    camera::visibility::RenderLayers,
    input::mouse::MouseWheel,
    prelude::*,
    render::extract_component::ExtractComponent,
    window::{PresentMode, PrimaryWindow, WindowLevel, WindowRef},
};

/// A context for building a window.
pub struct Builder<'a, 'w, 's, M = ()> {
    app: &'a App<'w, 's>,
    window: bevy::window::Window,
    // TODO: make cameras and lights an array
    camera: Option<Entity>,
    light: Option<Entity>,
    primary: bool,
    user_functions: UserFunctions<M>,
    clear_color: Option<Color>,
    hdr: bool,
}

/// For storing all user functions within the window.
#[derive(Debug)]
pub(crate) struct UserFunctions<M> {
    pub(crate) view: Option<View<M>>,
    pub(crate) key_pressed: Option<KeyPressedFn<M>>,
    pub(crate) key_released: Option<KeyReleasedFn<M>>,
    pub(crate) received_character: Option<ReceivedCharacterFn<M>>,
    pub(crate) mouse_moved: Option<MouseMovedFn<M>>,
    pub(crate) mouse_pressed: Option<MousePressedFn<M>>,
    pub(crate) mouse_released: Option<MouseReleasedFn<M>>,
    pub(crate) mouse_entered: Option<MouseEnteredFn<M>>,
    pub(crate) mouse_exited: Option<MouseExitedFn<M>>,
    pub(crate) mouse_wheel: Option<MouseWheelFn<M>>,
    pub(crate) moved: Option<MovedFn<M>>,
    pub(crate) resized: Option<ResizedFn<M>>,
    pub(crate) touch: Option<TouchFn<M>>,
    // pub(crate) touchpad_pressure: Option<TouchpadPressureFn<M>>,
    pub(crate) hovered_file: Option<HoveredFileFn<M>>,
    pub(crate) hovered_file_cancelled: Option<HoveredFileCancelledFn<M>>,
    pub(crate) dropped_file: Option<DroppedFileFn<M>>,
    pub(crate) focused: Option<FocusedFn<M>>,
    pub(crate) unfocused: Option<UnfocusedFn<M>>,
    pub(crate) closed: Option<ClosedFn<M>>,
}

impl<M> Default for UserFunctions<M> {
    fn default() -> Self {
        UserFunctions {
            view: None,
            key_pressed: None,
            key_released: None,
            received_character: None,
            mouse_moved: None,
            mouse_pressed: None,
            mouse_released: None,
            mouse_entered: None,
            mouse_exited: None,
            mouse_wheel: None,
            moved: None,
            resized: None,
            touch: None,
            // touchpad_pressure: None,
            hovered_file: None,
            hovered_file_cancelled: None,
            dropped_file: None,
            focused: None,
            unfocused: None,
            closed: None,
        }
    }
}

impl<M> Clone for UserFunctions<M> {
    fn clone(&self) -> Self {
        UserFunctions {
            view: self.view.clone(),
            key_pressed: self.key_pressed,
            key_released: self.key_released,
            received_character: self.received_character,
            mouse_moved: self.mouse_moved,
            mouse_pressed: self.mouse_pressed,
            mouse_released: self.mouse_released,
            mouse_entered: self.mouse_entered,
            mouse_exited: self.mouse_exited,
            mouse_wheel: self.mouse_wheel,
            moved: self.moved,
            resized: self.resized,
            touch: self.touch,
            // touchpad_pressure: self.touchpad_pressure,
            hovered_file: self.hovered_file,
            hovered_file_cancelled: self.hovered_file_cancelled,
            dropped_file: self.dropped_file,
            focused: self.focused,
            unfocused: self.unfocused,
            closed: self.closed,
        }
    }
}

#[derive(Component, Deref, DerefMut, ExtractComponent)]
pub(crate) struct WindowUserFunctions<M: 'static>(pub(crate) UserFunctions<M>);

/// The user function type for drawing their model to the surface of a single window.
pub type ViewFn<Model> = fn(&App<'_, '_>, &Model);

/// The same as `ViewFn`, but provides no user model to draw from.
///
/// Useful for simple, stateless sketching.
pub type SketchFn = fn(&App<'_, '_>);

/// The user's view function, whether with a model or without one.
pub(crate) enum View<M> {
    WithModel(ViewFn<M>),
    Sketch(SketchFn),
}

impl<M> Clone for View<M> {
    fn clone(&self) -> Self {
        match self {
            View::WithModel(f) => View::WithModel(*f),
            View::Sketch(f) => View::Sketch(*f),
        }
    }
}

/// A function for processing key press events.
pub type KeyPressedFn<Model> = fn(&App<'_, '_>, &mut Model, KeyCode);

/// A function for processing key release events.
pub type KeyReleasedFn<Model> = fn(&App<'_, '_>, &mut Model, KeyCode);

/// A function for processing received characters.
pub type ReceivedCharacterFn<Model> = fn(&App<'_, '_>, &mut Model, char);

/// A function for processing mouse moved events.
pub type MouseMovedFn<Model> = fn(&App<'_, '_>, &mut Model, Point2);

/// A function for processing mouse pressed events.
pub type MousePressedFn<Model> = fn(&App<'_, '_>, &mut Model, MouseButton);

/// A function for processing mouse released events.
pub type MouseReleasedFn<Model> = fn(&App<'_, '_>, &mut Model, MouseButton);

/// A function for processing mouse entered events.
pub type MouseEnteredFn<Model> = fn(&App<'_, '_>, &mut Model);

/// A function for processing mouse exited events.
pub type MouseExitedFn<Model> = fn(&App<'_, '_>, &mut Model);

/// A function for processing mouse wheel events.
pub type MouseWheelFn<Model> = fn(&App<'_, '_>, &mut Model, MouseWheel);

/// A function for processing window moved events.
pub type MovedFn<Model> = fn(&App<'_, '_>, &mut Model, IVec2);

/// A function for processing window resized events.
pub type ResizedFn<Model> = fn(&App<'_, '_>, &mut Model, Vec2);

/// A function for processing touch events.
pub type TouchFn<Model> = fn(&App<'_, '_>, &mut Model, TouchInput);

// https://github.com/bevyengine/bevy/issues/6174
// A function for processing touchpad pressure events.
// pub type TouchpadPressureFn<Model> = fn(&App, &mut Model, TouchpadPressure);

/// A function for processing hovered file events.
pub type HoveredFileFn<Model> = fn(&App<'_, '_>, &mut Model, PathBuf);

/// A function for processing hovered file cancelled events.
pub type HoveredFileCancelledFn<Model> = fn(&App<'_, '_>, &mut Model);

/// A function for processing dropped file events.
pub type DroppedFileFn<Model> = fn(&App<'_, '_>, &mut Model, PathBuf);

/// A function for processing window focused events.
pub type FocusedFn<Model> = fn(&App<'_, '_>, &mut Model);

/// A function for processing window unfocused events.
pub type UnfocusedFn<Model> = fn(&App<'_, '_>, &mut Model);

/// A function for processing window closed events.
pub type ClosedFn<Model> = fn(&App<'_, '_>, &mut Model);

/// The default [`PresentMode`] used for nannou windows.
///
/// [`Mailbox`](PresentMode::Mailbox) ("fast vsync") gives low latency without
/// tearing. Bevy falls back gracefully (`Mailbox` -> `Immediate` -> `Fifo`) on
/// surfaces that don't support it, so it is safe everywhere.
pub const DEFAULT_PRESENT_MODE: PresentMode = PresentMode::Mailbox;

impl<'a, 'w, 's, M> Builder<'a, 'w, 's, M>
where
    M: 'static,
{
    /// Begin building a new window.
    pub fn new(app: &'a App<'w, 's>) -> Self {
        Builder {
            app,
            window: bevy::window::Window {
                present_mode: DEFAULT_PRESENT_MODE,
                ..bevy::window::Window::default()
            },
            camera: None,
            light: None,
            primary: false,
            user_functions: UserFunctions::<M>::default(),
            clear_color: None,
            hdr: false,
        }
    }

    /// Build the window with some custom window parameters.
    pub fn window(mut self, window: bevy::window::Window) -> Self {
        self.window = window;
        self
    }

    /// Provide a simple function for drawing to the window.
    ///
    /// This is similar to `view` but does not provide access to user data via a Model type. This
    /// is useful for sketches where you don't require tracking any state.
    pub fn sketch(mut self, sketch_fn: SketchFn) -> Self {
        self.user_functions.view = Some(View::Sketch(sketch_fn));
        self
    }

    /// The **view** function that the app will call to allow you to present your Model to the
    /// surface of the window on your display.
    pub fn view(mut self, view_fn: ViewFn<M>) -> Self {
        self.user_functions.view = Some(View::WithModel(view_fn));
        self
    }

    /// Set the initial color of the window background
    /// when its contents are invalidated, e.g. upon window resize.
    pub fn clear_color<C>(mut self, color: C) -> Self
    where
        C: Into<Color>,
    {
        let color = color.into();
        self.clear_color = Some(color);
        self
    }

    /// A function for processing key press events associated with this window.
    pub fn key_pressed(mut self, f: KeyPressedFn<M>) -> Self {
        self.user_functions.key_pressed = Some(f);
        self
    }

    /// A function for processing key release events associated with this window.
    pub fn key_released(mut self, f: KeyReleasedFn<M>) -> Self {
        self.user_functions.key_released = Some(f);
        self
    }

    pub fn received_character(mut self, f: ReceivedCharacterFn<M>) -> Self {
        self.user_functions.received_character = Some(f);
        self
    }

    /// A function for processing mouse moved events associated with this window.
    pub fn mouse_moved(mut self, f: MouseMovedFn<M>) -> Self {
        self.user_functions.mouse_moved = Some(f);
        self
    }

    /// A function for processing mouse pressed events associated with this window.
    pub fn mouse_pressed(mut self, f: MousePressedFn<M>) -> Self {
        self.user_functions.mouse_pressed = Some(f);
        self
    }

    /// A function for processing mouse released events associated with this window.
    pub fn mouse_released(mut self, f: MouseReleasedFn<M>) -> Self {
        self.user_functions.mouse_released = Some(f);
        self
    }

    /// A function for processing mouse wheel events associated with this window.
    pub fn mouse_wheel(mut self, f: MouseWheelFn<M>) -> Self {
        self.user_functions.mouse_wheel = Some(f);
        self
    }

    /// A function for processing mouse entered events associated with this window.
    pub fn mouse_entered(mut self, f: MouseEnteredFn<M>) -> Self {
        self.user_functions.mouse_entered = Some(f);
        self
    }

    /// A function for processing mouse exited events associated with this window.
    pub fn mouse_exited(mut self, f: MouseExitedFn<M>) -> Self {
        self.user_functions.mouse_exited = Some(f);
        self
    }

    /// A function for processing touch events associated with this window.
    pub fn touch(mut self, f: TouchFn<M>) -> Self {
        self.user_functions.touch = Some(f);
        self
    }

    /// A function for processing window moved events associated with this window.
    pub fn moved(mut self, f: MovedFn<M>) -> Self {
        self.user_functions.moved = Some(f);
        self
    }

    /// A function for processing window resized events associated with this window.
    pub fn resized(mut self, f: ResizedFn<M>) -> Self {
        self.user_functions.resized = Some(f);
        self
    }

    /// A function for processing hovered file events associated with this window.
    pub fn hovered_file(mut self, f: HoveredFileFn<M>) -> Self {
        self.user_functions.hovered_file = Some(f);
        self
    }

    /// A function for processing hovered file cancelled events associated with this window.
    pub fn hovered_file_cancelled(mut self, f: HoveredFileCancelledFn<M>) -> Self {
        self.user_functions.hovered_file_cancelled = Some(f);
        self
    }

    /// A function for processing dropped file events associated with this window.
    pub fn dropped_file(mut self, f: DroppedFileFn<M>) -> Self {
        self.user_functions.dropped_file = Some(f);
        self
    }

    /// A function for processing the focused event associated with this window.
    pub fn focused(mut self, f: FocusedFn<M>) -> Self {
        self.user_functions.focused = Some(f);
        self
    }

    /// A function for processing the unfocused event associated with this window.
    pub fn unfocused(mut self, f: UnfocusedFn<M>) -> Self {
        self.user_functions.unfocused = Some(f);
        self
    }

    /// A function for processing the window closed event associated with this window.
    pub fn closed(mut self, f: ClosedFn<M>) -> Self {
        self.user_functions.closed = Some(f);
        self
    }

    pub fn hdr(mut self, hdr: bool) -> Self {
        self.hdr = hdr;
        self
    }

    /// Builds the window and its camera, returning the window's [`Entity`].
    ///
    /// The window and camera are spawned via the `App`'s deferred command queue, so they become
    /// available on the following frame. The returned [`Entity`] is reserved immediately and is
    /// safe to store and use straight away.
    pub fn build(self) -> Entity {
        let Builder {
            app,
            window,
            camera,
            light,
            primary,
            user_functions,
            clear_color,
            hdr,
        } = self;

        if cfg!(target_arch = "wasm32") && !primary {
            // TODO: figure out a way to dynamically attach to a canvas with new windows.
            panic!("Non-primary windows are not supported on wasm");
        }

        let layer = RenderLayers::layer(app.window_count());
        let user_functions = WindowUserFunctions(user_functions);
        // Remember the window so it can be read back before the deferred spawn is applied.
        let window_for_cache = window.clone();
        let half_z_range = default_camera_half_z_range(&window_for_cache);

        // On wasm we reuse the existing primary window (created up-front so the renderer has a
        // canvas) rather than spawning a new one.
        #[cfg(target_arch = "wasm32")]
        let existing_primary = Some(app.main_window().id());
        #[cfg(not(target_arch = "wasm32"))]
        let existing_primary: Option<Entity> = None;

        let window_entity = app.command_scope(move |mut commands| {
            let window_entity = match existing_primary {
                Some(entity) => {
                    commands
                        .entity(entity)
                        .insert((window, user_functions, layer.clone()));
                    entity
                }
                None => {
                    let mut window = commands.spawn((window, user_functions, layer.clone()));
                    if primary {
                        window.insert(PrimaryWindow);
                    }
                    window.id()
                }
            };

            match camera {
                // Point an existing camera at the new window.
                Some(camera) => {
                    commands.entity(camera).insert((
                        RenderTarget::Window(WindowRef::Entity(window_entity)),
                        layer.clone(),
                    ));
                }
                // Otherwise spawn a default camera that renders to the window.
                None => {
                    let mut camera = commands.spawn((
                        Camera {
                            clear_color: clear_color
                                .map(ClearColorConfig::Custom)
                                .unwrap_or(ClearColorConfig::None),
                            ..default()
                        },
                        RenderTarget::Window(WindowRef::Entity(window_entity)),
                        Transform::from_xyz(0.0, 0.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
                        Projection::Orthographic(OrthographicProjection {
                            near: -half_z_range,
                            far: half_z_range,
                            ..OrthographicProjection::default_3d()
                        }),
                        layer.clone(),
                        NannouCamera,
                        WindowDefaultCamera,
                    ));
                    if hdr {
                        camera.insert(Hdr);
                    }
                }
            }

            if let Some(light) = light {
                commands.entity(light).insert(layer.clone());
            }

            window_entity
        });
        app.record_pending_window(window_entity, primary, window_for_cache);
        window_entity
    }

    fn map_window<F>(self, map: F) -> Self
    where
        F: FnOnce(bevy::window::Window) -> bevy::window::Window,
    {
        Builder {
            window: map(self.window),
            ..self
        }
    }

    /// The camera to use for rendering to this window. Setting the camera will override the
    /// default camera that would be created for this window. The camera's render target
    /// will be set to the window when the window is built.
    pub fn camera(mut self, camera: Entity) -> Self {
        self.camera = Some(camera);
        self
    }

    pub fn light(mut self, light: Entity) -> Self {
        self.light = Some(light);
        self
    }

    /// Requests the window to be a specific size in points.
    ///
    /// This describes to the "inner" part of the window, not including desktop decorations like the
    /// title bar.
    pub fn size(self, width: u32, height: u32) -> Self {
        self.map_window(|mut w| {
            w.resolution.set(width as f32, height as f32);
            w
        })
    }

    /// Set the minimum size in points for the window.
    pub fn min_size(self, width: f32, height: f32) -> Self {
        self.map_window(|mut w| {
            w.resize_constraints = WindowResizeConstraints {
                min_width: width,
                min_height: height,
                ..w.resize_constraints
            };
            w
        })
    }

    /// Set the maximum size in points for the window.
    pub fn max_size(self, width: f32, height: f32) -> Self {
        self.map_window(|mut w| {
            w.resize_constraints = WindowResizeConstraints {
                max_width: width,
                max_height: height,
                ..w.resize_constraints
            };
            w
        })
    }

    /// Requests the window to be a specific size in pixels.
    ///
    /// This describes to the "inner" part of the window, not including desktop decorations like the
    /// title bar.
    pub fn size_pixels(self, width: u32, height: u32) -> Self {
        self.map_window(|mut w| {
            w.resolution.set_physical_resolution(width, height);
            w
        })
    }

    /// The [`PresentMode`] (a.k.a. swapchain / vsync mode) to use for this window.
    ///
    /// Defaults to [`DEFAULT_PRESENT_MODE`]. Bevy falls back gracefully on
    /// surfaces that don't support the requested mode, so any variant is safe
    /// to request.
    ///
    /// Note that non-blocking modes such as [`PresentMode::Mailbox`] and
    /// [`PresentMode::Immediate`] remove the vblank backpressure that throttles
    /// the update/draw loop, so they may increase CPU/GPU usage unless the frame
    /// rate is otherwise limited. Use [`PresentMode::Fifo`] for traditional
    /// "vsync on" behaviour.
    pub fn present_mode(self, present_mode: PresentMode) -> Self {
        self.map_window(|mut w| {
            w.present_mode = present_mode;
            w
        })
    }

    /// Whether or not the window should be resizable after creation.
    pub fn resizable(self, resizable: bool) -> Self {
        self.map_window(|mut w| {
            w.resizable = resizable;
            w
        })
    }

    /// Requests a specific title for the window.
    pub fn title<T>(self, title: T) -> Self
    where
        T: Into<String>,
    {
        self.map_window(|mut w| {
            w.title = title.into();
            w
        })
    }

    pub fn primary(mut self) -> Self {
        self.primary = true;
        self
    }

    /// Create the window fullscreened on the current monitor.
    pub fn fullscreen(self) -> Self {
        self.map_window(|mut w| {
            w.position = WindowPosition::Centered(MonitorSelection::Primary);
            w
        })
    }

    /// Move the window to the center of the given monitor.
    pub fn monitor(self, monitor: MonitorSelection) -> Self {
        self.map_window(|mut w| {
            w.position = WindowPosition::Centered(monitor);
            w
        })
    }

    /// Requests maximized mode.
    pub fn maximized(self, maximized: bool) -> Self {
        self.map_window(|mut w| {
            w.set_maximized(maximized);
            w
        })
    }

    /// Sets whether the window will be initially hidden or visible.
    pub fn visible(self, visible: bool) -> Self {
        self.map_window(|mut w| {
            w.visible = visible;
            w
        })
    }

    /// Sets whether the background of the window should be transparent.
    pub fn transparent(self, transparent: bool) -> Self {
        self.map_window(|mut w| {
            w.transparent = transparent;
            w
        })
    }

    /// Sets whether the window should have a border, a title bar, etc.
    pub fn decorations(self, decorations: bool) -> Self {
        self.map_window(|mut w| {
            w.decorations = decorations;
            w
        })
    }

    /// Sets whether or not the window will always be on top of other windows.
    pub fn always_on_top(self, always_on_top: bool) -> Self {
        self.map_window(|mut w| {
            w.window_level = if always_on_top {
                WindowLevel::AlwaysOnTop
            } else {
                WindowLevel::Normal
            };
            w
        })
    }
}

impl<M> fmt::Debug for View<M> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let variant = match *self {
            View::WithModel(ref v) => format!("WithModel({:?})", v),
            View::Sketch(_) => "Sketch".to_string(),
        };

        write!(f, "View::{}", variant)
    }
}

/// Marks the default camera spawned for a window when none was provided via the window
/// builder. Nannou manages this camera's orthographic projection to match its window.
#[derive(Component)]
pub(crate) struct WindowDefaultCamera;

// The half-extent of the z range visible to a window's default camera, matching the
// pre-bevy renderer: content within +-max(width, height) of the xy plane is visible.
fn default_camera_half_z_range(window: &bevy::window::Window) -> f32 {
    window.width().max(window.height())
}

/// Keep each default window camera's orthographic z range sized to its window, so that
/// 3D draw content up to the size of the window renders without near/far clipping while
/// depth precision stays proportionate to the scene.
pub(crate) fn update_default_camera_z_range(
    mut cameras: Query<(&mut Projection, &RenderTarget), With<WindowDefaultCamera>>,
    windows: Query<&bevy::window::Window>,
    primary_window: Query<Entity, With<PrimaryWindow>>,
) {
    for (mut projection, target) in cameras.iter_mut() {
        let window = match target {
            RenderTarget::Window(WindowRef::Entity(entity)) => *entity,
            RenderTarget::Window(WindowRef::Primary) => match primary_window.single() {
                Ok(entity) => entity,
                Err(_) => continue,
            },
            _ => continue,
        };
        let Ok(window) = windows.get(window) else {
            continue;
        };
        let half_z_range = default_camera_half_z_range(window);
        let Projection::Orthographic(ortho) = projection.as_ref() else {
            continue;
        };
        // Only write through the `Mut` when the range actually changes, to avoid
        // triggering change detection every frame.
        if ortho.near != -half_z_range || ortho.far != half_z_range {
            if let Projection::Orthographic(ortho) = &mut *projection {
                ortho.near = -half_z_range;
                ortho.far = half_z_range;
            }
        }
    }
}