window_manager_demo/
window_manager_demo.rs

1///! WindowManager example demonstrating simplified multi-window management.
2///!
3///! This example creates 3 windows using the WindowManager abstraction:
4///! - Red window
5///! - Green window
6///! - Blue window
7///!
8///! WindowManager automatically handles:
9///! - Window resize events
10///! - Graphics context sharing
11///! - HashMap boilerplate elimination
12///!
13///! Compare this to multi_window.rs to see the boilerplate reduction!
14
15use astrelis_core::logging;
16use astrelis_render::{
17    Color, GraphicsContext, RenderTarget, WindowContextDescriptor, WindowManager,
18};
19use astrelis_winit::{
20    FrameTime,
21    WindowId,
22    app::{run_app, App, AppCtx},
23    event::EventBatch,
24    window::{WindowBackend, WindowDescriptor, WinitPhysicalSize},
25};
26use std::collections::HashMap;
27
28struct WindowManagerApp {
29    window_manager: WindowManager,
30    window_colors: HashMap<WindowId, Color>,
31}
32
33fn main() {
34    logging::init();
35
36    run_app(|ctx| {
37        let graphics_ctx = GraphicsContext::new_owned_sync_or_panic();
38        let mut window_manager = WindowManager::new(graphics_ctx);
39        let mut window_colors = HashMap::new();
40
41        // Create 3 windows with different colors
42        let colors = [
43            Color::rgb(0.8, 0.2, 0.2), // Red
44            Color::rgb(0.2, 0.8, 0.2), // Green
45            Color::rgb(0.2, 0.2, 0.8), // Blue
46        ];
47
48        for (i, color) in colors.iter().enumerate() {
49            let window_id = window_manager.create_window_with_descriptor(
50                ctx,
51                WindowDescriptor {
52                    title: format!("Window {} - WindowManager Demo", i + 1),
53                    size: Some(WinitPhysicalSize::new(400.0, 300.0)),
54                    ..Default::default()
55                },
56                WindowContextDescriptor {
57                    format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
58                    ..Default::default()
59                },
60            ).expect("Failed to create window");
61
62            window_colors.insert(window_id, *color);
63        }
64
65        Box::new(WindowManagerApp {
66            window_manager,
67            window_colors,
68        })
69    });
70}
71
72impl App for WindowManagerApp {
73    fn update(&mut self, _ctx: &mut AppCtx, _time: &FrameTime) {
74        // Global logic - called once per frame
75        // (none needed for this example)
76    }
77
78    fn render(&mut self, _ctx: &mut AppCtx, window_id: WindowId, events: &mut EventBatch) {
79        // Get the color for this window
80        let Some(&color) = self.window_colors.get(&window_id) else {
81            return;
82        };
83
84        // WindowManager automatically handles:
85        // 1. Window lookup (no manual HashMap.get_mut)
86        // 2. Resize events (automatic)
87        // 3. Event dispatching
88        self.window_manager
89            .render_window(window_id, events, |window, _events| {
90                // No need to manually handle resize events!
91                // WindowManager already did that for us
92
93                // Just render!
94                let mut frame = window.begin_drawing();
95
96                frame.clear_and_render(RenderTarget::Surface, color, |_pass| {
97                    // Additional rendering would go here
98                });
99
100                frame.finish();
101            });
102    }
103}