pub struct WindowManager { /* private fields */ }Expand description
Manages multiple renderable windows with automatic event handling.
The WindowManager eliminates the boilerplate of manually managing a
HashMap<WindowId, RenderableWindow> and handling common events like resizing.
§Example
use astrelis_render::{WindowManager, GraphicsContext, RenderTarget, Color};
use astrelis_winit::app::{App, AppCtx};
use astrelis_winit::{WindowId, FrameTime};
use astrelis_winit::window::WindowBackend;
use astrelis_winit::event::EventBatch;
struct MyApp {
window_manager: WindowManager,
}
impl App for MyApp {
fn update(&mut self, _ctx: &mut AppCtx, _time: &FrameTime) {}
fn render(&mut self, _ctx: &mut AppCtx, window_id: WindowId, events: &mut EventBatch) {
self.window_manager.render_window(window_id, events, |window, _events| {
// Resize already handled automatically!
let mut frame = window.begin_drawing();
frame.clear_and_render(RenderTarget::Surface, Color::BLACK, |_pass| {
// Your rendering here
});
frame.finish();
});
}
}Implementations§
Source§impl WindowManager
impl WindowManager
Sourcepub fn new(graphics: Arc<GraphicsContext>) -> Self
pub fn new(graphics: Arc<GraphicsContext>) -> Self
Creates a new WindowManager with the given graphics context.
§Example
use astrelis_render::{WindowManager, GraphicsContext};
use std::sync::Arc;
let graphics = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
let window_manager = WindowManager::new(graphics);Examples found in repository?
41fn main() {
42 logging::init();
43
44 run_app(|ctx| {
45 let graphics_ctx = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
46 let mut window_manager = WindowManager::new(graphics_ctx);
47 let mut window_colors = HashMap::new();
48
49 // Create 3 windows with different colors
50 let colors = [
51 Color::rgb(0.8, 0.2, 0.2), // Red
52 Color::rgb(0.2, 0.8, 0.2), // Green
53 Color::rgb(0.2, 0.2, 0.8), // Blue
54 ];
55
56 for (i, color) in colors.iter().enumerate() {
57 let window_id = window_manager.create_window_with_descriptor(
58 ctx,
59 WindowDescriptor {
60 title: format!("Window {} - WindowManager Demo", i + 1),
61 size: Some(WinitPhysicalSize::new(400.0, 300.0)),
62 ..Default::default()
63 },
64 WindowContextDescriptor {
65 format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
66 ..Default::default()
67 },
68 ).expect("Failed to create window");
69
70 window_colors.insert(window_id, *color);
71 }
72
73 Box::new(WindowManagerApp {
74 window_manager,
75 window_colors,
76 })
77 });
78}Sourcepub fn create_window(
&mut self,
ctx: &mut AppCtx<'_>,
descriptor: WindowDescriptor,
) -> Result<WindowId, GraphicsError>
pub fn create_window( &mut self, ctx: &mut AppCtx<'_>, descriptor: WindowDescriptor, ) -> Result<WindowId, GraphicsError>
Creates a new window and adds it to the manager.
Returns the WindowId of the created window.
§Example
use astrelis_render::WindowManager;
use astrelis_winit::window::{WindowDescriptor, WindowBackend};
let window_id = window_manager.create_window(
ctx,
WindowDescriptor {
title: "My Window".to_string(),
..Default::default()
},
);Sourcepub fn create_window_with_descriptor(
&mut self,
ctx: &mut AppCtx<'_>,
descriptor: WindowDescriptor,
window_descriptor: WindowContextDescriptor,
) -> Result<WindowId, GraphicsError>
pub fn create_window_with_descriptor( &mut self, ctx: &mut AppCtx<'_>, descriptor: WindowDescriptor, window_descriptor: WindowContextDescriptor, ) -> Result<WindowId, GraphicsError>
Creates a new window with a custom rendering context descriptor.
§Example
use astrelis_render::{WindowManager, WindowContextDescriptor, wgpu};
use astrelis_winit::window::{WindowDescriptor, WindowBackend};
let window_id = window_manager.create_window_with_descriptor(
ctx,
WindowDescriptor::default(),
WindowContextDescriptor {
present_mode: Some(wgpu::PresentMode::Mailbox),
..Default::default()
},
);Examples found in repository?
41fn main() {
42 logging::init();
43
44 run_app(|ctx| {
45 let graphics_ctx = GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
46 let mut window_manager = WindowManager::new(graphics_ctx);
47 let mut window_colors = HashMap::new();
48
49 // Create 3 windows with different colors
50 let colors = [
51 Color::rgb(0.8, 0.2, 0.2), // Red
52 Color::rgb(0.2, 0.8, 0.2), // Green
53 Color::rgb(0.2, 0.2, 0.8), // Blue
54 ];
55
56 for (i, color) in colors.iter().enumerate() {
57 let window_id = window_manager.create_window_with_descriptor(
58 ctx,
59 WindowDescriptor {
60 title: format!("Window {} - WindowManager Demo", i + 1),
61 size: Some(WinitPhysicalSize::new(400.0, 300.0)),
62 ..Default::default()
63 },
64 WindowContextDescriptor {
65 format: Some(wgpu::TextureFormat::Bgra8UnormSrgb),
66 ..Default::default()
67 },
68 ).expect("Failed to create window");
69
70 window_colors.insert(window_id, *color);
71 }
72
73 Box::new(WindowManagerApp {
74 window_manager,
75 window_colors,
76 })
77 });
78}Sourcepub fn get_window(&self, id: WindowId) -> Option<&RenderableWindow>
pub fn get_window(&self, id: WindowId) -> Option<&RenderableWindow>
Gets a reference to a window by its ID.
Returns None if the window doesn’t exist.
Sourcepub fn get_window_mut(&mut self, id: WindowId) -> Option<&mut RenderableWindow>
pub fn get_window_mut(&mut self, id: WindowId) -> Option<&mut RenderableWindow>
Gets a mutable reference to a window by its ID.
Returns None if the window doesn’t exist.
Sourcepub fn remove_window(&mut self, id: WindowId) -> Option<RenderableWindow>
pub fn remove_window(&mut self, id: WindowId) -> Option<RenderableWindow>
Removes a window from the manager.
Returns the removed window if it existed.
Sourcepub fn window_count(&self) -> usize
pub fn window_count(&self) -> usize
Returns the number of windows being managed.
Sourcepub fn window_ids(&self) -> impl Iterator<Item = WindowId> + '_
pub fn window_ids(&self) -> impl Iterator<Item = WindowId> + '_
Returns an iterator over all window IDs.
Sourcepub fn render_window<F>(
&mut self,
id: WindowId,
events: &mut EventBatch,
render_fn: F,
)
pub fn render_window<F>( &mut self, id: WindowId, events: &mut EventBatch, render_fn: F, )
Renders a window with automatic event handling.
This method:
- Automatically handles common events (resize, etc.)
- Calls your render closure with the window and remaining events
- Returns immediately if the window doesn’t exist
§Example
use astrelis_render::{WindowManager, RenderTarget, Color};
use astrelis_winit::window::WindowBackend;
window_manager.render_window(window_id, events, |window, events| {
// Handle custom events if needed
events.dispatch(|_event| {
// Your event handling
astrelis_winit::event::HandleStatus::ignored()
});
// Render
let mut frame = window.begin_drawing();
frame.clear_and_render(RenderTarget::Surface, Color::BLACK, |_pass| {
// Your rendering
});
frame.finish();
});Examples found in repository?
86 fn render(&mut self, _ctx: &mut AppCtx, window_id: WindowId, events: &mut EventBatch) {
87 // Get the color for this window
88 let Some(&color) = self.window_colors.get(&window_id) else {
89 return;
90 };
91
92 // WindowManager automatically handles:
93 // 1. Window lookup (no manual HashMap.get_mut)
94 // 2. Resize events (automatic)
95 // 3. Event dispatching
96 self.window_manager
97 .render_window(window_id, events, |window, _events| {
98 // No need to manually handle resize events!
99 // WindowManager already did that for us
100
101 // Just render!
102 let mut frame = window.begin_drawing();
103
104 frame.clear_and_render(RenderTarget::Surface, color, |_pass| {
105 // Additional rendering would go here
106 });
107
108 frame.finish();
109 });
110 }Sourcepub fn render_window_result<F, E>(
&mut self,
id: WindowId,
events: &mut EventBatch,
render_fn: F,
) -> Result<(), E>
pub fn render_window_result<F, E>( &mut self, id: WindowId, events: &mut EventBatch, render_fn: F, ) -> Result<(), E>
Renders a window with automatic event handling, passing a closure that returns a result.
This is useful when rendering might fail and you want to propagate errors.
§Example
use astrelis_render::{WindowManager, RenderTarget, Color};
use astrelis_winit::window::WindowBackend;
window_manager.render_window_result(window_id, events, |window, _events| {
let mut frame = window.begin_drawing();
frame.clear_and_render(RenderTarget::Surface, Color::BLACK, |_pass| {
// Rendering that might fail
});
frame.finish();
Ok(())
})Sourcepub fn graphics(&self) -> &Arc<GraphicsContext>
pub fn graphics(&self) -> &Arc<GraphicsContext>
Gets the shared graphics context.
Sourcepub fn iter(&self) -> impl Iterator<Item = (WindowId, &RenderableWindow)>
pub fn iter(&self) -> impl Iterator<Item = (WindowId, &RenderableWindow)>
Iterates over all windows with their IDs.
Sourcepub fn iter_mut(
&mut self,
) -> impl Iterator<Item = (WindowId, &mut RenderableWindow)>
pub fn iter_mut( &mut self, ) -> impl Iterator<Item = (WindowId, &mut RenderableWindow)>
Iterates mutably over all windows with their IDs.
Auto Trait Implementations§
impl Freeze for WindowManager
impl !RefUnwindSafe for WindowManager
impl Send for WindowManager
impl Sync for WindowManager
impl Unpin for WindowManager
impl !UnwindSafe for WindowManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more