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.
This commit is contained in:
mitchmindtree 2016-11-11 22:08:46 +11:00
parent 97da37ef04
commit eb18b3d8b6
3 changed files with 27 additions and 4 deletions

View file

@ -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);

View file

@ -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<fn(u32, u32)>,
}
/// Error that can happen while creating a window or a headless renderer.

View file

@ -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.