From eb18b3d8b6eb04b426c3e3009cc5571743024609 Mon Sep 17 00:00:00 2001 From: mitchmindtree Date: Fri, 11 Nov 2016 22:08:46 +1100 Subject: [PATCH] Add a `WindowBuilder::with_window_resize_callback` method. This allows for passing the window_resize_callback fn during the window building stage. More importantly, this allows setting the callback without the need for mutable access to the Window, making it possible to set the callback in the downstream glium crate. This may solve tomaka/glium#1232 for most folk. --- examples/window.rs | 8 +++++--- src/lib.rs | 4 ++++ src/window.rs | 19 ++++++++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/examples/window.rs b/examples/window.rs index ad7abda9..633057ee 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -12,9 +12,11 @@ fn resize_callback(width: u32, height: u32) { } fn main() { - let mut window = winit::WindowBuilder::new().build().unwrap(); - window.set_title("A fantastic window!"); - window.set_window_resize_callback(Some(resize_callback as fn(u32, u32))); + let window = winit::WindowBuilder::new() + .with_title("A fantastic window!") + .with_window_resize_callback(resize_callback) + .build() + .unwrap(); for event in window.wait_events() { println!("{:?}", event); diff --git a/src/lib.rs b/src/lib.rs index 1f1c626b..de61b773 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,6 +107,10 @@ pub struct WindowBuilder { /// Platform-specific configuration. platform_specific: platform::PlatformSpecificWindowBuilderAttributes, + + /// A function called upon resizing, necessary to receive resize events on Mac and possibly + /// other systems. + window_resize_callback: Option, } /// Error that can happen while creating a window or a headless renderer. diff --git a/src/window.rs b/src/window.rs index d5603f74..99100004 100644 --- a/src/window.rs +++ b/src/window.rs @@ -18,6 +18,7 @@ impl WindowBuilder { WindowBuilder { window: Default::default(), platform_specific: Default::default(), + window_resize_callback: None, } } @@ -93,6 +94,15 @@ impl WindowBuilder { self } + /// Provides a resize callback that is called by Mac (and potentially other + /// operating systems) during resize operations. This can be used to repaint + /// during window resizing. + #[inline] + pub fn with_window_resize_callback(mut self, cb: fn(u32, u32)) -> WindowBuilder { + self.window_resize_callback = Some(cb); + self + } + /// Builds the window. /// /// Error should be very rare and only occur in case of permission denied, incompatible system, @@ -109,7 +119,14 @@ impl WindowBuilder { } // building - platform::Window::new(&self.window, &self.platform_specific).map(|w| Window { window: w }) + let mut w = try!(platform::Window::new(&self.window, &self.platform_specific)); + + // a window resize callback was given + if let Some(callback) = self.window_resize_callback { + w.set_window_resize_callback(Some(callback)); + } + + Ok(Window { window: w }) } /// Builds the window.