From 7864d02077d0cdedfb53232aa168573a2fe5b1a5 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sat, 13 Dec 2025 22:01:56 +0100 Subject: [PATCH] Remove indentation in fill.rs helper --- winit/examples/util/fill.rs | 198 +++++++++++++++++------------------- 1 file changed, 92 insertions(+), 106 deletions(-) diff --git a/winit/examples/util/fill.rs b/winit/examples/util/fill.rs index 9cacb8032..ff3ca198d 100644 --- a/winit/examples/util/fill.rs +++ b/winit/examples/util/fill.rs @@ -7,123 +7,109 @@ //! The `softbuffer` crate is used, largely because of its ease of use. `glutin` or `wgpu` could //! also be used to fill the window buffer, but they are more complicated to use. -#[allow(unused_imports)] -pub use platform::cleanup_window; -#[allow(unused_imports)] -pub use platform::fill_window; -#[allow(unused_imports)] -pub use platform::fill_window_with_animated_color; -#[allow(unused_imports)] -pub use platform::fill_window_with_color; +use std::cell::RefCell; +use std::collections::HashMap; +use std::mem; +use std::mem::ManuallyDrop; +use std::num::NonZeroU32; +#[cfg(not(web_platform))] +use std::time::Instant; -mod platform { - use std::cell::RefCell; - use std::collections::HashMap; - use std::mem; - use std::mem::ManuallyDrop; - use std::num::NonZeroU32; - #[cfg(not(web_platform))] - use std::time::Instant; +use softbuffer::{Context, Surface}; +#[cfg(web_platform)] +use web_time::Instant; +use winit::window::{Window, WindowId}; - use softbuffer::{Context, Surface}; - #[cfg(web_platform)] - use web_time::Instant; - use winit::window::{Window, WindowId}; +thread_local! { + // NOTE: You should never do things like that, create context and drop it before + // you drop the event loop. We do this for brevity to not blow up examples. We use + // ManuallyDrop to prevent destructors from running. + // + // A static, thread-local map of graphics contexts to open windows. + static GC: ManuallyDrop>> = const { ManuallyDrop::new(RefCell::new(None)) }; +} - thread_local! { - // NOTE: You should never do things like that, create context and drop it before - // you drop the event loop. We do this for brevity to not blow up examples. We use - // ManuallyDrop to prevent destructors from running. - // - // A static, thread-local map of graphics contexts to open windows. - static GC: ManuallyDrop>> = const { ManuallyDrop::new(RefCell::new(None)) }; - } +/// The graphics context used to draw to a window. +struct GraphicsContext { + /// The global softbuffer context. + context: RefCell>, - /// The graphics context used to draw to a window. - struct GraphicsContext { - /// The global softbuffer context. - context: RefCell>, + /// The hash map of window IDs to surfaces. + surfaces: HashMap>, +} - /// The hash map of window IDs to surfaces. - surfaces: HashMap>, - } - - impl GraphicsContext { - fn new(w: &dyn Window) -> Self { - Self { - context: RefCell::new( - Context::new(unsafe { - mem::transmute::<&'_ dyn Window, &'static dyn Window>(w) - }) +impl GraphicsContext { + fn new(w: &dyn Window) -> Self { + Self { + context: RefCell::new( + Context::new(unsafe { mem::transmute::<&'_ dyn Window, &'static dyn Window>(w) }) .expect("Failed to create a softbuffer context"), - ), - surfaces: HashMap::new(), - } - } - - fn create_surface( - &mut self, - window: &dyn Window, - ) -> &mut Surface<&'static dyn Window, &'static dyn Window> { - self.surfaces.entry(window.id()).or_insert_with(|| { - Surface::new(&self.context.borrow(), unsafe { - mem::transmute::<&'_ dyn Window, &'static dyn Window>(window) - }) - .expect("Failed to create a softbuffer surface") - }) - } - - fn destroy_surface(&mut self, window: &dyn Window) { - self.surfaces.remove(&window.id()); + ), + surfaces: HashMap::new(), } } - pub fn fill_window_with_color(window: &dyn Window, color: u32) { - GC.with(|gc| { - let size = window.surface_size(); - let (Some(width), Some(height)) = - (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) - else { - return; - }; - - // Either get the last context used or create a new one. - let mut gc = gc.borrow_mut(); - let surface = - gc.get_or_insert_with(|| GraphicsContext::new(window)).create_surface(window); - - // Fill a buffer with a solid color - - surface.resize(width, height).expect("Failed to resize the softbuffer surface"); - - let mut buffer = surface.buffer_mut().expect("Failed to get the softbuffer buffer"); - buffer.fill(color); - buffer.present().expect("Failed to present the softbuffer buffer"); + fn create_surface( + &mut self, + window: &dyn Window, + ) -> &mut Surface<&'static dyn Window, &'static dyn Window> { + self.surfaces.entry(window.id()).or_insert_with(|| { + Surface::new(&self.context.borrow(), unsafe { + mem::transmute::<&'_ dyn Window, &'static dyn Window>(window) + }) + .expect("Failed to create a softbuffer surface") }) } - #[allow(dead_code)] - pub fn fill_window(window: &dyn Window) { - fill_window_with_color(window, 0xff181818); - } - - #[allow(dead_code)] - pub fn fill_window_with_animated_color(window: &dyn Window, start: Instant) { - let time = start.elapsed().as_secs_f32() * 1.5; - let blue = (time.sin() * 255.0) as u32; - let green = ((time.cos() * 255.0) as u32) << 8; - let red = ((1.0 - time.sin() * 255.0) as u32) << 16; - let color = red | green | blue; - fill_window_with_color(window, color); - } - - #[allow(dead_code)] - pub fn cleanup_window(window: &dyn Window) { - GC.with(|gc| { - let mut gc = gc.borrow_mut(); - if let Some(context) = gc.as_mut() { - context.destroy_surface(window); - } - }); + fn destroy_surface(&mut self, window: &dyn Window) { + self.surfaces.remove(&window.id()); } } + +pub fn fill_window_with_color(window: &dyn Window, color: u32) { + GC.with(|gc| { + let size = window.surface_size(); + let (Some(width), Some(height)) = + (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) + else { + return; + }; + + // Either get the last context used or create a new one. + let mut gc = gc.borrow_mut(); + let surface = gc.get_or_insert_with(|| GraphicsContext::new(window)).create_surface(window); + + // Fill a buffer with a solid color + + surface.resize(width, height).expect("Failed to resize the softbuffer surface"); + + let mut buffer = surface.buffer_mut().expect("Failed to get the softbuffer buffer"); + buffer.fill(color); + buffer.present().expect("Failed to present the softbuffer buffer"); + }) +} + +#[allow(dead_code)] +pub fn fill_window(window: &dyn Window) { + fill_window_with_color(window, 0xff181818); +} + +#[allow(dead_code)] +pub fn fill_window_with_animated_color(window: &dyn Window, start: Instant) { + let time = start.elapsed().as_secs_f32() * 1.5; + let blue = (time.sin() * 255.0) as u32; + let green = ((time.cos() * 255.0) as u32) << 8; + let red = ((1.0 - time.sin() * 255.0) as u32) << 16; + let color = red | green | blue; + fill_window_with_color(window, color); +} + +#[allow(dead_code)] +pub fn cleanup_window(window: &dyn Window) { + GC.with(|gc| { + let mut gc = gc.borrow_mut(); + if let Some(context) = gc.as_mut() { + context.destroy_surface(window); + } + }); +}